blob: 1e6bca5c11a516bd77cec90fc2d62d2a790b02ca [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 /**
Adam Powell637d3372010-08-25 14:37:03 -07001997 * Controls the over-scroll mode for this view.
1998 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1999 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2000 * and {@link #OVER_SCROLL_NEVER}.
2001 */
2002 private int mOverScrollMode;
2003
2004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 * The parent this view is attached to.
2006 * {@hide}
2007 *
2008 * @see #getParent()
2009 */
2010 protected ViewParent mParent;
2011
2012 /**
2013 * {@hide}
2014 */
2015 AttachInfo mAttachInfo;
2016
2017 /**
2018 * {@hide}
2019 */
Romain Guy809a7f62009-05-14 15:44:42 -07002020 @ViewDebug.ExportedProperty(flagMapping = {
2021 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2022 name = "FORCE_LAYOUT"),
2023 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2024 name = "LAYOUT_REQUIRED"),
2025 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002026 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002027 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2028 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2029 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2030 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2031 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002033 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034
2035 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002036 * This view's request for the visibility of the status bar.
2037 * @hide
2038 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002039 @ViewDebug.ExportedProperty(flagMapping = {
2040 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2041 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2042 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2043 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2044 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2045 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2046 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2047 equals = SYSTEM_UI_FLAG_VISIBLE,
2048 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2049 })
Joe Onorato664644d2011-01-23 17:53:23 -08002050 int mSystemUiVisibility;
2051
2052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 * Count of how many windows this view has been attached to.
2054 */
2055 int mWindowAttachCount;
2056
2057 /**
2058 * The layout parameters associated with this view and used by the parent
2059 * {@link android.view.ViewGroup} to determine how this view should be
2060 * laid out.
2061 * {@hide}
2062 */
2063 protected ViewGroup.LayoutParams mLayoutParams;
2064
2065 /**
2066 * The view flags hold various views states.
2067 * {@hide}
2068 */
2069 @ViewDebug.ExportedProperty
2070 int mViewFlags;
2071
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002072 static class TransformationInfo {
2073 /**
2074 * The transform matrix for the View. This transform is calculated internally
2075 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2076 * is used by default. Do *not* use this variable directly; instead call
2077 * getMatrix(), which will automatically recalculate the matrix if necessary
2078 * to get the correct matrix based on the latest rotation and scale properties.
2079 */
2080 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002081
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002082 /**
2083 * The transform matrix for the View. This transform is calculated internally
2084 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2085 * is used by default. Do *not* use this variable directly; instead call
2086 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2087 * to get the correct matrix based on the latest rotation and scale properties.
2088 */
2089 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002090
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002091 /**
2092 * An internal variable that tracks whether we need to recalculate the
2093 * transform matrix, based on whether the rotation or scaleX/Y properties
2094 * have changed since the matrix was last calculated.
2095 */
2096 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002097
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002098 /**
2099 * An internal variable that tracks whether we need to recalculate the
2100 * transform matrix, based on whether the rotation or scaleX/Y properties
2101 * have changed since the matrix was last calculated.
2102 */
2103 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002104
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002105 /**
2106 * A 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. This variable
2109 * is only valid after a call to updateMatrix() or to a function that
2110 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2111 */
2112 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002113
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002114 /**
2115 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2116 */
2117 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002119 /**
2120 * This matrix is used when computing the matrix for 3D rotations.
2121 */
2122 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002123
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002124 /**
2125 * These prev values are used to recalculate a centered pivot point when necessary. The
2126 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2127 * set), so thes values are only used then as well.
2128 */
2129 private int mPrevWidth = -1;
2130 private int mPrevHeight = -1;
2131
2132 /**
2133 * The degrees rotation around the vertical axis through the pivot point.
2134 */
2135 @ViewDebug.ExportedProperty
2136 float mRotationY = 0f;
2137
2138 /**
2139 * The degrees rotation around the horizontal axis through the pivot point.
2140 */
2141 @ViewDebug.ExportedProperty
2142 float mRotationX = 0f;
2143
2144 /**
2145 * The degrees rotation around the pivot point.
2146 */
2147 @ViewDebug.ExportedProperty
2148 float mRotation = 0f;
2149
2150 /**
2151 * The amount of translation of the object away from its left property (post-layout).
2152 */
2153 @ViewDebug.ExportedProperty
2154 float mTranslationX = 0f;
2155
2156 /**
2157 * The amount of translation of the object away from its top property (post-layout).
2158 */
2159 @ViewDebug.ExportedProperty
2160 float mTranslationY = 0f;
2161
2162 /**
2163 * The amount of scale in the x direction around the pivot point. A
2164 * value of 1 means no scaling is applied.
2165 */
2166 @ViewDebug.ExportedProperty
2167 float mScaleX = 1f;
2168
2169 /**
2170 * The amount of scale in the y direction around the pivot point. A
2171 * value of 1 means no scaling is applied.
2172 */
2173 @ViewDebug.ExportedProperty
2174 float mScaleY = 1f;
2175
2176 /**
Chet Haasea33de552012-02-03 16:28:24 -08002177 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002178 */
2179 @ViewDebug.ExportedProperty
2180 float mPivotX = 0f;
2181
2182 /**
Chet Haasea33de552012-02-03 16:28:24 -08002183 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002184 */
2185 @ViewDebug.ExportedProperty
2186 float mPivotY = 0f;
2187
2188 /**
2189 * The opacity of the View. This is a value from 0 to 1, where 0 means
2190 * completely transparent and 1 means completely opaque.
2191 */
2192 @ViewDebug.ExportedProperty
2193 float mAlpha = 1f;
2194 }
2195
2196 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002197
Joe Malin32736f02011-01-19 16:14:20 -08002198 private boolean mLastIsOpaque;
2199
Chet Haasefd2b0022010-08-06 13:08:56 -07002200 /**
2201 * Convenience value to check for float values that are close enough to zero to be considered
2202 * zero.
2203 */
Romain Guy2542d192010-08-18 11:47:12 -07002204 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002205
2206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 * The distance in pixels from the left edge of this view's parent
2208 * to the left edge of this view.
2209 * {@hide}
2210 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002211 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 protected int mLeft;
2213 /**
2214 * The distance in pixels from the left edge of this view's parent
2215 * to the right edge of this view.
2216 * {@hide}
2217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002218 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 protected int mRight;
2220 /**
2221 * The distance in pixels from the top edge of this view's parent
2222 * to the top 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 mTop;
2227 /**
2228 * The distance in pixels from the top edge of this view's parent
2229 * to the bottom 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 mBottom;
2234
2235 /**
2236 * The offset, in pixels, by which the content of this view is scrolled
2237 * horizontally.
2238 * {@hide}
2239 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002240 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 protected int mScrollX;
2242 /**
2243 * The offset, in pixels, by which the content of this view is scrolled
2244 * vertically.
2245 * {@hide}
2246 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002247 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 protected int mScrollY;
2249
2250 /**
2251 * The left padding in pixels, that is the distance in pixels between the
2252 * left edge of this view and the left edge of its content.
2253 * {@hide}
2254 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002255 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 protected int mPaddingLeft;
2257 /**
2258 * The right padding in pixels, that is the distance in pixels between the
2259 * right edge of this view and the right edge of its content.
2260 * {@hide}
2261 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002262 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 protected int mPaddingRight;
2264 /**
2265 * The top padding in pixels, that is the distance in pixels between the
2266 * top edge of this view and the top 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 mPaddingTop;
2271 /**
2272 * The bottom padding in pixels, that is the distance in pixels between the
2273 * bottom edge of this view and the bottom 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 mPaddingBottom;
2278
2279 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002280 * Briefly describes the view and is primarily used for accessibility support.
2281 */
2282 private CharSequence mContentDescription;
2283
2284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002286 *
2287 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002289 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002290 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291
2292 /**
2293 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002294 *
2295 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002297 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002298 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002300 /**
Adam Powell20232d02010-12-08 21:08:53 -08002301 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002302 *
2303 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002304 */
2305 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002306 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002307
2308 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002309 * Cache if the user padding is relative.
2310 *
2311 */
2312 @ViewDebug.ExportedProperty(category = "padding")
2313 boolean mUserPaddingRelative;
2314
2315 /**
2316 * Cache the paddingStart set by the user to append to the scrollbar's size.
2317 *
2318 */
2319 @ViewDebug.ExportedProperty(category = "padding")
2320 int mUserPaddingStart;
2321
2322 /**
2323 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2324 *
2325 */
2326 @ViewDebug.ExportedProperty(category = "padding")
2327 int mUserPaddingEnd;
2328
2329 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002330 * @hide
2331 */
2332 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2333 /**
2334 * @hide
2335 */
2336 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 private Drawable mBGDrawable;
2339
2340 private int mBackgroundResource;
2341 private boolean mBackgroundSizeChanged;
2342
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002343 static class ListenerInfo {
2344 /**
2345 * Listener used to dispatch focus change events.
2346 * This field should be made private, so it is hidden from the SDK.
2347 * {@hide}
2348 */
2349 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002351 /**
2352 * Listeners for layout change events.
2353 */
2354 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002355
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002356 /**
2357 * Listeners for attach events.
2358 */
2359 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002360
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002361 /**
2362 * Listener used to dispatch click events.
2363 * This field should be made private, so it is hidden from the SDK.
2364 * {@hide}
2365 */
2366 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002368 /**
2369 * Listener used to dispatch long click events.
2370 * This field should be made private, so it is hidden from the SDK.
2371 * {@hide}
2372 */
2373 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002375 /**
2376 * Listener used to build the context menu.
2377 * This field should be made private, so it is hidden from the SDK.
2378 * {@hide}
2379 */
2380 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002382 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002384 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002386 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002387
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002388 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002389
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002390 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002391
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002392 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2393 }
2394
2395 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 /**
2398 * The application environment this view lives in.
2399 * This field should be made private, so it is hidden from the SDK.
2400 * {@hide}
2401 */
2402 protected Context mContext;
2403
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002404 private final Resources mResources;
2405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 private ScrollabilityCache mScrollCache;
2407
2408 private int[] mDrawableState = null;
2409
Romain Guy0211a0a2011-02-14 16:34:59 -08002410 /**
2411 * Set to true when drawing cache is enabled and cannot be created.
2412 *
2413 * @hide
2414 */
2415 public boolean mCachingFailed;
2416
Romain Guy02890fd2010-08-06 17:58:44 -07002417 private Bitmap mDrawingCache;
2418 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002419 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002420 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421
2422 /**
2423 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2424 * the user may specify which view to go to next.
2425 */
2426 private int mNextFocusLeftId = View.NO_ID;
2427
2428 /**
2429 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2430 * the user may specify which view to go to next.
2431 */
2432 private int mNextFocusRightId = View.NO_ID;
2433
2434 /**
2435 * When this view has focus and the next focus is {@link #FOCUS_UP},
2436 * the user may specify which view to go to next.
2437 */
2438 private int mNextFocusUpId = View.NO_ID;
2439
2440 /**
2441 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2442 * the user may specify which view to go to next.
2443 */
2444 private int mNextFocusDownId = View.NO_ID;
2445
Jeff Brown4e6319b2010-12-13 10:36:51 -08002446 /**
2447 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2448 * the user may specify which view to go to next.
2449 */
2450 int mNextFocusForwardId = View.NO_ID;
2451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002453 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002454 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002455 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 private UnsetPressedState mUnsetPressedState;
2458
2459 /**
2460 * Whether the long press's action has been invoked. The tap's action is invoked on the
2461 * up event while a long press is invoked as soon as the long press duration is reached, so
2462 * a long press could be performed before the tap is checked, in which case the tap's action
2463 * should not be invoked.
2464 */
2465 private boolean mHasPerformedLongPress;
2466
2467 /**
2468 * The minimum height of the view. We'll try our best to have the height
2469 * of this view to at least this amount.
2470 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002471 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 private int mMinHeight;
2473
2474 /**
2475 * The minimum width of the view. We'll try our best to have the width
2476 * of this view to at least this amount.
2477 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002478 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 private int mMinWidth;
2480
2481 /**
2482 * The delegate to handle touch events that are physically in this view
2483 * but should be handled by another view.
2484 */
2485 private TouchDelegate mTouchDelegate = null;
2486
2487 /**
2488 * Solid color to use as a background when creating the drawing cache. Enables
2489 * the cache to use 16 bit bitmaps instead of 32 bit.
2490 */
2491 private int mDrawingCacheBackgroundColor = 0;
2492
2493 /**
2494 * Special tree observer used when mAttachInfo is null.
2495 */
2496 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002497
Adam Powelle14579b2009-12-16 18:39:52 -08002498 /**
2499 * Cache the touch slop from the context that created the view.
2500 */
2501 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002504 * Object that handles automatic animation of view properties.
2505 */
2506 private ViewPropertyAnimator mAnimator = null;
2507
2508 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002509 * Flag indicating that a drag can cross window boundaries. When
2510 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2511 * with this flag set, all visible applications will be able to participate
2512 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002513 *
2514 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002515 */
2516 public static final int DRAG_FLAG_GLOBAL = 1;
2517
2518 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002519 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2520 */
2521 private float mVerticalScrollFactor;
2522
2523 /**
Adam Powell20232d02010-12-08 21:08:53 -08002524 * Position of the vertical scroll bar.
2525 */
2526 private int mVerticalScrollbarPosition;
2527
2528 /**
2529 * Position the scroll bar at the default position as determined by the system.
2530 */
2531 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2532
2533 /**
2534 * Position the scroll bar along the left edge.
2535 */
2536 public static final int SCROLLBAR_POSITION_LEFT = 1;
2537
2538 /**
2539 * Position the scroll bar along the right edge.
2540 */
2541 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2542
2543 /**
Romain Guy171c5922011-01-06 10:04:23 -08002544 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002545 *
2546 * @see #getLayerType()
2547 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002548 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002549 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002550 */
2551 public static final int LAYER_TYPE_NONE = 0;
2552
2553 /**
2554 * <p>Indicates that the view has a software layer. A software layer is backed
2555 * by a bitmap and causes the view to be rendered using Android's software
2556 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002557 *
Romain Guy171c5922011-01-06 10:04:23 -08002558 * <p>Software layers have various usages:</p>
2559 * <p>When the application is not using hardware acceleration, a software layer
2560 * is useful to apply a specific color filter and/or blending mode and/or
2561 * translucency to a view and all its children.</p>
2562 * <p>When the application is using hardware acceleration, a software layer
2563 * is useful to render drawing primitives not supported by the hardware
2564 * accelerated pipeline. It can also be used to cache a complex view tree
2565 * into a texture and reduce the complexity of drawing operations. For instance,
2566 * when animating a complex view tree with a translation, a software layer can
2567 * be used to render the view tree only once.</p>
2568 * <p>Software layers should be avoided when the affected view tree updates
2569 * often. Every update will require to re-render the software layer, which can
2570 * potentially be slow (particularly when hardware acceleration is turned on
2571 * since the layer will have to be uploaded into a hardware texture after every
2572 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002573 *
2574 * @see #getLayerType()
2575 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002576 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002577 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002578 */
2579 public static final int LAYER_TYPE_SOFTWARE = 1;
2580
2581 /**
2582 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2583 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2584 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2585 * rendering pipeline, but only if hardware acceleration is turned on for the
2586 * view hierarchy. When hardware acceleration is turned off, hardware layers
2587 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002588 *
Romain Guy171c5922011-01-06 10:04:23 -08002589 * <p>A hardware layer is useful to apply a specific color filter and/or
2590 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002591 * <p>A hardware layer can be used to cache a complex view tree into a
2592 * texture and reduce the complexity of drawing operations. For instance,
2593 * when animating a complex view tree with a translation, a hardware layer can
2594 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002595 * <p>A hardware layer can also be used to increase the rendering quality when
2596 * rotation transformations are applied on a view. It can also be used to
2597 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002598 *
2599 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002600 * @see #setLayerType(int, android.graphics.Paint)
2601 * @see #LAYER_TYPE_NONE
2602 * @see #LAYER_TYPE_SOFTWARE
2603 */
2604 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002605
Romain Guy3aaff3a2011-01-12 14:18:47 -08002606 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2607 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2608 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2609 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2610 })
Romain Guy171c5922011-01-06 10:04:23 -08002611 int mLayerType = LAYER_TYPE_NONE;
2612 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002613 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002614
2615 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002616 * Set to true when the view is sending hover accessibility events because it
2617 * is the innermost hovered view.
2618 */
2619 private boolean mSendingHoverAccessibilityEvents;
2620
2621 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07002622 * Delegate for injecting accessiblity functionality.
2623 */
2624 AccessibilityDelegate mAccessibilityDelegate;
2625
2626 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002627 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002628 */
2629 public static final int TEXT_DIRECTION_INHERIT = 0;
2630
2631 /**
2632 * Text direction is using "first strong algorithm". The first strong directional character
2633 * determines the paragraph direction. If there is no strong directional character, the
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002634 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002635 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002636 */
2637 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2638
2639 /**
2640 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2641 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002642 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002643 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002644 */
2645 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2646
2647 /**
2648 * Text direction is forced to LTR.
2649 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002650 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002651 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002652
2653 /**
2654 * Text direction is forced to RTL.
2655 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002656 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002657 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002658
2659 /**
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002660 * Text direction is coming from the system Locale.
2661 *
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002662 */
2663 public static final int TEXT_DIRECTION_LOCALE = 5;
2664
2665 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002666 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002667 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002668 */
2669 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2670
2671 /**
2672 * The text direction that has been defined by {@link #setTextDirection(int)}.
2673 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002674 */
2675 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002676 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2677 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2678 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2679 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002680 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2681 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002682 })
Doug Feltcb3791202011-07-07 11:57:48 -07002683 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002684
2685 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002686 * The resolved text direction. This needs resolution if the value is
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002687 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if it is
Doug Feltcb3791202011-07-07 11:57:48 -07002688 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2689 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002690 *
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002691 */
2692 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002693 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2694 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2695 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002696 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -08002697 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
2698 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002699 })
Doug Feltcb3791202011-07-07 11:57:48 -07002700 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002701
2702 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002703 * Consistency verifier for debugging purposes.
2704 * @hide
2705 */
2706 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2707 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2708 new InputEventConsistencyVerifier(this, 0) : null;
2709
2710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 * Simple constructor to use when creating a view from code.
2712 *
2713 * @param context The Context the view is running in, through which it can
2714 * access the current theme, resources, etc.
2715 */
2716 public View(Context context) {
2717 mContext = context;
2718 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002719 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002720 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002721 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002722 mUserPaddingStart = -1;
2723 mUserPaddingEnd = -1;
2724 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 }
2726
2727 /**
2728 * Constructor that is called when inflating a view from XML. This is called
2729 * when a view is being constructed from an XML file, supplying attributes
2730 * that were specified in the XML file. This version uses a default style of
2731 * 0, so the only attribute values applied are those in the Context's Theme
2732 * and the given AttributeSet.
2733 *
2734 * <p>
2735 * The method onFinishInflate() will be called after all children have been
2736 * added.
2737 *
2738 * @param context The Context the view is running in, through which it can
2739 * access the current theme, resources, etc.
2740 * @param attrs The attributes of the XML tag that is inflating the view.
2741 * @see #View(Context, AttributeSet, int)
2742 */
2743 public View(Context context, AttributeSet attrs) {
2744 this(context, attrs, 0);
2745 }
2746
2747 /**
2748 * Perform inflation from XML and apply a class-specific base style. This
2749 * constructor of View allows subclasses to use their own base style when
2750 * they are inflating. For example, a Button class's constructor would call
2751 * this version of the super class constructor and supply
2752 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2753 * the theme's button style to modify all of the base view attributes (in
2754 * particular its background) as well as the Button class's attributes.
2755 *
2756 * @param context The Context the view is running in, through which it can
2757 * access the current theme, resources, etc.
2758 * @param attrs The attributes of the XML tag that is inflating the view.
2759 * @param defStyle The default style to apply to this view. If 0, no style
2760 * will be applied (beyond what is included in the theme). This may
2761 * either be an attribute resource, whose value will be retrieved
2762 * from the current theme, or an explicit style resource.
2763 * @see #View(Context, AttributeSet)
2764 */
2765 public View(Context context, AttributeSet attrs, int defStyle) {
2766 this(context);
2767
2768 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2769 defStyle, 0);
2770
2771 Drawable background = null;
2772
2773 int leftPadding = -1;
2774 int topPadding = -1;
2775 int rightPadding = -1;
2776 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002777 int startPadding = -1;
2778 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779
2780 int padding = -1;
2781
2782 int viewFlagValues = 0;
2783 int viewFlagMasks = 0;
2784
2785 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 int x = 0;
2788 int y = 0;
2789
Chet Haase73066682010-11-29 15:55:32 -08002790 float tx = 0;
2791 float ty = 0;
2792 float rotation = 0;
2793 float rotationX = 0;
2794 float rotationY = 0;
2795 float sx = 1f;
2796 float sy = 1f;
2797 boolean transformSet = false;
2798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2800
Adam Powell637d3372010-08-25 14:37:03 -07002801 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 final int N = a.getIndexCount();
2803 for (int i = 0; i < N; i++) {
2804 int attr = a.getIndex(i);
2805 switch (attr) {
2806 case com.android.internal.R.styleable.View_background:
2807 background = a.getDrawable(attr);
2808 break;
2809 case com.android.internal.R.styleable.View_padding:
2810 padding = a.getDimensionPixelSize(attr, -1);
2811 break;
2812 case com.android.internal.R.styleable.View_paddingLeft:
2813 leftPadding = a.getDimensionPixelSize(attr, -1);
2814 break;
2815 case com.android.internal.R.styleable.View_paddingTop:
2816 topPadding = a.getDimensionPixelSize(attr, -1);
2817 break;
2818 case com.android.internal.R.styleable.View_paddingRight:
2819 rightPadding = a.getDimensionPixelSize(attr, -1);
2820 break;
2821 case com.android.internal.R.styleable.View_paddingBottom:
2822 bottomPadding = a.getDimensionPixelSize(attr, -1);
2823 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002824 case com.android.internal.R.styleable.View_paddingStart:
2825 startPadding = a.getDimensionPixelSize(attr, -1);
2826 break;
2827 case com.android.internal.R.styleable.View_paddingEnd:
2828 endPadding = a.getDimensionPixelSize(attr, -1);
2829 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 case com.android.internal.R.styleable.View_scrollX:
2831 x = a.getDimensionPixelOffset(attr, 0);
2832 break;
2833 case com.android.internal.R.styleable.View_scrollY:
2834 y = a.getDimensionPixelOffset(attr, 0);
2835 break;
Chet Haase73066682010-11-29 15:55:32 -08002836 case com.android.internal.R.styleable.View_alpha:
2837 setAlpha(a.getFloat(attr, 1f));
2838 break;
2839 case com.android.internal.R.styleable.View_transformPivotX:
2840 setPivotX(a.getDimensionPixelOffset(attr, 0));
2841 break;
2842 case com.android.internal.R.styleable.View_transformPivotY:
2843 setPivotY(a.getDimensionPixelOffset(attr, 0));
2844 break;
2845 case com.android.internal.R.styleable.View_translationX:
2846 tx = a.getDimensionPixelOffset(attr, 0);
2847 transformSet = true;
2848 break;
2849 case com.android.internal.R.styleable.View_translationY:
2850 ty = a.getDimensionPixelOffset(attr, 0);
2851 transformSet = true;
2852 break;
2853 case com.android.internal.R.styleable.View_rotation:
2854 rotation = a.getFloat(attr, 0);
2855 transformSet = true;
2856 break;
2857 case com.android.internal.R.styleable.View_rotationX:
2858 rotationX = a.getFloat(attr, 0);
2859 transformSet = true;
2860 break;
2861 case com.android.internal.R.styleable.View_rotationY:
2862 rotationY = a.getFloat(attr, 0);
2863 transformSet = true;
2864 break;
2865 case com.android.internal.R.styleable.View_scaleX:
2866 sx = a.getFloat(attr, 1f);
2867 transformSet = true;
2868 break;
2869 case com.android.internal.R.styleable.View_scaleY:
2870 sy = a.getFloat(attr, 1f);
2871 transformSet = true;
2872 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 case com.android.internal.R.styleable.View_id:
2874 mID = a.getResourceId(attr, NO_ID);
2875 break;
2876 case com.android.internal.R.styleable.View_tag:
2877 mTag = a.getText(attr);
2878 break;
2879 case com.android.internal.R.styleable.View_fitsSystemWindows:
2880 if (a.getBoolean(attr, false)) {
2881 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2882 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2883 }
2884 break;
2885 case com.android.internal.R.styleable.View_focusable:
2886 if (a.getBoolean(attr, false)) {
2887 viewFlagValues |= FOCUSABLE;
2888 viewFlagMasks |= FOCUSABLE_MASK;
2889 }
2890 break;
2891 case com.android.internal.R.styleable.View_focusableInTouchMode:
2892 if (a.getBoolean(attr, false)) {
2893 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2894 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2895 }
2896 break;
2897 case com.android.internal.R.styleable.View_clickable:
2898 if (a.getBoolean(attr, false)) {
2899 viewFlagValues |= CLICKABLE;
2900 viewFlagMasks |= CLICKABLE;
2901 }
2902 break;
2903 case com.android.internal.R.styleable.View_longClickable:
2904 if (a.getBoolean(attr, false)) {
2905 viewFlagValues |= LONG_CLICKABLE;
2906 viewFlagMasks |= LONG_CLICKABLE;
2907 }
2908 break;
2909 case com.android.internal.R.styleable.View_saveEnabled:
2910 if (!a.getBoolean(attr, true)) {
2911 viewFlagValues |= SAVE_DISABLED;
2912 viewFlagMasks |= SAVE_DISABLED_MASK;
2913 }
2914 break;
2915 case com.android.internal.R.styleable.View_duplicateParentState:
2916 if (a.getBoolean(attr, false)) {
2917 viewFlagValues |= DUPLICATE_PARENT_STATE;
2918 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2919 }
2920 break;
2921 case com.android.internal.R.styleable.View_visibility:
2922 final int visibility = a.getInt(attr, 0);
2923 if (visibility != 0) {
2924 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2925 viewFlagMasks |= VISIBILITY_MASK;
2926 }
2927 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002928 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002929 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002930 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002931 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002932 final int layoutDirection = a.getInt(attr, -1);
2933 if (layoutDirection != -1) {
2934 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002935 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002936 // Set to default (LAYOUT_DIRECTION_INHERIT)
2937 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002938 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002939 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002940 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 case com.android.internal.R.styleable.View_drawingCacheQuality:
2942 final int cacheQuality = a.getInt(attr, 0);
2943 if (cacheQuality != 0) {
2944 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2945 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2946 }
2947 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002948 case com.android.internal.R.styleable.View_contentDescription:
2949 mContentDescription = a.getString(attr);
2950 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2952 if (!a.getBoolean(attr, true)) {
2953 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2954 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2955 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002956 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2958 if (!a.getBoolean(attr, true)) {
2959 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2960 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2961 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002962 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 case R.styleable.View_scrollbars:
2964 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2965 if (scrollbars != SCROLLBARS_NONE) {
2966 viewFlagValues |= scrollbars;
2967 viewFlagMasks |= SCROLLBARS_MASK;
2968 initializeScrollbars(a);
2969 }
2970 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07002971 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07002973 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
2974 // Ignore the attribute starting with ICS
2975 break;
2976 }
2977 // With builds < ICS, fall through and apply fading edges
2978 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2980 if (fadingEdge != FADING_EDGE_NONE) {
2981 viewFlagValues |= fadingEdge;
2982 viewFlagMasks |= FADING_EDGE_MASK;
2983 initializeFadingEdge(a);
2984 }
2985 break;
2986 case R.styleable.View_scrollbarStyle:
2987 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2988 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2989 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2990 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2991 }
2992 break;
2993 case R.styleable.View_isScrollContainer:
2994 setScrollContainer = true;
2995 if (a.getBoolean(attr, false)) {
2996 setScrollContainer(true);
2997 }
2998 break;
2999 case com.android.internal.R.styleable.View_keepScreenOn:
3000 if (a.getBoolean(attr, false)) {
3001 viewFlagValues |= KEEP_SCREEN_ON;
3002 viewFlagMasks |= KEEP_SCREEN_ON;
3003 }
3004 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003005 case R.styleable.View_filterTouchesWhenObscured:
3006 if (a.getBoolean(attr, false)) {
3007 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3008 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3009 }
3010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 case R.styleable.View_nextFocusLeft:
3012 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3013 break;
3014 case R.styleable.View_nextFocusRight:
3015 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3016 break;
3017 case R.styleable.View_nextFocusUp:
3018 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3019 break;
3020 case R.styleable.View_nextFocusDown:
3021 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3022 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003023 case R.styleable.View_nextFocusForward:
3024 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3025 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 case R.styleable.View_minWidth:
3027 mMinWidth = a.getDimensionPixelSize(attr, 0);
3028 break;
3029 case R.styleable.View_minHeight:
3030 mMinHeight = a.getDimensionPixelSize(attr, 0);
3031 break;
Romain Guy9a817362009-05-01 10:57:14 -07003032 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003033 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003034 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003035 + "be used within a restricted context");
3036 }
3037
Romain Guy9a817362009-05-01 10:57:14 -07003038 final String handlerName = a.getString(attr);
3039 if (handlerName != null) {
3040 setOnClickListener(new OnClickListener() {
3041 private Method mHandler;
3042
3043 public void onClick(View v) {
3044 if (mHandler == null) {
3045 try {
3046 mHandler = getContext().getClass().getMethod(handlerName,
3047 View.class);
3048 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003049 int id = getId();
3050 String idText = id == NO_ID ? "" : " with id '"
3051 + getContext().getResources().getResourceEntryName(
3052 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003053 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003054 handlerName + "(View) in the activity "
3055 + getContext().getClass() + " for onClick handler"
3056 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003057 }
3058 }
3059
3060 try {
3061 mHandler.invoke(getContext(), View.this);
3062 } catch (IllegalAccessException e) {
3063 throw new IllegalStateException("Could not execute non "
3064 + "public method of the activity", e);
3065 } catch (InvocationTargetException e) {
3066 throw new IllegalStateException("Could not execute "
3067 + "method of the activity", e);
3068 }
3069 }
3070 });
3071 }
3072 break;
Adam Powell637d3372010-08-25 14:37:03 -07003073 case R.styleable.View_overScrollMode:
3074 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3075 break;
Adam Powell20232d02010-12-08 21:08:53 -08003076 case R.styleable.View_verticalScrollbarPosition:
3077 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3078 break;
Romain Guy171c5922011-01-06 10:04:23 -08003079 case R.styleable.View_layerType:
3080 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3081 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003082 case R.styleable.View_textDirection:
3083 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3084 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 }
3086 }
3087
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003088 a.recycle();
3089
Adam Powell637d3372010-08-25 14:37:03 -07003090 setOverScrollMode(overScrollMode);
3091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 if (background != null) {
3093 setBackgroundDrawable(background);
3094 }
3095
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003096 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3097
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003098 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3099 // layout direction). Those cached values will be used later during padding resolution.
3100 mUserPaddingStart = startPadding;
3101 mUserPaddingEnd = endPadding;
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 if (padding >= 0) {
3104 leftPadding = padding;
3105 topPadding = padding;
3106 rightPadding = padding;
3107 bottomPadding = padding;
3108 }
3109
3110 // If the user specified the padding (either with android:padding or
3111 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3112 // use the default padding or the padding from the background drawable
3113 // (stored at this point in mPadding*)
3114 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3115 topPadding >= 0 ? topPadding : mPaddingTop,
3116 rightPadding >= 0 ? rightPadding : mPaddingRight,
3117 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3118
3119 if (viewFlagMasks != 0) {
3120 setFlags(viewFlagValues, viewFlagMasks);
3121 }
3122
3123 // Needs to be called after mViewFlags is set
3124 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3125 recomputePadding();
3126 }
3127
3128 if (x != 0 || y != 0) {
3129 scrollTo(x, y);
3130 }
3131
Chet Haase73066682010-11-29 15:55:32 -08003132 if (transformSet) {
3133 setTranslationX(tx);
3134 setTranslationY(ty);
3135 setRotation(rotation);
3136 setRotationX(rotationX);
3137 setRotationY(rotationY);
3138 setScaleX(sx);
3139 setScaleY(sy);
3140 }
3141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3143 setScrollContainer(true);
3144 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003145
3146 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
3149 /**
3150 * Non-public constructor for use in testing
3151 */
3152 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003153 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 }
3155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 /**
3157 * <p>
3158 * Initializes the fading edges from a given set of styled attributes. This
3159 * method should be called by subclasses that need fading edges and when an
3160 * instance of these subclasses is created programmatically rather than
3161 * being inflated from XML. This method is automatically called when the XML
3162 * is inflated.
3163 * </p>
3164 *
3165 * @param a the styled attributes set to initialize the fading edges from
3166 */
3167 protected void initializeFadingEdge(TypedArray a) {
3168 initScrollCache();
3169
3170 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3171 R.styleable.View_fadingEdgeLength,
3172 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3173 }
3174
3175 /**
3176 * Returns the size of the vertical faded edges used to indicate that more
3177 * content in this view is visible.
3178 *
3179 * @return The size in pixels of the vertical faded edge or 0 if vertical
3180 * faded edges are not enabled for this view.
3181 * @attr ref android.R.styleable#View_fadingEdgeLength
3182 */
3183 public int getVerticalFadingEdgeLength() {
3184 if (isVerticalFadingEdgeEnabled()) {
3185 ScrollabilityCache cache = mScrollCache;
3186 if (cache != null) {
3187 return cache.fadingEdgeLength;
3188 }
3189 }
3190 return 0;
3191 }
3192
3193 /**
3194 * Set the size of the faded edge used to indicate that more content in this
3195 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003196 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3197 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3198 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 *
3200 * @param length The size in pixels of the faded edge used to indicate that more
3201 * content in this view is visible.
3202 */
3203 public void setFadingEdgeLength(int length) {
3204 initScrollCache();
3205 mScrollCache.fadingEdgeLength = length;
3206 }
3207
3208 /**
3209 * Returns the size of the horizontal faded edges used to indicate that more
3210 * content in this view is visible.
3211 *
3212 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3213 * faded edges are not enabled for this view.
3214 * @attr ref android.R.styleable#View_fadingEdgeLength
3215 */
3216 public int getHorizontalFadingEdgeLength() {
3217 if (isHorizontalFadingEdgeEnabled()) {
3218 ScrollabilityCache cache = mScrollCache;
3219 if (cache != null) {
3220 return cache.fadingEdgeLength;
3221 }
3222 }
3223 return 0;
3224 }
3225
3226 /**
3227 * Returns the width of the vertical scrollbar.
3228 *
3229 * @return The width in pixels of the vertical scrollbar or 0 if there
3230 * is no vertical scrollbar.
3231 */
3232 public int getVerticalScrollbarWidth() {
3233 ScrollabilityCache cache = mScrollCache;
3234 if (cache != null) {
3235 ScrollBarDrawable scrollBar = cache.scrollBar;
3236 if (scrollBar != null) {
3237 int size = scrollBar.getSize(true);
3238 if (size <= 0) {
3239 size = cache.scrollBarSize;
3240 }
3241 return size;
3242 }
3243 return 0;
3244 }
3245 return 0;
3246 }
3247
3248 /**
3249 * Returns the height of the horizontal scrollbar.
3250 *
3251 * @return The height in pixels of the horizontal scrollbar or 0 if
3252 * there is no horizontal scrollbar.
3253 */
3254 protected int getHorizontalScrollbarHeight() {
3255 ScrollabilityCache cache = mScrollCache;
3256 if (cache != null) {
3257 ScrollBarDrawable scrollBar = cache.scrollBar;
3258 if (scrollBar != null) {
3259 int size = scrollBar.getSize(false);
3260 if (size <= 0) {
3261 size = cache.scrollBarSize;
3262 }
3263 return size;
3264 }
3265 return 0;
3266 }
3267 return 0;
3268 }
3269
3270 /**
3271 * <p>
3272 * Initializes the scrollbars from a given set of styled attributes. This
3273 * method should be called by subclasses that need scrollbars and when an
3274 * instance of these subclasses is created programmatically rather than
3275 * being inflated from XML. This method is automatically called when the XML
3276 * is inflated.
3277 * </p>
3278 *
3279 * @param a the styled attributes set to initialize the scrollbars from
3280 */
3281 protected void initializeScrollbars(TypedArray a) {
3282 initScrollCache();
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003285
Mike Cleronf116bf82009-09-27 19:14:12 -07003286 if (scrollabilityCache.scrollBar == null) {
3287 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3288 }
Joe Malin32736f02011-01-19 16:14:20 -08003289
Romain Guy8bda2482010-03-02 11:42:11 -08003290 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291
Mike Cleronf116bf82009-09-27 19:14:12 -07003292 if (!fadeScrollbars) {
3293 scrollabilityCache.state = ScrollabilityCache.ON;
3294 }
3295 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003296
3297
Mike Cleronf116bf82009-09-27 19:14:12 -07003298 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3299 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3300 .getScrollBarFadeDuration());
3301 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3302 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3303 ViewConfiguration.getScrollDefaultDelay());
3304
Joe Malin32736f02011-01-19 16:14:20 -08003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3307 com.android.internal.R.styleable.View_scrollbarSize,
3308 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3309
3310 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3311 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3312
3313 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3314 if (thumb != null) {
3315 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3316 }
3317
3318 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3319 false);
3320 if (alwaysDraw) {
3321 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3322 }
3323
3324 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3325 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3326
3327 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3328 if (thumb != null) {
3329 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3330 }
3331
3332 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3333 false);
3334 if (alwaysDraw) {
3335 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3336 }
3337
3338 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003339 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
3341
3342 /**
3343 * <p>
3344 * Initalizes the scrollability cache if necessary.
3345 * </p>
3346 */
3347 private void initScrollCache() {
3348 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003349 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351 }
3352
3353 /**
Adam Powell20232d02010-12-08 21:08:53 -08003354 * Set the position of the vertical scroll bar. Should be one of
3355 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3356 * {@link #SCROLLBAR_POSITION_RIGHT}.
3357 *
3358 * @param position Where the vertical scroll bar should be positioned.
3359 */
3360 public void setVerticalScrollbarPosition(int position) {
3361 if (mVerticalScrollbarPosition != position) {
3362 mVerticalScrollbarPosition = position;
3363 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003364 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003365 }
3366 }
3367
3368 /**
3369 * @return The position where the vertical scroll bar will show, if applicable.
3370 * @see #setVerticalScrollbarPosition(int)
3371 */
3372 public int getVerticalScrollbarPosition() {
3373 return mVerticalScrollbarPosition;
3374 }
3375
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003376 ListenerInfo getListenerInfo() {
3377 if (mListenerInfo != null) {
3378 return mListenerInfo;
3379 }
3380 mListenerInfo = new ListenerInfo();
3381 return mListenerInfo;
3382 }
3383
Adam Powell20232d02010-12-08 21:08:53 -08003384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 * Register a callback to be invoked when focus of this view changed.
3386 *
3387 * @param l The callback that will run.
3388 */
3389 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003390 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392
3393 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003394 * Add a listener that will be called when the bounds of the view change due to
3395 * layout processing.
3396 *
3397 * @param listener The listener that will be called when layout bounds change.
3398 */
3399 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003400 ListenerInfo li = getListenerInfo();
3401 if (li.mOnLayoutChangeListeners == null) {
3402 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003403 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003404 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3405 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003406 }
Chet Haase21cd1382010-09-01 17:42:29 -07003407 }
3408
3409 /**
3410 * Remove a listener for layout changes.
3411 *
3412 * @param listener The listener for layout bounds change.
3413 */
3414 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003415 ListenerInfo li = mListenerInfo;
3416 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003417 return;
3418 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003419 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003420 }
3421
3422 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003423 * Add a listener for attach state changes.
3424 *
3425 * This listener will be called whenever this view is attached or detached
3426 * from a window. Remove the listener using
3427 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3428 *
3429 * @param listener Listener to attach
3430 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3431 */
3432 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003433 ListenerInfo li = getListenerInfo();
3434 if (li.mOnAttachStateChangeListeners == null) {
3435 li.mOnAttachStateChangeListeners
3436 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003437 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003438 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003439 }
3440
3441 /**
3442 * Remove a listener for attach state changes. The listener will receive no further
3443 * notification of window attach/detach events.
3444 *
3445 * @param listener Listener to remove
3446 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3447 */
3448 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003449 ListenerInfo li = mListenerInfo;
3450 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003451 return;
3452 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003453 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003454 }
3455
3456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 * Returns the focus-change callback registered for this view.
3458 *
3459 * @return The callback, or null if one is not registered.
3460 */
3461 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003462 ListenerInfo li = mListenerInfo;
3463 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 }
3465
3466 /**
3467 * Register a callback to be invoked when this view is clicked. If this view is not
3468 * clickable, it becomes clickable.
3469 *
3470 * @param l The callback that will run
3471 *
3472 * @see #setClickable(boolean)
3473 */
3474 public void setOnClickListener(OnClickListener l) {
3475 if (!isClickable()) {
3476 setClickable(true);
3477 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003478 getListenerInfo().mOnClickListener = l;
3479 }
3480
3481 /**
3482 * Return whether this view has an attached OnClickListener. Returns
3483 * true if there is a listener, false if there is none.
3484 */
3485 public boolean hasOnClickListeners() {
3486 ListenerInfo li = mListenerInfo;
3487 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 }
3489
3490 /**
3491 * Register a callback to be invoked when this view is clicked and held. If this view is not
3492 * long clickable, it becomes long clickable.
3493 *
3494 * @param l The callback that will run
3495 *
3496 * @see #setLongClickable(boolean)
3497 */
3498 public void setOnLongClickListener(OnLongClickListener l) {
3499 if (!isLongClickable()) {
3500 setLongClickable(true);
3501 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003502 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504
3505 /**
3506 * Register a callback to be invoked when the context menu for this view is
3507 * being built. If this view is not long clickable, it becomes long clickable.
3508 *
3509 * @param l The callback that will run
3510 *
3511 */
3512 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3513 if (!isLongClickable()) {
3514 setLongClickable(true);
3515 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003516 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 }
3518
3519 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003520 * Call this view's OnClickListener, if it is defined. Performs all normal
3521 * actions associated with clicking: reporting accessibility event, playing
3522 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 *
3524 * @return True there was an assigned OnClickListener that was called, false
3525 * otherwise is returned.
3526 */
3527 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003528 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3529
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003530 ListenerInfo li = mListenerInfo;
3531 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003533 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 return true;
3535 }
3536
3537 return false;
3538 }
3539
3540 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003541 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3542 * this only calls the listener, and does not do any associated clicking
3543 * actions like reporting an accessibility event.
3544 *
3545 * @return True there was an assigned OnClickListener that was called, false
3546 * otherwise is returned.
3547 */
3548 public boolean callOnClick() {
3549 ListenerInfo li = mListenerInfo;
3550 if (li != null && li.mOnClickListener != null) {
3551 li.mOnClickListener.onClick(this);
3552 return true;
3553 }
3554 return false;
3555 }
3556
3557 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003558 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3559 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003561 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 */
3563 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003564 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003567 ListenerInfo li = mListenerInfo;
3568 if (li != null && li.mOnLongClickListener != null) {
3569 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
3571 if (!handled) {
3572 handled = showContextMenu();
3573 }
3574 if (handled) {
3575 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3576 }
3577 return handled;
3578 }
3579
3580 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003581 * Performs button-related actions during a touch down event.
3582 *
3583 * @param event The event.
3584 * @return True if the down was consumed.
3585 *
3586 * @hide
3587 */
3588 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3589 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3590 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3591 return true;
3592 }
3593 }
3594 return false;
3595 }
3596
3597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 * Bring up the context menu for this view.
3599 *
3600 * @return Whether a context menu was displayed.
3601 */
3602 public boolean showContextMenu() {
3603 return getParent().showContextMenuForChild(this);
3604 }
3605
3606 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003607 * Bring up the context menu for this view, referring to the item under the specified point.
3608 *
3609 * @param x The referenced x coordinate.
3610 * @param y The referenced y coordinate.
3611 * @param metaState The keyboard modifiers that were pressed.
3612 * @return Whether a context menu was displayed.
3613 *
3614 * @hide
3615 */
3616 public boolean showContextMenu(float x, float y, int metaState) {
3617 return showContextMenu();
3618 }
3619
3620 /**
Adam Powell6e346362010-07-23 10:18:23 -07003621 * Start an action mode.
3622 *
3623 * @param callback Callback that will control the lifecycle of the action mode
3624 * @return The new action mode if it is started, null otherwise
3625 *
3626 * @see ActionMode
3627 */
3628 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08003629 ViewParent parent = getParent();
3630 if (parent == null) return null;
3631 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07003632 }
3633
3634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 * Register a callback to be invoked when a key is pressed in this view.
3636 * @param l the key listener to attach to this view
3637 */
3638 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003639 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 }
3641
3642 /**
3643 * Register a callback to be invoked when a touch event is sent to this view.
3644 * @param l the touch listener to attach to this view
3645 */
3646 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003647 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
3650 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003651 * Register a callback to be invoked when a generic motion event is sent to this view.
3652 * @param l the generic motion listener to attach to this view
3653 */
3654 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003655 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08003656 }
3657
3658 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003659 * Register a callback to be invoked when a hover event is sent to this view.
3660 * @param l the hover listener to attach to this view
3661 */
3662 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003663 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07003664 }
3665
3666 /**
Joe Malin32736f02011-01-19 16:14:20 -08003667 * Register a drag event listener callback object for this View. The parameter is
3668 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3669 * View, the system calls the
3670 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3671 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003672 */
3673 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003674 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07003675 }
3676
3677 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003678 * Give this view focus. This will cause
3679 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 *
3681 * Note: this does not check whether this {@link View} should get focus, it just
3682 * gives it focus no matter what. It should only be called internally by framework
3683 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3684 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003685 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3686 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 * focus moved when requestFocus() is called. It may not always
3688 * apply, in which case use the default View.FOCUS_DOWN.
3689 * @param previouslyFocusedRect The rectangle of the view that had focus
3690 * prior in this View's coordinate system.
3691 */
3692 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3693 if (DBG) {
3694 System.out.println(this + " requestFocus()");
3695 }
3696
3697 if ((mPrivateFlags & FOCUSED) == 0) {
3698 mPrivateFlags |= FOCUSED;
3699
3700 if (mParent != null) {
3701 mParent.requestChildFocus(this, this);
3702 }
3703
3704 onFocusChanged(true, direction, previouslyFocusedRect);
3705 refreshDrawableState();
3706 }
3707 }
3708
3709 /**
3710 * Request that a rectangle of this view be visible on the screen,
3711 * scrolling if necessary just enough.
3712 *
3713 * <p>A View should call this if it maintains some notion of which part
3714 * of its content is interesting. For example, a text editing view
3715 * should call this when its cursor moves.
3716 *
3717 * @param rectangle The rectangle.
3718 * @return Whether any parent scrolled.
3719 */
3720 public boolean requestRectangleOnScreen(Rect rectangle) {
3721 return requestRectangleOnScreen(rectangle, false);
3722 }
3723
3724 /**
3725 * Request that a rectangle of this view be visible on the screen,
3726 * scrolling if necessary just enough.
3727 *
3728 * <p>A View should call this if it maintains some notion of which part
3729 * of its content is interesting. For example, a text editing view
3730 * should call this when its cursor moves.
3731 *
3732 * <p>When <code>immediate</code> is set to true, scrolling will not be
3733 * animated.
3734 *
3735 * @param rectangle The rectangle.
3736 * @param immediate True to forbid animated scrolling, false otherwise
3737 * @return Whether any parent scrolled.
3738 */
3739 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3740 View child = this;
3741 ViewParent parent = mParent;
3742 boolean scrolled = false;
3743 while (parent != null) {
3744 scrolled |= parent.requestChildRectangleOnScreen(child,
3745 rectangle, immediate);
3746
3747 // offset rect so next call has the rectangle in the
3748 // coordinate system of its direct child.
3749 rectangle.offset(child.getLeft(), child.getTop());
3750 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3751
3752 if (!(parent instanceof View)) {
3753 break;
3754 }
Romain Guy8506ab42009-06-11 17:35:47 -07003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 child = (View) parent;
3757 parent = child.getParent();
3758 }
3759 return scrolled;
3760 }
3761
3762 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08003763 * Called when this view wants to give up focus. If focus is cleared
3764 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
3765 * <p>
3766 * <strong>Note:</strong> When a View clears focus the framework is trying
3767 * to give focus to the first focusable View from the top. Hence, if this
3768 * View is the first from the top that can take focus, then its focus will
3769 * not be cleared nor will the focus change callback be invoked.
3770 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 */
3772 public void clearFocus() {
3773 if (DBG) {
3774 System.out.println(this + " clearFocus()");
3775 }
3776
3777 if ((mPrivateFlags & FOCUSED) != 0) {
3778 mPrivateFlags &= ~FOCUSED;
3779
3780 if (mParent != null) {
3781 mParent.clearChildFocus(this);
3782 }
3783
3784 onFocusChanged(false, 0, null);
3785 refreshDrawableState();
3786 }
3787 }
3788
3789 /**
3790 * Called to clear the focus of a view that is about to be removed.
3791 * Doesn't call clearChildFocus, which prevents this view from taking
3792 * focus again before it has been removed from the parent
3793 */
3794 void clearFocusForRemoval() {
3795 if ((mPrivateFlags & FOCUSED) != 0) {
3796 mPrivateFlags &= ~FOCUSED;
3797
3798 onFocusChanged(false, 0, null);
3799 refreshDrawableState();
Svetoslav Ganovb36a0ac2012-02-14 17:46:47 -08003800
3801 // The view cleared focus and invoked the callbacks, so now is the
3802 // time to give focus to the the first focusable from the top to
3803 // ensure that the gain focus is announced after clear focus.
3804 getRootView().requestFocus(FOCUS_FORWARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 }
3806 }
3807
3808 /**
3809 * Called internally by the view system when a new view is getting focus.
3810 * This is what clears the old focus.
3811 */
3812 void unFocus() {
3813 if (DBG) {
3814 System.out.println(this + " unFocus()");
3815 }
3816
3817 if ((mPrivateFlags & FOCUSED) != 0) {
3818 mPrivateFlags &= ~FOCUSED;
3819
3820 onFocusChanged(false, 0, null);
3821 refreshDrawableState();
3822 }
3823 }
3824
3825 /**
3826 * Returns true if this view has focus iteself, or is the ancestor of the
3827 * view that has focus.
3828 *
3829 * @return True if this view has or contains focus, false otherwise.
3830 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003831 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 public boolean hasFocus() {
3833 return (mPrivateFlags & FOCUSED) != 0;
3834 }
3835
3836 /**
3837 * Returns true if this view is focusable or if it contains a reachable View
3838 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3839 * is a View whose parents do not block descendants focus.
3840 *
3841 * Only {@link #VISIBLE} views are considered focusable.
3842 *
3843 * @return True if the view is focusable or if the view contains a focusable
3844 * View, false otherwise.
3845 *
3846 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3847 */
3848 public boolean hasFocusable() {
3849 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3850 }
3851
3852 /**
3853 * Called by the view system when the focus state of this view changes.
3854 * When the focus change event is caused by directional navigation, direction
3855 * and previouslyFocusedRect provide insight into where the focus is coming from.
3856 * When overriding, be sure to call up through to the super class so that
3857 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003858 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003859 * @param gainFocus True if the View has focus; false otherwise.
3860 * @param direction The direction focus has moved when requestFocus()
3861 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003862 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3863 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3864 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3866 * system, of the previously focused view. If applicable, this will be
3867 * passed in as finer grained information about where the focus is coming
3868 * from (in addition to direction). Will be <code>null</code> otherwise.
3869 */
3870 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003871 if (gainFocus) {
3872 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3873 }
3874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 InputMethodManager imm = InputMethodManager.peekInstance();
3876 if (!gainFocus) {
3877 if (isPressed()) {
3878 setPressed(false);
3879 }
3880 if (imm != null && mAttachInfo != null
3881 && mAttachInfo.mHasWindowFocus) {
3882 imm.focusOut(this);
3883 }
Romain Guya2431d02009-04-30 16:30:00 -07003884 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 } else if (imm != null && mAttachInfo != null
3886 && mAttachInfo.mHasWindowFocus) {
3887 imm.focusIn(this);
3888 }
Romain Guy8506ab42009-06-11 17:35:47 -07003889
Romain Guy0fd89bf2011-01-26 15:41:30 -08003890 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003891 ListenerInfo li = mListenerInfo;
3892 if (li != null && li.mOnFocusChangeListener != null) {
3893 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
Joe Malin32736f02011-01-19 16:14:20 -08003895
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003896 if (mAttachInfo != null) {
3897 mAttachInfo.mKeyDispatchState.reset(this);
3898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 }
3900
3901 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003902 * Sends an accessibility event of the given type. If accessiiblity is
3903 * not enabled this method has no effect. The default implementation calls
3904 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3905 * to populate information about the event source (this View), then calls
3906 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3907 * populate the text content of the event source including its descendants,
3908 * and last calls
3909 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3910 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003911 * <p>
3912 * If an {@link AccessibilityDelegate} has been specified via calling
3913 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3914 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
3915 * responsible for handling this call.
3916 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003917 *
Scott Mainb303d832011-10-12 16:45:18 -07003918 * @param eventType The type of the event to send, as defined by several types from
3919 * {@link android.view.accessibility.AccessibilityEvent}, such as
3920 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
3921 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003922 *
3923 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3924 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3925 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003926 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07003927 */
3928 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003929 if (mAccessibilityDelegate != null) {
3930 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
3931 } else {
3932 sendAccessibilityEventInternal(eventType);
3933 }
3934 }
3935
3936 /**
3937 * @see #sendAccessibilityEvent(int)
3938 *
3939 * Note: Called from the default {@link AccessibilityDelegate}.
3940 */
3941 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003942 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3943 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3944 }
3945 }
3946
3947 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003948 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3949 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003950 * perform a check whether accessibility is enabled.
3951 * <p>
3952 * If an {@link AccessibilityDelegate} has been specified via calling
3953 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
3954 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
3955 * is responsible for handling this call.
3956 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003957 *
3958 * @param event The event to send.
3959 *
3960 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003961 */
3962 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003963 if (mAccessibilityDelegate != null) {
3964 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
3965 } else {
3966 sendAccessibilityEventUncheckedInternal(event);
3967 }
3968 }
3969
3970 /**
3971 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
3972 *
3973 * Note: Called from the default {@link AccessibilityDelegate}.
3974 */
3975 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003976 if (!isShown()) {
3977 return;
3978 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003979 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07003980 // Only a subset of accessibility events populates text content.
3981 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
3982 dispatchPopulateAccessibilityEvent(event);
3983 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003984 // In the beginning we called #isShown(), so we know that getParent() is not null.
3985 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003986 }
3987
3988 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003989 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3990 * to its children for adding their text content to the event. Note that the
3991 * event text is populated in a separate dispatch path since we add to the
3992 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003993 * A typical implementation will call
3994 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3995 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3996 * on each child. Override this method if custom population of the event text
3997 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07003998 * <p>
3999 * If an {@link AccessibilityDelegate} has been specified via calling
4000 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4001 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4002 * is responsible for handling this call.
4003 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004004 * <p>
4005 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4006 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4007 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004008 *
4009 * @param event The event.
4010 *
4011 * @return True if the event population was completed.
4012 */
4013 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004014 if (mAccessibilityDelegate != null) {
4015 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4016 } else {
4017 return dispatchPopulateAccessibilityEventInternal(event);
4018 }
4019 }
4020
4021 /**
4022 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4023 *
4024 * Note: Called from the default {@link AccessibilityDelegate}.
4025 */
4026 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004027 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004028 return false;
4029 }
4030
4031 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004032 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004033 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004034 * text content. While this method is free to modify event
4035 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004036 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4037 * <p>
4038 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004039 * to the text added by the super implementation:
4040 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004041 * super.onPopulateAccessibilityEvent(event);
4042 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4043 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4044 * mCurrentDate.getTimeInMillis(), flags);
4045 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004046 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004047 * <p>
4048 * If an {@link AccessibilityDelegate} has been specified via calling
4049 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4050 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4051 * is responsible for handling this call.
4052 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004053 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4054 * information to the event, in case the default implementation has basic information to add.
4055 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004056 *
4057 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004058 *
4059 * @see #sendAccessibilityEvent(int)
4060 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004061 */
4062 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004063 if (mAccessibilityDelegate != null) {
4064 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4065 } else {
4066 onPopulateAccessibilityEventInternal(event);
4067 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004068 }
4069
4070 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004071 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4072 *
4073 * Note: Called from the default {@link AccessibilityDelegate}.
4074 */
4075 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4076
4077 }
4078
4079 /**
4080 * Initializes an {@link AccessibilityEvent} with information about
4081 * this View which is the event source. In other words, the source of
4082 * an accessibility event is the view whose state change triggered firing
4083 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004084 * <p>
4085 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004086 * to properties set by the super implementation:
4087 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4088 * super.onInitializeAccessibilityEvent(event);
4089 * event.setPassword(true);
4090 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004091 * <p>
4092 * If an {@link AccessibilityDelegate} has been specified via calling
4093 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4094 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4095 * is responsible for handling this call.
4096 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004097 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4098 * information to the event, in case the default implementation has basic information to add.
4099 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004100 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004101 *
4102 * @see #sendAccessibilityEvent(int)
4103 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4104 */
4105 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004106 if (mAccessibilityDelegate != null) {
4107 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4108 } else {
4109 onInitializeAccessibilityEventInternal(event);
4110 }
4111 }
4112
4113 /**
4114 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4115 *
4116 * Note: Called from the default {@link AccessibilityDelegate}.
4117 */
4118 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004119 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004120 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004121 event.setPackageName(getContext().getPackageName());
4122 event.setEnabled(isEnabled());
4123 event.setContentDescription(mContentDescription);
4124
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004125 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
4126 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
4127 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4128 FOCUSABLES_ALL);
4129 event.setItemCount(focusablesTempList.size());
4130 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4131 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004132 }
4133 }
4134
4135 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004136 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4137 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4138 * This method is responsible for obtaining an accessibility node info from a
4139 * pool of reusable instances and calling
4140 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4141 * initialize the former.
4142 * <p>
4143 * Note: The client is responsible for recycling the obtained instance by calling
4144 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4145 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004146 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004147 * @return A populated {@link AccessibilityNodeInfo}.
4148 *
4149 * @see AccessibilityNodeInfo
4150 */
4151 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004152 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4153 if (provider != null) {
4154 return provider.createAccessibilityNodeInfo(View.NO_ID);
4155 } else {
4156 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4157 onInitializeAccessibilityNodeInfo(info);
4158 return info;
4159 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004160 }
4161
4162 /**
4163 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4164 * The base implementation sets:
4165 * <ul>
4166 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004167 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4168 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004169 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4170 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4171 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4172 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4173 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4174 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4175 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4176 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4177 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4178 * </ul>
4179 * <p>
4180 * Subclasses should override this method, call the super implementation,
4181 * and set additional attributes.
4182 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004183 * <p>
4184 * If an {@link AccessibilityDelegate} has been specified via calling
4185 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4186 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4187 * is responsible for handling this call.
4188 * </p>
4189 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004190 * @param info The instance to initialize.
4191 */
4192 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004193 if (mAccessibilityDelegate != null) {
4194 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4195 } else {
4196 onInitializeAccessibilityNodeInfoInternal(info);
4197 }
4198 }
4199
4200 /**
4201 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4202 *
4203 * Note: Called from the default {@link AccessibilityDelegate}.
4204 */
4205 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004206 Rect bounds = mAttachInfo.mTmpInvalRect;
4207 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004208 info.setBoundsInParent(bounds);
4209
4210 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
4211 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07004212 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004213 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
4214 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004215
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004216 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
4217 ViewParent parent = getParent();
4218 if (parent instanceof View) {
4219 View parentView = (View) parent;
4220 info.setParent(parentView);
4221 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004222 }
4223
4224 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004225 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004226 info.setContentDescription(getContentDescription());
4227
4228 info.setEnabled(isEnabled());
4229 info.setClickable(isClickable());
4230 info.setFocusable(isFocusable());
4231 info.setFocused(isFocused());
4232 info.setSelected(isSelected());
4233 info.setLongClickable(isLongClickable());
4234
4235 // TODO: These make sense only if we are in an AdapterView but all
4236 // views can be selected. Maybe from accessiiblity perspective
4237 // we should report as selectable view in an AdapterView.
4238 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4239 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4240
4241 if (isFocusable()) {
4242 if (isFocused()) {
4243 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4244 } else {
4245 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4246 }
4247 }
4248 }
4249
4250 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004251 * Sets a delegate for implementing accessibility support via compositon as
4252 * opposed to inheritance. The delegate's primary use is for implementing
4253 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4254 *
4255 * @param delegate The delegate instance.
4256 *
4257 * @see AccessibilityDelegate
4258 */
4259 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4260 mAccessibilityDelegate = delegate;
4261 }
4262
4263 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004264 * Gets the provider for managing a virtual view hierarchy rooted at this View
4265 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4266 * that explore the window content.
4267 * <p>
4268 * If this method returns an instance, this instance is responsible for managing
4269 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4270 * View including the one representing the View itself. Similarly the returned
4271 * instance is responsible for performing accessibility actions on any virtual
4272 * view or the root view itself.
4273 * </p>
4274 * <p>
4275 * If an {@link AccessibilityDelegate} has been specified via calling
4276 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4277 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4278 * is responsible for handling this call.
4279 * </p>
4280 *
4281 * @return The provider.
4282 *
4283 * @see AccessibilityNodeProvider
4284 */
4285 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4286 if (mAccessibilityDelegate != null) {
4287 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4288 } else {
4289 return null;
4290 }
4291 }
4292
4293 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004294 * Gets the unique identifier of this view on the screen for accessibility purposes.
4295 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4296 *
4297 * @return The view accessibility id.
4298 *
4299 * @hide
4300 */
4301 public int getAccessibilityViewId() {
4302 if (mAccessibilityViewId == NO_ID) {
4303 mAccessibilityViewId = sNextAccessibilityViewId++;
4304 }
4305 return mAccessibilityViewId;
4306 }
4307
4308 /**
4309 * Gets the unique identifier of the window in which this View reseides.
4310 *
4311 * @return The window accessibility id.
4312 *
4313 * @hide
4314 */
4315 public int getAccessibilityWindowId() {
4316 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4317 }
4318
4319 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004320 * Gets the {@link View} description. It briefly describes the view and is
4321 * primarily used for accessibility support. Set this property to enable
4322 * better accessibility support for your application. This is especially
4323 * true for views that do not have textual representation (For example,
4324 * ImageButton).
4325 *
4326 * @return The content descriptiopn.
4327 *
4328 * @attr ref android.R.styleable#View_contentDescription
4329 */
4330 public CharSequence getContentDescription() {
4331 return mContentDescription;
4332 }
4333
4334 /**
4335 * Sets the {@link View} description. It briefly describes the view and is
4336 * primarily used for accessibility support. Set this property to enable
4337 * better accessibility support for your application. This is especially
4338 * true for views that do not have textual representation (For example,
4339 * ImageButton).
4340 *
4341 * @param contentDescription The content description.
4342 *
4343 * @attr ref android.R.styleable#View_contentDescription
4344 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004345 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004346 public void setContentDescription(CharSequence contentDescription) {
4347 mContentDescription = contentDescription;
4348 }
4349
4350 /**
Romain Guya2431d02009-04-30 16:30:00 -07004351 * Invoked whenever this view loses focus, either by losing window focus or by losing
4352 * focus within its window. This method can be used to clear any state tied to the
4353 * focus. For instance, if a button is held pressed with the trackball and the window
4354 * loses focus, this method can be used to cancel the press.
4355 *
4356 * Subclasses of View overriding this method should always call super.onFocusLost().
4357 *
4358 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004359 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004360 *
4361 * @hide pending API council approval
4362 */
4363 protected void onFocusLost() {
4364 resetPressedState();
4365 }
4366
4367 private void resetPressedState() {
4368 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4369 return;
4370 }
4371
4372 if (isPressed()) {
4373 setPressed(false);
4374
4375 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004376 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004377 }
4378 }
4379 }
4380
4381 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 * Returns true if this view has focus
4383 *
4384 * @return True if this view has focus, false otherwise.
4385 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004386 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 public boolean isFocused() {
4388 return (mPrivateFlags & FOCUSED) != 0;
4389 }
4390
4391 /**
4392 * Find the view in the hierarchy rooted at this view that currently has
4393 * focus.
4394 *
4395 * @return The view that currently has focus, or null if no focused view can
4396 * be found.
4397 */
4398 public View findFocus() {
4399 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4400 }
4401
4402 /**
4403 * Change whether this view is one of the set of scrollable containers in
4404 * its window. This will be used to determine whether the window can
4405 * resize or must pan when a soft input area is open -- scrollable
4406 * containers allow the window to use resize mode since the container
4407 * will appropriately shrink.
4408 */
4409 public void setScrollContainer(boolean isScrollContainer) {
4410 if (isScrollContainer) {
4411 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4412 mAttachInfo.mScrollContainers.add(this);
4413 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4414 }
4415 mPrivateFlags |= SCROLL_CONTAINER;
4416 } else {
4417 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4418 mAttachInfo.mScrollContainers.remove(this);
4419 }
4420 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4421 }
4422 }
4423
4424 /**
4425 * Returns the quality of the drawing cache.
4426 *
4427 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4428 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4429 *
4430 * @see #setDrawingCacheQuality(int)
4431 * @see #setDrawingCacheEnabled(boolean)
4432 * @see #isDrawingCacheEnabled()
4433 *
4434 * @attr ref android.R.styleable#View_drawingCacheQuality
4435 */
4436 public int getDrawingCacheQuality() {
4437 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4438 }
4439
4440 /**
4441 * Set the drawing cache quality of this view. This value is used only when the
4442 * drawing cache is enabled
4443 *
4444 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4445 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4446 *
4447 * @see #getDrawingCacheQuality()
4448 * @see #setDrawingCacheEnabled(boolean)
4449 * @see #isDrawingCacheEnabled()
4450 *
4451 * @attr ref android.R.styleable#View_drawingCacheQuality
4452 */
4453 public void setDrawingCacheQuality(int quality) {
4454 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4455 }
4456
4457 /**
4458 * Returns whether the screen should remain on, corresponding to the current
4459 * value of {@link #KEEP_SCREEN_ON}.
4460 *
4461 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4462 *
4463 * @see #setKeepScreenOn(boolean)
4464 *
4465 * @attr ref android.R.styleable#View_keepScreenOn
4466 */
4467 public boolean getKeepScreenOn() {
4468 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4469 }
4470
4471 /**
4472 * Controls whether the screen should remain on, modifying the
4473 * value of {@link #KEEP_SCREEN_ON}.
4474 *
4475 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4476 *
4477 * @see #getKeepScreenOn()
4478 *
4479 * @attr ref android.R.styleable#View_keepScreenOn
4480 */
4481 public void setKeepScreenOn(boolean keepScreenOn) {
4482 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4483 }
4484
4485 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004486 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4487 * @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 -08004488 *
4489 * @attr ref android.R.styleable#View_nextFocusLeft
4490 */
4491 public int getNextFocusLeftId() {
4492 return mNextFocusLeftId;
4493 }
4494
4495 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004496 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4497 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4498 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499 *
4500 * @attr ref android.R.styleable#View_nextFocusLeft
4501 */
4502 public void setNextFocusLeftId(int nextFocusLeftId) {
4503 mNextFocusLeftId = nextFocusLeftId;
4504 }
4505
4506 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004507 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4508 * @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 -08004509 *
4510 * @attr ref android.R.styleable#View_nextFocusRight
4511 */
4512 public int getNextFocusRightId() {
4513 return mNextFocusRightId;
4514 }
4515
4516 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004517 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4518 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4519 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 *
4521 * @attr ref android.R.styleable#View_nextFocusRight
4522 */
4523 public void setNextFocusRightId(int nextFocusRightId) {
4524 mNextFocusRightId = nextFocusRightId;
4525 }
4526
4527 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004528 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4529 * @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 -08004530 *
4531 * @attr ref android.R.styleable#View_nextFocusUp
4532 */
4533 public int getNextFocusUpId() {
4534 return mNextFocusUpId;
4535 }
4536
4537 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004538 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4539 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4540 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 *
4542 * @attr ref android.R.styleable#View_nextFocusUp
4543 */
4544 public void setNextFocusUpId(int nextFocusUpId) {
4545 mNextFocusUpId = nextFocusUpId;
4546 }
4547
4548 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004549 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4550 * @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 -08004551 *
4552 * @attr ref android.R.styleable#View_nextFocusDown
4553 */
4554 public int getNextFocusDownId() {
4555 return mNextFocusDownId;
4556 }
4557
4558 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004559 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4560 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4561 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 *
4563 * @attr ref android.R.styleable#View_nextFocusDown
4564 */
4565 public void setNextFocusDownId(int nextFocusDownId) {
4566 mNextFocusDownId = nextFocusDownId;
4567 }
4568
4569 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004570 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4571 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4572 *
4573 * @attr ref android.R.styleable#View_nextFocusForward
4574 */
4575 public int getNextFocusForwardId() {
4576 return mNextFocusForwardId;
4577 }
4578
4579 /**
4580 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4581 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4582 * decide automatically.
4583 *
4584 * @attr ref android.R.styleable#View_nextFocusForward
4585 */
4586 public void setNextFocusForwardId(int nextFocusForwardId) {
4587 mNextFocusForwardId = nextFocusForwardId;
4588 }
4589
4590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 * Returns the visibility of this view and all of its ancestors
4592 *
4593 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4594 */
4595 public boolean isShown() {
4596 View current = this;
4597 //noinspection ConstantConditions
4598 do {
4599 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4600 return false;
4601 }
4602 ViewParent parent = current.mParent;
4603 if (parent == null) {
4604 return false; // We are not attached to the view root
4605 }
4606 if (!(parent instanceof View)) {
4607 return true;
4608 }
4609 current = (View) parent;
4610 } while (current != null);
4611
4612 return false;
4613 }
4614
4615 /**
4616 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4617 * is set
4618 *
4619 * @param insets Insets for system windows
4620 *
4621 * @return True if this view applied the insets, false otherwise
4622 */
4623 protected boolean fitSystemWindows(Rect insets) {
4624 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4625 mPaddingLeft = insets.left;
4626 mPaddingTop = insets.top;
4627 mPaddingRight = insets.right;
4628 mPaddingBottom = insets.bottom;
4629 requestLayout();
4630 return true;
4631 }
4632 return false;
4633 }
4634
4635 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004636 * Set whether or not this view should account for system screen decorations
4637 * such as the status bar and inset its content. This allows this view to be
4638 * positioned in absolute screen coordinates and remain visible to the user.
4639 *
4640 * <p>This should only be used by top-level window decor views.
4641 *
4642 * @param fitSystemWindows true to inset content for system screen decorations, false for
4643 * default behavior.
4644 *
4645 * @attr ref android.R.styleable#View_fitsSystemWindows
4646 */
4647 public void setFitsSystemWindows(boolean fitSystemWindows) {
4648 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4649 }
4650
4651 /**
4652 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4653 * will account for system screen decorations such as the status bar and inset its
4654 * content. This allows the view to be positioned in absolute screen coordinates
4655 * and remain visible to the user.
4656 *
4657 * @return true if this view will adjust its content bounds for system screen decorations.
4658 *
4659 * @attr ref android.R.styleable#View_fitsSystemWindows
4660 */
4661 public boolean fitsSystemWindows() {
4662 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4663 }
4664
4665 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 * Returns the visibility status for this view.
4667 *
4668 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4669 * @attr ref android.R.styleable#View_visibility
4670 */
4671 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004672 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4673 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4674 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 })
4676 public int getVisibility() {
4677 return mViewFlags & VISIBILITY_MASK;
4678 }
4679
4680 /**
4681 * Set the enabled state of this view.
4682 *
4683 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4684 * @attr ref android.R.styleable#View_visibility
4685 */
4686 @RemotableViewMethod
4687 public void setVisibility(int visibility) {
4688 setFlags(visibility, VISIBILITY_MASK);
4689 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4690 }
4691
4692 /**
4693 * Returns the enabled status for this view. The interpretation of the
4694 * enabled state varies by subclass.
4695 *
4696 * @return True if this view is enabled, false otherwise.
4697 */
4698 @ViewDebug.ExportedProperty
4699 public boolean isEnabled() {
4700 return (mViewFlags & ENABLED_MASK) == ENABLED;
4701 }
4702
4703 /**
4704 * Set the enabled state of this view. The interpretation of the enabled
4705 * state varies by subclass.
4706 *
4707 * @param enabled True if this view is enabled, false otherwise.
4708 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004709 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004711 if (enabled == isEnabled()) return;
4712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4714
4715 /*
4716 * The View most likely has to change its appearance, so refresh
4717 * the drawable state.
4718 */
4719 refreshDrawableState();
4720
4721 // Invalidate too, since the default behavior for views is to be
4722 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004723 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 }
4725
4726 /**
4727 * Set whether this view can receive the focus.
4728 *
4729 * Setting this to false will also ensure that this view is not focusable
4730 * in touch mode.
4731 *
4732 * @param focusable If true, this view can receive the focus.
4733 *
4734 * @see #setFocusableInTouchMode(boolean)
4735 * @attr ref android.R.styleable#View_focusable
4736 */
4737 public void setFocusable(boolean focusable) {
4738 if (!focusable) {
4739 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4740 }
4741 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4742 }
4743
4744 /**
4745 * Set whether this view can receive focus while in touch mode.
4746 *
4747 * Setting this to true will also ensure that this view is focusable.
4748 *
4749 * @param focusableInTouchMode If true, this view can receive the focus while
4750 * in touch mode.
4751 *
4752 * @see #setFocusable(boolean)
4753 * @attr ref android.R.styleable#View_focusableInTouchMode
4754 */
4755 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4756 // Focusable in touch mode should always be set before the focusable flag
4757 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4758 // which, in touch mode, will not successfully request focus on this view
4759 // because the focusable in touch mode flag is not set
4760 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4761 if (focusableInTouchMode) {
4762 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4763 }
4764 }
4765
4766 /**
4767 * Set whether this view should have sound effects enabled for events such as
4768 * clicking and touching.
4769 *
4770 * <p>You may wish to disable sound effects for a view if you already play sounds,
4771 * for instance, a dial key that plays dtmf tones.
4772 *
4773 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4774 * @see #isSoundEffectsEnabled()
4775 * @see #playSoundEffect(int)
4776 * @attr ref android.R.styleable#View_soundEffectsEnabled
4777 */
4778 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4779 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4780 }
4781
4782 /**
4783 * @return whether this view should have sound effects enabled for events such as
4784 * clicking and touching.
4785 *
4786 * @see #setSoundEffectsEnabled(boolean)
4787 * @see #playSoundEffect(int)
4788 * @attr ref android.R.styleable#View_soundEffectsEnabled
4789 */
4790 @ViewDebug.ExportedProperty
4791 public boolean isSoundEffectsEnabled() {
4792 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4793 }
4794
4795 /**
4796 * Set whether this view should have haptic feedback for events such as
4797 * long presses.
4798 *
4799 * <p>You may wish to disable haptic feedback if your view already controls
4800 * its own haptic feedback.
4801 *
4802 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4803 * @see #isHapticFeedbackEnabled()
4804 * @see #performHapticFeedback(int)
4805 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4806 */
4807 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4808 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4809 }
4810
4811 /**
4812 * @return whether this view should have haptic feedback enabled for events
4813 * long presses.
4814 *
4815 * @see #setHapticFeedbackEnabled(boolean)
4816 * @see #performHapticFeedback(int)
4817 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4818 */
4819 @ViewDebug.ExportedProperty
4820 public boolean isHapticFeedbackEnabled() {
4821 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4822 }
4823
4824 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004825 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004826 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004827 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4828 * {@link #LAYOUT_DIRECTION_RTL},
4829 * {@link #LAYOUT_DIRECTION_INHERIT} or
4830 * {@link #LAYOUT_DIRECTION_LOCALE}.
4831 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004832 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004833 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004834 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4835 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4836 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4837 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004838 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004839 public int getLayoutDirection() {
4840 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004841 }
4842
4843 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004844 * Set the layout direction for this view. This will propagate a reset of layout direction
4845 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004846 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004847 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4848 * {@link #LAYOUT_DIRECTION_RTL},
4849 * {@link #LAYOUT_DIRECTION_INHERIT} or
4850 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004851 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004852 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08004853 */
4854 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004855 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004856 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004857 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004858 // Setting the flag will also request a layout.
4859 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4860 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004861 }
4862
4863 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004864 * Returns the resolved layout direction for this view.
4865 *
4866 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4867 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004868 */
4869 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4870 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4871 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4872 })
4873 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004874 resolveLayoutDirectionIfNeeded();
4875 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004876 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4877 }
4878
4879 /**
4880 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4881 * layout attribute and/or the inherited value from the parent.</p>
4882 *
4883 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004884 */
4885 @ViewDebug.ExportedProperty(category = "layout")
4886 public boolean isLayoutRtl() {
4887 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4888 }
4889
4890 /**
Adam Powell539ee872012-02-03 19:00:49 -08004891 * Indicates whether the view is currently tracking transient state that the
4892 * app should not need to concern itself with saving and restoring, but that
4893 * the framework should take special note to preserve when possible.
4894 *
4895 * @return true if the view has transient state
4896 *
4897 * @hide
4898 */
4899 @ViewDebug.ExportedProperty(category = "layout")
4900 public boolean hasTransientState() {
4901 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
4902 }
4903
4904 /**
4905 * Set whether this view is currently tracking transient state that the
4906 * framework should attempt to preserve when possible.
4907 *
4908 * @param hasTransientState true if this view has transient state
4909 *
4910 * @hide
4911 */
4912 public void setHasTransientState(boolean hasTransientState) {
4913 if (hasTransientState() == hasTransientState) return;
4914
4915 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
4916 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
4917 if (mParent != null) {
4918 try {
4919 mParent.childHasTransientStateChanged(this, hasTransientState);
4920 } catch (AbstractMethodError e) {
4921 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
4922 " does not fully implement ViewParent", e);
4923 }
4924 }
4925 }
4926
4927 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 * If this view doesn't do any drawing on its own, set this flag to
4929 * allow further optimizations. By default, this flag is not set on
4930 * View, but could be set on some View subclasses such as ViewGroup.
4931 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004932 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4933 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 *
4935 * @param willNotDraw whether or not this View draw on its own
4936 */
4937 public void setWillNotDraw(boolean willNotDraw) {
4938 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4939 }
4940
4941 /**
4942 * Returns whether or not this View draws on its own.
4943 *
4944 * @return true if this view has nothing to draw, false otherwise
4945 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004946 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 public boolean willNotDraw() {
4948 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4949 }
4950
4951 /**
4952 * When a View's drawing cache is enabled, drawing is redirected to an
4953 * offscreen bitmap. Some views, like an ImageView, must be able to
4954 * bypass this mechanism if they already draw a single bitmap, to avoid
4955 * unnecessary usage of the memory.
4956 *
4957 * @param willNotCacheDrawing true if this view does not cache its
4958 * drawing, false otherwise
4959 */
4960 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4961 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4962 }
4963
4964 /**
4965 * Returns whether or not this View can cache its drawing or not.
4966 *
4967 * @return true if this view does not cache its drawing, false otherwise
4968 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004969 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 public boolean willNotCacheDrawing() {
4971 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4972 }
4973
4974 /**
4975 * Indicates whether this view reacts to click events or not.
4976 *
4977 * @return true if the view is clickable, false otherwise
4978 *
4979 * @see #setClickable(boolean)
4980 * @attr ref android.R.styleable#View_clickable
4981 */
4982 @ViewDebug.ExportedProperty
4983 public boolean isClickable() {
4984 return (mViewFlags & CLICKABLE) == CLICKABLE;
4985 }
4986
4987 /**
4988 * Enables or disables click events for this view. When a view
4989 * is clickable it will change its state to "pressed" on every click.
4990 * Subclasses should set the view clickable to visually react to
4991 * user's clicks.
4992 *
4993 * @param clickable true to make the view clickable, false otherwise
4994 *
4995 * @see #isClickable()
4996 * @attr ref android.R.styleable#View_clickable
4997 */
4998 public void setClickable(boolean clickable) {
4999 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5000 }
5001
5002 /**
5003 * Indicates whether this view reacts to long click events or not.
5004 *
5005 * @return true if the view is long clickable, false otherwise
5006 *
5007 * @see #setLongClickable(boolean)
5008 * @attr ref android.R.styleable#View_longClickable
5009 */
5010 public boolean isLongClickable() {
5011 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5012 }
5013
5014 /**
5015 * Enables or disables long click events for this view. When a view is long
5016 * clickable it reacts to the user holding down the button for a longer
5017 * duration than a tap. This event can either launch the listener or a
5018 * context menu.
5019 *
5020 * @param longClickable true to make the view long clickable, false otherwise
5021 * @see #isLongClickable()
5022 * @attr ref android.R.styleable#View_longClickable
5023 */
5024 public void setLongClickable(boolean longClickable) {
5025 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5026 }
5027
5028 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005029 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 *
5031 * @see #isClickable()
5032 * @see #setClickable(boolean)
5033 *
5034 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5035 * the View's internal state from a previously set "pressed" state.
5036 */
5037 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005038 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 if (pressed) {
5041 mPrivateFlags |= PRESSED;
5042 } else {
5043 mPrivateFlags &= ~PRESSED;
5044 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005045
5046 if (needsRefresh) {
5047 refreshDrawableState();
5048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 dispatchSetPressed(pressed);
5050 }
5051
5052 /**
5053 * Dispatch setPressed to all of this View's children.
5054 *
5055 * @see #setPressed(boolean)
5056 *
5057 * @param pressed The new pressed state
5058 */
5059 protected void dispatchSetPressed(boolean pressed) {
5060 }
5061
5062 /**
5063 * Indicates whether the view is currently in pressed state. Unless
5064 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5065 * the pressed state.
5066 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005067 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 * @see #isClickable()
5069 * @see #setClickable(boolean)
5070 *
5071 * @return true if the view is currently pressed, false otherwise
5072 */
5073 public boolean isPressed() {
5074 return (mPrivateFlags & PRESSED) == PRESSED;
5075 }
5076
5077 /**
5078 * Indicates whether this view will save its state (that is,
5079 * whether its {@link #onSaveInstanceState} method will be called).
5080 *
5081 * @return Returns true if the view state saving is enabled, else false.
5082 *
5083 * @see #setSaveEnabled(boolean)
5084 * @attr ref android.R.styleable#View_saveEnabled
5085 */
5086 public boolean isSaveEnabled() {
5087 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5088 }
5089
5090 /**
5091 * Controls whether the saving of this view's state is
5092 * enabled (that is, whether its {@link #onSaveInstanceState} method
5093 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005094 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 * for its state to be saved. This flag can only disable the
5096 * saving of this view; any child views may still have their state saved.
5097 *
5098 * @param enabled Set to false to <em>disable</em> state saving, or true
5099 * (the default) to allow it.
5100 *
5101 * @see #isSaveEnabled()
5102 * @see #setId(int)
5103 * @see #onSaveInstanceState()
5104 * @attr ref android.R.styleable#View_saveEnabled
5105 */
5106 public void setSaveEnabled(boolean enabled) {
5107 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5108 }
5109
Jeff Brown85a31762010-09-01 17:01:00 -07005110 /**
5111 * Gets whether the framework should discard touches when the view's
5112 * window is obscured by another visible window.
5113 * Refer to the {@link View} security documentation for more details.
5114 *
5115 * @return True if touch filtering is enabled.
5116 *
5117 * @see #setFilterTouchesWhenObscured(boolean)
5118 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5119 */
5120 @ViewDebug.ExportedProperty
5121 public boolean getFilterTouchesWhenObscured() {
5122 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5123 }
5124
5125 /**
5126 * Sets whether the framework should discard touches when the view's
5127 * window is obscured by another visible window.
5128 * Refer to the {@link View} security documentation for more details.
5129 *
5130 * @param enabled True if touch filtering should be enabled.
5131 *
5132 * @see #getFilterTouchesWhenObscured
5133 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5134 */
5135 public void setFilterTouchesWhenObscured(boolean enabled) {
5136 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5137 FILTER_TOUCHES_WHEN_OBSCURED);
5138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139
5140 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005141 * Indicates whether the entire hierarchy under this view will save its
5142 * state when a state saving traversal occurs from its parent. The default
5143 * is true; if false, these views will not be saved unless
5144 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5145 *
5146 * @return Returns true if the view state saving from parent is enabled, else false.
5147 *
5148 * @see #setSaveFromParentEnabled(boolean)
5149 */
5150 public boolean isSaveFromParentEnabled() {
5151 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5152 }
5153
5154 /**
5155 * Controls whether the entire hierarchy under this view will save its
5156 * state when a state saving traversal occurs from its parent. The default
5157 * is true; if false, these views will not be saved unless
5158 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5159 *
5160 * @param enabled Set to false to <em>disable</em> state saving, or true
5161 * (the default) to allow it.
5162 *
5163 * @see #isSaveFromParentEnabled()
5164 * @see #setId(int)
5165 * @see #onSaveInstanceState()
5166 */
5167 public void setSaveFromParentEnabled(boolean enabled) {
5168 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5169 }
5170
5171
5172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 * Returns whether this View is able to take focus.
5174 *
5175 * @return True if this view can take focus, or false otherwise.
5176 * @attr ref android.R.styleable#View_focusable
5177 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005178 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 public final boolean isFocusable() {
5180 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5181 }
5182
5183 /**
5184 * When a view is focusable, it may not want to take focus when in touch mode.
5185 * For example, a button would like focus when the user is navigating via a D-pad
5186 * so that the user can click on it, but once the user starts touching the screen,
5187 * the button shouldn't take focus
5188 * @return Whether the view is focusable in touch mode.
5189 * @attr ref android.R.styleable#View_focusableInTouchMode
5190 */
5191 @ViewDebug.ExportedProperty
5192 public final boolean isFocusableInTouchMode() {
5193 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5194 }
5195
5196 /**
5197 * Find the nearest view in the specified direction that can take focus.
5198 * This does not actually give focus to that view.
5199 *
5200 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5201 *
5202 * @return The nearest focusable in the specified direction, or null if none
5203 * can be found.
5204 */
5205 public View focusSearch(int direction) {
5206 if (mParent != null) {
5207 return mParent.focusSearch(this, direction);
5208 } else {
5209 return null;
5210 }
5211 }
5212
5213 /**
5214 * This method is the last chance for the focused view and its ancestors to
5215 * respond to an arrow key. This is called when the focused view did not
5216 * consume the key internally, nor could the view system find a new view in
5217 * the requested direction to give focus to.
5218 *
5219 * @param focused The currently focused view.
5220 * @param direction The direction focus wants to move. One of FOCUS_UP,
5221 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5222 * @return True if the this view consumed this unhandled move.
5223 */
5224 public boolean dispatchUnhandledMove(View focused, int direction) {
5225 return false;
5226 }
5227
5228 /**
5229 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005230 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005232 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5233 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 * @return The user specified next view, or null if there is none.
5235 */
5236 View findUserSetNextFocus(View root, int direction) {
5237 switch (direction) {
5238 case FOCUS_LEFT:
5239 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005240 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 case FOCUS_RIGHT:
5242 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005243 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 case FOCUS_UP:
5245 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005246 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 case FOCUS_DOWN:
5248 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005249 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005250 case FOCUS_FORWARD:
5251 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005252 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005253 case FOCUS_BACKWARD: {
5254 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005255 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005256 @Override
5257 public boolean apply(View t) {
5258 return t.mNextFocusForwardId == id;
5259 }
5260 });
5261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 }
5263 return null;
5264 }
5265
Jeff Brown4dfbec22011-08-15 14:55:37 -07005266 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5267 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5268 @Override
5269 public boolean apply(View t) {
5270 return t.mID == childViewId;
5271 }
5272 });
5273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 if (result == null) {
5275 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5276 + "by user for id " + childViewId);
5277 }
5278 return result;
5279 }
5280
5281 /**
5282 * Find and return all focusable views that are descendants of this view,
5283 * possibly including this view if it is focusable itself.
5284 *
5285 * @param direction The direction of the focus
5286 * @return A list of focusable views
5287 */
5288 public ArrayList<View> getFocusables(int direction) {
5289 ArrayList<View> result = new ArrayList<View>(24);
5290 addFocusables(result, direction);
5291 return result;
5292 }
5293
5294 /**
5295 * Add any focusable views that are descendants of this view (possibly
5296 * including this view if it is focusable itself) to views. If we are in touch mode,
5297 * only add views that are also focusable in touch mode.
5298 *
5299 * @param views Focusable views found so far
5300 * @param direction The direction of the focus
5301 */
5302 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005303 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305
svetoslavganov75986cf2009-05-14 22:28:01 -07005306 /**
5307 * Adds any focusable views that are descendants of this view (possibly
5308 * including this view if it is focusable itself) to views. This method
5309 * adds all focusable views regardless if we are in touch mode or
5310 * only views focusable in touch mode if we are in touch mode depending on
5311 * the focusable mode paramater.
5312 *
5313 * @param views Focusable views found so far or null if all we are interested is
5314 * the number of focusables.
5315 * @param direction The direction of the focus.
5316 * @param focusableMode The type of focusables to be added.
5317 *
5318 * @see #FOCUSABLES_ALL
5319 * @see #FOCUSABLES_TOUCH_MODE
5320 */
5321 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
5322 if (!isFocusable()) {
5323 return;
5324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325
svetoslavganov75986cf2009-05-14 22:28:01 -07005326 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
5327 isInTouchMode() && !isFocusableInTouchMode()) {
5328 return;
5329 }
5330
5331 if (views != null) {
5332 views.add(this);
5333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 }
5335
5336 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005337 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005338 * The search is performed by either the text that the View renders or the content
5339 * description that describes the view for accessibility purposes and the view does
5340 * not render or both. Clients can specify how the search is to be performed via
5341 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5342 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005343 *
5344 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005345 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005346 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005347 * @see #FIND_VIEWS_WITH_TEXT
5348 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5349 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005350 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005351 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005352 if (getAccessibilityNodeProvider() != null) {
5353 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5354 outViews.add(this);
5355 }
5356 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
5357 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005358 String searchedLowerCase = searched.toString().toLowerCase();
5359 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
5360 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
5361 outViews.add(this);
5362 }
5363 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005364 }
5365
5366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 * Find and return all touchable views that are descendants of this view,
5368 * possibly including this view if it is touchable itself.
5369 *
5370 * @return A list of touchable views
5371 */
5372 public ArrayList<View> getTouchables() {
5373 ArrayList<View> result = new ArrayList<View>();
5374 addTouchables(result);
5375 return result;
5376 }
5377
5378 /**
5379 * Add any touchable views that are descendants of this view (possibly
5380 * including this view if it is touchable itself) to views.
5381 *
5382 * @param views Touchable views found so far
5383 */
5384 public void addTouchables(ArrayList<View> views) {
5385 final int viewFlags = mViewFlags;
5386
5387 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5388 && (viewFlags & ENABLED_MASK) == ENABLED) {
5389 views.add(this);
5390 }
5391 }
5392
5393 /**
5394 * Call this to try to give focus to a specific view or to one of its
5395 * descendants.
5396 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005397 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5398 * false), or if it is focusable and it is not focusable in touch mode
5399 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005401 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 * have focus, and you want your parent to look for the next one.
5403 *
5404 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5405 * {@link #FOCUS_DOWN} and <code>null</code>.
5406 *
5407 * @return Whether this view or one of its descendants actually took focus.
5408 */
5409 public final boolean requestFocus() {
5410 return requestFocus(View.FOCUS_DOWN);
5411 }
5412
5413
5414 /**
5415 * Call this to try to give focus to a specific view or to one of its
5416 * descendants and give it a hint about what direction focus is heading.
5417 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005418 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5419 * false), or if it is focusable and it is not focusable in touch mode
5420 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005422 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 * have focus, and you want your parent to look for the next one.
5424 *
5425 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5426 * <code>null</code> set for the previously focused rectangle.
5427 *
5428 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5429 * @return Whether this view or one of its descendants actually took focus.
5430 */
5431 public final boolean requestFocus(int direction) {
5432 return requestFocus(direction, null);
5433 }
5434
5435 /**
5436 * Call this to try to give focus to a specific view or to one of its descendants
5437 * and give it hints about the direction and a specific rectangle that the focus
5438 * is coming from. The rectangle can help give larger views a finer grained hint
5439 * about where focus is coming from, and therefore, where to show selection, or
5440 * forward focus change internally.
5441 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005442 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5443 * false), or if it is focusable and it is not focusable in touch mode
5444 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 *
5446 * A View will not take focus if it is not visible.
5447 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005448 * A View will not take focus if one of its parents has
5449 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5450 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005452 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453 * have focus, and you want your parent to look for the next one.
5454 *
5455 * You may wish to override this method if your custom {@link View} has an internal
5456 * {@link View} that it wishes to forward the request to.
5457 *
5458 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5459 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5460 * to give a finer grained hint about where focus is coming from. May be null
5461 * if there is no hint.
5462 * @return Whether this view or one of its descendants actually took focus.
5463 */
5464 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5465 // need to be focusable
5466 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5467 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5468 return false;
5469 }
5470
5471 // need to be focusable in touch mode if in touch mode
5472 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005473 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5474 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 }
5476
5477 // need to not have any parents blocking us
5478 if (hasAncestorThatBlocksDescendantFocus()) {
5479 return false;
5480 }
5481
5482 handleFocusGainInternal(direction, previouslyFocusedRect);
5483 return true;
5484 }
5485
5486 /**
5487 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5488 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5489 * touch mode to request focus when they are touched.
5490 *
5491 * @return Whether this view or one of its descendants actually took focus.
5492 *
5493 * @see #isInTouchMode()
5494 *
5495 */
5496 public final boolean requestFocusFromTouch() {
5497 // Leave touch mode if we need to
5498 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005499 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005500 if (viewRoot != null) {
5501 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 }
5503 }
5504 return requestFocus(View.FOCUS_DOWN);
5505 }
5506
5507 /**
5508 * @return Whether any ancestor of this view blocks descendant focus.
5509 */
5510 private boolean hasAncestorThatBlocksDescendantFocus() {
5511 ViewParent ancestor = mParent;
5512 while (ancestor instanceof ViewGroup) {
5513 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5514 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5515 return true;
5516 } else {
5517 ancestor = vgAncestor.getParent();
5518 }
5519 }
5520 return false;
5521 }
5522
5523 /**
Romain Guya440b002010-02-24 15:57:54 -08005524 * @hide
5525 */
5526 public void dispatchStartTemporaryDetach() {
5527 onStartTemporaryDetach();
5528 }
5529
5530 /**
5531 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5533 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005534 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 */
5536 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005537 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005538 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005539 }
5540
5541 /**
5542 * @hide
5543 */
5544 public void dispatchFinishTemporaryDetach() {
5545 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 }
Romain Guy8506ab42009-06-11 17:35:47 -07005547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 /**
5549 * Called after {@link #onStartTemporaryDetach} when the container is done
5550 * changing the view.
5551 */
5552 public void onFinishTemporaryDetach() {
5553 }
Romain Guy8506ab42009-06-11 17:35:47 -07005554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005555 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005556 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5557 * for this view's window. Returns null if the view is not currently attached
5558 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005559 * just use the standard high-level event callbacks like
5560 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005561 */
5562 public KeyEvent.DispatcherState getKeyDispatcherState() {
5563 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5564 }
Joe Malin32736f02011-01-19 16:14:20 -08005565
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 * Dispatch a key event before it is processed by any input method
5568 * associated with the view hierarchy. This can be used to intercept
5569 * key events in special situations before the IME consumes them; a
5570 * typical example would be handling the BACK key to update the application's
5571 * UI instead of allowing the IME to see it and close itself.
5572 *
5573 * @param event The key event to be dispatched.
5574 * @return True if the event was handled, false otherwise.
5575 */
5576 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5577 return onKeyPreIme(event.getKeyCode(), event);
5578 }
5579
5580 /**
5581 * Dispatch a key event to the next view on the focus path. This path runs
5582 * from the top of the view tree down to the currently focused view. If this
5583 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5584 * the next node down the focus path. This method also fires any key
5585 * listeners.
5586 *
5587 * @param event The key event to be dispatched.
5588 * @return True if the event was handled, false otherwise.
5589 */
5590 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005591 if (mInputEventConsistencyVerifier != null) {
5592 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594
Jeff Brown21bc5c92011-02-28 18:27:14 -08005595 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005596 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005597 ListenerInfo li = mListenerInfo;
5598 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5599 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 return true;
5601 }
5602
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005603 if (event.dispatch(this, mAttachInfo != null
5604 ? mAttachInfo.mKeyDispatchState : null, this)) {
5605 return true;
5606 }
5607
5608 if (mInputEventConsistencyVerifier != null) {
5609 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5610 }
5611 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 }
5613
5614 /**
5615 * Dispatches a key shortcut event.
5616 *
5617 * @param event The key event to be dispatched.
5618 * @return True if the event was handled by the view, false otherwise.
5619 */
5620 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5621 return onKeyShortcut(event.getKeyCode(), event);
5622 }
5623
5624 /**
5625 * Pass the touch screen motion event down to the target view, or this
5626 * view if it is the target.
5627 *
5628 * @param event The motion event to be dispatched.
5629 * @return True if the event was handled by the view, false otherwise.
5630 */
5631 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005632 if (mInputEventConsistencyVerifier != null) {
5633 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5634 }
5635
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005636 if (onFilterTouchEventForSecurity(event)) {
5637 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005638 ListenerInfo li = mListenerInfo;
5639 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5640 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005641 return true;
5642 }
5643
5644 if (onTouchEvent(event)) {
5645 return true;
5646 }
Jeff Brown85a31762010-09-01 17:01:00 -07005647 }
5648
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005649 if (mInputEventConsistencyVerifier != null) {
5650 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005652 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005653 }
5654
5655 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005656 * Filter the touch event to apply security policies.
5657 *
5658 * @param event The motion event to be filtered.
5659 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005660 *
Jeff Brown85a31762010-09-01 17:01:00 -07005661 * @see #getFilterTouchesWhenObscured
5662 */
5663 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005664 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005665 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5666 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5667 // Window is obscured, drop this touch.
5668 return false;
5669 }
5670 return true;
5671 }
5672
5673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 * Pass a trackball motion event down to the focused view.
5675 *
5676 * @param event The motion event to be dispatched.
5677 * @return True if the event was handled by the view, false otherwise.
5678 */
5679 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005680 if (mInputEventConsistencyVerifier != null) {
5681 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5682 }
5683
Romain Guy02ccac62011-06-24 13:20:23 -07005684 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
5686
5687 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005688 * Dispatch a generic motion event.
5689 * <p>
5690 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5691 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005692 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005693 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005694 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005695 *
5696 * @param event The motion event to be dispatched.
5697 * @return True if the event was handled by the view, false otherwise.
5698 */
5699 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005700 if (mInputEventConsistencyVerifier != null) {
5701 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5702 }
5703
Jeff Browna032cc02011-03-07 16:56:21 -08005704 final int source = event.getSource();
5705 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5706 final int action = event.getAction();
5707 if (action == MotionEvent.ACTION_HOVER_ENTER
5708 || action == MotionEvent.ACTION_HOVER_MOVE
5709 || action == MotionEvent.ACTION_HOVER_EXIT) {
5710 if (dispatchHoverEvent(event)) {
5711 return true;
5712 }
5713 } else if (dispatchGenericPointerEvent(event)) {
5714 return true;
5715 }
5716 } else if (dispatchGenericFocusedEvent(event)) {
5717 return true;
5718 }
5719
Jeff Brown10b62902011-06-20 16:40:37 -07005720 if (dispatchGenericMotionEventInternal(event)) {
5721 return true;
5722 }
5723
5724 if (mInputEventConsistencyVerifier != null) {
5725 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5726 }
5727 return false;
5728 }
5729
5730 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005731 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005732 ListenerInfo li = mListenerInfo;
5733 if (li != null && li.mOnGenericMotionListener != null
5734 && (mViewFlags & ENABLED_MASK) == ENABLED
5735 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005736 return true;
5737 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005738
5739 if (onGenericMotionEvent(event)) {
5740 return true;
5741 }
5742
5743 if (mInputEventConsistencyVerifier != null) {
5744 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5745 }
5746 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005747 }
5748
5749 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005750 * Dispatch a hover event.
5751 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005752 * Do not call this method directly.
5753 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005754 * </p>
5755 *
5756 * @param event The motion event to be dispatched.
5757 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005758 */
5759 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005760 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07005761 ListenerInfo li = mListenerInfo;
5762 if (li != null && li.mOnHoverListener != null
5763 && (mViewFlags & ENABLED_MASK) == ENABLED
5764 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005765 return true;
5766 }
5767
Jeff Browna032cc02011-03-07 16:56:21 -08005768 return onHoverEvent(event);
5769 }
5770
5771 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005772 * Returns true if the view has a child to which it has recently sent
5773 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5774 * it does not have a hovered child, then it must be the innermost hovered view.
5775 * @hide
5776 */
5777 protected boolean hasHoveredChild() {
5778 return false;
5779 }
5780
5781 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005782 * Dispatch a generic motion event to the view under the first pointer.
5783 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005784 * Do not call this method directly.
5785 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005786 * </p>
5787 *
5788 * @param event The motion event to be dispatched.
5789 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005790 */
5791 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5792 return false;
5793 }
5794
5795 /**
5796 * Dispatch a generic motion event to the currently focused view.
5797 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005798 * Do not call this method directly.
5799 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005800 * </p>
5801 *
5802 * @param event The motion event to be dispatched.
5803 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005804 */
5805 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5806 return false;
5807 }
5808
5809 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005810 * Dispatch a pointer event.
5811 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005812 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5813 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5814 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005815 * and should not be expected to handle other pointing device features.
5816 * </p>
5817 *
5818 * @param event The motion event to be dispatched.
5819 * @return True if the event was handled by the view, false otherwise.
5820 * @hide
5821 */
5822 public final boolean dispatchPointerEvent(MotionEvent event) {
5823 if (event.isTouchEvent()) {
5824 return dispatchTouchEvent(event);
5825 } else {
5826 return dispatchGenericMotionEvent(event);
5827 }
5828 }
5829
5830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 * Called when the window containing this view gains or loses window focus.
5832 * ViewGroups should override to route to their children.
5833 *
5834 * @param hasFocus True if the window containing this view now has focus,
5835 * false otherwise.
5836 */
5837 public void dispatchWindowFocusChanged(boolean hasFocus) {
5838 onWindowFocusChanged(hasFocus);
5839 }
5840
5841 /**
5842 * Called when the window containing this view gains or loses focus. Note
5843 * that this is separate from view focus: to receive key events, both
5844 * your view and its window must have focus. If a window is displayed
5845 * on top of yours that takes input focus, then your own window will lose
5846 * focus but the view focus will remain unchanged.
5847 *
5848 * @param hasWindowFocus True if the window containing this view now has
5849 * focus, false otherwise.
5850 */
5851 public void onWindowFocusChanged(boolean hasWindowFocus) {
5852 InputMethodManager imm = InputMethodManager.peekInstance();
5853 if (!hasWindowFocus) {
5854 if (isPressed()) {
5855 setPressed(false);
5856 }
5857 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5858 imm.focusOut(this);
5859 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005860 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005861 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005862 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5864 imm.focusIn(this);
5865 }
5866 refreshDrawableState();
5867 }
5868
5869 /**
5870 * Returns true if this view is in a window that currently has window focus.
5871 * Note that this is not the same as the view itself having focus.
5872 *
5873 * @return True if this view is in a window that currently has window focus.
5874 */
5875 public boolean hasWindowFocus() {
5876 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5877 }
5878
5879 /**
Adam Powell326d8082009-12-09 15:10:07 -08005880 * Dispatch a view visibility change down the view hierarchy.
5881 * ViewGroups should override to route to their children.
5882 * @param changedView The view whose visibility changed. Could be 'this' or
5883 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005884 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5885 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005886 */
5887 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5888 onVisibilityChanged(changedView, visibility);
5889 }
5890
5891 /**
5892 * Called when the visibility of the view or an ancestor of the view is changed.
5893 * @param changedView The view whose visibility changed. Could be 'this' or
5894 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005895 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5896 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005897 */
5898 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005899 if (visibility == VISIBLE) {
5900 if (mAttachInfo != null) {
5901 initialAwakenScrollBars();
5902 } else {
5903 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5904 }
5905 }
Adam Powell326d8082009-12-09 15:10:07 -08005906 }
5907
5908 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005909 * Dispatch a hint about whether this view is displayed. For instance, when
5910 * a View moves out of the screen, it might receives a display hint indicating
5911 * the view is not displayed. Applications should not <em>rely</em> on this hint
5912 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005913 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005914 * @param hint A hint about whether or not this view is displayed:
5915 * {@link #VISIBLE} or {@link #INVISIBLE}.
5916 */
5917 public void dispatchDisplayHint(int hint) {
5918 onDisplayHint(hint);
5919 }
5920
5921 /**
5922 * Gives this view a hint about whether is displayed or not. For instance, when
5923 * a View moves out of the screen, it might receives a display hint indicating
5924 * the view is not displayed. Applications should not <em>rely</em> on this hint
5925 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005926 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005927 * @param hint A hint about whether or not this view is displayed:
5928 * {@link #VISIBLE} or {@link #INVISIBLE}.
5929 */
5930 protected void onDisplayHint(int hint) {
5931 }
5932
5933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 * Dispatch a window visibility change down the view hierarchy.
5935 * ViewGroups should override to route to their children.
5936 *
5937 * @param visibility The new visibility of the window.
5938 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005939 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 */
5941 public void dispatchWindowVisibilityChanged(int visibility) {
5942 onWindowVisibilityChanged(visibility);
5943 }
5944
5945 /**
5946 * Called when the window containing has change its visibility
5947 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5948 * that this tells you whether or not your window is being made visible
5949 * to the window manager; this does <em>not</em> tell you whether or not
5950 * your window is obscured by other windows on the screen, even if it
5951 * is itself visible.
5952 *
5953 * @param visibility The new visibility of the window.
5954 */
5955 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005956 if (visibility == VISIBLE) {
5957 initialAwakenScrollBars();
5958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 }
5960
5961 /**
5962 * Returns the current visibility of the window this view is attached to
5963 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5964 *
5965 * @return Returns the current visibility of the view's window.
5966 */
5967 public int getWindowVisibility() {
5968 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5969 }
5970
5971 /**
5972 * Retrieve the overall visible display size in which the window this view is
5973 * attached to has been positioned in. This takes into account screen
5974 * decorations above the window, for both cases where the window itself
5975 * is being position inside of them or the window is being placed under
5976 * then and covered insets are used for the window to position its content
5977 * inside. In effect, this tells you the available area where content can
5978 * be placed and remain visible to users.
5979 *
5980 * <p>This function requires an IPC back to the window manager to retrieve
5981 * the requested information, so should not be used in performance critical
5982 * code like drawing.
5983 *
5984 * @param outRect Filled in with the visible display frame. If the view
5985 * is not attached to a window, this is simply the raw display size.
5986 */
5987 public void getWindowVisibleDisplayFrame(Rect outRect) {
5988 if (mAttachInfo != null) {
5989 try {
5990 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5991 } catch (RemoteException e) {
5992 return;
5993 }
5994 // XXX This is really broken, and probably all needs to be done
5995 // in the window manager, and we need to know more about whether
5996 // we want the area behind or in front of the IME.
5997 final Rect insets = mAttachInfo.mVisibleInsets;
5998 outRect.left += insets.left;
5999 outRect.top += insets.top;
6000 outRect.right -= insets.right;
6001 outRect.bottom -= insets.bottom;
6002 return;
6003 }
6004 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07006005 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 }
6007
6008 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006009 * Dispatch a notification about a resource configuration change down
6010 * the view hierarchy.
6011 * ViewGroups should override to route to their children.
6012 *
6013 * @param newConfig The new resource configuration.
6014 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006015 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006016 */
6017 public void dispatchConfigurationChanged(Configuration newConfig) {
6018 onConfigurationChanged(newConfig);
6019 }
6020
6021 /**
6022 * Called when the current configuration of the resources being used
6023 * by the application have changed. You can use this to decide when
6024 * to reload resources that can changed based on orientation and other
6025 * configuration characterstics. You only need to use this if you are
6026 * not relying on the normal {@link android.app.Activity} mechanism of
6027 * recreating the activity instance upon a configuration change.
6028 *
6029 * @param newConfig The new resource configuration.
6030 */
6031 protected void onConfigurationChanged(Configuration newConfig) {
6032 }
6033
6034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006035 * Private function to aggregate all per-view attributes in to the view
6036 * root.
6037 */
6038 void dispatchCollectViewAttributes(int visibility) {
6039 performCollectViewAttributes(visibility);
6040 }
6041
6042 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08006043 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006044 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
6045 mAttachInfo.mKeepScreenOn = true;
6046 }
6047 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006048 ListenerInfo li = mListenerInfo;
6049 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006050 mAttachInfo.mHasSystemUiListeners = true;
6051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 }
6053 }
6054
6055 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08006056 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08006058 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
6059 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 ai.mRecomputeGlobalAttributes = true;
6061 }
6062 }
6063 }
6064
6065 /**
6066 * Returns whether the device is currently in touch mode. Touch mode is entered
6067 * once the user begins interacting with the device by touch, and affects various
6068 * things like whether focus is always visible to the user.
6069 *
6070 * @return Whether the device is in touch mode.
6071 */
6072 @ViewDebug.ExportedProperty
6073 public boolean isInTouchMode() {
6074 if (mAttachInfo != null) {
6075 return mAttachInfo.mInTouchMode;
6076 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006077 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
6079 }
6080
6081 /**
6082 * Returns the context the view is running in, through which it can
6083 * access the current theme, resources, etc.
6084 *
6085 * @return The view's Context.
6086 */
6087 @ViewDebug.CapturedViewProperty
6088 public final Context getContext() {
6089 return mContext;
6090 }
6091
6092 /**
6093 * Handle a key event before it is processed by any input method
6094 * associated with the view hierarchy. This can be used to intercept
6095 * key events in special situations before the IME consumes them; a
6096 * typical example would be handling the BACK key to update the application's
6097 * UI instead of allowing the IME to see it and close itself.
6098 *
6099 * @param keyCode The value in event.getKeyCode().
6100 * @param event Description of the key event.
6101 * @return If you handled the event, return true. If you want to allow the
6102 * event to be handled by the next receiver, return false.
6103 */
6104 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
6105 return false;
6106 }
6107
6108 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006109 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
6110 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
6112 * is released, if the view is enabled and clickable.
6113 *
6114 * @param keyCode A key code that represents the button pressed, from
6115 * {@link android.view.KeyEvent}.
6116 * @param event The KeyEvent object that defines the button action.
6117 */
6118 public boolean onKeyDown(int keyCode, KeyEvent event) {
6119 boolean result = false;
6120
6121 switch (keyCode) {
6122 case KeyEvent.KEYCODE_DPAD_CENTER:
6123 case KeyEvent.KEYCODE_ENTER: {
6124 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6125 return true;
6126 }
6127 // Long clickable items don't necessarily have to be clickable
6128 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
6129 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
6130 (event.getRepeatCount() == 0)) {
6131 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006132 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 return true;
6134 }
6135 break;
6136 }
6137 }
6138 return result;
6139 }
6140
6141 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006142 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
6143 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
6144 * the event).
6145 */
6146 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
6147 return false;
6148 }
6149
6150 /**
Jeff Brown995e7742010-12-22 16:59:36 -08006151 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
6152 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006153 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
6154 * {@link KeyEvent#KEYCODE_ENTER} is released.
6155 *
6156 * @param keyCode A key code that represents the button pressed, from
6157 * {@link android.view.KeyEvent}.
6158 * @param event The KeyEvent object that defines the button action.
6159 */
6160 public boolean onKeyUp(int keyCode, KeyEvent event) {
6161 boolean result = false;
6162
6163 switch (keyCode) {
6164 case KeyEvent.KEYCODE_DPAD_CENTER:
6165 case KeyEvent.KEYCODE_ENTER: {
6166 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
6167 return true;
6168 }
6169 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
6170 setPressed(false);
6171
6172 if (!mHasPerformedLongPress) {
6173 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006174 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175
6176 result = performClick();
6177 }
6178 }
6179 break;
6180 }
6181 }
6182 return result;
6183 }
6184
6185 /**
6186 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
6187 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
6188 * the event).
6189 *
6190 * @param keyCode A key code that represents the button pressed, from
6191 * {@link android.view.KeyEvent}.
6192 * @param repeatCount The number of times the action was made.
6193 * @param event The KeyEvent object that defines the button action.
6194 */
6195 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
6196 return false;
6197 }
6198
6199 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08006200 * Called on the focused view when a key shortcut event is not handled.
6201 * Override this method to implement local key shortcuts for the View.
6202 * Key shortcuts can also be implemented by setting the
6203 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 *
6205 * @param keyCode The value in event.getKeyCode().
6206 * @param event Description of the key event.
6207 * @return If you handled the event, return true. If you want to allow the
6208 * event to be handled by the next receiver, return false.
6209 */
6210 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
6211 return false;
6212 }
6213
6214 /**
6215 * Check whether the called view is a text editor, in which case it
6216 * would make sense to automatically display a soft input window for
6217 * it. Subclasses should override this if they implement
6218 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006219 * a call on that method would return a non-null InputConnection, and
6220 * they are really a first-class editor that the user would normally
6221 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07006222 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006223 * <p>The default implementation always returns false. This does
6224 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
6225 * will not be called or the user can not otherwise perform edits on your
6226 * view; it is just a hint to the system that this is not the primary
6227 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07006228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 * @return Returns true if this view is a text editor, else false.
6230 */
6231 public boolean onCheckIsTextEditor() {
6232 return false;
6233 }
Romain Guy8506ab42009-06-11 17:35:47 -07006234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 /**
6236 * Create a new InputConnection for an InputMethod to interact
6237 * with the view. The default implementation returns null, since it doesn't
6238 * support input methods. You can override this to implement such support.
6239 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07006240 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 * <p>When implementing this, you probably also want to implement
6242 * {@link #onCheckIsTextEditor()} to indicate you will return a
6243 * non-null InputConnection.
6244 *
6245 * @param outAttrs Fill in with attribute information about the connection.
6246 */
6247 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
6248 return null;
6249 }
6250
6251 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006252 * Called by the {@link android.view.inputmethod.InputMethodManager}
6253 * when a view who is not the current
6254 * input connection target is trying to make a call on the manager. The
6255 * default implementation returns false; you can override this to return
6256 * true for certain views if you are performing InputConnection proxying
6257 * to them.
6258 * @param view The View that is making the InputMethodManager call.
6259 * @return Return true to allow the call, false to reject.
6260 */
6261 public boolean checkInputConnectionProxy(View view) {
6262 return false;
6263 }
Romain Guy8506ab42009-06-11 17:35:47 -07006264
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07006265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 * Show the context menu for this view. It is not safe to hold on to the
6267 * menu after returning from this method.
6268 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07006269 * You should normally not overload this method. Overload
6270 * {@link #onCreateContextMenu(ContextMenu)} or define an
6271 * {@link OnCreateContextMenuListener} to add items to the context menu.
6272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 * @param menu The context menu to populate
6274 */
6275 public void createContextMenu(ContextMenu menu) {
6276 ContextMenuInfo menuInfo = getContextMenuInfo();
6277
6278 // Sets the current menu info so all items added to menu will have
6279 // my extra info set.
6280 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
6281
6282 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006283 ListenerInfo li = mListenerInfo;
6284 if (li != null && li.mOnCreateContextMenuListener != null) {
6285 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 }
6287
6288 // Clear the extra information so subsequent items that aren't mine don't
6289 // have my extra info.
6290 ((MenuBuilder)menu).setCurrentMenuInfo(null);
6291
6292 if (mParent != null) {
6293 mParent.createContextMenu(menu);
6294 }
6295 }
6296
6297 /**
6298 * Views should implement this if they have extra information to associate
6299 * with the context menu. The return result is supplied as a parameter to
6300 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
6301 * callback.
6302 *
6303 * @return Extra information about the item for which the context menu
6304 * should be shown. This information will vary across different
6305 * subclasses of View.
6306 */
6307 protected ContextMenuInfo getContextMenuInfo() {
6308 return null;
6309 }
6310
6311 /**
6312 * Views should implement this if the view itself is going to add items to
6313 * the context menu.
6314 *
6315 * @param menu the context menu to populate
6316 */
6317 protected void onCreateContextMenu(ContextMenu menu) {
6318 }
6319
6320 /**
6321 * Implement this method to handle trackball motion events. The
6322 * <em>relative</em> movement of the trackball since the last event
6323 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
6324 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
6325 * that a movement of 1 corresponds to the user pressing one DPAD key (so
6326 * they will often be fractional values, representing the more fine-grained
6327 * movement information available from a trackball).
6328 *
6329 * @param event The motion event.
6330 * @return True if the event was handled, false otherwise.
6331 */
6332 public boolean onTrackballEvent(MotionEvent event) {
6333 return false;
6334 }
6335
6336 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08006337 * Implement this method to handle generic motion events.
6338 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08006339 * Generic motion events describe joystick movements, mouse hovers, track pad
6340 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08006341 * {@link MotionEvent#getSource() source} of the motion event specifies
6342 * the class of input that was received. Implementations of this method
6343 * must examine the bits in the source before processing the event.
6344 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006345 * </p><p>
6346 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6347 * are delivered to the view under the pointer. All other generic motion events are
6348 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08006349 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07006350 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006351 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006352 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
6353 * // process the joystick movement...
6354 * return true;
6355 * }
6356 * }
6357 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
6358 * switch (event.getAction()) {
6359 * case MotionEvent.ACTION_HOVER_MOVE:
6360 * // process the mouse hover movement...
6361 * return true;
6362 * case MotionEvent.ACTION_SCROLL:
6363 * // process the scroll wheel movement...
6364 * return true;
6365 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006366 * }
6367 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07006368 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08006369 *
6370 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006371 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006372 */
6373 public boolean onGenericMotionEvent(MotionEvent event) {
6374 return false;
6375 }
6376
6377 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006378 * Implement this method to handle hover events.
6379 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006380 * This method is called whenever a pointer is hovering into, over, or out of the
6381 * bounds of a view and the view is not currently being touched.
6382 * Hover events are represented as pointer events with action
6383 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6384 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6385 * </p>
6386 * <ul>
6387 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6388 * when the pointer enters the bounds of the view.</li>
6389 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6390 * when the pointer has already entered the bounds of the view and has moved.</li>
6391 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6392 * when the pointer has exited the bounds of the view or when the pointer is
6393 * about to go down due to a button click, tap, or similar user action that
6394 * causes the view to be touched.</li>
6395 * </ul>
6396 * <p>
6397 * The view should implement this method to return true to indicate that it is
6398 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006399 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006400 * The default implementation calls {@link #setHovered} to update the hovered state
6401 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006402 * is enabled and is clickable. The default implementation also sends hover
6403 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006404 * </p>
6405 *
6406 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006407 * @return True if the view handled the hover event.
6408 *
6409 * @see #isHovered
6410 * @see #setHovered
6411 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006412 */
6413 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006414 // The root view may receive hover (or touch) events that are outside the bounds of
6415 // the window. This code ensures that we only send accessibility events for
6416 // hovers that are actually within the bounds of the root view.
6417 final int action = event.getAction();
6418 if (!mSendingHoverAccessibilityEvents) {
6419 if ((action == MotionEvent.ACTION_HOVER_ENTER
6420 || action == MotionEvent.ACTION_HOVER_MOVE)
6421 && !hasHoveredChild()
6422 && pointInView(event.getX(), event.getY())) {
6423 mSendingHoverAccessibilityEvents = true;
6424 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6425 }
6426 } else {
6427 if (action == MotionEvent.ACTION_HOVER_EXIT
6428 || (action == MotionEvent.ACTION_HOVER_MOVE
6429 && !pointInView(event.getX(), event.getY()))) {
6430 mSendingHoverAccessibilityEvents = false;
6431 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6432 }
Jeff Browna1b24182011-07-28 13:38:24 -07006433 }
6434
Jeff Brown87b7f802011-06-21 18:35:45 -07006435 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006436 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006437 case MotionEvent.ACTION_HOVER_ENTER:
6438 setHovered(true);
6439 break;
6440 case MotionEvent.ACTION_HOVER_EXIT:
6441 setHovered(false);
6442 break;
6443 }
Jeff Browna1b24182011-07-28 13:38:24 -07006444
6445 // Dispatch the event to onGenericMotionEvent before returning true.
6446 // This is to provide compatibility with existing applications that
6447 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6448 // break because of the new default handling for hoverable views
6449 // in onHoverEvent.
6450 // Note that onGenericMotionEvent will be called by default when
6451 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6452 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006453 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006454 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006455 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006456 }
6457
6458 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006459 * Returns true if the view should handle {@link #onHoverEvent}
6460 * by calling {@link #setHovered} to change its hovered state.
6461 *
6462 * @return True if the view is hoverable.
6463 */
6464 private boolean isHoverable() {
6465 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006466 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006467 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6468 return false;
6469 }
6470
6471 return (viewFlags & CLICKABLE) == CLICKABLE
6472 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6473 }
6474
6475 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006476 * Returns true if the view is currently hovered.
6477 *
6478 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006479 *
6480 * @see #setHovered
6481 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006482 */
Jeff Brown10b62902011-06-20 16:40:37 -07006483 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006484 public boolean isHovered() {
6485 return (mPrivateFlags & HOVERED) != 0;
6486 }
6487
6488 /**
6489 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006490 * <p>
6491 * Calling this method also changes the drawable state of the view. This
6492 * enables the view to react to hover by using different drawable resources
6493 * to change its appearance.
6494 * </p><p>
6495 * The {@link #onHoverChanged} method is called when the hovered state changes.
6496 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006497 *
6498 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006499 *
6500 * @see #isHovered
6501 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006502 */
6503 public void setHovered(boolean hovered) {
6504 if (hovered) {
6505 if ((mPrivateFlags & HOVERED) == 0) {
6506 mPrivateFlags |= HOVERED;
6507 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006508 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006509 }
6510 } else {
6511 if ((mPrivateFlags & HOVERED) != 0) {
6512 mPrivateFlags &= ~HOVERED;
6513 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006514 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006515 }
6516 }
6517 }
6518
6519 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006520 * Implement this method to handle hover state changes.
6521 * <p>
6522 * This method is called whenever the hover state changes as a result of a
6523 * call to {@link #setHovered}.
6524 * </p>
6525 *
6526 * @param hovered The current hover state, as returned by {@link #isHovered}.
6527 *
6528 * @see #isHovered
6529 * @see #setHovered
6530 */
6531 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006532 }
6533
6534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 * Implement this method to handle touch screen motion events.
6536 *
6537 * @param event The motion event.
6538 * @return True if the event was handled, false otherwise.
6539 */
6540 public boolean onTouchEvent(MotionEvent event) {
6541 final int viewFlags = mViewFlags;
6542
6543 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006544 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08006545 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 // A disabled view that is clickable still consumes the touch
6548 // events, it just doesn't respond to them.
6549 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6550 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6551 }
6552
6553 if (mTouchDelegate != null) {
6554 if (mTouchDelegate.onTouchEvent(event)) {
6555 return true;
6556 }
6557 }
6558
6559 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6560 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6561 switch (event.getAction()) {
6562 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006563 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6564 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 // take focus if we don't have it already and we should in
6566 // touch mode.
6567 boolean focusTaken = false;
6568 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6569 focusTaken = requestFocus();
6570 }
6571
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006572 if (prepressed) {
6573 // The button is being released before we actually
6574 // showed it as pressed. Make it show the pressed
6575 // state now (before scheduling the click) to ensure
6576 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08006577 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006578 }
Joe Malin32736f02011-01-19 16:14:20 -08006579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 if (!mHasPerformedLongPress) {
6581 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006582 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583
6584 // Only perform take click actions if we were in the pressed state
6585 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006586 // Use a Runnable and post this rather than calling
6587 // performClick directly. This lets other visual state
6588 // of the view update before click actions start.
6589 if (mPerformClick == null) {
6590 mPerformClick = new PerformClick();
6591 }
6592 if (!post(mPerformClick)) {
6593 performClick();
6594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 }
6596 }
6597
6598 if (mUnsetPressedState == null) {
6599 mUnsetPressedState = new UnsetPressedState();
6600 }
6601
Adam Powelle14579b2009-12-16 18:39:52 -08006602 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006603 postDelayed(mUnsetPressedState,
6604 ViewConfiguration.getPressedStateDuration());
6605 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 // If the post failed, unpress right now
6607 mUnsetPressedState.run();
6608 }
Adam Powelle14579b2009-12-16 18:39:52 -08006609 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 }
6611 break;
6612
6613 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006614 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006615
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006616 if (performButtonActionOnTouchDown(event)) {
6617 break;
6618 }
6619
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006620 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006621 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006622
6623 // For views inside a scrolling container, delay the pressed feedback for
6624 // a short period in case this is a scroll.
6625 if (isInScrollingContainer) {
6626 mPrivateFlags |= PREPRESSED;
6627 if (mPendingCheckForTap == null) {
6628 mPendingCheckForTap = new CheckForTap();
6629 }
6630 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6631 } else {
6632 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08006633 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006634 checkForLongClick(0);
6635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 break;
6637
6638 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08006639 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08006640 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 break;
6642
6643 case MotionEvent.ACTION_MOVE:
6644 final int x = (int) event.getX();
6645 final int y = (int) event.getY();
6646
6647 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006648 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006649 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006650 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006652 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006653 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006654
Adam Powell4d6f0662012-02-21 15:11:11 -08006655 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006657 }
6658 break;
6659 }
6660 return true;
6661 }
6662
6663 return false;
6664 }
6665
6666 /**
Adam Powell10298662011-08-14 18:26:30 -07006667 * @hide
6668 */
6669 public boolean isInScrollingContainer() {
6670 ViewParent p = getParent();
6671 while (p != null && p instanceof ViewGroup) {
6672 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6673 return true;
6674 }
6675 p = p.getParent();
6676 }
6677 return false;
6678 }
6679
6680 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006681 * Remove the longpress detection timer.
6682 */
6683 private void removeLongPressCallback() {
6684 if (mPendingCheckForLongPress != null) {
6685 removeCallbacks(mPendingCheckForLongPress);
6686 }
6687 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006688
6689 /**
6690 * Remove the pending click action
6691 */
6692 private void removePerformClickCallback() {
6693 if (mPerformClick != null) {
6694 removeCallbacks(mPerformClick);
6695 }
6696 }
6697
Adam Powelle14579b2009-12-16 18:39:52 -08006698 /**
Romain Guya440b002010-02-24 15:57:54 -08006699 * Remove the prepress detection timer.
6700 */
6701 private void removeUnsetPressCallback() {
6702 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6703 setPressed(false);
6704 removeCallbacks(mUnsetPressedState);
6705 }
6706 }
6707
6708 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006709 * Remove the tap detection timer.
6710 */
6711 private void removeTapCallback() {
6712 if (mPendingCheckForTap != null) {
6713 mPrivateFlags &= ~PREPRESSED;
6714 removeCallbacks(mPendingCheckForTap);
6715 }
6716 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006717
6718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 * Cancels a pending long press. Your subclass can use this if you
6720 * want the context menu to come up if the user presses and holds
6721 * at the same place, but you don't want it to come up if they press
6722 * and then move around enough to cause scrolling.
6723 */
6724 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006725 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006726
6727 /*
6728 * The prepressed state handled by the tap callback is a display
6729 * construct, but the tap callback will post a long press callback
6730 * less its own timeout. Remove it here.
6731 */
6732 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 }
6734
6735 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006736 * Remove the pending callback for sending a
6737 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6738 */
6739 private void removeSendViewScrolledAccessibilityEventCallback() {
6740 if (mSendViewScrolledAccessibilityEvent != null) {
6741 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6742 }
6743 }
6744
6745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 * Sets the TouchDelegate for this View.
6747 */
6748 public void setTouchDelegate(TouchDelegate delegate) {
6749 mTouchDelegate = delegate;
6750 }
6751
6752 /**
6753 * Gets the TouchDelegate for this View.
6754 */
6755 public TouchDelegate getTouchDelegate() {
6756 return mTouchDelegate;
6757 }
6758
6759 /**
6760 * Set flags controlling behavior of this view.
6761 *
6762 * @param flags Constant indicating the value which should be set
6763 * @param mask Constant indicating the bit range that should be changed
6764 */
6765 void setFlags(int flags, int mask) {
6766 int old = mViewFlags;
6767 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6768
6769 int changed = mViewFlags ^ old;
6770 if (changed == 0) {
6771 return;
6772 }
6773 int privateFlags = mPrivateFlags;
6774
6775 /* Check if the FOCUSABLE bit has changed */
6776 if (((changed & FOCUSABLE_MASK) != 0) &&
6777 ((privateFlags & HAS_BOUNDS) !=0)) {
6778 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6779 && ((privateFlags & FOCUSED) != 0)) {
6780 /* Give up focus if we are no longer focusable */
6781 clearFocus();
6782 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6783 && ((privateFlags & FOCUSED) == 0)) {
6784 /*
6785 * Tell the view system that we are now available to take focus
6786 * if no one else already has it.
6787 */
6788 if (mParent != null) mParent.focusableViewAvailable(this);
6789 }
6790 }
6791
6792 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6793 if ((changed & VISIBILITY_MASK) != 0) {
6794 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006795 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006796 * it was not visible. Marking it drawn ensures that the invalidation will
6797 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006798 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006799 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006800 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801
6802 needGlobalAttributesUpdate(true);
6803
6804 // a view becoming visible is worth notifying the parent
6805 // about in case nothing has focus. even if this specific view
6806 // isn't focusable, it may contain something that is, so let
6807 // the root view try to give this focus if nothing else does.
6808 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6809 mParent.focusableViewAvailable(this);
6810 }
6811 }
6812 }
6813
6814 /* Check if the GONE bit has changed */
6815 if ((changed & GONE) != 0) {
6816 needGlobalAttributesUpdate(false);
6817 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818
Romain Guyecd80ee2009-12-03 17:13:02 -08006819 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6820 if (hasFocus()) clearFocus();
6821 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006822 if (mParent instanceof View) {
6823 // GONE views noop invalidation, so invalidate the parent
6824 ((View) mParent).invalidate(true);
6825 }
6826 // Mark the view drawn to ensure that it gets invalidated properly the next
6827 // time it is visible and gets invalidated
6828 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006829 }
6830 if (mAttachInfo != null) {
6831 mAttachInfo.mViewVisibilityChanged = true;
6832 }
6833 }
6834
6835 /* Check if the VISIBLE bit has changed */
6836 if ((changed & INVISIBLE) != 0) {
6837 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006838 /*
6839 * If this view is becoming invisible, set the DRAWN flag so that
6840 * the next invalidate() will not be skipped.
6841 */
6842 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843
6844 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6845 // root view becoming invisible shouldn't clear focus
6846 if (getRootView() != this) {
6847 clearFocus();
6848 }
6849 }
6850 if (mAttachInfo != null) {
6851 mAttachInfo.mViewVisibilityChanged = true;
6852 }
6853 }
6854
Adam Powell326d8082009-12-09 15:10:07 -08006855 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006856 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08006857 ((ViewGroup) mParent).onChildVisibilityChanged(this,
6858 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08006859 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006860 } else if (mParent != null) {
6861 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006862 }
Adam Powell326d8082009-12-09 15:10:07 -08006863 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6864 }
6865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006866 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6867 destroyDrawingCache();
6868 }
6869
6870 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6871 destroyDrawingCache();
6872 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006873 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 }
6875
6876 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6877 destroyDrawingCache();
6878 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6879 }
6880
6881 if ((changed & DRAW_MASK) != 0) {
6882 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6883 if (mBGDrawable != null) {
6884 mPrivateFlags &= ~SKIP_DRAW;
6885 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6886 } else {
6887 mPrivateFlags |= SKIP_DRAW;
6888 }
6889 } else {
6890 mPrivateFlags &= ~SKIP_DRAW;
6891 }
6892 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006893 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 }
6895
6896 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006897 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006898 mParent.recomputeViewAttributes(this);
6899 }
6900 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006901
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006902 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006903 requestLayout();
6904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006905 }
6906
6907 /**
6908 * Change the view's z order in the tree, so it's on top of other sibling
6909 * views
6910 */
6911 public void bringToFront() {
6912 if (mParent != null) {
6913 mParent.bringChildToFront(this);
6914 }
6915 }
6916
6917 /**
6918 * This is called in response to an internal scroll in this view (i.e., the
6919 * view scrolled its own contents). This is typically as a result of
6920 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6921 * called.
6922 *
6923 * @param l Current horizontal scroll origin.
6924 * @param t Current vertical scroll origin.
6925 * @param oldl Previous horizontal scroll origin.
6926 * @param oldt Previous vertical scroll origin.
6927 */
6928 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006929 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6930 postSendViewScrolledAccessibilityEventCallback();
6931 }
6932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 mBackgroundSizeChanged = true;
6934
6935 final AttachInfo ai = mAttachInfo;
6936 if (ai != null) {
6937 ai.mViewScrollChanged = true;
6938 }
6939 }
6940
6941 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006942 * Interface definition for a callback to be invoked when the layout bounds of a view
6943 * changes due to layout processing.
6944 */
6945 public interface OnLayoutChangeListener {
6946 /**
6947 * Called when the focus state of a view has changed.
6948 *
6949 * @param v The view whose state has changed.
6950 * @param left The new value of the view's left property.
6951 * @param top The new value of the view's top property.
6952 * @param right The new value of the view's right property.
6953 * @param bottom The new value of the view's bottom property.
6954 * @param oldLeft The previous value of the view's left property.
6955 * @param oldTop The previous value of the view's top property.
6956 * @param oldRight The previous value of the view's right property.
6957 * @param oldBottom The previous value of the view's bottom property.
6958 */
6959 void onLayoutChange(View v, int left, int top, int right, int bottom,
6960 int oldLeft, int oldTop, int oldRight, int oldBottom);
6961 }
6962
6963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 * This is called during layout when the size of this view has changed. If
6965 * you were just added to the view hierarchy, you're called with the old
6966 * values of 0.
6967 *
6968 * @param w Current width of this view.
6969 * @param h Current height of this view.
6970 * @param oldw Old width of this view.
6971 * @param oldh Old height of this view.
6972 */
6973 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6974 }
6975
6976 /**
6977 * Called by draw to draw the child views. This may be overridden
6978 * by derived classes to gain control just before its children are drawn
6979 * (but after its own view has been drawn).
6980 * @param canvas the canvas on which to draw the view
6981 */
6982 protected void dispatchDraw(Canvas canvas) {
6983 }
6984
6985 /**
6986 * Gets the parent of this view. Note that the parent is a
6987 * ViewParent and not necessarily a View.
6988 *
6989 * @return Parent of this view.
6990 */
6991 public final ViewParent getParent() {
6992 return mParent;
6993 }
6994
6995 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006996 * Set the horizontal scrolled position of your view. This will cause a call to
6997 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6998 * invalidated.
6999 * @param value the x position to scroll to
7000 */
7001 public void setScrollX(int value) {
7002 scrollTo(value, mScrollY);
7003 }
7004
7005 /**
7006 * Set the vertical scrolled position of your view. This will cause a call to
7007 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7008 * invalidated.
7009 * @param value the y position to scroll to
7010 */
7011 public void setScrollY(int value) {
7012 scrollTo(mScrollX, value);
7013 }
7014
7015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016 * Return the scrolled left position of this view. This is the left edge of
7017 * the displayed part of your view. You do not need to draw any pixels
7018 * farther left, since those are outside of the frame of your view on
7019 * screen.
7020 *
7021 * @return The left edge of the displayed part of your view, in pixels.
7022 */
7023 public final int getScrollX() {
7024 return mScrollX;
7025 }
7026
7027 /**
7028 * Return the scrolled top position of this view. This is the top edge of
7029 * the displayed part of your view. You do not need to draw any pixels above
7030 * it, since those are outside of the frame of your view on screen.
7031 *
7032 * @return The top edge of the displayed part of your view, in pixels.
7033 */
7034 public final int getScrollY() {
7035 return mScrollY;
7036 }
7037
7038 /**
7039 * Return the width of the your view.
7040 *
7041 * @return The width of your view, in pixels.
7042 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007043 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 public final int getWidth() {
7045 return mRight - mLeft;
7046 }
7047
7048 /**
7049 * Return the height of your view.
7050 *
7051 * @return The height of your view, in pixels.
7052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007053 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 public final int getHeight() {
7055 return mBottom - mTop;
7056 }
7057
7058 /**
7059 * Return the visible drawing bounds of your view. Fills in the output
7060 * rectangle with the values from getScrollX(), getScrollY(),
7061 * getWidth(), and getHeight().
7062 *
7063 * @param outRect The (scrolled) drawing bounds of the view.
7064 */
7065 public void getDrawingRect(Rect outRect) {
7066 outRect.left = mScrollX;
7067 outRect.top = mScrollY;
7068 outRect.right = mScrollX + (mRight - mLeft);
7069 outRect.bottom = mScrollY + (mBottom - mTop);
7070 }
7071
7072 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007073 * Like {@link #getMeasuredWidthAndState()}, but only returns the
7074 * raw width component (that is the result is masked by
7075 * {@link #MEASURED_SIZE_MASK}).
7076 *
7077 * @return The raw measured width of this view.
7078 */
7079 public final int getMeasuredWidth() {
7080 return mMeasuredWidth & MEASURED_SIZE_MASK;
7081 }
7082
7083 /**
7084 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007085 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007086 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 * This should be used during measurement and layout calculations only. Use
7088 * {@link #getWidth()} to see how wide a view is after layout.
7089 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007090 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08007092 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 return mMeasuredWidth;
7094 }
7095
7096 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007097 * Like {@link #getMeasuredHeightAndState()}, but only returns the
7098 * raw width component (that is the result is masked by
7099 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08007101 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 */
7103 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08007104 return mMeasuredHeight & MEASURED_SIZE_MASK;
7105 }
7106
7107 /**
7108 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07007109 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08007110 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
7111 * This should be used during measurement and layout calculations only. Use
7112 * {@link #getHeight()} to see how wide a view is after layout.
7113 *
7114 * @return The measured width of this view as a bit mask.
7115 */
7116 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117 return mMeasuredHeight;
7118 }
7119
7120 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08007121 * Return only the state bits of {@link #getMeasuredWidthAndState()}
7122 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
7123 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
7124 * and the height component is at the shifted bits
7125 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
7126 */
7127 public final int getMeasuredState() {
7128 return (mMeasuredWidth&MEASURED_STATE_MASK)
7129 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
7130 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
7131 }
7132
7133 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007134 * The transform matrix of this view, which is calculated based on the current
7135 * roation, scale, and pivot properties.
7136 *
7137 * @see #getRotation()
7138 * @see #getScaleX()
7139 * @see #getScaleY()
7140 * @see #getPivotX()
7141 * @see #getPivotY()
7142 * @return The current transform matrix for the view
7143 */
7144 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007145 if (mTransformationInfo != null) {
7146 updateMatrix();
7147 return mTransformationInfo.mMatrix;
7148 }
7149 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07007150 }
7151
7152 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007153 * Utility function to determine if the value is far enough away from zero to be
7154 * considered non-zero.
7155 * @param value A floating point value to check for zero-ness
7156 * @return whether the passed-in value is far enough away from zero to be considered non-zero
7157 */
7158 private static boolean nonzero(float value) {
7159 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
7160 }
7161
7162 /**
Jeff Brown86671742010-09-30 20:00:15 -07007163 * Returns true if the transform matrix is the identity matrix.
7164 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08007165 *
Romain Guy33e72ae2010-07-17 12:40:29 -07007166 * @return True if the transform matrix is the identity matrix, false otherwise.
7167 */
Jeff Brown86671742010-09-30 20:00:15 -07007168 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007169 if (mTransformationInfo != null) {
7170 updateMatrix();
7171 return mTransformationInfo.mMatrixIsIdentity;
7172 }
7173 return true;
7174 }
7175
7176 void ensureTransformationInfo() {
7177 if (mTransformationInfo == null) {
7178 mTransformationInfo = new TransformationInfo();
7179 }
Jeff Brown86671742010-09-30 20:00:15 -07007180 }
7181
7182 /**
7183 * Recomputes the transform matrix if necessary.
7184 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007185 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007186 final TransformationInfo info = mTransformationInfo;
7187 if (info == null) {
7188 return;
7189 }
7190 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007191 // transform-related properties have changed since the last time someone
7192 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07007193
7194 // Figure out if we need to update the pivot point
7195 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007196 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
7197 info.mPrevWidth = mRight - mLeft;
7198 info.mPrevHeight = mBottom - mTop;
7199 info.mPivotX = info.mPrevWidth / 2f;
7200 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07007201 }
7202 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007203 info.mMatrix.reset();
7204 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
7205 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
7206 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
7207 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07007208 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007209 if (info.mCamera == null) {
7210 info.mCamera = new Camera();
7211 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07007212 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007213 info.mCamera.save();
7214 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
7215 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
7216 info.mCamera.getMatrix(info.matrix3D);
7217 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
7218 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
7219 info.mPivotY + info.mTranslationY);
7220 info.mMatrix.postConcat(info.matrix3D);
7221 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07007222 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007223 info.mMatrixDirty = false;
7224 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
7225 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007226 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 }
7228
7229 /**
7230 * Utility method to retrieve the inverse of the current mMatrix property.
7231 * We cache the matrix to avoid recalculating it when transform properties
7232 * have not changed.
7233 *
7234 * @return The inverse of the current matrix of this view.
7235 */
Jeff Brown86671742010-09-30 20:00:15 -07007236 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007237 final TransformationInfo info = mTransformationInfo;
7238 if (info != null) {
7239 updateMatrix();
7240 if (info.mInverseMatrixDirty) {
7241 if (info.mInverseMatrix == null) {
7242 info.mInverseMatrix = new Matrix();
7243 }
7244 info.mMatrix.invert(info.mInverseMatrix);
7245 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07007246 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007247 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07007248 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007249 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07007250 }
7251
7252 /**
Romain Guya5364ee2011-02-24 14:46:04 -08007253 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
7254 * views are drawn) from the camera to this view. The camera's distance
7255 * affects 3D transformations, for instance rotations around the X and Y
7256 * axis. If the rotationX or rotationY properties are changed and this view is
7257 * large (more than half the size of the screen), it is recommended to always
7258 * use a camera distance that's greater than the height (X axis rotation) or
7259 * the width (Y axis rotation) of this view.</p>
7260 *
7261 * <p>The distance of the camera from the view plane can have an affect on the
7262 * perspective distortion of the view when it is rotated around the x or y axis.
7263 * For example, a large distance will result in a large viewing angle, and there
7264 * will not be much perspective distortion of the view as it rotates. A short
7265 * distance may cause much more perspective distortion upon rotation, and can
7266 * also result in some drawing artifacts if the rotated view ends up partially
7267 * behind the camera (which is why the recommendation is to use a distance at
7268 * least as far as the size of the view, if the view is to be rotated.)</p>
7269 *
7270 * <p>The distance is expressed in "depth pixels." The default distance depends
7271 * on the screen density. For instance, on a medium density display, the
7272 * default distance is 1280. On a high density display, the default distance
7273 * is 1920.</p>
7274 *
7275 * <p>If you want to specify a distance that leads to visually consistent
7276 * results across various densities, use the following formula:</p>
7277 * <pre>
7278 * float scale = context.getResources().getDisplayMetrics().density;
7279 * view.setCameraDistance(distance * scale);
7280 * </pre>
7281 *
7282 * <p>The density scale factor of a high density display is 1.5,
7283 * and 1920 = 1280 * 1.5.</p>
7284 *
7285 * @param distance The distance in "depth pixels", if negative the opposite
7286 * value is used
7287 *
7288 * @see #setRotationX(float)
7289 * @see #setRotationY(float)
7290 */
7291 public void setCameraDistance(float distance) {
7292 invalidateParentCaches();
7293 invalidate(false);
7294
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007295 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08007296 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007297 final TransformationInfo info = mTransformationInfo;
7298 if (info.mCamera == null) {
7299 info.mCamera = new Camera();
7300 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08007301 }
7302
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007303 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
7304 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08007305
7306 invalidate(false);
7307 }
7308
7309 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007310 * The degrees that the view is rotated around the pivot point.
7311 *
Romain Guya5364ee2011-02-24 14:46:04 -08007312 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07007313 * @see #getPivotX()
7314 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007315 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007316 * @return The degrees of rotation.
7317 */
Chet Haasea5531132012-02-02 13:41:44 -08007318 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007319 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007320 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007321 }
7322
7323 /**
Chet Haase897247b2010-09-09 14:54:47 -07007324 * Sets the degrees that the view is rotated around the pivot point. Increasing values
7325 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07007326 *
7327 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007328 *
7329 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07007330 * @see #getPivotX()
7331 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007332 * @see #setRotationX(float)
7333 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08007334 *
7335 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07007336 */
7337 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007338 ensureTransformationInfo();
7339 final TransformationInfo info = mTransformationInfo;
7340 if (info.mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007341 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007342 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007343 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007344 info.mRotation = rotation;
7345 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007346 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007347 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007348 }
7349 }
7350
7351 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07007352 * The degrees that the view is rotated around the vertical axis through the pivot point.
7353 *
7354 * @see #getPivotX()
7355 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007356 * @see #setRotationY(float)
7357 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007358 * @return The degrees of Y rotation.
7359 */
Chet Haasea5531132012-02-02 13:41:44 -08007360 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007361 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007362 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007363 }
7364
7365 /**
Chet Haase897247b2010-09-09 14:54:47 -07007366 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
7367 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
7368 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007369 *
7370 * When rotating large views, it is recommended to adjust the camera distance
7371 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007372 *
7373 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007374 *
7375 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07007376 * @see #getPivotX()
7377 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007378 * @see #setRotation(float)
7379 * @see #setRotationX(float)
7380 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007381 *
7382 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07007383 */
7384 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007385 ensureTransformationInfo();
7386 final TransformationInfo info = mTransformationInfo;
7387 if (info.mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007388 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007389 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007390 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007391 info.mRotationY = rotationY;
7392 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007393 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007394 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007395 }
7396 }
7397
7398 /**
7399 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7400 *
7401 * @see #getPivotX()
7402 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007403 * @see #setRotationX(float)
7404 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007405 * @return The degrees of X rotation.
7406 */
Chet Haasea5531132012-02-02 13:41:44 -08007407 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07007408 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007409 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07007410 }
7411
7412 /**
Chet Haase897247b2010-09-09 14:54:47 -07007413 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7414 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7415 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007416 *
7417 * When rotating large views, it is recommended to adjust the camera distance
7418 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007419 *
7420 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007421 *
7422 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007423 * @see #getPivotX()
7424 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007425 * @see #setRotation(float)
7426 * @see #setRotationY(float)
7427 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007428 *
7429 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007430 */
7431 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007432 ensureTransformationInfo();
7433 final TransformationInfo info = mTransformationInfo;
7434 if (info.mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007435 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007436 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007437 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007438 info.mRotationX = rotationX;
7439 info.mMatrixDirty = true;
Chet Haasefd2b0022010-08-06 13:08:56 -07007440 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007441 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007442 }
7443 }
7444
7445 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007446 * The amount that the view is scaled in x around the pivot point, as a proportion of
7447 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7448 *
Joe Onorato93162322010-09-16 15:42:01 -04007449 * <p>By default, this is 1.0f.
7450 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007451 * @see #getPivotX()
7452 * @see #getPivotY()
7453 * @return The scaling factor.
7454 */
Chet Haasea5531132012-02-02 13:41:44 -08007455 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007456 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007457 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007458 }
7459
7460 /**
7461 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7462 * the view's unscaled width. A value of 1 means that no scaling is applied.
7463 *
7464 * @param scaleX The scaling factor.
7465 * @see #getPivotX()
7466 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007467 *
7468 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007469 */
7470 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007471 ensureTransformationInfo();
7472 final TransformationInfo info = mTransformationInfo;
7473 if (info.mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007474 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007475 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007476 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007477 info.mScaleX = scaleX;
7478 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007479 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007480 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007481 }
7482 }
7483
7484 /**
7485 * The amount that the view is scaled in y around the pivot point, as a proportion of
7486 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7487 *
Joe Onorato93162322010-09-16 15:42:01 -04007488 * <p>By default, this is 1.0f.
7489 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007490 * @see #getPivotX()
7491 * @see #getPivotY()
7492 * @return The scaling factor.
7493 */
Chet Haasea5531132012-02-02 13:41:44 -08007494 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007495 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007496 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007497 }
7498
7499 /**
7500 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7501 * the view's unscaled width. A value of 1 means that no scaling is applied.
7502 *
7503 * @param scaleY The scaling factor.
7504 * @see #getPivotX()
7505 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007506 *
7507 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 */
7509 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007510 ensureTransformationInfo();
7511 final TransformationInfo info = mTransformationInfo;
7512 if (info.mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007513 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007514 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007515 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007516 info.mScaleY = scaleY;
7517 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007518 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007519 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007520 }
7521 }
7522
7523 /**
7524 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7525 * and {@link #setScaleX(float) scaled}.
7526 *
7527 * @see #getRotation()
7528 * @see #getScaleX()
7529 * @see #getScaleY()
7530 * @see #getPivotY()
7531 * @return The x location of the pivot point.
7532 */
Chet Haasea5531132012-02-02 13:41:44 -08007533 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007534 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007535 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007536 }
7537
7538 /**
7539 * Sets the x location of the point around which the view is
7540 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007541 * By default, the pivot point is centered on the object.
7542 * Setting this property disables this behavior and causes the view to use only the
7543 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007544 *
7545 * @param pivotX The x location of the pivot point.
7546 * @see #getRotation()
7547 * @see #getScaleX()
7548 * @see #getScaleY()
7549 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007550 *
7551 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007552 */
7553 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007554 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007555 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007556 final TransformationInfo info = mTransformationInfo;
7557 if (info.mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007558 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007559 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007560 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007561 info.mPivotX = pivotX;
7562 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007563 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007564 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007565 }
7566 }
7567
7568 /**
7569 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7570 * and {@link #setScaleY(float) scaled}.
7571 *
7572 * @see #getRotation()
7573 * @see #getScaleX()
7574 * @see #getScaleY()
7575 * @see #getPivotY()
7576 * @return The y location of the pivot point.
7577 */
Chet Haasea5531132012-02-02 13:41:44 -08007578 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007579 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007580 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07007581 }
7582
7583 /**
7584 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007585 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7586 * Setting this property disables this behavior and causes the view to use only the
7587 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007588 *
7589 * @param pivotY The y location of the pivot point.
7590 * @see #getRotation()
7591 * @see #getScaleX()
7592 * @see #getScaleY()
7593 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007594 *
7595 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 */
7597 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007598 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07007599 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007600 final TransformationInfo info = mTransformationInfo;
7601 if (info.mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007602 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007603 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007604 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007605 info.mPivotY = pivotY;
7606 info.mMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07007607 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007608 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007609 }
7610 }
7611
7612 /**
7613 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7614 * completely transparent and 1 means the view is completely opaque.
7615 *
Joe Onorato93162322010-09-16 15:42:01 -04007616 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007617 * @return The opacity of the view.
7618 */
Chet Haasea5531132012-02-02 13:41:44 -08007619 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07007620 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007621 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07007622 }
7623
7624 /**
Romain Guy171c5922011-01-06 10:04:23 -08007625 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7626 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007627 *
Romain Guy171c5922011-01-06 10:04:23 -08007628 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7629 * responsible for applying the opacity itself. Otherwise, calling this method is
7630 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007631 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007632 *
Chet Haasea5531132012-02-02 13:41:44 -08007633 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
7634 * performance implications. It is generally best to use the alpha property sparingly and
7635 * transiently, as in the case of fading animations.</p>
7636 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007637 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007638 *
Joe Malin32736f02011-01-19 16:14:20 -08007639 * @see #setLayerType(int, android.graphics.Paint)
7640 *
Chet Haase73066682010-11-29 15:55:32 -08007641 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007642 */
7643 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007644 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007645 if (mTransformationInfo.mAlpha != alpha) {
7646 mTransformationInfo.mAlpha = alpha;
7647 invalidateParentCaches();
7648 if (onSetAlpha((int) (alpha * 255))) {
7649 mPrivateFlags |= ALPHA_SET;
7650 // subclass is handling alpha - don't optimize rendering cache invalidation
7651 invalidate(true);
7652 } else {
7653 mPrivateFlags &= ~ALPHA_SET;
7654 invalidate(false);
7655 }
Chet Haaseed032702010-10-01 14:05:54 -07007656 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007657 }
7658
7659 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007660 * Faster version of setAlpha() which performs the same steps except there are
7661 * no calls to invalidate(). The caller of this function should perform proper invalidation
7662 * on the parent and this object. The return value indicates whether the subclass handles
7663 * alpha (the return value for onSetAlpha()).
7664 *
7665 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007666 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
7667 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08007668 */
7669 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007670 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007671 if (mTransformationInfo.mAlpha != alpha) {
7672 mTransformationInfo.mAlpha = alpha;
7673 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7674 if (subclassHandlesAlpha) {
7675 mPrivateFlags |= ALPHA_SET;
7676 return true;
7677 } else {
7678 mPrivateFlags &= ~ALPHA_SET;
7679 }
Chet Haasea00f3862011-02-22 06:34:40 -08007680 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08007681 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08007682 }
7683
7684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 * Top position of this view relative to its parent.
7686 *
7687 * @return The top of this view, in pixels.
7688 */
7689 @ViewDebug.CapturedViewProperty
7690 public final int getTop() {
7691 return mTop;
7692 }
7693
7694 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007695 * Sets the top position of this view relative to its parent. This method is meant to be called
7696 * by the layout system and should not generally be called otherwise, because the property
7697 * may be changed at any time by the layout.
7698 *
7699 * @param top The top of this view, in pixels.
7700 */
7701 public final void setTop(int top) {
7702 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007703 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007704 final boolean matrixIsIdentity = mTransformationInfo == null
7705 || mTransformationInfo.mMatrixIsIdentity;
7706 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007707 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007708 int minTop;
7709 int yLoc;
7710 if (top < mTop) {
7711 minTop = top;
7712 yLoc = top - mTop;
7713 } else {
7714 minTop = mTop;
7715 yLoc = 0;
7716 }
Chet Haasee9140a72011-02-16 16:23:29 -08007717 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007718 }
7719 } else {
7720 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007721 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007722 }
7723
Chet Haaseed032702010-10-01 14:05:54 -07007724 int width = mRight - mLeft;
7725 int oldHeight = mBottom - mTop;
7726
Chet Haase21cd1382010-09-01 17:42:29 -07007727 mTop = top;
7728
Chet Haaseed032702010-10-01 14:05:54 -07007729 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7730
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007731 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007732 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7733 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007734 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007735 }
Chet Haase21cd1382010-09-01 17:42:29 -07007736 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007737 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007738 }
Chet Haase55dbb652010-12-21 20:15:08 -08007739 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007740 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007741 }
7742 }
7743
7744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 * Bottom position of this view relative to its parent.
7746 *
7747 * @return The bottom of this view, in pixels.
7748 */
7749 @ViewDebug.CapturedViewProperty
7750 public final int getBottom() {
7751 return mBottom;
7752 }
7753
7754 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007755 * True if this view has changed since the last time being drawn.
7756 *
7757 * @return The dirty state of this view.
7758 */
7759 public boolean isDirty() {
7760 return (mPrivateFlags & DIRTY_MASK) != 0;
7761 }
7762
7763 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007764 * Sets the bottom position of this view relative to its parent. This method is meant to be
7765 * called by the layout system and should not generally be called otherwise, because the
7766 * property may be changed at any time by the layout.
7767 *
7768 * @param bottom The bottom of this view, in pixels.
7769 */
7770 public final void setBottom(int bottom) {
7771 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007772 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007773 final boolean matrixIsIdentity = mTransformationInfo == null
7774 || mTransformationInfo.mMatrixIsIdentity;
7775 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007776 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007777 int maxBottom;
7778 if (bottom < mBottom) {
7779 maxBottom = mBottom;
7780 } else {
7781 maxBottom = bottom;
7782 }
Chet Haasee9140a72011-02-16 16:23:29 -08007783 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007784 }
7785 } else {
7786 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007787 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007788 }
7789
Chet Haaseed032702010-10-01 14:05:54 -07007790 int width = mRight - mLeft;
7791 int oldHeight = mBottom - mTop;
7792
Chet Haase21cd1382010-09-01 17:42:29 -07007793 mBottom = bottom;
7794
Chet Haaseed032702010-10-01 14:05:54 -07007795 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7796
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007797 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007798 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7799 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007800 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007801 }
Chet Haase21cd1382010-09-01 17:42:29 -07007802 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007803 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007804 }
Chet Haase55dbb652010-12-21 20:15:08 -08007805 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007806 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007807 }
7808 }
7809
7810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 * Left position of this view relative to its parent.
7812 *
7813 * @return The left edge of this view, in pixels.
7814 */
7815 @ViewDebug.CapturedViewProperty
7816 public final int getLeft() {
7817 return mLeft;
7818 }
7819
7820 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007821 * Sets the left position of this view relative to its parent. This method is meant to be called
7822 * by the layout system and should not generally be called otherwise, because the property
7823 * may be changed at any time by the layout.
7824 *
7825 * @param left The bottom of this view, in pixels.
7826 */
7827 public final void setLeft(int left) {
7828 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007829 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007830 final boolean matrixIsIdentity = mTransformationInfo == null
7831 || mTransformationInfo.mMatrixIsIdentity;
7832 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007833 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007834 int minLeft;
7835 int xLoc;
7836 if (left < mLeft) {
7837 minLeft = left;
7838 xLoc = left - mLeft;
7839 } else {
7840 minLeft = mLeft;
7841 xLoc = 0;
7842 }
Chet Haasee9140a72011-02-16 16:23:29 -08007843 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007844 }
7845 } else {
7846 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007847 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007848 }
7849
Chet Haaseed032702010-10-01 14:05:54 -07007850 int oldWidth = mRight - mLeft;
7851 int height = mBottom - mTop;
7852
Chet Haase21cd1382010-09-01 17:42:29 -07007853 mLeft = left;
7854
Chet Haaseed032702010-10-01 14:05:54 -07007855 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7856
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007857 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007858 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7859 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007860 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007861 }
Chet Haase21cd1382010-09-01 17:42:29 -07007862 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007863 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007864 }
Chet Haase55dbb652010-12-21 20:15:08 -08007865 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007866 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007867 }
7868 }
7869
7870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007871 * Right position of this view relative to its parent.
7872 *
7873 * @return The right edge of this view, in pixels.
7874 */
7875 @ViewDebug.CapturedViewProperty
7876 public final int getRight() {
7877 return mRight;
7878 }
7879
7880 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007881 * Sets the right position of this view relative to its parent. This method is meant to be called
7882 * by the layout system and should not generally be called otherwise, because the property
7883 * may be changed at any time by the layout.
7884 *
7885 * @param right The bottom of this view, in pixels.
7886 */
7887 public final void setRight(int right) {
7888 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007889 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007890 final boolean matrixIsIdentity = mTransformationInfo == null
7891 || mTransformationInfo.mMatrixIsIdentity;
7892 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007893 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007894 int maxRight;
7895 if (right < mRight) {
7896 maxRight = mRight;
7897 } else {
7898 maxRight = right;
7899 }
Chet Haasee9140a72011-02-16 16:23:29 -08007900 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007901 }
7902 } else {
7903 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007904 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007905 }
7906
Chet Haaseed032702010-10-01 14:05:54 -07007907 int oldWidth = mRight - mLeft;
7908 int height = mBottom - mTop;
7909
Chet Haase21cd1382010-09-01 17:42:29 -07007910 mRight = right;
7911
Chet Haaseed032702010-10-01 14:05:54 -07007912 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7913
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007914 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007915 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7916 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007917 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007918 }
Chet Haase21cd1382010-09-01 17:42:29 -07007919 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007920 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007921 }
Chet Haase55dbb652010-12-21 20:15:08 -08007922 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007923 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007924 }
7925 }
7926
7927 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007928 * The visual x position of this view, in pixels. This is equivalent to the
7929 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007930 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007931 *
Chet Haasedf030d22010-07-30 17:22:38 -07007932 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007933 */
Chet Haasea5531132012-02-02 13:41:44 -08007934 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007935 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007936 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007937 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007938
Chet Haasedf030d22010-07-30 17:22:38 -07007939 /**
7940 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7941 * {@link #setTranslationX(float) translationX} property to be the difference between
7942 * the x value passed in and the current {@link #getLeft() left} property.
7943 *
7944 * @param x The visual x position of this view, in pixels.
7945 */
7946 public void setX(float x) {
7947 setTranslationX(x - mLeft);
7948 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007949
Chet Haasedf030d22010-07-30 17:22:38 -07007950 /**
7951 * The visual y position of this view, in pixels. This is equivalent to the
7952 * {@link #setTranslationY(float) translationY} property plus the current
7953 * {@link #getTop() top} property.
7954 *
7955 * @return The visual y position of this view, in pixels.
7956 */
Chet Haasea5531132012-02-02 13:41:44 -08007957 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007958 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007959 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07007960 }
7961
7962 /**
7963 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7964 * {@link #setTranslationY(float) translationY} property to be the difference between
7965 * the y value passed in and the current {@link #getTop() top} property.
7966 *
7967 * @param y The visual y position of this view, in pixels.
7968 */
7969 public void setY(float y) {
7970 setTranslationY(y - mTop);
7971 }
7972
7973
7974 /**
7975 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7976 * This position is post-layout, in addition to wherever the object's
7977 * layout placed it.
7978 *
7979 * @return The horizontal position of this view relative to its left position, in pixels.
7980 */
Chet Haasea5531132012-02-02 13:41:44 -08007981 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07007982 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007983 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07007984 }
7985
7986 /**
7987 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7988 * This effectively positions the object post-layout, in addition to wherever the object's
7989 * layout placed it.
7990 *
7991 * @param translationX The horizontal position of this view relative to its left position,
7992 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007993 *
7994 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007995 */
7996 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07007997 ensureTransformationInfo();
7998 final TransformationInfo info = mTransformationInfo;
7999 if (info.mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008000 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008001 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008002 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008003 info.mTranslationX = translationX;
8004 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008005 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008006 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008007 }
8008 }
8009
8010 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008011 * The horizontal location of this view relative to its {@link #getTop() top} position.
8012 * This position is post-layout, in addition to wherever the object's
8013 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008014 *
Chet Haasedf030d22010-07-30 17:22:38 -07008015 * @return The vertical position of this view relative to its top position,
8016 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07008017 */
Chet Haasea5531132012-02-02 13:41:44 -08008018 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07008019 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008020 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008021 }
8022
8023 /**
Chet Haasedf030d22010-07-30 17:22:38 -07008024 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
8025 * This effectively positions the object post-layout, in addition to wherever the object's
8026 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07008027 *
Chet Haasedf030d22010-07-30 17:22:38 -07008028 * @param translationY The vertical position of this view relative to its top position,
8029 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08008030 *
8031 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07008032 */
Chet Haasedf030d22010-07-30 17:22:38 -07008033 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008034 ensureTransformationInfo();
8035 final TransformationInfo info = mTransformationInfo;
8036 if (info.mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008037 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07008038 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07008039 invalidate(false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008040 info.mTranslationY = translationY;
8041 info.mMatrixDirty = true;
Chet Haasedf030d22010-07-30 17:22:38 -07008042 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008043 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07008044 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008045 }
8046
8047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 * Hit rectangle in parent's coordinates
8049 *
8050 * @param outRect The hit rectangle of the view.
8051 */
8052 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07008053 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008054 final TransformationInfo info = mTransformationInfo;
8055 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008056 outRect.set(mLeft, mTop, mRight, mBottom);
8057 } else {
8058 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008059 tmpRect.set(-info.mPivotX, -info.mPivotY,
8060 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
8061 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07008062 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
8063 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07008064 }
8065 }
8066
8067 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07008068 * Determines whether the given point, in local coordinates is inside the view.
8069 */
8070 /*package*/ final boolean pointInView(float localX, float localY) {
8071 return localX >= 0 && localX < (mRight - mLeft)
8072 && localY >= 0 && localY < (mBottom - mTop);
8073 }
8074
8075 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008076 * Utility method to determine whether the given point, in local coordinates,
8077 * is inside the view, where the area of the view is expanded by the slop factor.
8078 * This method is called while processing touch-move events to determine if the event
8079 * is still within the view.
8080 */
8081 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07008082 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07008083 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 }
8085
8086 /**
8087 * When a view has focus and the user navigates away from it, the next view is searched for
8088 * starting from the rectangle filled in by this method.
8089 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07008090 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
8091 * of the view. However, if your view maintains some idea of internal selection,
8092 * such as a cursor, or a selected row or column, you should override this method and
8093 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 *
8095 * @param r The rectangle to fill in, in this view's coordinates.
8096 */
8097 public void getFocusedRect(Rect r) {
8098 getDrawingRect(r);
8099 }
8100
8101 /**
8102 * If some part of this view is not clipped by any of its parents, then
8103 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01008104 * coordinates (without taking possible View rotations into account), offset
8105 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
8106 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 *
8108 * @param r If true is returned, r holds the global coordinates of the
8109 * visible portion of this view.
8110 * @param globalOffset If true is returned, globalOffset holds the dx,dy
8111 * between this view and its root. globalOffet may be null.
8112 * @return true if r is non-empty (i.e. part of the view is visible at the
8113 * root level.
8114 */
8115 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
8116 int width = mRight - mLeft;
8117 int height = mBottom - mTop;
8118 if (width > 0 && height > 0) {
8119 r.set(0, 0, width, height);
8120 if (globalOffset != null) {
8121 globalOffset.set(-mScrollX, -mScrollY);
8122 }
8123 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
8124 }
8125 return false;
8126 }
8127
8128 public final boolean getGlobalVisibleRect(Rect r) {
8129 return getGlobalVisibleRect(r, null);
8130 }
8131
8132 public final boolean getLocalVisibleRect(Rect r) {
8133 Point offset = new Point();
8134 if (getGlobalVisibleRect(r, offset)) {
8135 r.offset(-offset.x, -offset.y); // make r local
8136 return true;
8137 }
8138 return false;
8139 }
8140
8141 /**
8142 * Offset this view's vertical location by the specified number of pixels.
8143 *
8144 * @param offset the number of pixels to offset the view by
8145 */
8146 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008147 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008148 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008149 final boolean matrixIsIdentity = mTransformationInfo == null
8150 || mTransformationInfo.mMatrixIsIdentity;
8151 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008152 final ViewParent p = mParent;
8153 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008154 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008155 int minTop;
8156 int maxBottom;
8157 int yLoc;
8158 if (offset < 0) {
8159 minTop = mTop + offset;
8160 maxBottom = mBottom;
8161 yLoc = offset;
8162 } else {
8163 minTop = mTop;
8164 maxBottom = mBottom + offset;
8165 yLoc = 0;
8166 }
8167 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
8168 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008169 }
8170 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008171 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008172 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008173
Chet Haasec3aa3612010-06-17 08:50:37 -07008174 mTop += offset;
8175 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008176
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008177 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008179 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008180 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008181 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 }
8184
8185 /**
8186 * Offset this view's horizontal location by the specified amount of pixels.
8187 *
8188 * @param offset the numer of pixels to offset the view by
8189 */
8190 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008191 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07008192 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008193 final boolean matrixIsIdentity = mTransformationInfo == null
8194 || mTransformationInfo.mMatrixIsIdentity;
8195 if (matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008196 final ViewParent p = mParent;
8197 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008198 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008199 int minLeft;
8200 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008201 if (offset < 0) {
8202 minLeft = mLeft + offset;
8203 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008204 } else {
8205 minLeft = mLeft;
8206 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07008207 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008208 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07008209 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07008210 }
8211 } else {
Chet Haaseed032702010-10-01 14:05:54 -07008212 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008213 }
Romain Guy33e72ae2010-07-17 12:40:29 -07008214
Chet Haasec3aa3612010-06-17 08:50:37 -07008215 mLeft += offset;
8216 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07008217
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008218 if (!matrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008219 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07008220 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07008221 }
Chet Haase678e0ad2011-01-25 09:37:18 -08008222 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07008223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 }
8225
8226 /**
8227 * Get the LayoutParams associated with this view. All views should have
8228 * layout parameters. These supply parameters to the <i>parent</i> of this
8229 * view specifying how it should be arranged. There are many subclasses of
8230 * ViewGroup.LayoutParams, and these correspond to the different subclasses
8231 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08008232 *
8233 * This method may return null if this View is not attached to a parent
8234 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
8235 * was not invoked successfully. When a View is attached to a parent
8236 * ViewGroup, this method must not return null.
8237 *
8238 * @return The LayoutParams associated with this view, or null if no
8239 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07008241 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 public ViewGroup.LayoutParams getLayoutParams() {
8243 return mLayoutParams;
8244 }
8245
8246 /**
8247 * Set the layout parameters associated with this view. These supply
8248 * parameters to the <i>parent</i> of this view specifying how it should be
8249 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
8250 * correspond to the different subclasses of ViewGroup that are responsible
8251 * for arranging their children.
8252 *
Romain Guy01c174b2011-02-22 11:51:06 -08008253 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 */
8255 public void setLayoutParams(ViewGroup.LayoutParams params) {
8256 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08008257 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 }
8259 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08008260 if (mParent instanceof ViewGroup) {
8261 ((ViewGroup) mParent).onSetLayoutParams(this, params);
8262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008263 requestLayout();
8264 }
8265
8266 /**
8267 * Set the scrolled position of your view. This will cause a call to
8268 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8269 * invalidated.
8270 * @param x the x position to scroll to
8271 * @param y the y position to scroll to
8272 */
8273 public void scrollTo(int x, int y) {
8274 if (mScrollX != x || mScrollY != y) {
8275 int oldX = mScrollX;
8276 int oldY = mScrollY;
8277 mScrollX = x;
8278 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008279 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07008281 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008282 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 }
8285 }
8286
8287 /**
8288 * Move the scrolled position of your view. This will cause a call to
8289 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8290 * invalidated.
8291 * @param x the amount of pixels to scroll by horizontally
8292 * @param y the amount of pixels to scroll by vertically
8293 */
8294 public void scrollBy(int x, int y) {
8295 scrollTo(mScrollX + x, mScrollY + y);
8296 }
8297
8298 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008299 * <p>Trigger the scrollbars to draw. When invoked this method starts an
8300 * animation to fade the scrollbars out after a default delay. If a subclass
8301 * provides animated scrolling, the start delay should equal the duration
8302 * of the scrolling animation.</p>
8303 *
8304 * <p>The animation starts only if at least one of the scrollbars is
8305 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
8306 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8307 * this method returns true, and false otherwise. If the animation is
8308 * started, this method calls {@link #invalidate()}; in that case the
8309 * caller should not call {@link #invalidate()}.</p>
8310 *
8311 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07008312 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07008313 *
8314 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
8315 * and {@link #scrollTo(int, int)}.</p>
8316 *
8317 * @return true if the animation is played, false otherwise
8318 *
8319 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07008320 * @see #scrollBy(int, int)
8321 * @see #scrollTo(int, int)
8322 * @see #isHorizontalScrollBarEnabled()
8323 * @see #isVerticalScrollBarEnabled()
8324 * @see #setHorizontalScrollBarEnabled(boolean)
8325 * @see #setVerticalScrollBarEnabled(boolean)
8326 */
8327 protected boolean awakenScrollBars() {
8328 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07008329 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07008330 }
8331
8332 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07008333 * Trigger the scrollbars to draw.
8334 * This method differs from awakenScrollBars() only in its default duration.
8335 * initialAwakenScrollBars() will show the scroll bars for longer than
8336 * usual to give the user more of a chance to notice them.
8337 *
8338 * @return true if the animation is played, false otherwise.
8339 */
8340 private boolean initialAwakenScrollBars() {
8341 return mScrollCache != null &&
8342 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
8343 }
8344
8345 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07008346 * <p>
8347 * Trigger the scrollbars to draw. When invoked this method starts an
8348 * animation to fade the scrollbars out after a fixed delay. If a subclass
8349 * provides animated scrolling, the start delay should equal the duration of
8350 * the scrolling animation.
8351 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008352 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008353 * <p>
8354 * The animation starts only if at least one of the scrollbars is enabled,
8355 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8356 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8357 * this method returns true, and false otherwise. If the animation is
8358 * started, this method calls {@link #invalidate()}; in that case the caller
8359 * should not call {@link #invalidate()}.
8360 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008361 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008362 * <p>
8363 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07008364 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07008365 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008366 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008367 * @param startDelay the delay, in milliseconds, after which the animation
8368 * should start; when the delay is 0, the animation starts
8369 * immediately
8370 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008371 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008372 * @see #scrollBy(int, int)
8373 * @see #scrollTo(int, int)
8374 * @see #isHorizontalScrollBarEnabled()
8375 * @see #isVerticalScrollBarEnabled()
8376 * @see #setHorizontalScrollBarEnabled(boolean)
8377 * @see #setVerticalScrollBarEnabled(boolean)
8378 */
8379 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008380 return awakenScrollBars(startDelay, true);
8381 }
Joe Malin32736f02011-01-19 16:14:20 -08008382
Mike Cleron290947b2009-09-29 18:34:32 -07008383 /**
8384 * <p>
8385 * Trigger the scrollbars to draw. When invoked this method starts an
8386 * animation to fade the scrollbars out after a fixed delay. If a subclass
8387 * provides animated scrolling, the start delay should equal the duration of
8388 * the scrolling animation.
8389 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008390 *
Mike Cleron290947b2009-09-29 18:34:32 -07008391 * <p>
8392 * The animation starts only if at least one of the scrollbars is enabled,
8393 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8394 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8395 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008396 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008397 * is set to true; in that case the caller
8398 * should not call {@link #invalidate()}.
8399 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008400 *
Mike Cleron290947b2009-09-29 18:34:32 -07008401 * <p>
8402 * This method should be invoked everytime a subclass directly updates the
8403 * scroll parameters.
8404 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008405 *
Mike Cleron290947b2009-09-29 18:34:32 -07008406 * @param startDelay the delay, in milliseconds, after which the animation
8407 * should start; when the delay is 0, the animation starts
8408 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008409 *
Mike Cleron290947b2009-09-29 18:34:32 -07008410 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008411 *
Mike Cleron290947b2009-09-29 18:34:32 -07008412 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008413 *
Mike Cleron290947b2009-09-29 18:34:32 -07008414 * @see #scrollBy(int, int)
8415 * @see #scrollTo(int, int)
8416 * @see #isHorizontalScrollBarEnabled()
8417 * @see #isVerticalScrollBarEnabled()
8418 * @see #setHorizontalScrollBarEnabled(boolean)
8419 * @see #setVerticalScrollBarEnabled(boolean)
8420 */
8421 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008422 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008423
Mike Cleronf116bf82009-09-27 19:14:12 -07008424 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8425 return false;
8426 }
8427
8428 if (scrollCache.scrollBar == null) {
8429 scrollCache.scrollBar = new ScrollBarDrawable();
8430 }
8431
8432 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8433
Mike Cleron290947b2009-09-29 18:34:32 -07008434 if (invalidate) {
8435 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008436 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008437 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008438
8439 if (scrollCache.state == ScrollabilityCache.OFF) {
8440 // FIXME: this is copied from WindowManagerService.
8441 // We should get this value from the system when it
8442 // is possible to do so.
8443 final int KEY_REPEAT_FIRST_DELAY = 750;
8444 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8445 }
8446
8447 // Tell mScrollCache when we should start fading. This may
8448 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008449 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008450 scrollCache.fadeStartTime = fadeStartTime;
8451 scrollCache.state = ScrollabilityCache.ON;
8452
8453 // Schedule our fader to run, unscheduling any old ones first
8454 if (mAttachInfo != null) {
8455 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8456 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8457 }
8458
8459 return true;
8460 }
8461
8462 return false;
8463 }
8464
8465 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008466 * Do not invalidate views which are not visible and which are not running an animation. They
8467 * will not get drawn and they should not set dirty flags as if they will be drawn
8468 */
8469 private boolean skipInvalidate() {
8470 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8471 (!(mParent instanceof ViewGroup) ||
8472 !((ViewGroup) mParent).isViewTransitioning(this));
8473 }
8474 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008475 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008476 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8477 * in the future. This must be called from a UI thread. To call from a non-UI
8478 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 *
8480 * WARNING: This method is destructive to dirty.
8481 * @param dirty the rectangle representing the bounds of the dirty region
8482 */
8483 public void invalidate(Rect dirty) {
8484 if (ViewDebug.TRACE_HIERARCHY) {
8485 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8486 }
8487
Chet Haaseaceafe62011-08-26 15:44:33 -07008488 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008489 return;
8490 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008491 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008492 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8493 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008495 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008496 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 final ViewParent p = mParent;
8498 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008499 //noinspection PointlessBooleanExpression,ConstantConditions
8500 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8501 if (p != null && ai != null && ai.mHardwareAccelerated) {
8502 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008503 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008504 p.invalidateChild(this, null);
8505 return;
8506 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008508 if (p != null && ai != null) {
8509 final int scrollX = mScrollX;
8510 final int scrollY = mScrollY;
8511 final Rect r = ai.mTmpInvalRect;
8512 r.set(dirty.left - scrollX, dirty.top - scrollY,
8513 dirty.right - scrollX, dirty.bottom - scrollY);
8514 mParent.invalidateChild(this, r);
8515 }
8516 }
8517 }
8518
8519 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07008520 * 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 -08008521 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008522 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8523 * will be called at some point in the future. This must be called from
8524 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 * @param l the left position of the dirty region
8526 * @param t the top position of the dirty region
8527 * @param r the right position of the dirty region
8528 * @param b the bottom position of the dirty region
8529 */
8530 public void invalidate(int l, int t, int r, int b) {
8531 if (ViewDebug.TRACE_HIERARCHY) {
8532 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8533 }
8534
Chet Haaseaceafe62011-08-26 15:44:33 -07008535 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008536 return;
8537 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008538 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008539 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8540 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008541 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008542 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07008543 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008544 final ViewParent p = mParent;
8545 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008546 //noinspection PointlessBooleanExpression,ConstantConditions
8547 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8548 if (p != null && ai != null && ai.mHardwareAccelerated) {
8549 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008550 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008551 p.invalidateChild(this, null);
8552 return;
8553 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 if (p != null && ai != null && l < r && t < b) {
8556 final int scrollX = mScrollX;
8557 final int scrollY = mScrollY;
8558 final Rect tmpr = ai.mTmpInvalRect;
8559 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8560 p.invalidateChild(this, tmpr);
8561 }
8562 }
8563 }
8564
8565 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008566 * Invalidate the whole view. If the view is visible,
8567 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8568 * the future. This must be called from a UI thread. To call from a non-UI thread,
8569 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 */
8571 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008572 invalidate(true);
8573 }
Joe Malin32736f02011-01-19 16:14:20 -08008574
Chet Haaseed032702010-10-01 14:05:54 -07008575 /**
8576 * This is where the invalidate() work actually happens. A full invalidate()
8577 * causes the drawing cache to be invalidated, but this function can be called with
8578 * invalidateCache set to false to skip that invalidation step for cases that do not
8579 * need it (for example, a component that remains at the same dimensions with the same
8580 * content).
8581 *
8582 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8583 * well. This is usually true for a full invalidate, but may be set to false if the
8584 * View's contents or dimensions have not changed.
8585 */
Romain Guy849d0a32011-02-01 17:20:48 -08008586 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008587 if (ViewDebug.TRACE_HIERARCHY) {
8588 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8589 }
8590
Chet Haaseaceafe62011-08-26 15:44:33 -07008591 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008592 return;
8593 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008594 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008595 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008596 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8597 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008598 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07008599 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07008600 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008601 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008602 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008605 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008606 //noinspection PointlessBooleanExpression,ConstantConditions
8607 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8608 if (p != null && ai != null && ai.mHardwareAccelerated) {
8609 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008610 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008611 p.invalidateChild(this, null);
8612 return;
8613 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008614 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008616 if (p != null && ai != null) {
8617 final Rect r = ai.mTmpInvalRect;
8618 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8619 // Don't call invalidate -- we don't want to internally scroll
8620 // our own bounds
8621 p.invalidateChild(this, r);
8622 }
8623 }
8624 }
8625
8626 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008627 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008628 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8629 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008630 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8631 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008632 *
8633 * @hide
8634 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008635 protected void invalidateParentCaches() {
8636 if (mParent instanceof View) {
8637 ((View) mParent).mPrivateFlags |= INVALIDATED;
8638 }
8639 }
Joe Malin32736f02011-01-19 16:14:20 -08008640
Romain Guy0fd89bf2011-01-26 15:41:30 -08008641 /**
8642 * Used to indicate that the parent of this view should be invalidated. This functionality
8643 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8644 * which is necessary when various parent-managed properties of the view change, such as
8645 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8646 * an invalidation event to the parent.
8647 *
8648 * @hide
8649 */
8650 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008651 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008652 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008653 }
8654 }
8655
8656 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008657 * Indicates whether this View is opaque. An opaque View guarantees that it will
8658 * draw all the pixels overlapping its bounds using a fully opaque color.
8659 *
8660 * Subclasses of View should override this method whenever possible to indicate
8661 * whether an instance is opaque. Opaque Views are treated in a special way by
8662 * the View hierarchy, possibly allowing it to perform optimizations during
8663 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008664 *
Romain Guy24443ea2009-05-11 11:56:30 -07008665 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008666 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008667 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008668 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008669 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008670 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
8671 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008672 }
8673
Adam Powell20232d02010-12-08 21:08:53 -08008674 /**
8675 * @hide
8676 */
8677 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008678 // Opaque if:
8679 // - Has a background
8680 // - Background is opaque
8681 // - Doesn't have scrollbars or scrollbars are inside overlay
8682
8683 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8684 mPrivateFlags |= OPAQUE_BACKGROUND;
8685 } else {
8686 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8687 }
8688
8689 final int flags = mViewFlags;
8690 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8691 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8692 mPrivateFlags |= OPAQUE_SCROLLBARS;
8693 } else {
8694 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8695 }
8696 }
8697
8698 /**
8699 * @hide
8700 */
8701 protected boolean hasOpaqueScrollbars() {
8702 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008703 }
8704
8705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 * @return A handler associated with the thread running the View. This
8707 * handler can be used to pump events in the UI events queue.
8708 */
8709 public Handler getHandler() {
8710 if (mAttachInfo != null) {
8711 return mAttachInfo.mHandler;
8712 }
8713 return null;
8714 }
8715
8716 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08008717 * Gets the view root associated with the View.
8718 * @return The view root, or null if none.
8719 * @hide
8720 */
8721 public ViewRootImpl getViewRootImpl() {
8722 if (mAttachInfo != null) {
8723 return mAttachInfo.mViewRootImpl;
8724 }
8725 return null;
8726 }
8727
8728 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008729 * <p>Causes the Runnable to be added to the message queue.
8730 * The runnable will be run on the user interface thread.</p>
8731 *
8732 * <p>This method can be invoked from outside of the UI thread
8733 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008734 *
8735 * @param action The Runnable that will be executed.
8736 *
8737 * @return Returns true if the Runnable was successfully placed in to the
8738 * message queue. Returns false on failure, usually because the
8739 * looper processing the message queue is exiting.
8740 */
8741 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008742 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008743 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008744 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008745 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008746 // Assume that post will succeed later
8747 ViewRootImpl.getRunQueue().post(action);
8748 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008749 }
8750
8751 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008752 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008753 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008754 * The runnable will be run on the user interface thread.</p>
8755 *
8756 * <p>This method can be invoked from outside of the UI thread
8757 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 *
8759 * @param action The Runnable that will be executed.
8760 * @param delayMillis The delay (in milliseconds) until the Runnable
8761 * will be executed.
8762 *
8763 * @return true if the Runnable was successfully placed in to the
8764 * message queue. Returns false on failure, usually because the
8765 * looper processing the message queue is exiting. Note that a
8766 * result of true does not mean the Runnable will be processed --
8767 * if the looper is quit before the delivery time of the message
8768 * occurs then the message will be dropped.
8769 */
8770 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008771 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008772 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008773 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774 }
Jeff Browna175a5b2012-02-15 19:18:31 -08008775 // Assume that post will succeed later
8776 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
8777 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 }
8779
8780 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008781 * <p>Removes the specified Runnable from the message queue.</p>
8782 *
8783 * <p>This method can be invoked from outside of the UI thread
8784 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008785 *
8786 * @param action The Runnable to remove from the message handling queue
8787 *
8788 * @return true if this view could ask the Handler to remove the Runnable,
8789 * false otherwise. When the returned value is true, the Runnable
8790 * may or may not have been actually removed from the message queue
8791 * (for instance, if the Runnable was not in the queue already.)
8792 */
8793 public boolean removeCallbacks(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008794 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008795 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008796 attachInfo.mHandler.removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 } else {
8798 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008799 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008801 return true;
8802 }
8803
8804 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008805 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8806 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 *
Romain Guye63a4f32011-08-11 11:33:31 -07008808 * <p>This method can be invoked from outside of the UI thread
8809 * only when this View is attached to a window.</p>
8810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 * @see #invalidate()
8812 */
8813 public void postInvalidate() {
8814 postInvalidateDelayed(0);
8815 }
8816
8817 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008818 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8819 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8820 *
8821 * <p>This method can be invoked from outside of the UI thread
8822 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008823 *
8824 * @param left The left coordinate of the rectangle to invalidate.
8825 * @param top The top coordinate of the rectangle to invalidate.
8826 * @param right The right coordinate of the rectangle to invalidate.
8827 * @param bottom The bottom coordinate of the rectangle to invalidate.
8828 *
8829 * @see #invalidate(int, int, int, int)
8830 * @see #invalidate(Rect)
8831 */
8832 public void postInvalidate(int left, int top, int right, int bottom) {
8833 postInvalidateDelayed(0, left, top, right, bottom);
8834 }
8835
8836 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008837 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8838 * loop. Waits for the specified amount of time.</p>
8839 *
8840 * <p>This method can be invoked from outside of the UI thread
8841 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 *
8843 * @param delayMilliseconds the duration in milliseconds to delay the
8844 * invalidation by
8845 */
8846 public void postInvalidateDelayed(long delayMilliseconds) {
8847 // We try only with the AttachInfo because there's no point in invalidating
8848 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008849 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008850 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08008851 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008852 }
8853 }
8854
8855 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008856 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8857 * through the event loop. Waits for the specified amount of time.</p>
8858 *
8859 * <p>This method can be invoked from outside of the UI thread
8860 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008861 *
8862 * @param delayMilliseconds the duration in milliseconds to delay the
8863 * invalidation by
8864 * @param left The left coordinate of the rectangle to invalidate.
8865 * @param top The top coordinate of the rectangle to invalidate.
8866 * @param right The right coordinate of the rectangle to invalidate.
8867 * @param bottom The bottom coordinate of the rectangle to invalidate.
8868 */
8869 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8870 int right, int bottom) {
8871
8872 // We try only with the AttachInfo because there's no point in invalidating
8873 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -08008874 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07008875 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8877 info.target = this;
8878 info.left = left;
8879 info.top = top;
8880 info.right = right;
8881 info.bottom = bottom;
8882
Jeff Browna175a5b2012-02-15 19:18:31 -08008883 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008884 }
8885 }
8886
8887 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008888 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8889 * This event is sent at most once every
8890 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8891 */
8892 private void postSendViewScrolledAccessibilityEventCallback() {
8893 if (mSendViewScrolledAccessibilityEvent == null) {
8894 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8895 }
8896 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8897 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8898 postDelayed(mSendViewScrolledAccessibilityEvent,
8899 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8900 }
8901 }
8902
8903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 * Called by a parent to request that a child update its values for mScrollX
8905 * and mScrollY if necessary. This will typically be done if the child is
8906 * animating a scroll using a {@link android.widget.Scroller Scroller}
8907 * object.
8908 */
8909 public void computeScroll() {
8910 }
8911
8912 /**
8913 * <p>Indicate whether the horizontal edges are faded when the view is
8914 * scrolled horizontally.</p>
8915 *
8916 * @return true if the horizontal edges should are faded on scroll, false
8917 * otherwise
8918 *
8919 * @see #setHorizontalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008920 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008921 */
8922 public boolean isHorizontalFadingEdgeEnabled() {
8923 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8924 }
8925
8926 /**
8927 * <p>Define whether the horizontal edges should be faded when this view
8928 * is scrolled horizontally.</p>
8929 *
8930 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8931 * be faded when the view is scrolled
8932 * horizontally
8933 *
8934 * @see #isHorizontalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008935 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 */
8937 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8938 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8939 if (horizontalFadingEdgeEnabled) {
8940 initScrollCache();
8941 }
8942
8943 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8944 }
8945 }
8946
8947 /**
8948 * <p>Indicate whether the vertical edges are faded when the view is
8949 * scrolled horizontally.</p>
8950 *
8951 * @return true if the vertical edges should are faded on scroll, false
8952 * otherwise
8953 *
8954 * @see #setVerticalFadingEdgeEnabled(boolean)
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008955 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008956 */
8957 public boolean isVerticalFadingEdgeEnabled() {
8958 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8959 }
8960
8961 /**
8962 * <p>Define whether the vertical edges should be faded when this view
8963 * is scrolled vertically.</p>
8964 *
8965 * @param verticalFadingEdgeEnabled true if the vertical edges should
8966 * be faded when the view is scrolled
8967 * vertically
8968 *
8969 * @see #isVerticalFadingEdgeEnabled()
Romain Guy1ef3fdb2011-09-09 15:30:30 -07008970 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 */
8972 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8973 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8974 if (verticalFadingEdgeEnabled) {
8975 initScrollCache();
8976 }
8977
8978 mViewFlags ^= FADING_EDGE_VERTICAL;
8979 }
8980 }
8981
8982 /**
8983 * Returns the strength, or intensity, of the top faded edge. The strength is
8984 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8985 * returns 0.0 or 1.0 but no value in between.
8986 *
8987 * Subclasses should override this method to provide a smoother fade transition
8988 * when scrolling occurs.
8989 *
8990 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8991 */
8992 protected float getTopFadingEdgeStrength() {
8993 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8994 }
8995
8996 /**
8997 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8998 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8999 * returns 0.0 or 1.0 but no value in between.
9000 *
9001 * Subclasses should override this method to provide a smoother fade transition
9002 * when scrolling occurs.
9003 *
9004 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
9005 */
9006 protected float getBottomFadingEdgeStrength() {
9007 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
9008 computeVerticalScrollRange() ? 1.0f : 0.0f;
9009 }
9010
9011 /**
9012 * Returns the strength, or intensity, of the left faded edge. The strength is
9013 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9014 * returns 0.0 or 1.0 but no value in between.
9015 *
9016 * Subclasses should override this method to provide a smoother fade transition
9017 * when scrolling occurs.
9018 *
9019 * @return the intensity of the left fade as a float between 0.0f and 1.0f
9020 */
9021 protected float getLeftFadingEdgeStrength() {
9022 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
9023 }
9024
9025 /**
9026 * Returns the strength, or intensity, of the right faded edge. The strength is
9027 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
9028 * returns 0.0 or 1.0 but no value in between.
9029 *
9030 * Subclasses should override this method to provide a smoother fade transition
9031 * when scrolling occurs.
9032 *
9033 * @return the intensity of the right fade as a float between 0.0f and 1.0f
9034 */
9035 protected float getRightFadingEdgeStrength() {
9036 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
9037 computeHorizontalScrollRange() ? 1.0f : 0.0f;
9038 }
9039
9040 /**
9041 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
9042 * scrollbar is not drawn by default.</p>
9043 *
9044 * @return true if the horizontal scrollbar should be painted, false
9045 * otherwise
9046 *
9047 * @see #setHorizontalScrollBarEnabled(boolean)
9048 */
9049 public boolean isHorizontalScrollBarEnabled() {
9050 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9051 }
9052
9053 /**
9054 * <p>Define whether the horizontal scrollbar should be drawn or not. The
9055 * scrollbar is not drawn by default.</p>
9056 *
9057 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
9058 * be painted
9059 *
9060 * @see #isHorizontalScrollBarEnabled()
9061 */
9062 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
9063 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
9064 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009065 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009066 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009067 }
9068 }
9069
9070 /**
9071 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
9072 * scrollbar is not drawn by default.</p>
9073 *
9074 * @return true if the vertical scrollbar should be painted, false
9075 * otherwise
9076 *
9077 * @see #setVerticalScrollBarEnabled(boolean)
9078 */
9079 public boolean isVerticalScrollBarEnabled() {
9080 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
9081 }
9082
9083 /**
9084 * <p>Define whether the vertical scrollbar should be drawn or not. The
9085 * scrollbar is not drawn by default.</p>
9086 *
9087 * @param verticalScrollBarEnabled true if the vertical scrollbar should
9088 * be painted
9089 *
9090 * @see #isVerticalScrollBarEnabled()
9091 */
9092 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
9093 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
9094 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07009095 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009096 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 }
9098 }
9099
Adam Powell20232d02010-12-08 21:08:53 -08009100 /**
9101 * @hide
9102 */
9103 protected void recomputePadding() {
9104 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 }
Joe Malin32736f02011-01-19 16:14:20 -08009106
Mike Cleronfe81d382009-09-28 14:22:16 -07009107 /**
9108 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08009109 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009110 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08009111 *
Mike Cleronfe81d382009-09-28 14:22:16 -07009112 */
9113 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
9114 initScrollCache();
9115 final ScrollabilityCache scrollabilityCache = mScrollCache;
9116 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009117 if (fadeScrollbars) {
9118 scrollabilityCache.state = ScrollabilityCache.OFF;
9119 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07009120 scrollabilityCache.state = ScrollabilityCache.ON;
9121 }
9122 }
Joe Malin32736f02011-01-19 16:14:20 -08009123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 /**
Joe Malin32736f02011-01-19 16:14:20 -08009125 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009126 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08009127 *
Mike Cleron52f0a642009-09-28 18:21:37 -07009128 * @return true if scrollbar fading is enabled
9129 */
9130 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08009131 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07009132 }
Joe Malin32736f02011-01-19 16:14:20 -08009133
Mike Cleron52f0a642009-09-28 18:21:37 -07009134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009135 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
9136 * inset. When inset, they add to the padding of the view. And the scrollbars
9137 * can be drawn inside the padding area or on the edge of the view. For example,
9138 * if a view has a background drawable and you want to draw the scrollbars
9139 * inside the padding specified by the drawable, you can use
9140 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
9141 * appear at the edge of the view, ignoring the padding, then you can use
9142 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
9143 * @param style the style of the scrollbars. Should be one of
9144 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
9145 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
9146 * @see #SCROLLBARS_INSIDE_OVERLAY
9147 * @see #SCROLLBARS_INSIDE_INSET
9148 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9149 * @see #SCROLLBARS_OUTSIDE_INSET
9150 */
9151 public void setScrollBarStyle(int style) {
9152 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
9153 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07009154 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009155 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 }
9157 }
9158
9159 /**
9160 * <p>Returns the current scrollbar style.</p>
9161 * @return the current scrollbar style
9162 * @see #SCROLLBARS_INSIDE_OVERLAY
9163 * @see #SCROLLBARS_INSIDE_INSET
9164 * @see #SCROLLBARS_OUTSIDE_OVERLAY
9165 * @see #SCROLLBARS_OUTSIDE_INSET
9166 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07009167 @ViewDebug.ExportedProperty(mapping = {
9168 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
9169 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
9170 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
9171 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
9172 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 public int getScrollBarStyle() {
9174 return mViewFlags & SCROLLBARS_STYLE_MASK;
9175 }
9176
9177 /**
9178 * <p>Compute the horizontal range that the horizontal scrollbar
9179 * represents.</p>
9180 *
9181 * <p>The range is expressed in arbitrary units that must be the same as the
9182 * units used by {@link #computeHorizontalScrollExtent()} and
9183 * {@link #computeHorizontalScrollOffset()}.</p>
9184 *
9185 * <p>The default range is the drawing width of this view.</p>
9186 *
9187 * @return the total horizontal range represented by the horizontal
9188 * scrollbar
9189 *
9190 * @see #computeHorizontalScrollExtent()
9191 * @see #computeHorizontalScrollOffset()
9192 * @see android.widget.ScrollBarDrawable
9193 */
9194 protected int computeHorizontalScrollRange() {
9195 return getWidth();
9196 }
9197
9198 /**
9199 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
9200 * within the horizontal range. This value is used to compute the position
9201 * of the thumb within the scrollbar's track.</p>
9202 *
9203 * <p>The range is expressed in arbitrary units that must be the same as the
9204 * units used by {@link #computeHorizontalScrollRange()} and
9205 * {@link #computeHorizontalScrollExtent()}.</p>
9206 *
9207 * <p>The default offset is the scroll offset of this view.</p>
9208 *
9209 * @return the horizontal offset of the scrollbar's thumb
9210 *
9211 * @see #computeHorizontalScrollRange()
9212 * @see #computeHorizontalScrollExtent()
9213 * @see android.widget.ScrollBarDrawable
9214 */
9215 protected int computeHorizontalScrollOffset() {
9216 return mScrollX;
9217 }
9218
9219 /**
9220 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
9221 * within the horizontal range. This value is used to compute the length
9222 * of the thumb within the scrollbar's track.</p>
9223 *
9224 * <p>The range is expressed in arbitrary units that must be the same as the
9225 * units used by {@link #computeHorizontalScrollRange()} and
9226 * {@link #computeHorizontalScrollOffset()}.</p>
9227 *
9228 * <p>The default extent is the drawing width of this view.</p>
9229 *
9230 * @return the horizontal extent of the scrollbar's thumb
9231 *
9232 * @see #computeHorizontalScrollRange()
9233 * @see #computeHorizontalScrollOffset()
9234 * @see android.widget.ScrollBarDrawable
9235 */
9236 protected int computeHorizontalScrollExtent() {
9237 return getWidth();
9238 }
9239
9240 /**
9241 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
9242 *
9243 * <p>The range is expressed in arbitrary units that must be the same as the
9244 * units used by {@link #computeVerticalScrollExtent()} and
9245 * {@link #computeVerticalScrollOffset()}.</p>
9246 *
9247 * @return the total vertical range represented by the vertical scrollbar
9248 *
9249 * <p>The default range is the drawing height of this view.</p>
9250 *
9251 * @see #computeVerticalScrollExtent()
9252 * @see #computeVerticalScrollOffset()
9253 * @see android.widget.ScrollBarDrawable
9254 */
9255 protected int computeVerticalScrollRange() {
9256 return getHeight();
9257 }
9258
9259 /**
9260 * <p>Compute the vertical offset of the vertical scrollbar's thumb
9261 * within the horizontal range. This value is used to compute the position
9262 * of the thumb within the scrollbar's track.</p>
9263 *
9264 * <p>The range is expressed in arbitrary units that must be the same as the
9265 * units used by {@link #computeVerticalScrollRange()} and
9266 * {@link #computeVerticalScrollExtent()}.</p>
9267 *
9268 * <p>The default offset is the scroll offset of this view.</p>
9269 *
9270 * @return the vertical offset of the scrollbar's thumb
9271 *
9272 * @see #computeVerticalScrollRange()
9273 * @see #computeVerticalScrollExtent()
9274 * @see android.widget.ScrollBarDrawable
9275 */
9276 protected int computeVerticalScrollOffset() {
9277 return mScrollY;
9278 }
9279
9280 /**
9281 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
9282 * within the vertical range. This value is used to compute the length
9283 * of the thumb within the scrollbar's track.</p>
9284 *
9285 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08009286 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 * {@link #computeVerticalScrollOffset()}.</p>
9288 *
9289 * <p>The default extent is the drawing height of this view.</p>
9290 *
9291 * @return the vertical extent of the scrollbar's thumb
9292 *
9293 * @see #computeVerticalScrollRange()
9294 * @see #computeVerticalScrollOffset()
9295 * @see android.widget.ScrollBarDrawable
9296 */
9297 protected int computeVerticalScrollExtent() {
9298 return getHeight();
9299 }
9300
9301 /**
Adam Powell69159442011-06-13 17:53:06 -07009302 * Check if this view can be scrolled horizontally in a certain direction.
9303 *
9304 * @param direction Negative to check scrolling left, positive to check scrolling right.
9305 * @return true if this view can be scrolled in the specified direction, false otherwise.
9306 */
9307 public boolean canScrollHorizontally(int direction) {
9308 final int offset = computeHorizontalScrollOffset();
9309 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
9310 if (range == 0) return false;
9311 if (direction < 0) {
9312 return offset > 0;
9313 } else {
9314 return offset < range - 1;
9315 }
9316 }
9317
9318 /**
9319 * Check if this view can be scrolled vertically in a certain direction.
9320 *
9321 * @param direction Negative to check scrolling up, positive to check scrolling down.
9322 * @return true if this view can be scrolled in the specified direction, false otherwise.
9323 */
9324 public boolean canScrollVertically(int direction) {
9325 final int offset = computeVerticalScrollOffset();
9326 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
9327 if (range == 0) return false;
9328 if (direction < 0) {
9329 return offset > 0;
9330 } else {
9331 return offset < range - 1;
9332 }
9333 }
9334
9335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009336 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
9337 * scrollbars are painted only if they have been awakened first.</p>
9338 *
9339 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08009340 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009341 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009342 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08009343 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009344 // scrollbars are drawn only when the animation is running
9345 final ScrollabilityCache cache = mScrollCache;
9346 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009347
Mike Cleronf116bf82009-09-27 19:14:12 -07009348 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009349
Mike Cleronf116bf82009-09-27 19:14:12 -07009350 if (state == ScrollabilityCache.OFF) {
9351 return;
9352 }
Joe Malin32736f02011-01-19 16:14:20 -08009353
Mike Cleronf116bf82009-09-27 19:14:12 -07009354 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009355
Mike Cleronf116bf82009-09-27 19:14:12 -07009356 if (state == ScrollabilityCache.FADING) {
9357 // We're fading -- get our fade interpolation
9358 if (cache.interpolatorValues == null) {
9359 cache.interpolatorValues = new float[1];
9360 }
Joe Malin32736f02011-01-19 16:14:20 -08009361
Mike Cleronf116bf82009-09-27 19:14:12 -07009362 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009363
Mike Cleronf116bf82009-09-27 19:14:12 -07009364 // Stops the animation if we're done
9365 if (cache.scrollBarInterpolator.timeToValues(values) ==
9366 Interpolator.Result.FREEZE_END) {
9367 cache.state = ScrollabilityCache.OFF;
9368 } else {
9369 cache.scrollBar.setAlpha(Math.round(values[0]));
9370 }
Joe Malin32736f02011-01-19 16:14:20 -08009371
9372 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009373 // drawing. We only want this when we're fading so that
9374 // we prevent excessive redraws
9375 invalidate = true;
9376 } else {
9377 // We're just on -- but we may have been fading before so
9378 // reset alpha
9379 cache.scrollBar.setAlpha(255);
9380 }
9381
Joe Malin32736f02011-01-19 16:14:20 -08009382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 final int viewFlags = mViewFlags;
9384
9385 final boolean drawHorizontalScrollBar =
9386 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9387 final boolean drawVerticalScrollBar =
9388 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9389 && !isVerticalScrollBarHidden();
9390
9391 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9392 final int width = mRight - mLeft;
9393 final int height = mBottom - mTop;
9394
9395 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396
Mike Reede8853fc2009-09-04 14:01:48 -04009397 final int scrollX = mScrollX;
9398 final int scrollY = mScrollY;
9399 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9400
Mike Cleronf116bf82009-09-27 19:14:12 -07009401 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009403 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009404 int size = scrollBar.getSize(false);
9405 if (size <= 0) {
9406 size = cache.scrollBarSize;
9407 }
9408
Mike Cleronf116bf82009-09-27 19:14:12 -07009409 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009410 computeHorizontalScrollOffset(),
9411 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009412 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009413 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009414 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009415 left = scrollX + (mPaddingLeft & inside);
9416 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9417 bottom = top + size;
9418 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9419 if (invalidate) {
9420 invalidate(left, top, right, bottom);
9421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 }
9423
9424 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009425 int size = scrollBar.getSize(true);
9426 if (size <= 0) {
9427 size = cache.scrollBarSize;
9428 }
9429
Mike Reede8853fc2009-09-04 14:01:48 -04009430 scrollBar.setParameters(computeVerticalScrollRange(),
9431 computeVerticalScrollOffset(),
9432 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009433 switch (mVerticalScrollbarPosition) {
9434 default:
9435 case SCROLLBAR_POSITION_DEFAULT:
9436 case SCROLLBAR_POSITION_RIGHT:
9437 left = scrollX + width - size - (mUserPaddingRight & inside);
9438 break;
9439 case SCROLLBAR_POSITION_LEFT:
9440 left = scrollX + (mUserPaddingLeft & inside);
9441 break;
9442 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009443 top = scrollY + (mPaddingTop & inside);
9444 right = left + size;
9445 bottom = scrollY + height - (mUserPaddingBottom & inside);
9446 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9447 if (invalidate) {
9448 invalidate(left, top, right, bottom);
9449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009450 }
9451 }
9452 }
9453 }
Romain Guy8506ab42009-06-11 17:35:47 -07009454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009456 * 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 -08009457 * FastScroller is visible.
9458 * @return whether to temporarily hide the vertical scrollbar
9459 * @hide
9460 */
9461 protected boolean isVerticalScrollBarHidden() {
9462 return false;
9463 }
9464
9465 /**
9466 * <p>Draw the horizontal scrollbar if
9467 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 * @param canvas the canvas on which to draw the scrollbar
9470 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 *
9472 * @see #isHorizontalScrollBarEnabled()
9473 * @see #computeHorizontalScrollRange()
9474 * @see #computeHorizontalScrollExtent()
9475 * @see #computeHorizontalScrollOffset()
9476 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009477 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009478 */
Romain Guy8fb95422010-08-17 18:38:51 -07009479 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9480 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009481 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009482 scrollBar.draw(canvas);
9483 }
Mike Reede8853fc2009-09-04 14:01:48 -04009484
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9487 * returns true.</p>
9488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009489 * @param canvas the canvas on which to draw the scrollbar
9490 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 *
9492 * @see #isVerticalScrollBarEnabled()
9493 * @see #computeVerticalScrollRange()
9494 * @see #computeVerticalScrollExtent()
9495 * @see #computeVerticalScrollOffset()
9496 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009497 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009498 */
Romain Guy8fb95422010-08-17 18:38:51 -07009499 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9500 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009501 scrollBar.setBounds(l, t, r, b);
9502 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009503 }
9504
9505 /**
9506 * Implement this to do your drawing.
9507 *
9508 * @param canvas the canvas on which the background will be drawn
9509 */
9510 protected void onDraw(Canvas canvas) {
9511 }
9512
9513 /*
9514 * Caller is responsible for calling requestLayout if necessary.
9515 * (This allows addViewInLayout to not request a new layout.)
9516 */
9517 void assignParent(ViewParent parent) {
9518 if (mParent == null) {
9519 mParent = parent;
9520 } else if (parent == null) {
9521 mParent = null;
9522 } else {
9523 throw new RuntimeException("view " + this + " being added, but"
9524 + " it already has a parent");
9525 }
9526 }
9527
9528 /**
9529 * This is called when the view is attached to a window. At this point it
9530 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009531 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9532 * however it may be called any time before the first onDraw -- including
9533 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534 *
9535 * @see #onDetachedFromWindow()
9536 */
9537 protected void onAttachedToWindow() {
9538 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9539 mParent.requestTransparentRegion(this);
9540 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009541 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9542 initialAwakenScrollBars();
9543 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9544 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009545 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009546 // Order is important here: LayoutDirection MUST be resolved before Padding
9547 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009548 resolveLayoutDirectionIfNeeded();
9549 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009550 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009551 if (isFocused()) {
9552 InputMethodManager imm = InputMethodManager.peekInstance();
9553 imm.focusIn(this);
9554 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009555 }
Cibu Johny86666632010-02-22 13:01:02 -08009556
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009557 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009558 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9559 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009560 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009561 private void resolveLayoutDirectionIfNeeded() {
9562 // Do not resolve if it is not needed
9563 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9564
9565 // Clear any previous layout direction resolution
9566 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9567
9568 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009569 switch (getLayoutDirection()) {
9570 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009571 // We cannot do the resolution if there is no parent
9572 if (mParent == null) return;
9573
Cibu Johny86666632010-02-22 13:01:02 -08009574 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009575 if (mParent instanceof ViewGroup) {
9576 ViewGroup viewGroup = ((ViewGroup) mParent);
9577
9578 // Check if the parent view group can resolve
9579 if (! viewGroup.canResolveLayoutDirection()) {
9580 return;
9581 }
9582 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9583 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9584 }
Cibu Johny86666632010-02-22 13:01:02 -08009585 }
9586 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009587 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009588 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009589 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009590 case LAYOUT_DIRECTION_LOCALE:
9591 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009592 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009593 }
9594 break;
9595 default:
9596 // Nothing to do, LTR by default
9597 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009598
9599 // Set to resolved
9600 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009601 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009602 }
9603
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009604 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009605 * Called when layout direction has been resolved.
9606 *
9607 * The default implementation does nothing.
9608 */
9609 public void onResolvedLayoutDirectionChanged() {
9610 }
9611
9612 /**
9613 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009614 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009615 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009616 // If the user specified the absolute padding (either with android:padding or
9617 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9618 // use the default padding or the padding from the background drawable
9619 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009620 int resolvedLayoutDirection = getResolvedLayoutDirection();
9621 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009622 case LAYOUT_DIRECTION_RTL:
9623 // Start user padding override Right user padding. Otherwise, if Right user
9624 // padding is not defined, use the default Right padding. If Right user padding
9625 // is defined, just use it.
9626 if (mUserPaddingStart >= 0) {
9627 mUserPaddingRight = mUserPaddingStart;
9628 } else if (mUserPaddingRight < 0) {
9629 mUserPaddingRight = mPaddingRight;
9630 }
9631 if (mUserPaddingEnd >= 0) {
9632 mUserPaddingLeft = mUserPaddingEnd;
9633 } else if (mUserPaddingLeft < 0) {
9634 mUserPaddingLeft = mPaddingLeft;
9635 }
9636 break;
9637 case LAYOUT_DIRECTION_LTR:
9638 default:
9639 // Start user padding override Left user padding. Otherwise, if Left user
9640 // padding is not defined, use the default left padding. If Left user padding
9641 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009642 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009643 mUserPaddingLeft = mUserPaddingStart;
9644 } else if (mUserPaddingLeft < 0) {
9645 mUserPaddingLeft = mPaddingLeft;
9646 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009647 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009648 mUserPaddingRight = mUserPaddingEnd;
9649 } else if (mUserPaddingRight < 0) {
9650 mUserPaddingRight = mPaddingRight;
9651 }
9652 }
9653
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009654 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9655
9656 recomputePadding();
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009657 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009658 }
9659
9660 /**
9661 * Resolve padding depending on the layout direction. Subclasses that care about
9662 * padding resolution should override this method. The default implementation does
9663 * nothing.
9664 *
9665 * @param layoutDirection the direction of the layout
9666 *
Fabrice Di Meglioccb15622012-02-15 15:52:19 -08009667 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009668 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009669 }
9670
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009671 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009672 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009673 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009674 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009675 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009676 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009677 switch (getLayoutDirection()) {
9678 case LAYOUT_DIRECTION_INHERIT:
9679 return (mParent != null);
9680 default:
9681 return true;
9682 }
9683 }
9684
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009685 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009686 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
9687 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009688 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009689 public void resetResolvedLayoutDirection() {
9690 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009691 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009692 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -08009693 // Reset also the text direction
9694 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009695 }
9696
9697 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -08009698 * Called during reset of resolved layout direction.
9699 *
9700 * Subclasses need to override this method to clear cached information that depends on the
9701 * resolved layout direction, or to inform child views that inherit their layout direction.
9702 *
9703 * The default implementation does nothing.
9704 */
9705 public void onResolvedLayoutDirectionReset() {
9706 }
9707
9708 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009709 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009710 *
9711 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -08009712 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009713 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009714 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -08009715 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
9717
9718 /**
9719 * This is called when the view is detached from a window. At this point it
9720 * no longer has a surface for drawing.
9721 *
9722 * @see #onAttachedToWindow()
9723 */
9724 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009725 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009726
Romain Guya440b002010-02-24 15:57:54 -08009727 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009728 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009729 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009730 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009733
Romain Guy6d7475d2011-07-27 16:28:21 -07009734 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009735
Chet Haasedaf98e92011-01-10 14:10:36 -08009736 if (mDisplayList != null) {
9737 mDisplayList.invalidate();
9738 }
9739
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009740 if (mAttachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009741 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009742 }
9743
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009744 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009745
9746 resetResolvedLayoutDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009747 }
9748
9749 /**
9750 * @return The number of times this view has been attached to a window
9751 */
9752 protected int getWindowAttachCount() {
9753 return mWindowAttachCount;
9754 }
9755
9756 /**
9757 * Retrieve a unique token identifying the window this view is attached to.
9758 * @return Return the window's token for use in
9759 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9760 */
9761 public IBinder getWindowToken() {
9762 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9763 }
9764
9765 /**
9766 * Retrieve a unique token identifying the top-level "real" window of
9767 * the window that this view is attached to. That is, this is like
9768 * {@link #getWindowToken}, except if the window this view in is a panel
9769 * window (attached to another containing window), then the token of
9770 * the containing window is returned instead.
9771 *
9772 * @return Returns the associated window token, either
9773 * {@link #getWindowToken()} or the containing window's token.
9774 */
9775 public IBinder getApplicationWindowToken() {
9776 AttachInfo ai = mAttachInfo;
9777 if (ai != null) {
9778 IBinder appWindowToken = ai.mPanelParentWindowToken;
9779 if (appWindowToken == null) {
9780 appWindowToken = ai.mWindowToken;
9781 }
9782 return appWindowToken;
9783 }
9784 return null;
9785 }
9786
9787 /**
9788 * Retrieve private session object this view hierarchy is using to
9789 * communicate with the window manager.
9790 * @return the session object to communicate with the window manager
9791 */
9792 /*package*/ IWindowSession getWindowSession() {
9793 return mAttachInfo != null ? mAttachInfo.mSession : null;
9794 }
9795
9796 /**
9797 * @param info the {@link android.view.View.AttachInfo} to associated with
9798 * this view
9799 */
9800 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9801 //System.out.println("Attached! " + this);
9802 mAttachInfo = info;
9803 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009804 // We will need to evaluate the drawable state at least once.
9805 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 if (mFloatingTreeObserver != null) {
9807 info.mTreeObserver.merge(mFloatingTreeObserver);
9808 mFloatingTreeObserver = null;
9809 }
9810 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9811 mAttachInfo.mScrollContainers.add(this);
9812 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9813 }
9814 performCollectViewAttributes(visibility);
9815 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009816
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009817 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009818 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009819 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009820 if (listeners != null && listeners.size() > 0) {
9821 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9822 // perform the dispatching. The iterator is a safe guard against listeners that
9823 // could mutate the list by calling the various add/remove methods. This prevents
9824 // the array from being modified while we iterate it.
9825 for (OnAttachStateChangeListener listener : listeners) {
9826 listener.onViewAttachedToWindow(this);
9827 }
9828 }
9829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 int vis = info.mWindowVisibility;
9831 if (vis != GONE) {
9832 onWindowVisibilityChanged(vis);
9833 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009834 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9835 // If nobody has evaluated the drawable state yet, then do it now.
9836 refreshDrawableState();
9837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009838 }
9839
9840 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 AttachInfo info = mAttachInfo;
9842 if (info != null) {
9843 int vis = info.mWindowVisibility;
9844 if (vis != GONE) {
9845 onWindowVisibilityChanged(GONE);
9846 }
9847 }
9848
9849 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009851 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -08009852 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07009853 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -08009854 if (listeners != null && listeners.size() > 0) {
9855 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9856 // perform the dispatching. The iterator is a safe guard against listeners that
9857 // could mutate the list by calling the various add/remove methods. This prevents
9858 // the array from being modified while we iterate it.
9859 for (OnAttachStateChangeListener listener : listeners) {
9860 listener.onViewDetachedFromWindow(this);
9861 }
9862 }
9863
Romain Guy01d5edc2011-01-28 11:28:53 -08009864 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 mAttachInfo.mScrollContainers.remove(this);
9866 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9867 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 mAttachInfo = null;
9870 }
9871
9872 /**
9873 * Store this view hierarchy's frozen state into the given container.
9874 *
9875 * @param container The SparseArray in which to save the view's state.
9876 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009877 * @see #restoreHierarchyState(android.util.SparseArray)
9878 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9879 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 */
9881 public void saveHierarchyState(SparseArray<Parcelable> container) {
9882 dispatchSaveInstanceState(container);
9883 }
9884
9885 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009886 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9887 * this view and its children. May be overridden to modify how freezing happens to a
9888 * 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 -08009889 *
9890 * @param container The SparseArray in which to save the view's state.
9891 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009892 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9893 * @see #saveHierarchyState(android.util.SparseArray)
9894 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009895 */
9896 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9897 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9898 mPrivateFlags &= ~SAVE_STATE_CALLED;
9899 Parcelable state = onSaveInstanceState();
9900 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9901 throw new IllegalStateException(
9902 "Derived class did not call super.onSaveInstanceState()");
9903 }
9904 if (state != null) {
9905 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9906 // + ": " + state);
9907 container.put(mID, state);
9908 }
9909 }
9910 }
9911
9912 /**
9913 * Hook allowing a view to generate a representation of its internal state
9914 * that can later be used to create a new instance with that same state.
9915 * This state should only contain information that is not persistent or can
9916 * not be reconstructed later. For example, you will never store your
9917 * current position on screen because that will be computed again when a
9918 * new instance of the view is placed in its view hierarchy.
9919 * <p>
9920 * Some examples of things you may store here: the current cursor position
9921 * in a text view (but usually not the text itself since that is stored in a
9922 * content provider or other persistent storage), the currently selected
9923 * item in a list view.
9924 *
9925 * @return Returns a Parcelable object containing the view's current dynamic
9926 * state, or null if there is nothing interesting to save. The
9927 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009928 * @see #onRestoreInstanceState(android.os.Parcelable)
9929 * @see #saveHierarchyState(android.util.SparseArray)
9930 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 * @see #setSaveEnabled(boolean)
9932 */
9933 protected Parcelable onSaveInstanceState() {
9934 mPrivateFlags |= SAVE_STATE_CALLED;
9935 return BaseSavedState.EMPTY_STATE;
9936 }
9937
9938 /**
9939 * Restore this view hierarchy's frozen state from the given container.
9940 *
9941 * @param container The SparseArray which holds previously frozen states.
9942 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009943 * @see #saveHierarchyState(android.util.SparseArray)
9944 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9945 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 */
9947 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9948 dispatchRestoreInstanceState(container);
9949 }
9950
9951 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009952 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9953 * state for this view and its children. May be overridden to modify how restoring
9954 * happens to a view's children; for example, some views may want to not store state
9955 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 *
9957 * @param container The SparseArray which holds previously saved state.
9958 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009959 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9960 * @see #restoreHierarchyState(android.util.SparseArray)
9961 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 */
9963 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9964 if (mID != NO_ID) {
9965 Parcelable state = container.get(mID);
9966 if (state != null) {
9967 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9968 // + ": " + state);
9969 mPrivateFlags &= ~SAVE_STATE_CALLED;
9970 onRestoreInstanceState(state);
9971 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9972 throw new IllegalStateException(
9973 "Derived class did not call super.onRestoreInstanceState()");
9974 }
9975 }
9976 }
9977 }
9978
9979 /**
9980 * Hook allowing a view to re-apply a representation of its internal state that had previously
9981 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9982 * null state.
9983 *
9984 * @param state The frozen state that had previously been returned by
9985 * {@link #onSaveInstanceState}.
9986 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009987 * @see #onSaveInstanceState()
9988 * @see #restoreHierarchyState(android.util.SparseArray)
9989 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 */
9991 protected void onRestoreInstanceState(Parcelable state) {
9992 mPrivateFlags |= SAVE_STATE_CALLED;
9993 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009994 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9995 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009996 + "when two views of different type have the same id in the same hierarchy. "
9997 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009998 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 }
10000 }
10001
10002 /**
10003 * <p>Return the time at which the drawing of the view hierarchy started.</p>
10004 *
10005 * @return the drawing start time in milliseconds
10006 */
10007 public long getDrawingTime() {
10008 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
10009 }
10010
10011 /**
10012 * <p>Enables or disables the duplication of the parent's state into this view. When
10013 * duplication is enabled, this view gets its drawable state from its parent rather
10014 * than from its own internal properties.</p>
10015 *
10016 * <p>Note: in the current implementation, setting this property to true after the
10017 * view was added to a ViewGroup might have no effect at all. This property should
10018 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
10019 *
10020 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
10021 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010022 *
Gilles Debunnefb817032011-01-13 13:52:49 -080010023 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
10024 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 *
10026 * @param enabled True to enable duplication of the parent's drawable state, false
10027 * to disable it.
10028 *
10029 * @see #getDrawableState()
10030 * @see #isDuplicateParentStateEnabled()
10031 */
10032 public void setDuplicateParentStateEnabled(boolean enabled) {
10033 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
10034 }
10035
10036 /**
10037 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
10038 *
10039 * @return True if this view's drawable state is duplicated from the parent,
10040 * false otherwise
10041 *
10042 * @see #getDrawableState()
10043 * @see #setDuplicateParentStateEnabled(boolean)
10044 */
10045 public boolean isDuplicateParentStateEnabled() {
10046 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
10047 }
10048
10049 /**
Romain Guy171c5922011-01-06 10:04:23 -080010050 * <p>Specifies the type of layer backing this view. The layer can be
10051 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
10052 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010053 *
Romain Guy171c5922011-01-06 10:04:23 -080010054 * <p>A layer is associated with an optional {@link android.graphics.Paint}
10055 * instance that controls how the layer is composed on screen. The following
10056 * properties of the paint are taken into account when composing the layer:</p>
10057 * <ul>
10058 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
10059 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
10060 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
10061 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080010062 *
Romain Guy171c5922011-01-06 10:04:23 -080010063 * <p>If this view has an alpha value set to < 1.0 by calling
10064 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
10065 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
10066 * equivalent to setting a hardware layer on this view and providing a paint with
10067 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080010068 *
Romain Guy171c5922011-01-06 10:04:23 -080010069 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
10070 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
10071 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010072 *
Romain Guy171c5922011-01-06 10:04:23 -080010073 * @param layerType The ype of layer to use with this view, must be one of
10074 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10075 * {@link #LAYER_TYPE_HARDWARE}
10076 * @param paint The paint used to compose the layer. This argument is optional
10077 * and can be null. It is ignored when the layer type is
10078 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080010079 *
10080 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080010081 * @see #LAYER_TYPE_NONE
10082 * @see #LAYER_TYPE_SOFTWARE
10083 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080010084 * @see #setAlpha(float)
10085 *
Romain Guy171c5922011-01-06 10:04:23 -080010086 * @attr ref android.R.styleable#View_layerType
10087 */
10088 public void setLayerType(int layerType, Paint paint) {
10089 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080010090 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080010091 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
10092 }
Chet Haasedaf98e92011-01-10 14:10:36 -080010093
Romain Guyd6cd5722011-01-17 14:42:41 -080010094 if (layerType == mLayerType) {
10095 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
10096 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010097 invalidateParentCaches();
10098 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080010099 }
10100 return;
10101 }
Romain Guy171c5922011-01-06 10:04:23 -080010102
10103 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080010104 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070010105 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010106 destroyLayer();
Romain Guy31f2c2e2011-11-21 10:55:41 -080010107 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080010108 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070010109 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080010110 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080010111 default:
10112 break;
Romain Guy171c5922011-01-06 10:04:23 -080010113 }
10114
10115 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080010116 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
10117 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
10118 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080010119
Romain Guy0fd89bf2011-01-26 15:41:30 -080010120 invalidateParentCaches();
10121 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080010122 }
10123
10124 /**
Romain Guy59c7f802011-09-29 17:21:45 -070010125 * Indicates whether this view has a static layer. A view with layer type
10126 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
10127 * dynamic.
10128 */
10129 boolean hasStaticLayer() {
10130 return mLayerType == LAYER_TYPE_NONE;
10131 }
10132
10133 /**
Romain Guy171c5922011-01-06 10:04:23 -080010134 * Indicates what type of layer is currently associated with this view. By default
10135 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
10136 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
10137 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080010138 *
Romain Guy171c5922011-01-06 10:04:23 -080010139 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
10140 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080010141 *
10142 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080010143 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080010144 * @see #LAYER_TYPE_NONE
10145 * @see #LAYER_TYPE_SOFTWARE
10146 * @see #LAYER_TYPE_HARDWARE
10147 */
10148 public int getLayerType() {
10149 return mLayerType;
10150 }
Joe Malin32736f02011-01-19 16:14:20 -080010151
Romain Guy6c319ca2011-01-11 14:29:25 -080010152 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080010153 * Forces this view's layer to be created and this view to be rendered
10154 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
10155 * invoking this method will have no effect.
10156 *
10157 * This method can for instance be used to render a view into its layer before
10158 * starting an animation. If this view is complex, rendering into the layer
10159 * before starting the animation will avoid skipping frames.
10160 *
10161 * @throws IllegalStateException If this view is not attached to a window
10162 *
10163 * @see #setLayerType(int, android.graphics.Paint)
10164 */
10165 public void buildLayer() {
10166 if (mLayerType == LAYER_TYPE_NONE) return;
10167
10168 if (mAttachInfo == null) {
10169 throw new IllegalStateException("This view must be attached to a window first");
10170 }
10171
10172 switch (mLayerType) {
10173 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080010174 if (mAttachInfo.mHardwareRenderer != null &&
10175 mAttachInfo.mHardwareRenderer.isEnabled() &&
10176 mAttachInfo.mHardwareRenderer.validate()) {
10177 getHardwareLayer();
10178 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010179 break;
10180 case LAYER_TYPE_SOFTWARE:
10181 buildDrawingCache(true);
10182 break;
10183 }
10184 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010185
10186 // Make sure the HardwareRenderer.validate() was invoked before calling this method
10187 void flushLayer() {
10188 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
10189 mHardwareLayer.flush();
10190 }
10191 }
Romain Guyf1ae1062011-03-02 18:16:04 -080010192
10193 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080010194 * <p>Returns a hardware layer that can be used to draw this view again
10195 * without executing its draw method.</p>
10196 *
10197 * @return A HardwareLayer ready to render, or null if an error occurred.
10198 */
Romain Guy5e7f7662011-01-24 22:35:56 -080010199 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070010200 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
10201 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080010202 return null;
10203 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080010204
10205 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080010206
10207 final int width = mRight - mLeft;
10208 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080010209
Romain Guy6c319ca2011-01-11 14:29:25 -080010210 if (width == 0 || height == 0) {
10211 return null;
10212 }
10213
10214 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
10215 if (mHardwareLayer == null) {
10216 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
10217 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -080010218 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010219 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
10220 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -080010221 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010222 }
10223
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010224 // The layer is not valid if the underlying GPU resources cannot be allocated
10225 if (!mHardwareLayer.isValid()) {
10226 return null;
10227 }
10228
Romain Guy59a12ca2011-06-09 17:48:21 -070010229 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -080010230 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070010231
10232 // Make sure all the GPU resources have been properly allocated
10233 if (canvas == null) {
10234 mHardwareLayer.end(currentCanvas);
10235 return null;
10236 }
10237
Romain Guy5e7f7662011-01-24 22:35:56 -080010238 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010239 try {
10240 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -080010241 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -080010242 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080010243
Chet Haase88172fe2011-03-07 17:36:33 -080010244 final int restoreCount = canvas.save();
10245
Romain Guy6c319ca2011-01-11 14:29:25 -080010246 computeScroll();
10247 canvas.translate(-mScrollX, -mScrollY);
10248
Romain Guy6c319ca2011-01-11 14:29:25 -080010249 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -080010250
Romain Guy6c319ca2011-01-11 14:29:25 -080010251 // Fast path for layouts with no backgrounds
10252 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10253 mPrivateFlags &= ~DIRTY_MASK;
10254 dispatchDraw(canvas);
10255 } else {
10256 draw(canvas);
10257 }
Joe Malin32736f02011-01-19 16:14:20 -080010258
Chet Haase88172fe2011-03-07 17:36:33 -080010259 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -080010260 } finally {
10261 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -080010262 mHardwareLayer.end(currentCanvas);
10263 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -080010264 }
10265 }
10266
10267 return mHardwareLayer;
10268 }
Romain Guy171c5922011-01-06 10:04:23 -080010269
Romain Guy589b0bb2011-10-10 13:57:47 -070010270 /**
10271 * Destroys this View's hardware layer if possible.
10272 *
10273 * @return True if the layer was destroyed, false otherwise.
10274 *
10275 * @see #setLayerType(int, android.graphics.Paint)
10276 * @see #LAYER_TYPE_HARDWARE
10277 */
Romain Guy65b345f2011-07-27 18:51:50 -070010278 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -070010279 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080010280 AttachInfo info = mAttachInfo;
10281 if (info != null && info.mHardwareRenderer != null &&
10282 info.mHardwareRenderer.isEnabled() && info.mHardwareRenderer.validate()) {
10283 mHardwareLayer.destroy();
10284 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080010285
Romain Guy9c4b79a2011-11-10 19:23:58 -080010286 invalidate(true);
10287 invalidateParentCaches();
10288 }
Romain Guy65b345f2011-07-27 18:51:50 -070010289 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070010290 }
Romain Guy65b345f2011-07-27 18:51:50 -070010291 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070010292 }
10293
Romain Guy171c5922011-01-06 10:04:23 -080010294 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080010295 * Destroys all hardware rendering resources. This method is invoked
10296 * when the system needs to reclaim resources. Upon execution of this
10297 * method, you should free any OpenGL resources created by the view.
10298 *
10299 * Note: you <strong>must</strong> call
10300 * <code>super.destroyHardwareResources()</code> when overriding
10301 * this method.
10302 *
10303 * @hide
10304 */
10305 protected void destroyHardwareResources() {
10306 destroyLayer();
10307 }
10308
10309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
10311 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
10312 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
10313 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
10314 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
10315 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010316 *
Romain Guy171c5922011-01-06 10:04:23 -080010317 * <p>Enabling the drawing cache is similar to
10318 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080010319 * acceleration is turned off. When hardware acceleration is turned on, enabling the
10320 * drawing cache has no effect on rendering because the system uses a different mechanism
10321 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
10322 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
10323 * for information on how to enable software and hardware layers.</p>
10324 *
10325 * <p>This API can be used to manually generate
10326 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
10327 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 *
10329 * @param enabled true to enable the drawing cache, false otherwise
10330 *
10331 * @see #isDrawingCacheEnabled()
10332 * @see #getDrawingCache()
10333 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080010334 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 */
10336 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010337 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010338 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
10339 }
10340
10341 /**
10342 * <p>Indicates whether the drawing cache is enabled for this view.</p>
10343 *
10344 * @return true if the drawing cache is enabled
10345 *
10346 * @see #setDrawingCacheEnabled(boolean)
10347 * @see #getDrawingCache()
10348 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010349 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 public boolean isDrawingCacheEnabled() {
10351 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
10352 }
10353
10354 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080010355 * Debugging utility which recursively outputs the dirty state of a view and its
10356 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080010357 *
Chet Haasedaf98e92011-01-10 14:10:36 -080010358 * @hide
10359 */
Romain Guy676b1732011-02-14 14:45:33 -080010360 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080010361 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
10362 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
10363 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
10364 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
10365 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
10366 if (clear) {
10367 mPrivateFlags &= clearMask;
10368 }
10369 if (this instanceof ViewGroup) {
10370 ViewGroup parent = (ViewGroup) this;
10371 final int count = parent.getChildCount();
10372 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080010373 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080010374 child.outputDirtyFlags(indent + " ", clear, clearMask);
10375 }
10376 }
10377 }
10378
10379 /**
10380 * This method is used by ViewGroup to cause its children to restore or recreate their
10381 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
10382 * to recreate its own display list, which would happen if it went through the normal
10383 * draw/dispatchDraw mechanisms.
10384 *
10385 * @hide
10386 */
10387 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080010388
10389 /**
10390 * A view that is not attached or hardware accelerated cannot create a display list.
10391 * This method checks these conditions and returns the appropriate result.
10392 *
10393 * @return true if view has the ability to create a display list, false otherwise.
10394 *
10395 * @hide
10396 */
10397 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080010398 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080010399 }
Joe Malin32736f02011-01-19 16:14:20 -080010400
Chet Haasedaf98e92011-01-10 14:10:36 -080010401 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080010402 * @return The HardwareRenderer associated with that view or null if hardware rendering
10403 * is not supported or this this has not been attached to a window.
10404 *
10405 * @hide
10406 */
10407 public HardwareRenderer getHardwareRenderer() {
10408 if (mAttachInfo != null) {
10409 return mAttachInfo.mHardwareRenderer;
10410 }
10411 return null;
10412 }
10413
10414 /**
Romain Guyb051e892010-09-28 19:09:36 -070010415 * <p>Returns a display list that can be used to draw this view again
10416 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010417 *
Romain Guyb051e892010-09-28 19:09:36 -070010418 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080010419 *
10420 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070010421 */
Chet Haasedaf98e92011-01-10 14:10:36 -080010422 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -080010423 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -070010424 return null;
10425 }
10426
Chet Haasedaf98e92011-01-10 14:10:36 -080010427 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
10428 mDisplayList == null || !mDisplayList.isValid() ||
10429 mRecreateDisplayList)) {
10430 // Don't need to recreate the display list, just need to tell our
10431 // children to restore/recreate theirs
10432 if (mDisplayList != null && mDisplayList.isValid() &&
10433 !mRecreateDisplayList) {
10434 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10435 mPrivateFlags &= ~DIRTY_MASK;
10436 dispatchGetDisplayList();
10437
10438 return mDisplayList;
10439 }
10440
10441 // If we got here, we're recreating it. Mark it as such to ensure that
10442 // we copy in child display lists into ours in drawChild()
10443 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -080010444 if (mDisplayList == null) {
Romain Guy13631f32012-01-30 17:41:55 -080010445 final String name = getClass().getSimpleName();
10446 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
Chet Haasedaf98e92011-01-10 14:10:36 -080010447 // If we're creating a new display list, make sure our parent gets invalidated
10448 // since they will need to recreate their display list to account for this
10449 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -080010450 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -080010451 }
Romain Guyb051e892010-09-28 19:09:36 -070010452
10453 final HardwareCanvas canvas = mDisplayList.start();
Romain Guye080af32011-09-08 15:03:39 -070010454 int restoreCount = 0;
Romain Guyb051e892010-09-28 19:09:36 -070010455 try {
10456 int width = mRight - mLeft;
10457 int height = mBottom - mTop;
10458
10459 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010460 // The dirty rect should always be null for a display list
10461 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010462
Chet Haasedaf98e92011-01-10 14:10:36 -080010463 computeScroll();
Romain Guye080af32011-09-08 15:03:39 -070010464
10465 restoreCount = canvas.save();
Chet Haasedaf98e92011-01-10 14:10:36 -080010466 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010467 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010468 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010469
Romain Guyb051e892010-09-28 19:09:36 -070010470 // Fast path for layouts with no backgrounds
10471 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010472 dispatchDraw(canvas);
10473 } else {
10474 draw(canvas);
10475 }
Romain Guyb051e892010-09-28 19:09:36 -070010476 } finally {
Romain Guye080af32011-09-08 15:03:39 -070010477 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -070010478 canvas.onPostDraw();
10479
10480 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010481 }
Chet Haase77785f92011-01-25 23:22:09 -080010482 } else {
10483 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10484 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010485 }
10486
10487 return mDisplayList;
10488 }
10489
10490 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010491 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010492 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010493 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010494 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010495 * @see #getDrawingCache(boolean)
10496 */
10497 public Bitmap getDrawingCache() {
10498 return getDrawingCache(false);
10499 }
10500
10501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10503 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10504 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10505 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10506 * request the drawing cache by calling this method and draw it on screen if the
10507 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010508 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010509 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10510 * this method will create a bitmap of the same size as this view. Because this bitmap
10511 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10512 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10513 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10514 * size than the view. This implies that your application must be able to handle this
10515 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010516 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010517 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10518 * the current density of the screen when the application is in compatibility
10519 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010521 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010522 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 * @see #setDrawingCacheEnabled(boolean)
10524 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010525 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 * @see #destroyDrawingCache()
10527 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010528 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10530 return null;
10531 }
10532 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010533 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 }
Romain Guy02890fd2010-08-06 17:58:44 -070010535 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537
10538 /**
10539 * <p>Frees the resources used by the drawing cache. If you call
10540 * {@link #buildDrawingCache()} manually without calling
10541 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10542 * should cleanup the cache with this method afterwards.</p>
10543 *
10544 * @see #setDrawingCacheEnabled(boolean)
10545 * @see #buildDrawingCache()
10546 * @see #getDrawingCache()
10547 */
10548 public void destroyDrawingCache() {
10549 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010550 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 mDrawingCache = null;
10552 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010553 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010554 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010555 mUnscaledDrawingCache = null;
10556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 }
10558
10559 /**
10560 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070010561 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 * view will always be drawn on top of a solid color.
10563 *
10564 * @param color The background color to use for the drawing cache's bitmap
10565 *
10566 * @see #setDrawingCacheEnabled(boolean)
10567 * @see #buildDrawingCache()
10568 * @see #getDrawingCache()
10569 */
10570 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010571 if (color != mDrawingCacheBackgroundColor) {
10572 mDrawingCacheBackgroundColor = color;
10573 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
10576
10577 /**
10578 * @see #setDrawingCacheBackgroundColor(int)
10579 *
10580 * @return The background color to used for the drawing cache's bitmap
10581 */
10582 public int getDrawingCacheBackgroundColor() {
10583 return mDrawingCacheBackgroundColor;
10584 }
10585
10586 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010587 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010588 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010589 * @see #buildDrawingCache(boolean)
10590 */
10591 public void buildDrawingCache() {
10592 buildDrawingCache(false);
10593 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010594
Romain Guyfbd8f692009-06-26 14:51:58 -070010595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10597 *
10598 * <p>If you call {@link #buildDrawingCache()} manually without calling
10599 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10600 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010601 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010602 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10603 * this method will create a bitmap of the same size as this view. Because this bitmap
10604 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10605 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10606 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10607 * size than the view. This implies that your application must be able to handle this
10608 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010609 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010610 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10611 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010612 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010613 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 *
10615 * @see #getDrawingCache()
10616 * @see #destroyDrawingCache()
10617 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010618 public void buildDrawingCache(boolean autoScale) {
10619 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010620 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010621 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622
10623 if (ViewDebug.TRACE_HIERARCHY) {
10624 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626
Romain Guy8506ab42009-06-11 17:35:47 -070010627 int width = mRight - mLeft;
10628 int height = mBottom - mTop;
10629
10630 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010631 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010632
Romain Guye1123222009-06-29 14:24:56 -070010633 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010634 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10635 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637
10638 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010639 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010640 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641
10642 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010643 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010644 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10646 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010647 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 return;
10649 }
10650
10651 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010652 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653
10654 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 Bitmap.Config quality;
10656 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010657 // Never pick ARGB_4444 because it looks awful
10658 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10660 case DRAWING_CACHE_QUALITY_AUTO:
10661 quality = Bitmap.Config.ARGB_8888;
10662 break;
10663 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010664 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 break;
10666 case DRAWING_CACHE_QUALITY_HIGH:
10667 quality = Bitmap.Config.ARGB_8888;
10668 break;
10669 default:
10670 quality = Bitmap.Config.ARGB_8888;
10671 break;
10672 }
10673 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010674 // Optimization for translucent windows
10675 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010676 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010677 }
10678
10679 // Try to cleanup memory
10680 if (bitmap != null) bitmap.recycle();
10681
10682 try {
10683 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010684 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010685 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010686 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010687 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010688 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010689 }
Adam Powell26153a32010-11-08 15:22:27 -080010690 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 } catch (OutOfMemoryError e) {
10692 // If there is not enough memory to create the bitmap cache, just
10693 // ignore the issue as bitmap caches are not required to draw the
10694 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010695 if (autoScale) {
10696 mDrawingCache = null;
10697 } else {
10698 mUnscaledDrawingCache = null;
10699 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010700 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 return;
10702 }
10703
10704 clear = drawingCacheBackgroundColor != 0;
10705 }
10706
10707 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 if (attachInfo != null) {
10709 canvas = attachInfo.mCanvas;
10710 if (canvas == null) {
10711 canvas = new Canvas();
10712 }
10713 canvas.setBitmap(bitmap);
10714 // Temporarily clobber the cached Canvas in case one of our children
10715 // is also using a drawing cache. Without this, the children would
10716 // steal the canvas by attaching their own bitmap to it and bad, bad
10717 // thing would happen (invisible views, corrupted drawings, etc.)
10718 attachInfo.mCanvas = null;
10719 } else {
10720 // This case should hopefully never or seldom happen
10721 canvas = new Canvas(bitmap);
10722 }
10723
10724 if (clear) {
10725 bitmap.eraseColor(drawingCacheBackgroundColor);
10726 }
10727
10728 computeScroll();
10729 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010730
Romain Guye1123222009-06-29 14:24:56 -070010731 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010732 final float scale = attachInfo.mApplicationScale;
10733 canvas.scale(scale, scale);
10734 }
Joe Malin32736f02011-01-19 16:14:20 -080010735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 canvas.translate(-mScrollX, -mScrollY);
10737
Romain Guy5bcdff42009-05-14 21:27:18 -070010738 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010739 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10740 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010741 mPrivateFlags |= DRAWING_CACHE_VALID;
10742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743
10744 // Fast path for layouts with no backgrounds
10745 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10746 if (ViewDebug.TRACE_HIERARCHY) {
10747 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10748 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010749 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 dispatchDraw(canvas);
10751 } else {
10752 draw(canvas);
10753 }
10754
10755 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010756 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010757
10758 if (attachInfo != null) {
10759 // Restore the cached Canvas for our siblings
10760 attachInfo.mCanvas = canvas;
10761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 }
10763 }
10764
10765 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010766 * Create a snapshot of the view into a bitmap. We should probably make
10767 * some form of this public, but should think about the API.
10768 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010769 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010770 int width = mRight - mLeft;
10771 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010772
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010773 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010774 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010775 width = (int) ((width * scale) + 0.5f);
10776 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010777
Romain Guy8c11e312009-09-14 15:15:30 -070010778 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010779 if (bitmap == null) {
10780 throw new OutOfMemoryError();
10781 }
10782
Romain Guyc529d8d2011-09-06 15:01:39 -070010783 Resources resources = getResources();
10784 if (resources != null) {
10785 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10786 }
Joe Malin32736f02011-01-19 16:14:20 -080010787
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010788 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010789 if (attachInfo != null) {
10790 canvas = attachInfo.mCanvas;
10791 if (canvas == null) {
10792 canvas = new Canvas();
10793 }
10794 canvas.setBitmap(bitmap);
10795 // Temporarily clobber the cached Canvas in case one of our children
10796 // is also using a drawing cache. Without this, the children would
10797 // steal the canvas by attaching their own bitmap to it and bad, bad
10798 // things would happen (invisible views, corrupted drawings, etc.)
10799 attachInfo.mCanvas = null;
10800 } else {
10801 // This case should hopefully never or seldom happen
10802 canvas = new Canvas(bitmap);
10803 }
10804
Romain Guy5bcdff42009-05-14 21:27:18 -070010805 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010806 bitmap.eraseColor(backgroundColor);
10807 }
10808
10809 computeScroll();
10810 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010811 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010812 canvas.translate(-mScrollX, -mScrollY);
10813
Romain Guy5bcdff42009-05-14 21:27:18 -070010814 // Temporarily remove the dirty mask
10815 int flags = mPrivateFlags;
10816 mPrivateFlags &= ~DIRTY_MASK;
10817
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010818 // Fast path for layouts with no backgrounds
10819 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10820 dispatchDraw(canvas);
10821 } else {
10822 draw(canvas);
10823 }
10824
Romain Guy5bcdff42009-05-14 21:27:18 -070010825 mPrivateFlags = flags;
10826
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010827 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010828 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010829
10830 if (attachInfo != null) {
10831 // Restore the cached Canvas for our siblings
10832 attachInfo.mCanvas = canvas;
10833 }
Romain Guy8506ab42009-06-11 17:35:47 -070010834
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010835 return bitmap;
10836 }
10837
10838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 * Indicates whether this View is currently in edit mode. A View is usually
10840 * in edit mode when displayed within a developer tool. For instance, if
10841 * this View is being drawn by a visual user interface builder, this method
10842 * should return true.
10843 *
10844 * Subclasses should check the return value of this method to provide
10845 * different behaviors if their normal behavior might interfere with the
10846 * host environment. For instance: the class spawns a thread in its
10847 * constructor, the drawing code relies on device-specific features, etc.
10848 *
10849 * This method is usually checked in the drawing code of custom widgets.
10850 *
10851 * @return True if this View is in edit mode, false otherwise.
10852 */
10853 public boolean isInEditMode() {
10854 return false;
10855 }
10856
10857 /**
10858 * If the View draws content inside its padding and enables fading edges,
10859 * it needs to support padding offsets. Padding offsets are added to the
10860 * fading edges to extend the length of the fade so that it covers pixels
10861 * drawn inside the padding.
10862 *
10863 * Subclasses of this class should override this method if they need
10864 * to draw content inside the padding.
10865 *
10866 * @return True if padding offset must be applied, false otherwise.
10867 *
10868 * @see #getLeftPaddingOffset()
10869 * @see #getRightPaddingOffset()
10870 * @see #getTopPaddingOffset()
10871 * @see #getBottomPaddingOffset()
10872 *
10873 * @since CURRENT
10874 */
10875 protected boolean isPaddingOffsetRequired() {
10876 return false;
10877 }
10878
10879 /**
10880 * Amount by which to extend the left fading region. Called only when
10881 * {@link #isPaddingOffsetRequired()} returns true.
10882 *
10883 * @return The left padding offset in pixels.
10884 *
10885 * @see #isPaddingOffsetRequired()
10886 *
10887 * @since CURRENT
10888 */
10889 protected int getLeftPaddingOffset() {
10890 return 0;
10891 }
10892
10893 /**
10894 * Amount by which to extend the right fading region. Called only when
10895 * {@link #isPaddingOffsetRequired()} returns true.
10896 *
10897 * @return The right padding offset in pixels.
10898 *
10899 * @see #isPaddingOffsetRequired()
10900 *
10901 * @since CURRENT
10902 */
10903 protected int getRightPaddingOffset() {
10904 return 0;
10905 }
10906
10907 /**
10908 * Amount by which to extend the top fading region. Called only when
10909 * {@link #isPaddingOffsetRequired()} returns true.
10910 *
10911 * @return The top padding offset in pixels.
10912 *
10913 * @see #isPaddingOffsetRequired()
10914 *
10915 * @since CURRENT
10916 */
10917 protected int getTopPaddingOffset() {
10918 return 0;
10919 }
10920
10921 /**
10922 * Amount by which to extend the bottom fading region. Called only when
10923 * {@link #isPaddingOffsetRequired()} returns true.
10924 *
10925 * @return The bottom padding offset in pixels.
10926 *
10927 * @see #isPaddingOffsetRequired()
10928 *
10929 * @since CURRENT
10930 */
10931 protected int getBottomPaddingOffset() {
10932 return 0;
10933 }
10934
10935 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010936 * @hide
10937 * @param offsetRequired
10938 */
10939 protected int getFadeTop(boolean offsetRequired) {
10940 int top = mPaddingTop;
10941 if (offsetRequired) top += getTopPaddingOffset();
10942 return top;
10943 }
10944
10945 /**
10946 * @hide
10947 * @param offsetRequired
10948 */
10949 protected int getFadeHeight(boolean offsetRequired) {
10950 int padding = mPaddingTop;
10951 if (offsetRequired) padding += getTopPaddingOffset();
10952 return mBottom - mTop - mPaddingBottom - padding;
10953 }
10954
10955 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010956 * <p>Indicates whether this view is attached to an hardware accelerated
10957 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010958 *
Romain Guy2bffd262010-09-12 17:40:02 -070010959 * <p>Even if this method returns true, it does not mean that every call
10960 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10961 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10962 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10963 * window is hardware accelerated,
10964 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10965 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010966 *
Romain Guy2bffd262010-09-12 17:40:02 -070010967 * @return True if the view is attached to a window and the window is
10968 * hardware accelerated; false in any other case.
10969 */
10970 public boolean isHardwareAccelerated() {
10971 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10972 }
Joe Malin32736f02011-01-19 16:14:20 -080010973
Romain Guy2bffd262010-09-12 17:40:02 -070010974 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080010975 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
10976 * case of an active Animation being run on the view.
10977 */
10978 private boolean drawAnimation(ViewGroup parent, long drawingTime,
10979 Animation a, boolean scalingRequired) {
10980 Transformation invalidationTransform;
10981 final int flags = parent.mGroupFlags;
10982 final boolean initialized = a.isInitialized();
10983 if (!initialized) {
10984 a.initialize(mRight - mLeft, mBottom - mTop, getWidth(), getHeight());
10985 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
10986 onAnimationStart();
10987 }
10988
10989 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
10990 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
10991 if (parent.mInvalidationTransformation == null) {
10992 parent.mInvalidationTransformation = new Transformation();
10993 }
10994 invalidationTransform = parent.mInvalidationTransformation;
10995 a.getTransformation(drawingTime, invalidationTransform, 1f);
10996 } else {
10997 invalidationTransform = parent.mChildTransformation;
10998 }
10999 if (more) {
11000 if (!a.willChangeBounds()) {
11001 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
11002 parent.FLAG_OPTIMIZE_INVALIDATE) {
11003 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
11004 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
11005 // The child need to draw an animation, potentially offscreen, so
11006 // make sure we do not cancel invalidate requests
11007 parent.mPrivateFlags |= DRAW_ANIMATION;
11008 parent.invalidate(mLeft, mTop, mRight, mBottom);
11009 }
11010 } else {
11011 if (parent.mInvalidateRegion == null) {
11012 parent.mInvalidateRegion = new RectF();
11013 }
11014 final RectF region = parent.mInvalidateRegion;
11015 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
11016 invalidationTransform);
11017
11018 // The child need to draw an animation, potentially offscreen, so
11019 // make sure we do not cancel invalidate requests
11020 parent.mPrivateFlags |= DRAW_ANIMATION;
11021
11022 final int left = mLeft + (int) region.left;
11023 final int top = mTop + (int) region.top;
11024 parent.invalidate(left, top, left + (int) (region.width() + .5f),
11025 top + (int) (region.height() + .5f));
11026 }
11027 }
11028 return more;
11029 }
11030
11031 /**
Chet Haase64a48c12012-02-13 16:33:29 -080011032 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
11033 * This draw() method is an implementation detail and is not intended to be overridden or
11034 * to be called from anywhere else other than ViewGroup.drawChild().
11035 */
11036 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
11037 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080011038 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080011039 final int flags = parent.mGroupFlags;
11040
11041 if ((flags & parent.FLAG_CLEAR_TRANSFORMATION) == parent.FLAG_CLEAR_TRANSFORMATION) {
11042 parent.mChildTransformation.clear();
11043 parent.mGroupFlags &= ~parent.FLAG_CLEAR_TRANSFORMATION;
11044 }
11045
11046 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080011047 boolean concatMatrix = false;
11048
11049 boolean scalingRequired = false;
11050 boolean caching;
11051 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
11052
11053 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
11054 if ((flags & parent.FLAG_CHILDREN_DRAWN_WITH_CACHE) == parent.FLAG_CHILDREN_DRAWN_WITH_CACHE ||
11055 (flags & parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) == parent.FLAG_ALWAYS_DRAWN_WITH_CACHE) {
11056 caching = true;
11057 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
11058 } else {
11059 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
11060 }
11061
Chet Haasebcca79a2012-02-14 08:45:14 -080011062 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080011063 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011064 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080011065 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080011066 transformToApply = parent.mChildTransformation;
Chet Haase64a48c12012-02-13 16:33:29 -080011067 } else if ((flags & parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) ==
11068 parent.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011069 final boolean hasTransform =
11070 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080011071 if (hasTransform) {
11072 final int transformType = parent.mChildTransformation.getTransformationType();
11073 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
11074 parent.mChildTransformation : null;
11075 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
11076 }
11077 }
11078
11079 concatMatrix |= !childHasIdentityMatrix;
11080
11081 // Sets the flag as early as possible to allow draw() implementations
11082 // to call invalidate() successfully when doing animations
11083 mPrivateFlags |= DRAWN;
11084
Chet Haasebcca79a2012-02-14 08:45:14 -080011085 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080011086 (mPrivateFlags & DRAW_ANIMATION) == 0) {
11087 return more;
11088 }
11089
11090 if (hardwareAccelerated) {
11091 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
11092 // retain the flag's value temporarily in the mRecreateDisplayList flag
11093 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
11094 mPrivateFlags &= ~INVALIDATED;
11095 }
11096
11097 computeScroll();
11098
11099 final int sx = mScrollX;
11100 final int sy = mScrollY;
11101
11102 DisplayList displayList = null;
11103 Bitmap cache = null;
11104 boolean hasDisplayList = false;
11105 if (caching) {
11106 if (!hardwareAccelerated) {
11107 if (layerType != LAYER_TYPE_NONE) {
11108 layerType = LAYER_TYPE_SOFTWARE;
11109 buildDrawingCache(true);
11110 }
11111 cache = getDrawingCache(true);
11112 } else {
11113 switch (layerType) {
11114 case LAYER_TYPE_SOFTWARE:
11115 buildDrawingCache(true);
11116 cache = getDrawingCache(true);
11117 break;
11118 case LAYER_TYPE_NONE:
11119 // Delay getting the display list until animation-driven alpha values are
11120 // set up and possibly passed on to the view
11121 hasDisplayList = canHaveDisplayList();
11122 break;
11123 }
11124 }
11125 }
11126
11127 final boolean hasNoCache = cache == null || hasDisplayList;
11128 final boolean offsetForScroll = cache == null && !hasDisplayList &&
11129 layerType != LAYER_TYPE_HARDWARE;
11130
11131 final int restoreTo = canvas.save();
11132 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011133 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080011134 } else {
Chet Haasebcca79a2012-02-14 08:45:14 -080011135 canvas.translate(mLeft, mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011136 if (scalingRequired) {
11137 // mAttachInfo cannot be null, otherwise scalingRequired == false
11138 final float scale = 1.0f / mAttachInfo.mApplicationScale;
11139 canvas.scale(scale, scale);
11140 }
11141 }
11142
11143 float alpha = getAlpha();
11144 if (transformToApply != null || alpha < 1.0f || !hasIdentityMatrix()) {
11145 if (transformToApply != null || !childHasIdentityMatrix) {
11146 int transX = 0;
11147 int transY = 0;
11148
11149 if (offsetForScroll) {
11150 transX = -sx;
11151 transY = -sy;
11152 }
11153
11154 if (transformToApply != null) {
11155 if (concatMatrix) {
11156 // Undo the scroll translation, apply the transformation matrix,
11157 // then redo the scroll translate to get the correct result.
11158 canvas.translate(-transX, -transY);
11159 canvas.concat(transformToApply.getMatrix());
11160 canvas.translate(transX, transY);
11161 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11162 }
11163
11164 float transformAlpha = transformToApply.getAlpha();
11165 if (transformAlpha < 1.0f) {
11166 alpha *= transformToApply.getAlpha();
11167 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11168 }
11169 }
11170
11171 if (!childHasIdentityMatrix) {
11172 canvas.translate(-transX, -transY);
11173 canvas.concat(getMatrix());
11174 canvas.translate(transX, transY);
11175 }
11176 }
11177
11178 if (alpha < 1.0f) {
11179 parent.mGroupFlags |= parent.FLAG_CLEAR_TRANSFORMATION;
11180 if (hasNoCache) {
11181 final int multipliedAlpha = (int) (255 * alpha);
11182 if (!onSetAlpha(multipliedAlpha)) {
11183 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11184 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN ||
11185 layerType != LAYER_TYPE_NONE) {
11186 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
11187 }
11188 if (layerType == LAYER_TYPE_NONE) {
11189 final int scrollX = hasDisplayList ? 0 : sx;
11190 final int scrollY = hasDisplayList ? 0 : sy;
Chet Haasebcca79a2012-02-14 08:45:14 -080011191 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
11192 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080011193 }
11194 } else {
11195 // Alpha is handled by the child directly, clobber the layer's alpha
11196 mPrivateFlags |= ALPHA_SET;
11197 }
11198 }
11199 }
11200 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11201 onSetAlpha(255);
11202 mPrivateFlags &= ~ALPHA_SET;
11203 }
11204
11205 if ((flags & parent.FLAG_CLIP_CHILDREN) == parent.FLAG_CLIP_CHILDREN) {
11206 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011207 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080011208 } else {
11209 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080011210 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080011211 } else {
11212 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
11213 }
11214 }
11215 }
11216
11217 if (hasDisplayList) {
11218 displayList = getDisplayList();
11219 if (!displayList.isValid()) {
11220 // Uncommon, but possible. If a view is removed from the hierarchy during the call
11221 // to getDisplayList(), the display list will be marked invalid and we should not
11222 // try to use it again.
11223 displayList = null;
11224 hasDisplayList = false;
11225 }
11226 }
11227
11228 if (hasNoCache) {
11229 boolean layerRendered = false;
11230 if (layerType == LAYER_TYPE_HARDWARE) {
11231 final HardwareLayer layer = getHardwareLayer();
11232 if (layer != null && layer.isValid()) {
11233 mLayerPaint.setAlpha((int) (alpha * 255));
11234 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
11235 layerRendered = true;
11236 } else {
11237 final int scrollX = hasDisplayList ? 0 : sx;
11238 final int scrollY = hasDisplayList ? 0 : sy;
11239 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080011240 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080011241 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11242 }
11243 }
11244
11245 if (!layerRendered) {
11246 if (!hasDisplayList) {
11247 // Fast path for layouts with no backgrounds
11248 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11249 if (ViewDebug.TRACE_HIERARCHY) {
11250 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
11251 }
11252 mPrivateFlags &= ~DIRTY_MASK;
11253 dispatchDraw(canvas);
11254 } else {
11255 draw(canvas);
11256 }
11257 } else {
11258 mPrivateFlags &= ~DIRTY_MASK;
Chet Haasebcca79a2012-02-14 08:45:14 -080011259 ((HardwareCanvas) canvas).drawDisplayList(displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -080011260 mRight - mLeft, mBottom - mTop, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080011261 }
11262 }
11263 } else if (cache != null) {
11264 mPrivateFlags &= ~DIRTY_MASK;
11265 Paint cachePaint;
11266
11267 if (layerType == LAYER_TYPE_NONE) {
11268 cachePaint = parent.mCachePaint;
11269 if (cachePaint == null) {
11270 cachePaint = new Paint();
11271 cachePaint.setDither(false);
11272 parent.mCachePaint = cachePaint;
11273 }
11274 if (alpha < 1.0f) {
11275 cachePaint.setAlpha((int) (alpha * 255));
11276 parent.mGroupFlags |= parent.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haasebcca79a2012-02-14 08:45:14 -080011277 } else if ((flags & parent.FLAG_ALPHA_LOWER_THAN_ONE) ==
11278 parent.FLAG_ALPHA_LOWER_THAN_ONE) {
Chet Haase64a48c12012-02-13 16:33:29 -080011279 cachePaint.setAlpha(255);
11280 parent.mGroupFlags &= ~parent.FLAG_ALPHA_LOWER_THAN_ONE;
11281 }
11282 } else {
11283 cachePaint = mLayerPaint;
11284 cachePaint.setAlpha((int) (alpha * 255));
11285 }
11286 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
11287 }
11288
11289 canvas.restoreToCount(restoreTo);
11290
11291 if (a != null && !more) {
11292 if (!hardwareAccelerated && !a.getFillAfter()) {
11293 onSetAlpha(255);
11294 }
11295 parent.finishAnimatingView(this, a);
11296 }
11297
11298 if (more && hardwareAccelerated) {
11299 // invalidation is the trigger to recreate display lists, so if we're using
11300 // display lists to render, force an invalidate to allow the animation to
11301 // continue drawing another frame
11302 parent.invalidate(true);
11303 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
11304 // alpha animations should cause the child to recreate its display list
11305 invalidate(true);
11306 }
11307 }
11308
11309 mRecreateDisplayList = false;
11310
11311 return more;
11312 }
11313
11314 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 * Manually render this view (and all of its children) to the given Canvas.
11316 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011317 * called. When implementing a view, implement
11318 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
11319 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 *
11321 * @param canvas The Canvas to which the View is rendered.
11322 */
11323 public void draw(Canvas canvas) {
11324 if (ViewDebug.TRACE_HIERARCHY) {
11325 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
11326 }
11327
Romain Guy5bcdff42009-05-14 21:27:18 -070011328 final int privateFlags = mPrivateFlags;
11329 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
11330 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
11331 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070011332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 /*
11334 * Draw traversal performs several drawing steps which must be executed
11335 * in the appropriate order:
11336 *
11337 * 1. Draw the background
11338 * 2. If necessary, save the canvas' layers to prepare for fading
11339 * 3. Draw view's content
11340 * 4. Draw children
11341 * 5. If necessary, draw the fading edges and restore layers
11342 * 6. Draw decorations (scrollbars for instance)
11343 */
11344
11345 // Step 1, draw the background, if needed
11346 int saveCount;
11347
Romain Guy24443ea2009-05-11 11:56:30 -070011348 if (!dirtyOpaque) {
11349 final Drawable background = mBGDrawable;
11350 if (background != null) {
11351 final int scrollX = mScrollX;
11352 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353
Romain Guy24443ea2009-05-11 11:56:30 -070011354 if (mBackgroundSizeChanged) {
11355 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
11356 mBackgroundSizeChanged = false;
11357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011358
Romain Guy24443ea2009-05-11 11:56:30 -070011359 if ((scrollX | scrollY) == 0) {
11360 background.draw(canvas);
11361 } else {
11362 canvas.translate(scrollX, scrollY);
11363 background.draw(canvas);
11364 canvas.translate(-scrollX, -scrollY);
11365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366 }
11367 }
11368
11369 // skip step 2 & 5 if possible (common case)
11370 final int viewFlags = mViewFlags;
11371 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
11372 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
11373 if (!verticalEdges && !horizontalEdges) {
11374 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011375 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376
11377 // Step 4, draw the children
11378 dispatchDraw(canvas);
11379
11380 // Step 6, draw decorations (scrollbars)
11381 onDrawScrollBars(canvas);
11382
11383 // we're done...
11384 return;
11385 }
11386
11387 /*
11388 * Here we do the full fledged routine...
11389 * (this is an uncommon case where speed matters less,
11390 * this is why we repeat some of the tests that have been
11391 * done above)
11392 */
11393
11394 boolean drawTop = false;
11395 boolean drawBottom = false;
11396 boolean drawLeft = false;
11397 boolean drawRight = false;
11398
11399 float topFadeStrength = 0.0f;
11400 float bottomFadeStrength = 0.0f;
11401 float leftFadeStrength = 0.0f;
11402 float rightFadeStrength = 0.0f;
11403
11404 // Step 2, save the canvas' layers
11405 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011406
11407 final boolean offsetRequired = isPaddingOffsetRequired();
11408 if (offsetRequired) {
11409 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 }
11411
11412 int left = mScrollX + paddingLeft;
11413 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070011414 int top = mScrollY + getFadeTop(offsetRequired);
11415 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416
11417 if (offsetRequired) {
11418 right += getRightPaddingOffset();
11419 bottom += getBottomPaddingOffset();
11420 }
11421
11422 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011423 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
11424 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425
11426 // clip the fade length if top and bottom fades overlap
11427 // overlapping fades produce odd-looking artifacts
11428 if (verticalEdges && (top + length > bottom - length)) {
11429 length = (bottom - top) / 2;
11430 }
11431
11432 // also clip horizontal fades if necessary
11433 if (horizontalEdges && (left + length > right - length)) {
11434 length = (right - left) / 2;
11435 }
11436
11437 if (verticalEdges) {
11438 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011439 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011440 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011441 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 }
11443
11444 if (horizontalEdges) {
11445 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011446 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011447 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011448 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 }
11450
11451 saveCount = canvas.getSaveCount();
11452
11453 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070011454 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
11456
11457 if (drawTop) {
11458 canvas.saveLayer(left, top, right, top + length, null, flags);
11459 }
11460
11461 if (drawBottom) {
11462 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
11463 }
11464
11465 if (drawLeft) {
11466 canvas.saveLayer(left, top, left + length, bottom, null, flags);
11467 }
11468
11469 if (drawRight) {
11470 canvas.saveLayer(right - length, top, right, bottom, null, flags);
11471 }
11472 } else {
11473 scrollabilityCache.setFadeColor(solidColor);
11474 }
11475
11476 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070011477 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478
11479 // Step 4, draw the children
11480 dispatchDraw(canvas);
11481
11482 // Step 5, draw the fade effect and restore layers
11483 final Paint p = scrollabilityCache.paint;
11484 final Matrix matrix = scrollabilityCache.matrix;
11485 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486
11487 if (drawTop) {
11488 matrix.setScale(1, fadeHeight * topFadeStrength);
11489 matrix.postTranslate(left, top);
11490 fade.setLocalMatrix(matrix);
11491 canvas.drawRect(left, top, right, top + length, p);
11492 }
11493
11494 if (drawBottom) {
11495 matrix.setScale(1, fadeHeight * bottomFadeStrength);
11496 matrix.postRotate(180);
11497 matrix.postTranslate(left, bottom);
11498 fade.setLocalMatrix(matrix);
11499 canvas.drawRect(left, bottom - length, right, bottom, p);
11500 }
11501
11502 if (drawLeft) {
11503 matrix.setScale(1, fadeHeight * leftFadeStrength);
11504 matrix.postRotate(-90);
11505 matrix.postTranslate(left, top);
11506 fade.setLocalMatrix(matrix);
11507 canvas.drawRect(left, top, left + length, bottom, p);
11508 }
11509
11510 if (drawRight) {
11511 matrix.setScale(1, fadeHeight * rightFadeStrength);
11512 matrix.postRotate(90);
11513 matrix.postTranslate(right, top);
11514 fade.setLocalMatrix(matrix);
11515 canvas.drawRect(right - length, top, right, bottom, p);
11516 }
11517
11518 canvas.restoreToCount(saveCount);
11519
11520 // Step 6, draw decorations (scrollbars)
11521 onDrawScrollBars(canvas);
11522 }
11523
11524 /**
11525 * Override this if your view is known to always be drawn on top of a solid color background,
11526 * and needs to draw fading edges. Returning a non-zero color enables the view system to
11527 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
11528 * should be set to 0xFF.
11529 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011530 * @see #setVerticalFadingEdgeEnabled(boolean)
11531 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 *
11533 * @return The known solid color background for this view, or 0 if the color may vary
11534 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011535 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 public int getSolidColor() {
11537 return 0;
11538 }
11539
11540 /**
11541 * Build a human readable string representation of the specified view flags.
11542 *
11543 * @param flags the view flags to convert to a string
11544 * @return a String representing the supplied flags
11545 */
11546 private static String printFlags(int flags) {
11547 String output = "";
11548 int numFlags = 0;
11549 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
11550 output += "TAKES_FOCUS";
11551 numFlags++;
11552 }
11553
11554 switch (flags & VISIBILITY_MASK) {
11555 case INVISIBLE:
11556 if (numFlags > 0) {
11557 output += " ";
11558 }
11559 output += "INVISIBLE";
11560 // USELESS HERE numFlags++;
11561 break;
11562 case GONE:
11563 if (numFlags > 0) {
11564 output += " ";
11565 }
11566 output += "GONE";
11567 // USELESS HERE numFlags++;
11568 break;
11569 default:
11570 break;
11571 }
11572 return output;
11573 }
11574
11575 /**
11576 * Build a human readable string representation of the specified private
11577 * view flags.
11578 *
11579 * @param privateFlags the private view flags to convert to a string
11580 * @return a String representing the supplied flags
11581 */
11582 private static String printPrivateFlags(int privateFlags) {
11583 String output = "";
11584 int numFlags = 0;
11585
11586 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
11587 output += "WANTS_FOCUS";
11588 numFlags++;
11589 }
11590
11591 if ((privateFlags & FOCUSED) == FOCUSED) {
11592 if (numFlags > 0) {
11593 output += " ";
11594 }
11595 output += "FOCUSED";
11596 numFlags++;
11597 }
11598
11599 if ((privateFlags & SELECTED) == SELECTED) {
11600 if (numFlags > 0) {
11601 output += " ";
11602 }
11603 output += "SELECTED";
11604 numFlags++;
11605 }
11606
11607 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
11608 if (numFlags > 0) {
11609 output += " ";
11610 }
11611 output += "IS_ROOT_NAMESPACE";
11612 numFlags++;
11613 }
11614
11615 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
11616 if (numFlags > 0) {
11617 output += " ";
11618 }
11619 output += "HAS_BOUNDS";
11620 numFlags++;
11621 }
11622
11623 if ((privateFlags & DRAWN) == DRAWN) {
11624 if (numFlags > 0) {
11625 output += " ";
11626 }
11627 output += "DRAWN";
11628 // USELESS HERE numFlags++;
11629 }
11630 return output;
11631 }
11632
11633 /**
11634 * <p>Indicates whether or not this view's layout will be requested during
11635 * the next hierarchy layout pass.</p>
11636 *
11637 * @return true if the layout will be forced during next layout pass
11638 */
11639 public boolean isLayoutRequested() {
11640 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
11641 }
11642
11643 /**
11644 * Assign a size and position to a view and all of its
11645 * descendants
11646 *
11647 * <p>This is the second phase of the layout mechanism.
11648 * (The first is measuring). In this phase, each parent calls
11649 * layout on all of its children to position them.
11650 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080011651 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 *
Chet Haase9c087442011-01-12 16:20:16 -080011653 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 * Derived classes with children should override
11655 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080011656 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011657 *
11658 * @param l Left position, relative to parent
11659 * @param t Top position, relative to parent
11660 * @param r Right position, relative to parent
11661 * @param b Bottom position, relative to parent
11662 */
Romain Guy5429e1d2010-09-07 12:38:00 -070011663 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080011664 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070011665 int oldL = mLeft;
11666 int oldT = mTop;
11667 int oldB = mBottom;
11668 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 boolean changed = setFrame(l, t, r, b);
11670 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
11671 if (ViewDebug.TRACE_HIERARCHY) {
11672 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
11673 }
11674
11675 onLayout(changed, l, t, r, b);
11676 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070011677
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011678 ListenerInfo li = mListenerInfo;
11679 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070011680 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011681 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070011682 int numListeners = listenersCopy.size();
11683 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070011684 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070011685 }
11686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011687 }
11688 mPrivateFlags &= ~FORCE_LAYOUT;
11689 }
11690
11691 /**
11692 * Called from layout when this view should
11693 * assign a size and position to each of its children.
11694 *
11695 * Derived classes with children should override
11696 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070011697 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011698 * @param changed This is a new size or position for this view
11699 * @param left Left position, relative to parent
11700 * @param top Top position, relative to parent
11701 * @param right Right position, relative to parent
11702 * @param bottom Bottom position, relative to parent
11703 */
11704 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
11705 }
11706
11707 /**
11708 * Assign a size and position to this view.
11709 *
11710 * This is called from layout.
11711 *
11712 * @param left Left position, relative to parent
11713 * @param top Top position, relative to parent
11714 * @param right Right position, relative to parent
11715 * @param bottom Bottom position, relative to parent
11716 * @return true if the new size and position are different than the
11717 * previous ones
11718 * {@hide}
11719 */
11720 protected boolean setFrame(int left, int top, int right, int bottom) {
11721 boolean changed = false;
11722
11723 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070011724 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 + right + "," + bottom + ")");
11726 }
11727
11728 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
11729 changed = true;
11730
11731 // Remember our drawn bit
11732 int drawn = mPrivateFlags & DRAWN;
11733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011734 int oldWidth = mRight - mLeft;
11735 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070011736 int newWidth = right - left;
11737 int newHeight = bottom - top;
11738 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
11739
11740 // Invalidate our old position
11741 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742
11743 mLeft = left;
11744 mTop = top;
11745 mRight = right;
11746 mBottom = bottom;
11747
11748 mPrivateFlags |= HAS_BOUNDS;
11749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011750
Chet Haase75755e22011-07-18 17:48:25 -070011751 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011752 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
11753 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070011754 if (mTransformationInfo != null) {
11755 mTransformationInfo.mMatrixDirty = true;
11756 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
11759 }
11760
11761 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
11762 // If we are visible, force the DRAWN bit to on so that
11763 // this invalidate will go through (at least to our parent).
11764 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080011765 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011766 // the DRAWN bit.
11767 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070011768 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080011769 // parent display list may need to be recreated based on a change in the bounds
11770 // of any child
11771 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 }
11773
11774 // Reset drawn bit to original value (invalidate turns it off)
11775 mPrivateFlags |= drawn;
11776
11777 mBackgroundSizeChanged = true;
11778 }
11779 return changed;
11780 }
11781
11782 /**
11783 * Finalize inflating a view from XML. This is called as the last phase
11784 * of inflation, after all child views have been added.
11785 *
11786 * <p>Even if the subclass overrides onFinishInflate, they should always be
11787 * sure to call the super method, so that we get called.
11788 */
11789 protected void onFinishInflate() {
11790 }
11791
11792 /**
11793 * Returns the resources associated with this view.
11794 *
11795 * @return Resources object.
11796 */
11797 public Resources getResources() {
11798 return mResources;
11799 }
11800
11801 /**
11802 * Invalidates the specified Drawable.
11803 *
11804 * @param drawable the drawable to invalidate
11805 */
11806 public void invalidateDrawable(Drawable drawable) {
11807 if (verifyDrawable(drawable)) {
11808 final Rect dirty = drawable.getBounds();
11809 final int scrollX = mScrollX;
11810 final int scrollY = mScrollY;
11811
11812 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11813 dirty.right + scrollX, dirty.bottom + scrollY);
11814 }
11815 }
11816
11817 /**
11818 * Schedules an action on a drawable to occur at a specified time.
11819 *
11820 * @param who the recipient of the action
11821 * @param what the action to run on the drawable
11822 * @param when the time at which the action must occur. Uses the
11823 * {@link SystemClock#uptimeMillis} timebase.
11824 */
11825 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080011826 if (verifyDrawable(who) && what != null) {
11827 if (mAttachInfo != null) {
11828 mAttachInfo.mHandler.postAtTime(what, who, when);
11829 } else {
11830 ViewRootImpl.getRunQueue().postDelayed(what, when - SystemClock.uptimeMillis());
11831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011832 }
11833 }
11834
11835 /**
11836 * Cancels a scheduled action on a drawable.
11837 *
11838 * @param who the recipient of the action
11839 * @param what the action to cancel
11840 */
11841 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080011842 if (verifyDrawable(who) && what != null) {
11843 if (mAttachInfo != null) {
11844 mAttachInfo.mHandler.removeCallbacks(what, who);
11845 } else {
11846 ViewRootImpl.getRunQueue().removeCallbacks(what);
11847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 }
11849 }
11850
11851 /**
11852 * Unschedule any events associated with the given Drawable. This can be
11853 * used when selecting a new Drawable into a view, so that the previous
11854 * one is completely unscheduled.
11855 *
11856 * @param who The Drawable to unschedule.
11857 *
11858 * @see #drawableStateChanged
11859 */
11860 public void unscheduleDrawable(Drawable who) {
11861 if (mAttachInfo != null) {
11862 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11863 }
11864 }
11865
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011866 /**
11867 * Return the layout direction of a given Drawable.
11868 *
11869 * @param who the Drawable to query
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011870 */
11871 public int getResolvedLayoutDirection(Drawable who) {
11872 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011873 }
11874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011875 /**
11876 * If your view subclass is displaying its own Drawable objects, it should
11877 * override this function and return true for any Drawable it is
11878 * displaying. This allows animations for those drawables to be
11879 * scheduled.
11880 *
11881 * <p>Be sure to call through to the super class when overriding this
11882 * function.
11883 *
11884 * @param who The Drawable to verify. Return true if it is one you are
11885 * displaying, else return the result of calling through to the
11886 * super class.
11887 *
11888 * @return boolean If true than the Drawable is being displayed in the
11889 * view; else false and it is not allowed to animate.
11890 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011891 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11892 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011893 */
11894 protected boolean verifyDrawable(Drawable who) {
11895 return who == mBGDrawable;
11896 }
11897
11898 /**
11899 * This function is called whenever the state of the view changes in such
11900 * a way that it impacts the state of drawables being shown.
11901 *
11902 * <p>Be sure to call through to the superclass when overriding this
11903 * function.
11904 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011905 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906 */
11907 protected void drawableStateChanged() {
11908 Drawable d = mBGDrawable;
11909 if (d != null && d.isStateful()) {
11910 d.setState(getDrawableState());
11911 }
11912 }
11913
11914 /**
11915 * Call this to force a view to update its drawable state. This will cause
11916 * drawableStateChanged to be called on this view. Views that are interested
11917 * in the new state should call getDrawableState.
11918 *
11919 * @see #drawableStateChanged
11920 * @see #getDrawableState
11921 */
11922 public void refreshDrawableState() {
11923 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11924 drawableStateChanged();
11925
11926 ViewParent parent = mParent;
11927 if (parent != null) {
11928 parent.childDrawableStateChanged(this);
11929 }
11930 }
11931
11932 /**
11933 * Return an array of resource IDs of the drawable states representing the
11934 * current state of the view.
11935 *
11936 * @return The current drawable state
11937 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011938 * @see Drawable#setState(int[])
11939 * @see #drawableStateChanged()
11940 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011941 */
11942 public final int[] getDrawableState() {
11943 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11944 return mDrawableState;
11945 } else {
11946 mDrawableState = onCreateDrawableState(0);
11947 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11948 return mDrawableState;
11949 }
11950 }
11951
11952 /**
11953 * Generate the new {@link android.graphics.drawable.Drawable} state for
11954 * this view. This is called by the view
11955 * system when the cached Drawable state is determined to be invalid. To
11956 * retrieve the current state, you should use {@link #getDrawableState}.
11957 *
11958 * @param extraSpace if non-zero, this is the number of extra entries you
11959 * would like in the returned array in which you can place your own
11960 * states.
11961 *
11962 * @return Returns an array holding the current {@link Drawable} state of
11963 * the view.
11964 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011965 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011966 */
11967 protected int[] onCreateDrawableState(int extraSpace) {
11968 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11969 mParent instanceof View) {
11970 return ((View) mParent).onCreateDrawableState(extraSpace);
11971 }
11972
11973 int[] drawableState;
11974
11975 int privateFlags = mPrivateFlags;
11976
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011977 int viewStateIndex = 0;
11978 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11979 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11980 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011981 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011982 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11983 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011984 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11985 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011986 // This is set if HW acceleration is requested, even if the current
11987 // process doesn't allow it. This is just to allow app preview
11988 // windows to better match their app.
11989 viewStateIndex |= VIEW_STATE_ACCELERATED;
11990 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011991 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992
Christopher Tate3d4bf172011-03-28 16:16:46 -070011993 final int privateFlags2 = mPrivateFlags2;
11994 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11995 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 drawableState = VIEW_STATE_SETS[viewStateIndex];
11998
11999 //noinspection ConstantIfStatement
12000 if (false) {
12001 Log.i("View", "drawableStateIndex=" + viewStateIndex);
12002 Log.i("View", toString()
12003 + " pressed=" + ((privateFlags & PRESSED) != 0)
12004 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
12005 + " fo=" + hasFocus()
12006 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012007 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008 + ": " + Arrays.toString(drawableState));
12009 }
12010
12011 if (extraSpace == 0) {
12012 return drawableState;
12013 }
12014
12015 final int[] fullState;
12016 if (drawableState != null) {
12017 fullState = new int[drawableState.length + extraSpace];
12018 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
12019 } else {
12020 fullState = new int[extraSpace];
12021 }
12022
12023 return fullState;
12024 }
12025
12026 /**
12027 * Merge your own state values in <var>additionalState</var> into the base
12028 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012029 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012030 *
12031 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070012032 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012033 * own additional state values.
12034 *
12035 * @param additionalState The additional state values you would like
12036 * added to <var>baseState</var>; this array is not modified.
12037 *
12038 * @return As a convenience, the <var>baseState</var> array you originally
12039 * passed into the function is returned.
12040 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012041 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 */
12043 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
12044 final int N = baseState.length;
12045 int i = N - 1;
12046 while (i >= 0 && baseState[i] == 0) {
12047 i--;
12048 }
12049 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
12050 return baseState;
12051 }
12052
12053 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070012054 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
12055 * on all Drawable objects associated with this view.
12056 */
12057 public void jumpDrawablesToCurrentState() {
12058 if (mBGDrawable != null) {
12059 mBGDrawable.jumpToCurrentState();
12060 }
12061 }
12062
12063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012064 * Sets the background color for this view.
12065 * @param color the color of the background
12066 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012067 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070012069 if (mBGDrawable instanceof ColorDrawable) {
12070 ((ColorDrawable) mBGDrawable).setColor(color);
12071 } else {
12072 setBackgroundDrawable(new ColorDrawable(color));
12073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012074 }
12075
12076 /**
12077 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070012078 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079 * @param resid The identifier of the resource.
12080 * @attr ref android.R.styleable#View_background
12081 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000012082 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012083 public void setBackgroundResource(int resid) {
12084 if (resid != 0 && resid == mBackgroundResource) {
12085 return;
12086 }
12087
12088 Drawable d= null;
12089 if (resid != 0) {
12090 d = mResources.getDrawable(resid);
12091 }
12092 setBackgroundDrawable(d);
12093
12094 mBackgroundResource = resid;
12095 }
12096
12097 /**
12098 * Set the background to a given Drawable, or remove the background. If the
12099 * background has padding, this View's padding is set to the background's
12100 * padding. However, when a background is removed, this View's padding isn't
12101 * touched. If setting the padding is desired, please use
12102 * {@link #setPadding(int, int, int, int)}.
12103 *
12104 * @param d The Drawable to use as the background, or null to remove the
12105 * background
12106 */
12107 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070012108 if (d == mBGDrawable) {
12109 return;
12110 }
12111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112 boolean requestLayout = false;
12113
12114 mBackgroundResource = 0;
12115
12116 /*
12117 * Regardless of whether we're setting a new background or not, we want
12118 * to clear the previous drawable.
12119 */
12120 if (mBGDrawable != null) {
12121 mBGDrawable.setCallback(null);
12122 unscheduleDrawable(mBGDrawable);
12123 }
12124
12125 if (d != null) {
12126 Rect padding = sThreadLocal.get();
12127 if (padding == null) {
12128 padding = new Rect();
12129 sThreadLocal.set(padding);
12130 }
12131 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012132 switch (d.getResolvedLayoutDirectionSelf()) {
12133 case LAYOUT_DIRECTION_RTL:
12134 setPadding(padding.right, padding.top, padding.left, padding.bottom);
12135 break;
12136 case LAYOUT_DIRECTION_LTR:
12137 default:
12138 setPadding(padding.left, padding.top, padding.right, padding.bottom);
12139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012140 }
12141
12142 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
12143 // if it has a different minimum size, we should layout again
12144 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
12145 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
12146 requestLayout = true;
12147 }
12148
12149 d.setCallback(this);
12150 if (d.isStateful()) {
12151 d.setState(getDrawableState());
12152 }
12153 d.setVisible(getVisibility() == VISIBLE, false);
12154 mBGDrawable = d;
12155
12156 if ((mPrivateFlags & SKIP_DRAW) != 0) {
12157 mPrivateFlags &= ~SKIP_DRAW;
12158 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
12159 requestLayout = true;
12160 }
12161 } else {
12162 /* Remove the background */
12163 mBGDrawable = null;
12164
12165 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
12166 /*
12167 * This view ONLY drew the background before and we're removing
12168 * the background, so now it won't draw anything
12169 * (hence we SKIP_DRAW)
12170 */
12171 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
12172 mPrivateFlags |= SKIP_DRAW;
12173 }
12174
12175 /*
12176 * When the background is set, we try to apply its padding to this
12177 * View. When the background is removed, we don't touch this View's
12178 * padding. This is noted in the Javadocs. Hence, we don't need to
12179 * requestLayout(), the invalidate() below is sufficient.
12180 */
12181
12182 // The old background's minimum size could have affected this
12183 // View's layout, so let's requestLayout
12184 requestLayout = true;
12185 }
12186
Romain Guy8f1344f52009-05-15 16:03:59 -070012187 computeOpaqueFlags();
12188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 if (requestLayout) {
12190 requestLayout();
12191 }
12192
12193 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012194 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012195 }
12196
12197 /**
12198 * Gets the background drawable
12199 * @return The drawable used as the background for this view, if any.
12200 */
12201 public Drawable getBackground() {
12202 return mBGDrawable;
12203 }
12204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012205 /**
12206 * Sets the padding. The view may add on the space required to display
12207 * the scrollbars, depending on the style and visibility of the scrollbars.
12208 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
12209 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
12210 * from the values set in this call.
12211 *
12212 * @attr ref android.R.styleable#View_padding
12213 * @attr ref android.R.styleable#View_paddingBottom
12214 * @attr ref android.R.styleable#View_paddingLeft
12215 * @attr ref android.R.styleable#View_paddingRight
12216 * @attr ref android.R.styleable#View_paddingTop
12217 * @param left the left padding in pixels
12218 * @param top the top padding in pixels
12219 * @param right the right padding in pixels
12220 * @param bottom the bottom padding in pixels
12221 */
12222 public void setPadding(int left, int top, int right, int bottom) {
12223 boolean changed = false;
12224
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012225 mUserPaddingRelative = false;
12226
Adam Powell20232d02010-12-08 21:08:53 -080012227 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 mUserPaddingRight = right;
12229 mUserPaddingBottom = bottom;
12230
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012231 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070012232
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012233 // Common case is there are no scroll bars.
12234 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012235 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080012236 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012237 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080012238 switch (mVerticalScrollbarPosition) {
12239 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012240 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
12241 left += offset;
12242 } else {
12243 right += offset;
12244 }
12245 break;
Adam Powell20232d02010-12-08 21:08:53 -080012246 case SCROLLBAR_POSITION_RIGHT:
12247 right += offset;
12248 break;
12249 case SCROLLBAR_POSITION_LEFT:
12250 left += offset;
12251 break;
12252 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012253 }
Adam Powell20232d02010-12-08 21:08:53 -080012254 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012255 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
12256 ? 0 : getHorizontalScrollbarHeight();
12257 }
12258 }
Romain Guy8506ab42009-06-11 17:35:47 -070012259
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012260 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012261 changed = true;
12262 mPaddingLeft = left;
12263 }
12264 if (mPaddingTop != top) {
12265 changed = true;
12266 mPaddingTop = top;
12267 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012268 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012269 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012270 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012272 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070012274 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012275 }
12276
12277 if (changed) {
12278 requestLayout();
12279 }
12280 }
12281
12282 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012283 * Sets the relative padding. The view may add on the space required to display
12284 * the scrollbars, depending on the style and visibility of the scrollbars.
12285 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
12286 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
12287 * from the values set in this call.
12288 *
12289 * @attr ref android.R.styleable#View_padding
12290 * @attr ref android.R.styleable#View_paddingBottom
12291 * @attr ref android.R.styleable#View_paddingStart
12292 * @attr ref android.R.styleable#View_paddingEnd
12293 * @attr ref android.R.styleable#View_paddingTop
12294 * @param start the start padding in pixels
12295 * @param top the top padding in pixels
12296 * @param end the end padding in pixels
12297 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012298 */
12299 public void setPaddingRelative(int start, int top, int end, int bottom) {
12300 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070012301
12302 mUserPaddingStart = start;
12303 mUserPaddingEnd = end;
12304
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012305 switch(getResolvedLayoutDirection()) {
12306 case LAYOUT_DIRECTION_RTL:
12307 setPadding(end, top, start, bottom);
12308 break;
12309 case LAYOUT_DIRECTION_LTR:
12310 default:
12311 setPadding(start, top, end, bottom);
12312 }
12313 }
12314
12315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012316 * Returns the top padding of this view.
12317 *
12318 * @return the top padding in pixels
12319 */
12320 public int getPaddingTop() {
12321 return mPaddingTop;
12322 }
12323
12324 /**
12325 * Returns the bottom padding of this view. If there are inset and enabled
12326 * scrollbars, this value may include the space required to display the
12327 * scrollbars as well.
12328 *
12329 * @return the bottom padding in pixels
12330 */
12331 public int getPaddingBottom() {
12332 return mPaddingBottom;
12333 }
12334
12335 /**
12336 * Returns the left padding of this view. If there are inset and enabled
12337 * scrollbars, this value may include the space required to display the
12338 * scrollbars as well.
12339 *
12340 * @return the left padding in pixels
12341 */
12342 public int getPaddingLeft() {
12343 return mPaddingLeft;
12344 }
12345
12346 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012347 * Returns the start padding of this view. If there are inset and enabled
12348 * scrollbars, this value may include the space required to display the
12349 * scrollbars as well.
12350 *
12351 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012352 */
12353 public int getPaddingStart() {
12354 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12355 mPaddingRight : mPaddingLeft;
12356 }
12357
12358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012359 * Returns the right padding of this view. If there are inset and enabled
12360 * scrollbars, this value may include the space required to display the
12361 * scrollbars as well.
12362 *
12363 * @return the right padding in pixels
12364 */
12365 public int getPaddingRight() {
12366 return mPaddingRight;
12367 }
12368
12369 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012370 * Returns the end padding of this view. If there are inset and enabled
12371 * scrollbars, this value may include the space required to display the
12372 * scrollbars as well.
12373 *
12374 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012375 */
12376 public int getPaddingEnd() {
12377 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
12378 mPaddingLeft : mPaddingRight;
12379 }
12380
12381 /**
12382 * Return if the padding as been set thru relative values
12383 * {@link #setPaddingRelative(int, int, int, int)} or thru
12384 * @attr ref android.R.styleable#View_paddingStart or
12385 * @attr ref android.R.styleable#View_paddingEnd
12386 *
12387 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070012388 */
12389 public boolean isPaddingRelative() {
12390 return mUserPaddingRelative;
12391 }
12392
12393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012394 * Changes the selection state of this view. A view can be selected or not.
12395 * Note that selection is not the same as focus. Views are typically
12396 * selected in the context of an AdapterView like ListView or GridView;
12397 * the selected view is the view that is highlighted.
12398 *
12399 * @param selected true if the view must be selected, false otherwise
12400 */
12401 public void setSelected(boolean selected) {
12402 if (((mPrivateFlags & SELECTED) != 0) != selected) {
12403 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070012404 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080012405 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012406 refreshDrawableState();
12407 dispatchSetSelected(selected);
12408 }
12409 }
12410
12411 /**
12412 * Dispatch setSelected to all of this View's children.
12413 *
12414 * @see #setSelected(boolean)
12415 *
12416 * @param selected The new selected state
12417 */
12418 protected void dispatchSetSelected(boolean selected) {
12419 }
12420
12421 /**
12422 * Indicates the selection state of this view.
12423 *
12424 * @return true if the view is selected, false otherwise
12425 */
12426 @ViewDebug.ExportedProperty
12427 public boolean isSelected() {
12428 return (mPrivateFlags & SELECTED) != 0;
12429 }
12430
12431 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012432 * Changes the activated state of this view. A view can be activated or not.
12433 * Note that activation is not the same as selection. Selection is
12434 * a transient property, representing the view (hierarchy) the user is
12435 * currently interacting with. Activation is a longer-term state that the
12436 * user can move views in and out of. For example, in a list view with
12437 * single or multiple selection enabled, the views in the current selection
12438 * set are activated. (Um, yeah, we are deeply sorry about the terminology
12439 * here.) The activated state is propagated down to children of the view it
12440 * is set on.
12441 *
12442 * @param activated true if the view must be activated, false otherwise
12443 */
12444 public void setActivated(boolean activated) {
12445 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
12446 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012447 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012448 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070012449 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070012450 }
12451 }
12452
12453 /**
12454 * Dispatch setActivated to all of this View's children.
12455 *
12456 * @see #setActivated(boolean)
12457 *
12458 * @param activated The new activated state
12459 */
12460 protected void dispatchSetActivated(boolean activated) {
12461 }
12462
12463 /**
12464 * Indicates the activation state of this view.
12465 *
12466 * @return true if the view is activated, false otherwise
12467 */
12468 @ViewDebug.ExportedProperty
12469 public boolean isActivated() {
12470 return (mPrivateFlags & ACTIVATED) != 0;
12471 }
12472
12473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012474 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
12475 * observer can be used to get notifications when global events, like
12476 * layout, happen.
12477 *
12478 * The returned ViewTreeObserver observer is not guaranteed to remain
12479 * valid for the lifetime of this View. If the caller of this method keeps
12480 * a long-lived reference to ViewTreeObserver, it should always check for
12481 * the return value of {@link ViewTreeObserver#isAlive()}.
12482 *
12483 * @return The ViewTreeObserver for this view's hierarchy.
12484 */
12485 public ViewTreeObserver getViewTreeObserver() {
12486 if (mAttachInfo != null) {
12487 return mAttachInfo.mTreeObserver;
12488 }
12489 if (mFloatingTreeObserver == null) {
12490 mFloatingTreeObserver = new ViewTreeObserver();
12491 }
12492 return mFloatingTreeObserver;
12493 }
12494
12495 /**
12496 * <p>Finds the topmost view in the current view hierarchy.</p>
12497 *
12498 * @return the topmost view containing this view
12499 */
12500 public View getRootView() {
12501 if (mAttachInfo != null) {
12502 final View v = mAttachInfo.mRootView;
12503 if (v != null) {
12504 return v;
12505 }
12506 }
Romain Guy8506ab42009-06-11 17:35:47 -070012507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012508 View parent = this;
12509
12510 while (parent.mParent != null && parent.mParent instanceof View) {
12511 parent = (View) parent.mParent;
12512 }
12513
12514 return parent;
12515 }
12516
12517 /**
12518 * <p>Computes the coordinates of this view on the screen. The argument
12519 * must be an array of two integers. After the method returns, the array
12520 * contains the x and y location in that order.</p>
12521 *
12522 * @param location an array of two integers in which to hold the coordinates
12523 */
12524 public void getLocationOnScreen(int[] location) {
12525 getLocationInWindow(location);
12526
12527 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070012528 if (info != null) {
12529 location[0] += info.mWindowLeft;
12530 location[1] += info.mWindowTop;
12531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 }
12533
12534 /**
12535 * <p>Computes the coordinates of this view in its window. The argument
12536 * must be an array of two integers. After the method returns, the array
12537 * contains the x and y location in that order.</p>
12538 *
12539 * @param location an array of two integers in which to hold the coordinates
12540 */
12541 public void getLocationInWindow(int[] location) {
12542 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012543 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 }
12545
Gilles Debunne6583ce52011-12-06 18:09:02 -080012546 if (mAttachInfo == null) {
12547 // When the view is not attached to a window, this method does not make sense
12548 location[0] = location[1] = 0;
12549 return;
12550 }
12551
Gilles Debunnecea45132011-11-24 02:19:27 +010012552 float[] position = mAttachInfo.mTmpTransformLocation;
12553 position[0] = position[1] = 0.0f;
12554
12555 if (!hasIdentityMatrix()) {
12556 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012558
Gilles Debunnecea45132011-11-24 02:19:27 +010012559 position[0] += mLeft;
12560 position[1] += mTop;
12561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012562 ViewParent viewParent = mParent;
12563 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010012564 final View view = (View) viewParent;
12565
12566 position[0] -= view.mScrollX;
12567 position[1] -= view.mScrollY;
12568
12569 if (!view.hasIdentityMatrix()) {
12570 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070012571 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012572
12573 position[0] += view.mLeft;
12574 position[1] += view.mTop;
12575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012576 viewParent = view.mParent;
12577 }
Romain Guy8506ab42009-06-11 17:35:47 -070012578
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012579 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010012581 final ViewRootImpl vr = (ViewRootImpl) viewParent;
12582 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583 }
Gilles Debunnecea45132011-11-24 02:19:27 +010012584
12585 location[0] = (int) (position[0] + 0.5f);
12586 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587 }
12588
12589 /**
12590 * {@hide}
12591 * @param id the id of the view to be found
12592 * @return the view of the specified id, null if cannot be found
12593 */
12594 protected View findViewTraversal(int id) {
12595 if (id == mID) {
12596 return this;
12597 }
12598 return null;
12599 }
12600
12601 /**
12602 * {@hide}
12603 * @param tag the tag of the view to be found
12604 * @return the view of specified tag, null if cannot be found
12605 */
12606 protected View findViewWithTagTraversal(Object tag) {
12607 if (tag != null && tag.equals(mTag)) {
12608 return this;
12609 }
12610 return null;
12611 }
12612
12613 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012614 * {@hide}
12615 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070012616 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080012617 * @return The first view that matches the predicate or null.
12618 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070012619 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080012620 if (predicate.apply(this)) {
12621 return this;
12622 }
12623 return null;
12624 }
12625
12626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012627 * Look for a child view with the given id. If this view has the given
12628 * id, return this view.
12629 *
12630 * @param id The id to search for.
12631 * @return The view that has the given id in the hierarchy or null
12632 */
12633 public final View findViewById(int id) {
12634 if (id < 0) {
12635 return null;
12636 }
12637 return findViewTraversal(id);
12638 }
12639
12640 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070012641 * Finds a view by its unuque and stable accessibility id.
12642 *
12643 * @param accessibilityId The searched accessibility id.
12644 * @return The found view.
12645 */
12646 final View findViewByAccessibilityId(int accessibilityId) {
12647 if (accessibilityId < 0) {
12648 return null;
12649 }
12650 return findViewByAccessibilityIdTraversal(accessibilityId);
12651 }
12652
12653 /**
12654 * Performs the traversal to find a view by its unuque and stable accessibility id.
12655 *
12656 * <strong>Note:</strong>This method does not stop at the root namespace
12657 * boundary since the user can touch the screen at an arbitrary location
12658 * potentially crossing the root namespace bounday which will send an
12659 * accessibility event to accessibility services and they should be able
12660 * to obtain the event source. Also accessibility ids are guaranteed to be
12661 * unique in the window.
12662 *
12663 * @param accessibilityId The accessibility id.
12664 * @return The found view.
12665 */
12666 View findViewByAccessibilityIdTraversal(int accessibilityId) {
12667 if (getAccessibilityViewId() == accessibilityId) {
12668 return this;
12669 }
12670 return null;
12671 }
12672
12673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674 * Look for a child view with the given tag. If this view has the given
12675 * tag, return this view.
12676 *
12677 * @param tag The tag to search for, using "tag.equals(getTag())".
12678 * @return The View that has the given tag in the hierarchy or null
12679 */
12680 public final View findViewWithTag(Object tag) {
12681 if (tag == null) {
12682 return null;
12683 }
12684 return findViewWithTagTraversal(tag);
12685 }
12686
12687 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080012688 * {@hide}
12689 * Look for a child view that matches the specified predicate.
12690 * If this view matches the predicate, return this view.
12691 *
12692 * @param predicate The predicate to evaluate.
12693 * @return The first view that matches the predicate or null.
12694 */
12695 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070012696 return findViewByPredicateTraversal(predicate, null);
12697 }
12698
12699 /**
12700 * {@hide}
12701 * Look for a child view that matches the specified predicate,
12702 * starting with the specified view and its descendents and then
12703 * recusively searching the ancestors and siblings of that view
12704 * until this view is reached.
12705 *
12706 * This method is useful in cases where the predicate does not match
12707 * a single unique view (perhaps multiple views use the same id)
12708 * and we are trying to find the view that is "closest" in scope to the
12709 * starting view.
12710 *
12711 * @param start The view to start from.
12712 * @param predicate The predicate to evaluate.
12713 * @return The first view that matches the predicate or null.
12714 */
12715 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
12716 View childToSkip = null;
12717 for (;;) {
12718 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
12719 if (view != null || start == this) {
12720 return view;
12721 }
12722
12723 ViewParent parent = start.getParent();
12724 if (parent == null || !(parent instanceof View)) {
12725 return null;
12726 }
12727
12728 childToSkip = start;
12729 start = (View) parent;
12730 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080012731 }
12732
12733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012734 * Sets the identifier for this view. The identifier does not have to be
12735 * unique in this view's hierarchy. The identifier should be a positive
12736 * number.
12737 *
12738 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070012739 * @see #getId()
12740 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 *
12742 * @param id a number used to identify the view
12743 *
12744 * @attr ref android.R.styleable#View_id
12745 */
12746 public void setId(int id) {
12747 mID = id;
12748 }
12749
12750 /**
12751 * {@hide}
12752 *
12753 * @param isRoot true if the view belongs to the root namespace, false
12754 * otherwise
12755 */
12756 public void setIsRootNamespace(boolean isRoot) {
12757 if (isRoot) {
12758 mPrivateFlags |= IS_ROOT_NAMESPACE;
12759 } else {
12760 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
12761 }
12762 }
12763
12764 /**
12765 * {@hide}
12766 *
12767 * @return true if the view belongs to the root namespace, false otherwise
12768 */
12769 public boolean isRootNamespace() {
12770 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
12771 }
12772
12773 /**
12774 * Returns this view's identifier.
12775 *
12776 * @return a positive integer used to identify the view or {@link #NO_ID}
12777 * if the view has no ID
12778 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070012779 * @see #setId(int)
12780 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012781 * @attr ref android.R.styleable#View_id
12782 */
12783 @ViewDebug.CapturedViewProperty
12784 public int getId() {
12785 return mID;
12786 }
12787
12788 /**
12789 * Returns this view's tag.
12790 *
12791 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070012792 *
12793 * @see #setTag(Object)
12794 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012795 */
12796 @ViewDebug.ExportedProperty
12797 public Object getTag() {
12798 return mTag;
12799 }
12800
12801 /**
12802 * Sets the tag associated with this view. A tag can be used to mark
12803 * a view in its hierarchy and does not have to be unique within the
12804 * hierarchy. Tags can also be used to store data within a view without
12805 * resorting to another data structure.
12806 *
12807 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070012808 *
12809 * @see #getTag()
12810 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012811 */
12812 public void setTag(final Object tag) {
12813 mTag = tag;
12814 }
12815
12816 /**
Romain Guyd90a3312009-05-06 14:54:28 -070012817 * Returns the tag associated with this view and the specified key.
12818 *
12819 * @param key The key identifying the tag
12820 *
12821 * @return the Object stored in this view as a tag
12822 *
12823 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070012824 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070012825 */
12826 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012827 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070012828 return null;
12829 }
12830
12831 /**
12832 * Sets a tag associated with this view and a key. A tag can be used
12833 * to mark a view in its hierarchy and does not have to be unique within
12834 * the hierarchy. Tags can also be used to store data within a view
12835 * without resorting to another data structure.
12836 *
12837 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070012838 * application to ensure it is unique (see the <a
12839 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
12840 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070012841 * the Android framework or not associated with any package will cause
12842 * an {@link IllegalArgumentException} to be thrown.
12843 *
12844 * @param key The key identifying the tag
12845 * @param tag An Object to tag the view with
12846 *
12847 * @throws IllegalArgumentException If they specified key is not valid
12848 *
12849 * @see #setTag(Object)
12850 * @see #getTag(int)
12851 */
12852 public void setTag(int key, final Object tag) {
12853 // If the package id is 0x00 or 0x01, it's either an undefined package
12854 // or a framework id
12855 if ((key >>> 24) < 2) {
12856 throw new IllegalArgumentException("The key must be an application-specific "
12857 + "resource id.");
12858 }
12859
Adam Powell2b2f6d62011-09-23 11:15:39 -070012860 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012861 }
12862
12863 /**
12864 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12865 * framework id.
12866 *
12867 * @hide
12868 */
12869 public void setTagInternal(int key, Object tag) {
12870 if ((key >>> 24) != 0x1) {
12871 throw new IllegalArgumentException("The key must be a framework-specific "
12872 + "resource id.");
12873 }
12874
Adam Powell2b2f6d62011-09-23 11:15:39 -070012875 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012876 }
12877
Adam Powell2b2f6d62011-09-23 11:15:39 -070012878 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070012879 if (mKeyedTags == null) {
12880 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070012881 }
12882
Adam Powell7db82ac2011-09-22 19:44:04 -070012883 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012884 }
12885
12886 /**
Romain Guy13922e02009-05-12 17:56:14 -070012887 * @param consistency The type of consistency. See ViewDebug for more information.
12888 *
12889 * @hide
12890 */
12891 protected boolean dispatchConsistencyCheck(int consistency) {
12892 return onConsistencyCheck(consistency);
12893 }
12894
12895 /**
12896 * Method that subclasses should implement to check their consistency. The type of
12897 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012898 *
Romain Guy13922e02009-05-12 17:56:14 -070012899 * @param consistency The type of consistency. See ViewDebug for more information.
12900 *
12901 * @throws IllegalStateException if the view is in an inconsistent state.
12902 *
12903 * @hide
12904 */
12905 protected boolean onConsistencyCheck(int consistency) {
12906 boolean result = true;
12907
12908 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12909 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12910
12911 if (checkLayout) {
12912 if (getParent() == null) {
12913 result = false;
12914 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12915 "View " + this + " does not have a parent.");
12916 }
12917
12918 if (mAttachInfo == null) {
12919 result = false;
12920 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12921 "View " + this + " is not attached to a window.");
12922 }
12923 }
12924
12925 if (checkDrawing) {
12926 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12927 // from their draw() method
12928
12929 if ((mPrivateFlags & DRAWN) != DRAWN &&
12930 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12931 result = false;
12932 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12933 "View " + this + " was invalidated but its drawing cache is valid.");
12934 }
12935 }
12936
12937 return result;
12938 }
12939
12940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012941 * Prints information about this view in the log output, with the tag
12942 * {@link #VIEW_LOG_TAG}.
12943 *
12944 * @hide
12945 */
12946 public void debug() {
12947 debug(0);
12948 }
12949
12950 /**
12951 * Prints information about this view in the log output, with the tag
12952 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12953 * indentation defined by the <code>depth</code>.
12954 *
12955 * @param depth the indentation level
12956 *
12957 * @hide
12958 */
12959 protected void debug(int depth) {
12960 String output = debugIndent(depth - 1);
12961
12962 output += "+ " + this;
12963 int id = getId();
12964 if (id != -1) {
12965 output += " (id=" + id + ")";
12966 }
12967 Object tag = getTag();
12968 if (tag != null) {
12969 output += " (tag=" + tag + ")";
12970 }
12971 Log.d(VIEW_LOG_TAG, output);
12972
12973 if ((mPrivateFlags & FOCUSED) != 0) {
12974 output = debugIndent(depth) + " FOCUSED";
12975 Log.d(VIEW_LOG_TAG, output);
12976 }
12977
12978 output = debugIndent(depth);
12979 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12980 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12981 + "} ";
12982 Log.d(VIEW_LOG_TAG, output);
12983
12984 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12985 || mPaddingBottom != 0) {
12986 output = debugIndent(depth);
12987 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12988 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12989 Log.d(VIEW_LOG_TAG, output);
12990 }
12991
12992 output = debugIndent(depth);
12993 output += "mMeasureWidth=" + mMeasuredWidth +
12994 " mMeasureHeight=" + mMeasuredHeight;
12995 Log.d(VIEW_LOG_TAG, output);
12996
12997 output = debugIndent(depth);
12998 if (mLayoutParams == null) {
12999 output += "BAD! no layout params";
13000 } else {
13001 output = mLayoutParams.debug(output);
13002 }
13003 Log.d(VIEW_LOG_TAG, output);
13004
13005 output = debugIndent(depth);
13006 output += "flags={";
13007 output += View.printFlags(mViewFlags);
13008 output += "}";
13009 Log.d(VIEW_LOG_TAG, output);
13010
13011 output = debugIndent(depth);
13012 output += "privateFlags={";
13013 output += View.printPrivateFlags(mPrivateFlags);
13014 output += "}";
13015 Log.d(VIEW_LOG_TAG, output);
13016 }
13017
13018 /**
13019 * Creates an string of whitespaces used for indentation.
13020 *
13021 * @param depth the indentation level
13022 * @return a String containing (depth * 2 + 3) * 2 white spaces
13023 *
13024 * @hide
13025 */
13026 protected static String debugIndent(int depth) {
13027 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
13028 for (int i = 0; i < (depth * 2) + 3; i++) {
13029 spaces.append(' ').append(' ');
13030 }
13031 return spaces.toString();
13032 }
13033
13034 /**
13035 * <p>Return the offset of the widget's text baseline from the widget's top
13036 * boundary. If this widget does not support baseline alignment, this
13037 * method returns -1. </p>
13038 *
13039 * @return the offset of the baseline within the widget's bounds or -1
13040 * if baseline alignment is not supported
13041 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070013042 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013043 public int getBaseline() {
13044 return -1;
13045 }
13046
13047 /**
13048 * Call this when something has changed which has invalidated the
13049 * layout of this view. This will schedule a layout pass of the view
13050 * tree.
13051 */
13052 public void requestLayout() {
13053 if (ViewDebug.TRACE_HIERARCHY) {
13054 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
13055 }
13056
13057 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013058 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013059
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013060 if (mParent != null) {
13061 if (mLayoutParams != null) {
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080013062 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070013063 }
13064 if (!mParent.isLayoutRequested()) {
13065 mParent.requestLayout();
13066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013067 }
13068 }
13069
13070 /**
13071 * Forces this view to be laid out during the next layout pass.
13072 * This method does not call requestLayout() or forceLayout()
13073 * on the parent.
13074 */
13075 public void forceLayout() {
13076 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080013077 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013078 }
13079
13080 /**
13081 * <p>
13082 * This is called to find out how big a view should be. The parent
13083 * supplies constraint information in the width and height parameters.
13084 * </p>
13085 *
13086 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080013087 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013088 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080013089 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 * </p>
13091 *
13092 *
13093 * @param widthMeasureSpec Horizontal space requirements as imposed by the
13094 * parent
13095 * @param heightMeasureSpec Vertical space requirements as imposed by the
13096 * parent
13097 *
13098 * @see #onMeasure(int, int)
13099 */
13100 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
13101 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
13102 widthMeasureSpec != mOldWidthMeasureSpec ||
13103 heightMeasureSpec != mOldHeightMeasureSpec) {
13104
13105 // first clears the measured dimension flag
13106 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
13107
13108 if (ViewDebug.TRACE_HIERARCHY) {
13109 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
13110 }
13111
13112 // measure ourselves, this should set the measured dimension flag back
13113 onMeasure(widthMeasureSpec, heightMeasureSpec);
13114
13115 // flag not set, setMeasuredDimension() was not invoked, we raise
13116 // an exception to warn the developer
13117 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
13118 throw new IllegalStateException("onMeasure() did not set the"
13119 + " measured dimension by calling"
13120 + " setMeasuredDimension()");
13121 }
13122
13123 mPrivateFlags |= LAYOUT_REQUIRED;
13124 }
13125
13126 mOldWidthMeasureSpec = widthMeasureSpec;
13127 mOldHeightMeasureSpec = heightMeasureSpec;
13128 }
13129
13130 /**
13131 * <p>
13132 * Measure the view and its content to determine the measured width and the
13133 * measured height. This method is invoked by {@link #measure(int, int)} and
13134 * should be overriden by subclasses to provide accurate and efficient
13135 * measurement of their contents.
13136 * </p>
13137 *
13138 * <p>
13139 * <strong>CONTRACT:</strong> When overriding this method, you
13140 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
13141 * measured width and height of this view. Failure to do so will trigger an
13142 * <code>IllegalStateException</code>, thrown by
13143 * {@link #measure(int, int)}. Calling the superclass'
13144 * {@link #onMeasure(int, int)} is a valid use.
13145 * </p>
13146 *
13147 * <p>
13148 * The base class implementation of measure defaults to the background size,
13149 * unless a larger size is allowed by the MeasureSpec. Subclasses should
13150 * override {@link #onMeasure(int, int)} to provide better measurements of
13151 * their content.
13152 * </p>
13153 *
13154 * <p>
13155 * If this method is overridden, it is the subclass's responsibility to make
13156 * sure the measured height and width are at least the view's minimum height
13157 * and width ({@link #getSuggestedMinimumHeight()} and
13158 * {@link #getSuggestedMinimumWidth()}).
13159 * </p>
13160 *
13161 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
13162 * The requirements are encoded with
13163 * {@link android.view.View.MeasureSpec}.
13164 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
13165 * The requirements are encoded with
13166 * {@link android.view.View.MeasureSpec}.
13167 *
13168 * @see #getMeasuredWidth()
13169 * @see #getMeasuredHeight()
13170 * @see #setMeasuredDimension(int, int)
13171 * @see #getSuggestedMinimumHeight()
13172 * @see #getSuggestedMinimumWidth()
13173 * @see android.view.View.MeasureSpec#getMode(int)
13174 * @see android.view.View.MeasureSpec#getSize(int)
13175 */
13176 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
13177 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
13178 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
13179 }
13180
13181 /**
13182 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
13183 * measured width and measured height. Failing to do so will trigger an
13184 * exception at measurement time.</p>
13185 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080013186 * @param measuredWidth The measured width of this view. May be a complex
13187 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13188 * {@link #MEASURED_STATE_TOO_SMALL}.
13189 * @param measuredHeight The measured height of this view. May be a complex
13190 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
13191 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192 */
13193 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
13194 mMeasuredWidth = measuredWidth;
13195 mMeasuredHeight = measuredHeight;
13196
13197 mPrivateFlags |= MEASURED_DIMENSION_SET;
13198 }
13199
13200 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080013201 * Merge two states as returned by {@link #getMeasuredState()}.
13202 * @param curState The current state as returned from a view or the result
13203 * of combining multiple views.
13204 * @param newState The new view state to combine.
13205 * @return Returns a new integer reflecting the combination of the two
13206 * states.
13207 */
13208 public static int combineMeasuredStates(int curState, int newState) {
13209 return curState | newState;
13210 }
13211
13212 /**
13213 * Version of {@link #resolveSizeAndState(int, int, int)}
13214 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
13215 */
13216 public static int resolveSize(int size, int measureSpec) {
13217 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
13218 }
13219
13220 /**
13221 * Utility to reconcile a desired size and state, with constraints imposed
13222 * by a MeasureSpec. Will take the desired size, unless a different size
13223 * is imposed by the constraints. The returned value is a compound integer,
13224 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
13225 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
13226 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013227 *
13228 * @param size How big the view wants to be
13229 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080013230 * @return Size information bit mask as defined by
13231 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013232 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080013233 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013234 int result = size;
13235 int specMode = MeasureSpec.getMode(measureSpec);
13236 int specSize = MeasureSpec.getSize(measureSpec);
13237 switch (specMode) {
13238 case MeasureSpec.UNSPECIFIED:
13239 result = size;
13240 break;
13241 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080013242 if (specSize < size) {
13243 result = specSize | MEASURED_STATE_TOO_SMALL;
13244 } else {
13245 result = size;
13246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013247 break;
13248 case MeasureSpec.EXACTLY:
13249 result = specSize;
13250 break;
13251 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080013252 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013253 }
13254
13255 /**
13256 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070013257 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013258 * by the MeasureSpec.
13259 *
13260 * @param size Default size for this view
13261 * @param measureSpec Constraints imposed by the parent
13262 * @return The size this view should be.
13263 */
13264 public static int getDefaultSize(int size, int measureSpec) {
13265 int result = size;
13266 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070013267 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013268
13269 switch (specMode) {
13270 case MeasureSpec.UNSPECIFIED:
13271 result = size;
13272 break;
13273 case MeasureSpec.AT_MOST:
13274 case MeasureSpec.EXACTLY:
13275 result = specSize;
13276 break;
13277 }
13278 return result;
13279 }
13280
13281 /**
13282 * Returns the suggested minimum height that the view should use. This
13283 * returns the maximum of the view's minimum height
13284 * and the background's minimum height
13285 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
13286 * <p>
13287 * When being used in {@link #onMeasure(int, int)}, the caller should still
13288 * ensure the returned height is within the requirements of the parent.
13289 *
13290 * @return The suggested minimum height of the view.
13291 */
13292 protected int getSuggestedMinimumHeight() {
13293 int suggestedMinHeight = mMinHeight;
13294
13295 if (mBGDrawable != null) {
13296 final int bgMinHeight = mBGDrawable.getMinimumHeight();
13297 if (suggestedMinHeight < bgMinHeight) {
13298 suggestedMinHeight = bgMinHeight;
13299 }
13300 }
13301
13302 return suggestedMinHeight;
13303 }
13304
13305 /**
13306 * Returns the suggested minimum width that the view should use. This
13307 * returns the maximum of the view's minimum width)
13308 * and the background's minimum width
13309 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
13310 * <p>
13311 * When being used in {@link #onMeasure(int, int)}, the caller should still
13312 * ensure the returned width is within the requirements of the parent.
13313 *
13314 * @return The suggested minimum width of the view.
13315 */
13316 protected int getSuggestedMinimumWidth() {
13317 int suggestedMinWidth = mMinWidth;
13318
13319 if (mBGDrawable != null) {
13320 final int bgMinWidth = mBGDrawable.getMinimumWidth();
13321 if (suggestedMinWidth < bgMinWidth) {
13322 suggestedMinWidth = bgMinWidth;
13323 }
13324 }
13325
13326 return suggestedMinWidth;
13327 }
13328
13329 /**
13330 * Sets the minimum height of the view. It is not guaranteed the view will
13331 * be able to achieve this minimum height (for example, if its parent layout
13332 * constrains it with less available height).
13333 *
13334 * @param minHeight The minimum height the view will try to be.
13335 */
13336 public void setMinimumHeight(int minHeight) {
13337 mMinHeight = minHeight;
13338 }
13339
13340 /**
13341 * Sets the minimum width of the view. It is not guaranteed the view will
13342 * be able to achieve this minimum width (for example, if its parent layout
13343 * constrains it with less available width).
13344 *
13345 * @param minWidth The minimum width the view will try to be.
13346 */
13347 public void setMinimumWidth(int minWidth) {
13348 mMinWidth = minWidth;
13349 }
13350
13351 /**
13352 * Get the animation currently associated with this view.
13353 *
13354 * @return The animation that is currently playing or
13355 * scheduled to play for this view.
13356 */
13357 public Animation getAnimation() {
13358 return mCurrentAnimation;
13359 }
13360
13361 /**
13362 * Start the specified animation now.
13363 *
13364 * @param animation the animation to start now
13365 */
13366 public void startAnimation(Animation animation) {
13367 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
13368 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080013369 invalidateParentCaches();
13370 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013371 }
13372
13373 /**
13374 * Cancels any animations for this view.
13375 */
13376 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080013377 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080013378 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080013379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013380 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013381 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 }
13383
13384 /**
13385 * Sets the next animation to play for this view.
13386 * If you want the animation to play immediately, use
13387 * startAnimation. This method provides allows fine-grained
13388 * control over the start time and invalidation, but you
13389 * must make sure that 1) the animation has a start time set, and
13390 * 2) the view will be invalidated when the animation is supposed to
13391 * start.
13392 *
13393 * @param animation The next animation, or null.
13394 */
13395 public void setAnimation(Animation animation) {
13396 mCurrentAnimation = animation;
13397 if (animation != null) {
13398 animation.reset();
13399 }
13400 }
13401
13402 /**
13403 * Invoked by a parent ViewGroup to notify the start of the animation
13404 * currently associated with this view. If you override this method,
13405 * always call super.onAnimationStart();
13406 *
13407 * @see #setAnimation(android.view.animation.Animation)
13408 * @see #getAnimation()
13409 */
13410 protected void onAnimationStart() {
13411 mPrivateFlags |= ANIMATION_STARTED;
13412 }
13413
13414 /**
13415 * Invoked by a parent ViewGroup to notify the end of the animation
13416 * currently associated with this view. If you override this method,
13417 * always call super.onAnimationEnd();
13418 *
13419 * @see #setAnimation(android.view.animation.Animation)
13420 * @see #getAnimation()
13421 */
13422 protected void onAnimationEnd() {
13423 mPrivateFlags &= ~ANIMATION_STARTED;
13424 }
13425
13426 /**
13427 * Invoked if there is a Transform that involves alpha. Subclass that can
13428 * draw themselves with the specified alpha should return true, and then
13429 * respect that alpha when their onDraw() is called. If this returns false
13430 * then the view may be redirected to draw into an offscreen buffer to
13431 * fulfill the request, which will look fine, but may be slower than if the
13432 * subclass handles it internally. The default implementation returns false.
13433 *
13434 * @param alpha The alpha (0..255) to apply to the view's drawing
13435 * @return true if the view can draw with the specified alpha.
13436 */
13437 protected boolean onSetAlpha(int alpha) {
13438 return false;
13439 }
13440
13441 /**
13442 * This is used by the RootView to perform an optimization when
13443 * the view hierarchy contains one or several SurfaceView.
13444 * SurfaceView is always considered transparent, but its children are not,
13445 * therefore all View objects remove themselves from the global transparent
13446 * region (passed as a parameter to this function).
13447 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013448 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013449 *
13450 * @return Returns true if the effective visibility of the view at this
13451 * point is opaque, regardless of the transparent region; returns false
13452 * if it is possible for underlying windows to be seen behind the view.
13453 *
13454 * {@hide}
13455 */
13456 public boolean gatherTransparentRegion(Region region) {
13457 final AttachInfo attachInfo = mAttachInfo;
13458 if (region != null && attachInfo != null) {
13459 final int pflags = mPrivateFlags;
13460 if ((pflags & SKIP_DRAW) == 0) {
13461 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
13462 // remove it from the transparent region.
13463 final int[] location = attachInfo.mTransparentLocation;
13464 getLocationInWindow(location);
13465 region.op(location[0], location[1], location[0] + mRight - mLeft,
13466 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
13467 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
13468 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
13469 // exists, so we remove the background drawable's non-transparent
13470 // parts from this transparent region.
13471 applyDrawableToTransparentRegion(mBGDrawable, region);
13472 }
13473 }
13474 return true;
13475 }
13476
13477 /**
13478 * Play a sound effect for this view.
13479 *
13480 * <p>The framework will play sound effects for some built in actions, such as
13481 * clicking, but you may wish to play these effects in your widget,
13482 * for instance, for internal navigation.
13483 *
13484 * <p>The sound effect will only be played if sound effects are enabled by the user, and
13485 * {@link #isSoundEffectsEnabled()} is true.
13486 *
13487 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
13488 */
13489 public void playSoundEffect(int soundConstant) {
13490 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
13491 return;
13492 }
13493 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
13494 }
13495
13496 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013497 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013498 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013499 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 *
13501 * <p>The framework will provide haptic feedback for some built in actions,
13502 * such as long presses, but you may wish to provide feedback for your
13503 * own widget.
13504 *
13505 * <p>The feedback will only be performed if
13506 * {@link #isHapticFeedbackEnabled()} is true.
13507 *
13508 * @param feedbackConstant One of the constants defined in
13509 * {@link HapticFeedbackConstants}
13510 */
13511 public boolean performHapticFeedback(int feedbackConstant) {
13512 return performHapticFeedback(feedbackConstant, 0);
13513 }
13514
13515 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013516 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070013517 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070013518 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519 *
13520 * @param feedbackConstant One of the constants defined in
13521 * {@link HapticFeedbackConstants}
13522 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
13523 */
13524 public boolean performHapticFeedback(int feedbackConstant, int flags) {
13525 if (mAttachInfo == null) {
13526 return false;
13527 }
Romain Guyf607bdc2010-09-10 19:20:06 -070013528 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070013529 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013530 && !isHapticFeedbackEnabled()) {
13531 return false;
13532 }
Romain Guy812ccbe2010-06-01 14:07:24 -070013533 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
13534 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013535 }
13536
13537 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013538 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013539 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13540 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013541 */
13542 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040013543 if (visibility != mSystemUiVisibility) {
13544 mSystemUiVisibility = visibility;
13545 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13546 mParent.recomputeViewAttributes(this);
13547 }
Joe Onorato664644d2011-01-23 17:53:23 -080013548 }
13549 }
13550
13551 /**
13552 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040013553 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13554 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013555 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080013556 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080013557 return mSystemUiVisibility;
13558 }
13559
Scott Mainec6331b2011-05-24 16:55:56 -070013560 /**
13561 * Set a listener to receive callbacks when the visibility of the system bar changes.
13562 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
13563 */
Joe Onorato664644d2011-01-23 17:53:23 -080013564 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013565 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080013566 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
13567 mParent.recomputeViewAttributes(this);
13568 }
13569 }
13570
13571 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013572 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
13573 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080013574 */
13575 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013576 ListenerInfo li = mListenerInfo;
13577 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
13578 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080013579 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080013580 }
13581 }
13582
Dianne Hackborn9a230e02011-10-06 11:51:27 -070013583 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
13584 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
13585 if (val != mSystemUiVisibility) {
13586 setSystemUiVisibility(val);
13587 }
13588 }
13589
Joe Onorato664644d2011-01-23 17:53:23 -080013590 /**
Joe Malin32736f02011-01-19 16:14:20 -080013591 * Creates an image that the system displays during the drag and drop
13592 * operation. This is called a &quot;drag shadow&quot;. The default implementation
13593 * for a DragShadowBuilder based on a View returns an image that has exactly the same
13594 * appearance as the given View. The default also positions the center of the drag shadow
13595 * directly under the touch point. If no View is provided (the constructor with no parameters
13596 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
13597 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
13598 * default is an invisible drag shadow.
13599 * <p>
13600 * You are not required to use the View you provide to the constructor as the basis of the
13601 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
13602 * anything you want as the drag shadow.
13603 * </p>
13604 * <p>
13605 * You pass a DragShadowBuilder object to the system when you start the drag. The system
13606 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
13607 * size and position of the drag shadow. It uses this data to construct a
13608 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
13609 * so that your application can draw the shadow image in the Canvas.
13610 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070013611 *
13612 * <div class="special reference">
13613 * <h3>Developer Guides</h3>
13614 * <p>For a guide to implementing drag and drop features, read the
13615 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
13616 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070013617 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013618 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070013619 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070013620
13621 /**
Joe Malin32736f02011-01-19 16:14:20 -080013622 * Constructs a shadow image builder based on a View. By default, the resulting drag
13623 * shadow will have the same appearance and dimensions as the View, with the touch point
13624 * over the center of the View.
13625 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070013626 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013627 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070013628 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070013629 }
13630
Christopher Tate17ed60c2011-01-18 12:50:26 -080013631 /**
13632 * Construct a shadow builder object with no associated View. This
13633 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
13634 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
13635 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080013636 * reference to any View object. If they are not overridden, then the result is an
13637 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080013638 */
13639 public DragShadowBuilder() {
13640 mView = new WeakReference<View>(null);
13641 }
13642
13643 /**
13644 * Returns the View object that had been passed to the
13645 * {@link #View.DragShadowBuilder(View)}
13646 * constructor. If that View parameter was {@code null} or if the
13647 * {@link #View.DragShadowBuilder()}
13648 * constructor was used to instantiate the builder object, this method will return
13649 * null.
13650 *
13651 * @return The View object associate with this builder object.
13652 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070013653 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070013654 final public View getView() {
13655 return mView.get();
13656 }
13657
Christopher Tate2c095f32010-10-04 14:13:40 -070013658 /**
Joe Malin32736f02011-01-19 16:14:20 -080013659 * Provides the metrics for the shadow image. These include the dimensions of
13660 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070013661 * be centered under the touch location while dragging.
13662 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013663 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080013664 * same as the dimensions of the View itself and centers the shadow under
13665 * the touch point.
13666 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070013667 *
Joe Malin32736f02011-01-19 16:14:20 -080013668 * @param shadowSize A {@link android.graphics.Point} containing the width and height
13669 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
13670 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
13671 * image.
13672 *
13673 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
13674 * shadow image that should be underneath the touch point during the drag and drop
13675 * operation. Your application must set {@link android.graphics.Point#x} to the
13676 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070013677 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013678 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070013679 final View view = mView.get();
13680 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013681 shadowSize.set(view.getWidth(), view.getHeight());
13682 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070013683 } else {
13684 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
13685 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013686 }
13687
13688 /**
Joe Malin32736f02011-01-19 16:14:20 -080013689 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
13690 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013691 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070013692 *
Joe Malin32736f02011-01-19 16:14:20 -080013693 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070013694 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013695 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070013696 final View view = mView.get();
13697 if (view != null) {
13698 view.draw(canvas);
13699 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013700 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070013701 }
Christopher Tate2c095f32010-10-04 14:13:40 -070013702 }
13703 }
13704
13705 /**
Joe Malin32736f02011-01-19 16:14:20 -080013706 * Starts a drag and drop operation. When your application calls this method, it passes a
13707 * {@link android.view.View.DragShadowBuilder} object to the system. The
13708 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
13709 * to get metrics for the drag shadow, and then calls the object's
13710 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
13711 * <p>
13712 * Once the system has the drag shadow, it begins the drag and drop operation by sending
13713 * drag events to all the View objects in your application that are currently visible. It does
13714 * this either by calling the View object's drag listener (an implementation of
13715 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
13716 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
13717 * Both are passed a {@link android.view.DragEvent} object that has a
13718 * {@link android.view.DragEvent#getAction()} value of
13719 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
13720 * </p>
13721 * <p>
13722 * Your application can invoke startDrag() on any attached View object. The View object does not
13723 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
13724 * be related to the View the user selected for dragging.
13725 * </p>
13726 * @param data A {@link android.content.ClipData} object pointing to the data to be
13727 * transferred by the drag and drop operation.
13728 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
13729 * drag shadow.
13730 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
13731 * drop operation. This Object is put into every DragEvent object sent by the system during the
13732 * current drag.
13733 * <p>
13734 * myLocalState is a lightweight mechanism for the sending information from the dragged View
13735 * to the target Views. For example, it can contain flags that differentiate between a
13736 * a copy operation and a move operation.
13737 * </p>
13738 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
13739 * so the parameter should be set to 0.
13740 * @return {@code true} if the method completes successfully, or
13741 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
13742 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070013743 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013744 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013745 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070013746 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013747 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070013748 }
13749 boolean okay = false;
13750
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013751 Point shadowSize = new Point();
13752 Point shadowTouchPoint = new Point();
13753 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070013754
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013755 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
13756 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
13757 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070013758 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013759
Chris Tatea32dcf72010-10-14 12:13:50 -070013760 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013761 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
13762 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070013763 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013764 Surface surface = new Surface();
13765 try {
13766 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080013767 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070013768 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070013769 + " surface=" + surface);
13770 if (token != null) {
13771 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070013772 try {
Chris Tate6b391282010-10-14 15:48:59 -070013773 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013774 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070013775 } finally {
13776 surface.unlockCanvasAndPost(canvas);
13777 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013778
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070013779 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080013780
13781 // Cache the local state object for delivery with DragEvents
13782 root.setLocalDragState(myLocalState);
13783
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013784 // repurpose 'shadowSize' for the last touch point
13785 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070013786
Christopher Tatea53146c2010-09-07 11:57:52 -070013787 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080013788 shadowSize.x, shadowSize.y,
13789 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070013790 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070013791
13792 // Off and running! Release our local surface instance; the drag
13793 // shadow surface is now managed by the system process.
13794 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070013795 }
13796 } catch (Exception e) {
13797 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
13798 surface.destroy();
13799 }
13800
13801 return okay;
13802 }
13803
Christopher Tatea53146c2010-09-07 11:57:52 -070013804 /**
Joe Malin32736f02011-01-19 16:14:20 -080013805 * Handles drag events sent by the system following a call to
13806 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
13807 *<p>
13808 * When the system calls this method, it passes a
13809 * {@link android.view.DragEvent} object. A call to
13810 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
13811 * in DragEvent. The method uses these to determine what is happening in the drag and drop
13812 * operation.
13813 * @param event The {@link android.view.DragEvent} sent by the system.
13814 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
13815 * in DragEvent, indicating the type of drag event represented by this object.
13816 * @return {@code true} if the method was successful, otherwise {@code false}.
13817 * <p>
13818 * The method should return {@code true} in response to an action type of
13819 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
13820 * operation.
13821 * </p>
13822 * <p>
13823 * The method should also return {@code true} in response to an action type of
13824 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
13825 * {@code false} if it didn't.
13826 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013827 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070013828 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070013829 return false;
13830 }
13831
13832 /**
Joe Malin32736f02011-01-19 16:14:20 -080013833 * Detects if this View is enabled and has a drag event listener.
13834 * If both are true, then it calls the drag event listener with the
13835 * {@link android.view.DragEvent} it received. If the drag event listener returns
13836 * {@code true}, then dispatchDragEvent() returns {@code true}.
13837 * <p>
13838 * For all other cases, the method calls the
13839 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
13840 * method and returns its result.
13841 * </p>
13842 * <p>
13843 * This ensures that a drag event is always consumed, even if the View does not have a drag
13844 * event listener. However, if the View has a listener and the listener returns true, then
13845 * onDragEvent() is not called.
13846 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070013847 */
13848 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080013849 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013850 ListenerInfo li = mListenerInfo;
13851 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
13852 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070013853 return true;
13854 }
Christopher Tatea53146c2010-09-07 11:57:52 -070013855 return onDragEvent(event);
13856 }
13857
Christopher Tate3d4bf172011-03-28 16:16:46 -070013858 boolean canAcceptDrag() {
13859 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13860 }
13861
Christopher Tatea53146c2010-09-07 11:57:52 -070013862 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013863 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13864 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013865 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013866 */
13867 public void onCloseSystemDialogs(String reason) {
13868 }
Joe Malin32736f02011-01-19 16:14:20 -080013869
Dianne Hackbornffa42482009-09-23 22:20:11 -070013870 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013871 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013872 * update a Region being computed for
13873 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013874 * that any non-transparent parts of the Drawable are removed from the
13875 * given transparent region.
13876 *
13877 * @param dr The Drawable whose transparency is to be applied to the region.
13878 * @param region A Region holding the current transparency information,
13879 * where any parts of the region that are set are considered to be
13880 * transparent. On return, this region will be modified to have the
13881 * transparency information reduced by the corresponding parts of the
13882 * Drawable that are not transparent.
13883 * {@hide}
13884 */
13885 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13886 if (DBG) {
13887 Log.i("View", "Getting transparent region for: " + this);
13888 }
13889 final Region r = dr.getTransparentRegion();
13890 final Rect db = dr.getBounds();
13891 final AttachInfo attachInfo = mAttachInfo;
13892 if (r != null && attachInfo != null) {
13893 final int w = getRight()-getLeft();
13894 final int h = getBottom()-getTop();
13895 if (db.left > 0) {
13896 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13897 r.op(0, 0, db.left, h, Region.Op.UNION);
13898 }
13899 if (db.right < w) {
13900 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13901 r.op(db.right, 0, w, h, Region.Op.UNION);
13902 }
13903 if (db.top > 0) {
13904 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13905 r.op(0, 0, w, db.top, Region.Op.UNION);
13906 }
13907 if (db.bottom < h) {
13908 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13909 r.op(0, db.bottom, w, h, Region.Op.UNION);
13910 }
13911 final int[] location = attachInfo.mTransparentLocation;
13912 getLocationInWindow(location);
13913 r.translate(location[0], location[1]);
13914 region.op(r, Region.Op.INTERSECT);
13915 } else {
13916 region.op(db, Region.Op.DIFFERENCE);
13917 }
13918 }
13919
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013920 private void checkForLongClick(int delayOffset) {
13921 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13922 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013924 if (mPendingCheckForLongPress == null) {
13925 mPendingCheckForLongPress = new CheckForLongPress();
13926 }
13927 mPendingCheckForLongPress.rememberWindowAttachCount();
13928 postDelayed(mPendingCheckForLongPress,
13929 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 }
13932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013933 /**
13934 * Inflate a view from an XML resource. This convenience method wraps the {@link
13935 * LayoutInflater} class, which provides a full range of options for view inflation.
13936 *
13937 * @param context The Context object for your activity or application.
13938 * @param resource The resource ID to inflate
13939 * @param root A view group that will be the parent. Used to properly inflate the
13940 * layout_* parameters.
13941 * @see LayoutInflater
13942 */
13943 public static View inflate(Context context, int resource, ViewGroup root) {
13944 LayoutInflater factory = LayoutInflater.from(context);
13945 return factory.inflate(resource, root);
13946 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013948 /**
Adam Powell637d3372010-08-25 14:37:03 -070013949 * Scroll the view with standard behavior for scrolling beyond the normal
13950 * content boundaries. Views that call this method should override
13951 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13952 * results of an over-scroll operation.
13953 *
13954 * Views can use this method to handle any touch or fling-based scrolling.
13955 *
13956 * @param deltaX Change in X in pixels
13957 * @param deltaY Change in Y in pixels
13958 * @param scrollX Current X scroll value in pixels before applying deltaX
13959 * @param scrollY Current Y scroll value in pixels before applying deltaY
13960 * @param scrollRangeX Maximum content scroll range along the X axis
13961 * @param scrollRangeY Maximum content scroll range along the Y axis
13962 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13963 * along the X axis.
13964 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13965 * along the Y axis.
13966 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13967 * @return true if scrolling was clamped to an over-scroll boundary along either
13968 * axis, false otherwise.
13969 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013970 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013971 protected boolean overScrollBy(int deltaX, int deltaY,
13972 int scrollX, int scrollY,
13973 int scrollRangeX, int scrollRangeY,
13974 int maxOverScrollX, int maxOverScrollY,
13975 boolean isTouchEvent) {
13976 final int overScrollMode = mOverScrollMode;
13977 final boolean canScrollHorizontal =
13978 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13979 final boolean canScrollVertical =
13980 computeVerticalScrollRange() > computeVerticalScrollExtent();
13981 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13982 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13983 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13984 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13985
13986 int newScrollX = scrollX + deltaX;
13987 if (!overScrollHorizontal) {
13988 maxOverScrollX = 0;
13989 }
13990
13991 int newScrollY = scrollY + deltaY;
13992 if (!overScrollVertical) {
13993 maxOverScrollY = 0;
13994 }
13995
13996 // Clamp values if at the limits and record
13997 final int left = -maxOverScrollX;
13998 final int right = maxOverScrollX + scrollRangeX;
13999 final int top = -maxOverScrollY;
14000 final int bottom = maxOverScrollY + scrollRangeY;
14001
14002 boolean clampedX = false;
14003 if (newScrollX > right) {
14004 newScrollX = right;
14005 clampedX = true;
14006 } else if (newScrollX < left) {
14007 newScrollX = left;
14008 clampedX = true;
14009 }
14010
14011 boolean clampedY = false;
14012 if (newScrollY > bottom) {
14013 newScrollY = bottom;
14014 clampedY = true;
14015 } else if (newScrollY < top) {
14016 newScrollY = top;
14017 clampedY = true;
14018 }
14019
14020 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
14021
14022 return clampedX || clampedY;
14023 }
14024
14025 /**
14026 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
14027 * respond to the results of an over-scroll operation.
14028 *
14029 * @param scrollX New X scroll value in pixels
14030 * @param scrollY New Y scroll value in pixels
14031 * @param clampedX True if scrollX was clamped to an over-scroll boundary
14032 * @param clampedY True if scrollY was clamped to an over-scroll boundary
14033 */
14034 protected void onOverScrolled(int scrollX, int scrollY,
14035 boolean clampedX, boolean clampedY) {
14036 // Intentionally empty.
14037 }
14038
14039 /**
14040 * Returns the over-scroll mode for this view. The result will be
14041 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14042 * (allow over-scrolling only if the view content is larger than the container),
14043 * or {@link #OVER_SCROLL_NEVER}.
14044 *
14045 * @return This view's over-scroll mode.
14046 */
14047 public int getOverScrollMode() {
14048 return mOverScrollMode;
14049 }
14050
14051 /**
14052 * Set the over-scroll mode for this view. Valid over-scroll modes are
14053 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
14054 * (allow over-scrolling only if the view content is larger than the container),
14055 * or {@link #OVER_SCROLL_NEVER}.
14056 *
14057 * Setting the over-scroll mode of a view will have an effect only if the
14058 * view is capable of scrolling.
14059 *
14060 * @param overScrollMode The new over-scroll mode for this view.
14061 */
14062 public void setOverScrollMode(int overScrollMode) {
14063 if (overScrollMode != OVER_SCROLL_ALWAYS &&
14064 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
14065 overScrollMode != OVER_SCROLL_NEVER) {
14066 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
14067 }
14068 mOverScrollMode = overScrollMode;
14069 }
14070
14071 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014072 * Gets a scale factor that determines the distance the view should scroll
14073 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
14074 * @return The vertical scroll scale factor.
14075 * @hide
14076 */
14077 protected float getVerticalScrollFactor() {
14078 if (mVerticalScrollFactor == 0) {
14079 TypedValue outValue = new TypedValue();
14080 if (!mContext.getTheme().resolveAttribute(
14081 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
14082 throw new IllegalStateException(
14083 "Expected theme to define listPreferredItemHeight.");
14084 }
14085 mVerticalScrollFactor = outValue.getDimension(
14086 mContext.getResources().getDisplayMetrics());
14087 }
14088 return mVerticalScrollFactor;
14089 }
14090
14091 /**
14092 * Gets a scale factor that determines the distance the view should scroll
14093 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
14094 * @return The horizontal scroll scale factor.
14095 * @hide
14096 */
14097 protected float getHorizontalScrollFactor() {
14098 // TODO: Should use something else.
14099 return getVerticalScrollFactor();
14100 }
14101
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014102 /**
14103 * Return the value specifying the text direction or policy that was set with
14104 * {@link #setTextDirection(int)}.
14105 *
14106 * @return the defined text direction. It can be one of:
14107 *
14108 * {@link #TEXT_DIRECTION_INHERIT},
14109 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14110 * {@link #TEXT_DIRECTION_ANY_RTL},
14111 * {@link #TEXT_DIRECTION_LTR},
14112 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014113 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014114 */
14115 public int getTextDirection() {
14116 return mTextDirection;
14117 }
14118
14119 /**
14120 * Set the text direction.
14121 *
14122 * @param textDirection the direction to set. Should be one of:
14123 *
14124 * {@link #TEXT_DIRECTION_INHERIT},
14125 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14126 * {@link #TEXT_DIRECTION_ANY_RTL},
14127 * {@link #TEXT_DIRECTION_LTR},
14128 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014129 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014130 */
14131 public void setTextDirection(int textDirection) {
14132 if (textDirection != mTextDirection) {
14133 mTextDirection = textDirection;
14134 resetResolvedTextDirection();
14135 requestLayout();
14136 }
14137 }
14138
14139 /**
14140 * Return the resolved text direction.
14141 *
14142 * @return the resolved text direction. Return one of:
14143 *
Doug Feltcb3791202011-07-07 11:57:48 -070014144 * {@link #TEXT_DIRECTION_FIRST_STRONG}
14145 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014146 * {@link #TEXT_DIRECTION_LTR},
14147 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio4c1e00a2012-01-05 19:05:55 -080014148 * {@link #TEXT_DIRECTION_LOCALE},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014149 */
14150 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014151 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014152 resolveTextDirection();
14153 }
14154 return mResolvedTextDirection;
14155 }
14156
14157 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014158 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
14159 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014160 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014161 public void resolveTextDirection() {
14162 if (mResolvedTextDirection != TEXT_DIRECTION_INHERIT) {
14163 // Resolution has already been done.
14164 return;
14165 }
Doug Feltcb3791202011-07-07 11:57:48 -070014166 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
14167 mResolvedTextDirection = mTextDirection;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014168 } else if (mParent != null && mParent instanceof ViewGroup) {
Doug Feltcb3791202011-07-07 11:57:48 -070014169 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014170 } else {
14171 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Doug Feltcb3791202011-07-07 11:57:48 -070014172 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014173 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014174 }
14175
14176 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014177 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014178 * resolution should override this method.
14179 *
14180 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014181 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014182 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014183 }
14184
14185 /**
14186 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014187 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014188 * reset is done.
14189 */
14190 public void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070014191 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014192 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080014193 }
14194
14195 /**
14196 * Called when text direction is reset. Subclasses that care about text direction reset should
14197 * override this method and do a reset of the text direction of their children. The default
14198 * implementation does nothing.
14199 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080014200 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070014201 }
14202
Chet Haaseb39f0512011-05-24 14:36:40 -070014203 //
14204 // Properties
14205 //
14206 /**
14207 * A Property wrapper around the <code>alpha</code> functionality handled by the
14208 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
14209 */
Chet Haased47f1532011-12-16 11:18:52 -080014210 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014211 @Override
14212 public void setValue(View object, float value) {
14213 object.setAlpha(value);
14214 }
14215
14216 @Override
14217 public Float get(View object) {
14218 return object.getAlpha();
14219 }
14220 };
14221
14222 /**
14223 * A Property wrapper around the <code>translationX</code> functionality handled by the
14224 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
14225 */
Chet Haased47f1532011-12-16 11:18:52 -080014226 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014227 @Override
14228 public void setValue(View object, float value) {
14229 object.setTranslationX(value);
14230 }
14231
14232 @Override
14233 public Float get(View object) {
14234 return object.getTranslationX();
14235 }
14236 };
14237
14238 /**
14239 * A Property wrapper around the <code>translationY</code> functionality handled by the
14240 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
14241 */
Chet Haased47f1532011-12-16 11:18:52 -080014242 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014243 @Override
14244 public void setValue(View object, float value) {
14245 object.setTranslationY(value);
14246 }
14247
14248 @Override
14249 public Float get(View object) {
14250 return object.getTranslationY();
14251 }
14252 };
14253
14254 /**
14255 * A Property wrapper around the <code>x</code> functionality handled by the
14256 * {@link View#setX(float)} and {@link View#getX()} methods.
14257 */
Chet Haased47f1532011-12-16 11:18:52 -080014258 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014259 @Override
14260 public void setValue(View object, float value) {
14261 object.setX(value);
14262 }
14263
14264 @Override
14265 public Float get(View object) {
14266 return object.getX();
14267 }
14268 };
14269
14270 /**
14271 * A Property wrapper around the <code>y</code> functionality handled by the
14272 * {@link View#setY(float)} and {@link View#getY()} methods.
14273 */
Chet Haased47f1532011-12-16 11:18:52 -080014274 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014275 @Override
14276 public void setValue(View object, float value) {
14277 object.setY(value);
14278 }
14279
14280 @Override
14281 public Float get(View object) {
14282 return object.getY();
14283 }
14284 };
14285
14286 /**
14287 * A Property wrapper around the <code>rotation</code> functionality handled by the
14288 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
14289 */
Chet Haased47f1532011-12-16 11:18:52 -080014290 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014291 @Override
14292 public void setValue(View object, float value) {
14293 object.setRotation(value);
14294 }
14295
14296 @Override
14297 public Float get(View object) {
14298 return object.getRotation();
14299 }
14300 };
14301
14302 /**
14303 * A Property wrapper around the <code>rotationX</code> functionality handled by the
14304 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
14305 */
Chet Haased47f1532011-12-16 11:18:52 -080014306 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014307 @Override
14308 public void setValue(View object, float value) {
14309 object.setRotationX(value);
14310 }
14311
14312 @Override
14313 public Float get(View object) {
14314 return object.getRotationX();
14315 }
14316 };
14317
14318 /**
14319 * A Property wrapper around the <code>rotationY</code> functionality handled by the
14320 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
14321 */
Chet Haased47f1532011-12-16 11:18:52 -080014322 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014323 @Override
14324 public void setValue(View object, float value) {
14325 object.setRotationY(value);
14326 }
14327
14328 @Override
14329 public Float get(View object) {
14330 return object.getRotationY();
14331 }
14332 };
14333
14334 /**
14335 * A Property wrapper around the <code>scaleX</code> functionality handled by the
14336 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
14337 */
Chet Haased47f1532011-12-16 11:18:52 -080014338 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014339 @Override
14340 public void setValue(View object, float value) {
14341 object.setScaleX(value);
14342 }
14343
14344 @Override
14345 public Float get(View object) {
14346 return object.getScaleX();
14347 }
14348 };
14349
14350 /**
14351 * A Property wrapper around the <code>scaleY</code> functionality handled by the
14352 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
14353 */
Chet Haased47f1532011-12-16 11:18:52 -080014354 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070014355 @Override
14356 public void setValue(View object, float value) {
14357 object.setScaleY(value);
14358 }
14359
14360 @Override
14361 public Float get(View object) {
14362 return object.getScaleY();
14363 }
14364 };
14365
Jeff Brown33bbfd22011-02-24 20:55:35 -080014366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
14368 * Each MeasureSpec represents a requirement for either the width or the height.
14369 * A MeasureSpec is comprised of a size and a mode. There are three possible
14370 * modes:
14371 * <dl>
14372 * <dt>UNSPECIFIED</dt>
14373 * <dd>
14374 * The parent has not imposed any constraint on the child. It can be whatever size
14375 * it wants.
14376 * </dd>
14377 *
14378 * <dt>EXACTLY</dt>
14379 * <dd>
14380 * The parent has determined an exact size for the child. The child is going to be
14381 * given those bounds regardless of how big it wants to be.
14382 * </dd>
14383 *
14384 * <dt>AT_MOST</dt>
14385 * <dd>
14386 * The child can be as large as it wants up to the specified size.
14387 * </dd>
14388 * </dl>
14389 *
14390 * MeasureSpecs are implemented as ints to reduce object allocation. This class
14391 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
14392 */
14393 public static class MeasureSpec {
14394 private static final int MODE_SHIFT = 30;
14395 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
14396
14397 /**
14398 * Measure specification mode: The parent has not imposed any constraint
14399 * on the child. It can be whatever size it wants.
14400 */
14401 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
14402
14403 /**
14404 * Measure specification mode: The parent has determined an exact size
14405 * for the child. The child is going to be given those bounds regardless
14406 * of how big it wants to be.
14407 */
14408 public static final int EXACTLY = 1 << MODE_SHIFT;
14409
14410 /**
14411 * Measure specification mode: The child can be as large as it wants up
14412 * to the specified size.
14413 */
14414 public static final int AT_MOST = 2 << MODE_SHIFT;
14415
14416 /**
14417 * Creates a measure specification based on the supplied size and mode.
14418 *
14419 * The mode must always be one of the following:
14420 * <ul>
14421 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
14422 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
14423 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
14424 * </ul>
14425 *
14426 * @param size the size of the measure specification
14427 * @param mode the mode of the measure specification
14428 * @return the measure specification based on size and mode
14429 */
14430 public static int makeMeasureSpec(int size, int mode) {
14431 return size + mode;
14432 }
14433
14434 /**
14435 * Extracts the mode from the supplied measure specification.
14436 *
14437 * @param measureSpec the measure specification to extract the mode from
14438 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
14439 * {@link android.view.View.MeasureSpec#AT_MOST} or
14440 * {@link android.view.View.MeasureSpec#EXACTLY}
14441 */
14442 public static int getMode(int measureSpec) {
14443 return (measureSpec & MODE_MASK);
14444 }
14445
14446 /**
14447 * Extracts the size from the supplied measure specification.
14448 *
14449 * @param measureSpec the measure specification to extract the size from
14450 * @return the size in pixels defined in the supplied measure specification
14451 */
14452 public static int getSize(int measureSpec) {
14453 return (measureSpec & ~MODE_MASK);
14454 }
14455
14456 /**
14457 * Returns a String representation of the specified measure
14458 * specification.
14459 *
14460 * @param measureSpec the measure specification to convert to a String
14461 * @return a String with the following format: "MeasureSpec: MODE SIZE"
14462 */
14463 public static String toString(int measureSpec) {
14464 int mode = getMode(measureSpec);
14465 int size = getSize(measureSpec);
14466
14467 StringBuilder sb = new StringBuilder("MeasureSpec: ");
14468
14469 if (mode == UNSPECIFIED)
14470 sb.append("UNSPECIFIED ");
14471 else if (mode == EXACTLY)
14472 sb.append("EXACTLY ");
14473 else if (mode == AT_MOST)
14474 sb.append("AT_MOST ");
14475 else
14476 sb.append(mode).append(" ");
14477
14478 sb.append(size);
14479 return sb.toString();
14480 }
14481 }
14482
14483 class CheckForLongPress implements Runnable {
14484
14485 private int mOriginalWindowAttachCount;
14486
14487 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070014488 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014489 && mOriginalWindowAttachCount == mWindowAttachCount) {
14490 if (performLongClick()) {
14491 mHasPerformedLongPress = true;
14492 }
14493 }
14494 }
14495
14496 public void rememberWindowAttachCount() {
14497 mOriginalWindowAttachCount = mWindowAttachCount;
14498 }
14499 }
Joe Malin32736f02011-01-19 16:14:20 -080014500
Adam Powelle14579b2009-12-16 18:39:52 -080014501 private final class CheckForTap implements Runnable {
14502 public void run() {
14503 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080014504 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070014505 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080014506 }
14507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014508
Adam Powella35d7682010-03-12 14:48:13 -080014509 private final class PerformClick implements Runnable {
14510 public void run() {
14511 performClick();
14512 }
14513 }
14514
Dianne Hackborn63042d62011-01-26 18:56:29 -080014515 /** @hide */
14516 public void hackTurnOffWindowResizeAnim(boolean off) {
14517 mAttachInfo.mTurnOffWindowResizeAnim = off;
14518 }
Joe Malin32736f02011-01-19 16:14:20 -080014519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014520 /**
Chet Haasea00f3862011-02-22 06:34:40 -080014521 * This method returns a ViewPropertyAnimator object, which can be used to animate
14522 * specific properties on this View.
14523 *
14524 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
14525 */
14526 public ViewPropertyAnimator animate() {
14527 if (mAnimator == null) {
14528 mAnimator = new ViewPropertyAnimator(this);
14529 }
14530 return mAnimator;
14531 }
14532
14533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014534 * Interface definition for a callback to be invoked when a key event is
14535 * dispatched to this view. The callback will be invoked before the key
14536 * event is given to the view.
14537 */
14538 public interface OnKeyListener {
14539 /**
14540 * Called when a key is dispatched to a view. This allows listeners to
14541 * get a chance to respond before the target view.
14542 *
14543 * @param v The view the key has been dispatched to.
14544 * @param keyCode The code for the physical key that was pressed
14545 * @param event The KeyEvent object containing full information about
14546 * the event.
14547 * @return True if the listener has consumed the event, false otherwise.
14548 */
14549 boolean onKey(View v, int keyCode, KeyEvent event);
14550 }
14551
14552 /**
14553 * Interface definition for a callback to be invoked when a touch event is
14554 * dispatched to this view. The callback will be invoked before the touch
14555 * event is given to the view.
14556 */
14557 public interface OnTouchListener {
14558 /**
14559 * Called when a touch event is dispatched to a view. This allows listeners to
14560 * get a chance to respond before the target view.
14561 *
14562 * @param v The view the touch event has been dispatched to.
14563 * @param event The MotionEvent object containing full information about
14564 * the event.
14565 * @return True if the listener has consumed the event, false otherwise.
14566 */
14567 boolean onTouch(View v, MotionEvent event);
14568 }
14569
14570 /**
Jeff Brown10b62902011-06-20 16:40:37 -070014571 * Interface definition for a callback to be invoked when a hover event is
14572 * dispatched to this view. The callback will be invoked before the hover
14573 * event is given to the view.
14574 */
14575 public interface OnHoverListener {
14576 /**
14577 * Called when a hover event is dispatched to a view. This allows listeners to
14578 * get a chance to respond before the target view.
14579 *
14580 * @param v The view the hover event has been dispatched to.
14581 * @param event The MotionEvent object containing full information about
14582 * the event.
14583 * @return True if the listener has consumed the event, false otherwise.
14584 */
14585 boolean onHover(View v, MotionEvent event);
14586 }
14587
14588 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080014589 * Interface definition for a callback to be invoked when a generic motion event is
14590 * dispatched to this view. The callback will be invoked before the generic motion
14591 * event is given to the view.
14592 */
14593 public interface OnGenericMotionListener {
14594 /**
14595 * Called when a generic motion event is dispatched to a view. This allows listeners to
14596 * get a chance to respond before the target view.
14597 *
14598 * @param v The view the generic motion event has been dispatched to.
14599 * @param event The MotionEvent object containing full information about
14600 * the event.
14601 * @return True if the listener has consumed the event, false otherwise.
14602 */
14603 boolean onGenericMotion(View v, MotionEvent event);
14604 }
14605
14606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014607 * Interface definition for a callback to be invoked when a view has been clicked and held.
14608 */
14609 public interface OnLongClickListener {
14610 /**
14611 * Called when a view has been clicked and held.
14612 *
14613 * @param v The view that was clicked and held.
14614 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080014615 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014616 */
14617 boolean onLongClick(View v);
14618 }
14619
14620 /**
Chris Tate32affef2010-10-18 15:29:21 -070014621 * Interface definition for a callback to be invoked when a drag is being dispatched
14622 * to this view. The callback will be invoked before the hosting view's own
14623 * onDrag(event) method. If the listener wants to fall back to the hosting view's
14624 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070014625 *
14626 * <div class="special reference">
14627 * <h3>Developer Guides</h3>
14628 * <p>For a guide to implementing drag and drop features, read the
14629 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
14630 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070014631 */
14632 public interface OnDragListener {
14633 /**
14634 * Called when a drag event is dispatched to a view. This allows listeners
14635 * to get a chance to override base View behavior.
14636 *
Joe Malin32736f02011-01-19 16:14:20 -080014637 * @param v The View that received the drag event.
14638 * @param event The {@link android.view.DragEvent} object for the drag event.
14639 * @return {@code true} if the drag event was handled successfully, or {@code false}
14640 * if the drag event was not handled. Note that {@code false} will trigger the View
14641 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070014642 */
14643 boolean onDrag(View v, DragEvent event);
14644 }
14645
14646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 * Interface definition for a callback to be invoked when the focus state of
14648 * a view changed.
14649 */
14650 public interface OnFocusChangeListener {
14651 /**
14652 * Called when the focus state of a view has changed.
14653 *
14654 * @param v The view whose state has changed.
14655 * @param hasFocus The new focus state of v.
14656 */
14657 void onFocusChange(View v, boolean hasFocus);
14658 }
14659
14660 /**
14661 * Interface definition for a callback to be invoked when a view is clicked.
14662 */
14663 public interface OnClickListener {
14664 /**
14665 * Called when a view has been clicked.
14666 *
14667 * @param v The view that was clicked.
14668 */
14669 void onClick(View v);
14670 }
14671
14672 /**
14673 * Interface definition for a callback to be invoked when the context menu
14674 * for this view is being built.
14675 */
14676 public interface OnCreateContextMenuListener {
14677 /**
14678 * Called when the context menu for this view is being built. It is not
14679 * safe to hold onto the menu after this method returns.
14680 *
14681 * @param menu The context menu that is being built
14682 * @param v The view for which the context menu is being built
14683 * @param menuInfo Extra information about the item for which the
14684 * context menu should be shown. This information will vary
14685 * depending on the class of v.
14686 */
14687 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
14688 }
14689
Joe Onorato664644d2011-01-23 17:53:23 -080014690 /**
14691 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014692 * visibility. This reports <strong>global</strong> changes to the system UI
14693 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080014694 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070014695 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080014696 */
14697 public interface OnSystemUiVisibilityChangeListener {
14698 /**
14699 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070014700 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080014701 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040014702 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014703 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
14704 * <strong>global</strong> state of the UI visibility flags, not what your
14705 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080014706 */
14707 public void onSystemUiVisibilityChange(int visibility);
14708 }
14709
Adam Powell4afd62b2011-02-18 15:02:18 -080014710 /**
14711 * Interface definition for a callback to be invoked when this view is attached
14712 * or detached from its window.
14713 */
14714 public interface OnAttachStateChangeListener {
14715 /**
14716 * Called when the view is attached to a window.
14717 * @param v The view that was attached
14718 */
14719 public void onViewAttachedToWindow(View v);
14720 /**
14721 * Called when the view is detached from a window.
14722 * @param v The view that was detached
14723 */
14724 public void onViewDetachedFromWindow(View v);
14725 }
14726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014727 private final class UnsetPressedState implements Runnable {
14728 public void run() {
14729 setPressed(false);
14730 }
14731 }
14732
14733 /**
14734 * Base class for derived classes that want to save and restore their own
14735 * state in {@link android.view.View#onSaveInstanceState()}.
14736 */
14737 public static class BaseSavedState extends AbsSavedState {
14738 /**
14739 * Constructor used when reading from a parcel. Reads the state of the superclass.
14740 *
14741 * @param source
14742 */
14743 public BaseSavedState(Parcel source) {
14744 super(source);
14745 }
14746
14747 /**
14748 * Constructor called by derived classes when creating their SavedState objects
14749 *
14750 * @param superState The state of the superclass of this view
14751 */
14752 public BaseSavedState(Parcelable superState) {
14753 super(superState);
14754 }
14755
14756 public static final Parcelable.Creator<BaseSavedState> CREATOR =
14757 new Parcelable.Creator<BaseSavedState>() {
14758 public BaseSavedState createFromParcel(Parcel in) {
14759 return new BaseSavedState(in);
14760 }
14761
14762 public BaseSavedState[] newArray(int size) {
14763 return new BaseSavedState[size];
14764 }
14765 };
14766 }
14767
14768 /**
14769 * A set of information given to a view when it is attached to its parent
14770 * window.
14771 */
14772 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014773 interface Callbacks {
14774 void playSoundEffect(int effectId);
14775 boolean performHapticFeedback(int effectId, boolean always);
14776 }
14777
14778 /**
14779 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
14780 * to a Handler. This class contains the target (View) to invalidate and
14781 * the coordinates of the dirty rectangle.
14782 *
14783 * For performance purposes, this class also implements a pool of up to
14784 * POOL_LIMIT objects that get reused. This reduces memory allocations
14785 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014786 */
Romain Guyd928d682009-03-31 17:52:16 -070014787 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014788 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070014789 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
14790 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070014791 public InvalidateInfo newInstance() {
14792 return new InvalidateInfo();
14793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014794
Romain Guyd928d682009-03-31 17:52:16 -070014795 public void onAcquired(InvalidateInfo element) {
14796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014797
Romain Guyd928d682009-03-31 17:52:16 -070014798 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070014799 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070014800 }
14801 }, POOL_LIMIT)
14802 );
14803
14804 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014805 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806
14807 View target;
14808
14809 int left;
14810 int top;
14811 int right;
14812 int bottom;
14813
Romain Guyd928d682009-03-31 17:52:16 -070014814 public void setNextPoolable(InvalidateInfo element) {
14815 mNext = element;
14816 }
14817
14818 public InvalidateInfo getNextPoolable() {
14819 return mNext;
14820 }
14821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014822 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070014823 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014824 }
14825
14826 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070014827 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014828 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014829
14830 public boolean isPooled() {
14831 return mIsPooled;
14832 }
14833
14834 public void setPooled(boolean isPooled) {
14835 mIsPooled = isPooled;
14836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014837 }
14838
14839 final IWindowSession mSession;
14840
14841 final IWindow mWindow;
14842
14843 final IBinder mWindowToken;
14844
14845 final Callbacks mRootCallbacks;
14846
Romain Guy59a12ca2011-06-09 17:48:21 -070014847 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080014848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 /**
14850 * The top view of the hierarchy.
14851 */
14852 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070014853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 IBinder mPanelParentWindowToken;
14855 Surface mSurface;
14856
Romain Guyb051e892010-09-28 19:09:36 -070014857 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014858 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070014859 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080014860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014861 /**
Romain Guy8506ab42009-06-11 17:35:47 -070014862 * Scale factor used by the compatibility mode
14863 */
14864 float mApplicationScale;
14865
14866 /**
14867 * Indicates whether the application is in compatibility mode
14868 */
14869 boolean mScalingRequired;
14870
14871 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014872 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080014873 */
14874 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080014875
Dianne Hackborn63042d62011-01-26 18:56:29 -080014876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014877 * Left position of this view's window
14878 */
14879 int mWindowLeft;
14880
14881 /**
14882 * Top position of this view's window
14883 */
14884 int mWindowTop;
14885
14886 /**
Adam Powell26153a32010-11-08 15:22:27 -080014887 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014888 */
Adam Powell26153a32010-11-08 15:22:27 -080014889 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014890
14891 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014892 * For windows that are full-screen but using insets to layout inside
14893 * of the screen decorations, these are the current insets for the
14894 * content of the window.
14895 */
14896 final Rect mContentInsets = new Rect();
14897
14898 /**
14899 * For windows that are full-screen but using insets to layout inside
14900 * of the screen decorations, these are the current insets for the
14901 * actual visible parts of the window.
14902 */
14903 final Rect mVisibleInsets = new Rect();
14904
14905 /**
14906 * The internal insets given by this window. This value is
14907 * supplied by the client (through
14908 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14909 * be given to the window manager when changed to be used in laying
14910 * out windows behind it.
14911 */
14912 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14913 = new ViewTreeObserver.InternalInsetsInfo();
14914
14915 /**
14916 * All views in the window's hierarchy that serve as scroll containers,
14917 * used to determine if the window can be resized or must be panned
14918 * to adjust for a soft input area.
14919 */
14920 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14921
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014922 final KeyEvent.DispatcherState mKeyDispatchState
14923 = new KeyEvent.DispatcherState();
14924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 /**
14926 * Indicates whether the view's window currently has the focus.
14927 */
14928 boolean mHasWindowFocus;
14929
14930 /**
14931 * The current visibility of the window.
14932 */
14933 int mWindowVisibility;
14934
14935 /**
14936 * Indicates the time at which drawing started to occur.
14937 */
14938 long mDrawingTime;
14939
14940 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014941 * Indicates whether or not ignoring the DIRTY_MASK flags.
14942 */
14943 boolean mIgnoreDirtyState;
14944
14945 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014946 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14947 * to avoid clearing that flag prematurely.
14948 */
14949 boolean mSetIgnoreDirtyState = false;
14950
14951 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014952 * Indicates whether the view's window is currently in touch mode.
14953 */
14954 boolean mInTouchMode;
14955
14956 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014957 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014958 * the next time it performs a traversal
14959 */
14960 boolean mRecomputeGlobalAttributes;
14961
14962 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070014963 * Always report new attributes at next traversal.
14964 */
14965 boolean mForceReportNewAttributes;
14966
14967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968 * Set during a traveral if any views want to keep the screen on.
14969 */
14970 boolean mKeepScreenOn;
14971
14972 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014973 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14974 */
14975 int mSystemUiVisibility;
14976
14977 /**
14978 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14979 * attached.
14980 */
14981 boolean mHasSystemUiListeners;
14982
14983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014984 * Set if the visibility of any views has changed.
14985 */
14986 boolean mViewVisibilityChanged;
14987
14988 /**
14989 * Set to true if a view has been scrolled.
14990 */
14991 boolean mViewScrollChanged;
14992
14993 /**
14994 * Global to the view hierarchy used as a temporary for dealing with
14995 * x/y points in the transparent region computations.
14996 */
14997 final int[] mTransparentLocation = new int[2];
14998
14999 /**
15000 * Global to the view hierarchy used as a temporary for dealing with
15001 * x/y points in the ViewGroup.invalidateChild implementation.
15002 */
15003 final int[] mInvalidateChildLocation = new int[2];
15004
Chet Haasec3aa3612010-06-17 08:50:37 -070015005
15006 /**
15007 * Global to the view hierarchy used as a temporary for dealing with
15008 * x/y location when view is transformed.
15009 */
15010 final float[] mTmpTransformLocation = new float[2];
15011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015012 /**
15013 * The view tree observer used to dispatch global events like
15014 * layout, pre-draw, touch mode change, etc.
15015 */
15016 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
15017
15018 /**
15019 * A Canvas used by the view hierarchy to perform bitmap caching.
15020 */
15021 Canvas mCanvas;
15022
15023 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080015024 * The view root impl.
15025 */
15026 final ViewRootImpl mViewRootImpl;
15027
15028 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015029 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015030 * handler can be used to pump events in the UI events queue.
15031 */
15032 final Handler mHandler;
15033
15034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015035 * Temporary for use in computing invalidate rectangles while
15036 * calling up the hierarchy.
15037 */
15038 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070015039
15040 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070015041 * Temporary for use in computing hit areas with transformed views
15042 */
15043 final RectF mTmpTransformRect = new RectF();
15044
15045 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070015046 * Temporary list for use in collecting focusable descendents of a view.
15047 */
15048 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
15049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015050 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070015051 * The id of the window for accessibility purposes.
15052 */
15053 int mAccessibilityWindowId = View.NO_ID;
15054
15055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 * Creates a new set of attachment information with the specified
15057 * events handler and thread.
15058 *
15059 * @param handler the events handler the view must use
15060 */
15061 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080015062 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015063 mSession = session;
15064 mWindow = window;
15065 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080015066 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015067 mHandler = handler;
15068 mRootCallbacks = effectPlayer;
15069 }
15070 }
15071
15072 /**
15073 * <p>ScrollabilityCache holds various fields used by a View when scrolling
15074 * is supported. This avoids keeping too many unused fields in most
15075 * instances of View.</p>
15076 */
Mike Cleronf116bf82009-09-27 19:14:12 -070015077 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080015078
Mike Cleronf116bf82009-09-27 19:14:12 -070015079 /**
15080 * Scrollbars are not visible
15081 */
15082 public static final int OFF = 0;
15083
15084 /**
15085 * Scrollbars are visible
15086 */
15087 public static final int ON = 1;
15088
15089 /**
15090 * Scrollbars are fading away
15091 */
15092 public static final int FADING = 2;
15093
15094 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080015095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015096 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070015097 public int scrollBarDefaultDelayBeforeFade;
15098 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099
15100 public int scrollBarSize;
15101 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070015102 public float[] interpolatorValues;
15103 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104
15105 public final Paint paint;
15106 public final Matrix matrix;
15107 public Shader shader;
15108
Mike Cleronf116bf82009-09-27 19:14:12 -070015109 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
15110
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015111 private static final float[] OPAQUE = { 255 };
15112 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080015113
Mike Cleronf116bf82009-09-27 19:14:12 -070015114 /**
15115 * When fading should start. This time moves into the future every time
15116 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
15117 */
15118 public long fadeStartTime;
15119
15120
15121 /**
15122 * The current state of the scrollbars: ON, OFF, or FADING
15123 */
15124 public int state = OFF;
15125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 private int mLastColor;
15127
Mike Cleronf116bf82009-09-27 19:14:12 -070015128 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015129 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
15130 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070015131 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
15132 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133
15134 paint = new Paint();
15135 matrix = new Matrix();
15136 // use use a height of 1, and then wack the matrix each time we
15137 // actually use it.
15138 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140 paint.setShader(shader);
15141 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070015142 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015143 }
Romain Guy8506ab42009-06-11 17:35:47 -070015144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015145 public void setFadeColor(int color) {
15146 if (color != 0 && color != mLastColor) {
15147 mLastColor = color;
15148 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070015149
Romain Guye55e1a72009-08-27 10:42:26 -070015150 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
15151 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070015152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015153 paint.setShader(shader);
15154 // Restore the default transfer mode (src_over)
15155 paint.setXfermode(null);
15156 }
15157 }
Joe Malin32736f02011-01-19 16:14:20 -080015158
Mike Cleronf116bf82009-09-27 19:14:12 -070015159 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070015160 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070015161 if (now >= fadeStartTime) {
15162
15163 // the animation fades the scrollbars out by changing
15164 // the opacity (alpha) from fully opaque to fully
15165 // transparent
15166 int nextFrame = (int) now;
15167 int framesCount = 0;
15168
15169 Interpolator interpolator = scrollBarInterpolator;
15170
15171 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015172 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070015173
15174 // End transparent
15175 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080015176 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070015177
15178 state = FADING;
15179
15180 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080015181 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070015182 }
15183 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070015184 }
Mike Cleronf116bf82009-09-27 19:14:12 -070015185
Svetoslav Ganova0156172011-06-26 17:55:44 -070015186 /**
15187 * Resuable callback for sending
15188 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
15189 */
15190 private class SendViewScrolledAccessibilityEvent implements Runnable {
15191 public volatile boolean mIsPending;
15192
15193 public void run() {
15194 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
15195 mIsPending = false;
15196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015197 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015198
15199 /**
15200 * <p>
15201 * This class represents a delegate that can be registered in a {@link View}
15202 * to enhance accessibility support via composition rather via inheritance.
15203 * It is specifically targeted to widget developers that extend basic View
15204 * classes i.e. classes in package android.view, that would like their
15205 * applications to be backwards compatible.
15206 * </p>
15207 * <p>
15208 * A scenario in which a developer would like to use an accessibility delegate
15209 * is overriding a method introduced in a later API version then the minimal API
15210 * version supported by the application. For example, the method
15211 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
15212 * in API version 4 when the accessibility APIs were first introduced. If a
15213 * developer would like his application to run on API version 4 devices (assuming
15214 * all other APIs used by the application are version 4 or lower) and take advantage
15215 * of this method, instead of overriding the method which would break the application's
15216 * backwards compatibility, he can override the corresponding method in this
15217 * delegate and register the delegate in the target View if the API version of
15218 * the system is high enough i.e. the API version is same or higher to the API
15219 * version that introduced
15220 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
15221 * </p>
15222 * <p>
15223 * Here is an example implementation:
15224 * </p>
15225 * <code><pre><p>
15226 * if (Build.VERSION.SDK_INT >= 14) {
15227 * // If the API version is equal of higher than the version in
15228 * // which onInitializeAccessibilityNodeInfo was introduced we
15229 * // register a delegate with a customized implementation.
15230 * View view = findViewById(R.id.view_id);
15231 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
15232 * public void onInitializeAccessibilityNodeInfo(View host,
15233 * AccessibilityNodeInfo info) {
15234 * // Let the default implementation populate the info.
15235 * super.onInitializeAccessibilityNodeInfo(host, info);
15236 * // Set some other information.
15237 * info.setEnabled(host.isEnabled());
15238 * }
15239 * });
15240 * }
15241 * </code></pre></p>
15242 * <p>
15243 * This delegate contains methods that correspond to the accessibility methods
15244 * in View. If a delegate has been specified the implementation in View hands
15245 * off handling to the corresponding method in this delegate. The default
15246 * implementation the delegate methods behaves exactly as the corresponding
15247 * method in View for the case of no accessibility delegate been set. Hence,
15248 * to customize the behavior of a View method, clients can override only the
15249 * corresponding delegate method without altering the behavior of the rest
15250 * accessibility related methods of the host view.
15251 * </p>
15252 */
15253 public static class AccessibilityDelegate {
15254
15255 /**
15256 * Sends an accessibility event of the given type. If accessibility is not
15257 * enabled this method has no effect.
15258 * <p>
15259 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
15260 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
15261 * been set.
15262 * </p>
15263 *
15264 * @param host The View hosting the delegate.
15265 * @param eventType The type of the event to send.
15266 *
15267 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
15268 */
15269 public void sendAccessibilityEvent(View host, int eventType) {
15270 host.sendAccessibilityEventInternal(eventType);
15271 }
15272
15273 /**
15274 * Sends an accessibility event. This method behaves exactly as
15275 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
15276 * empty {@link AccessibilityEvent} and does not perform a check whether
15277 * accessibility is enabled.
15278 * <p>
15279 * The default implementation behaves as
15280 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15281 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
15282 * the case of no accessibility delegate been set.
15283 * </p>
15284 *
15285 * @param host The View hosting the delegate.
15286 * @param event The event to send.
15287 *
15288 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15289 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
15290 */
15291 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
15292 host.sendAccessibilityEventUncheckedInternal(event);
15293 }
15294
15295 /**
15296 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
15297 * to its children for adding their text content to the event.
15298 * <p>
15299 * The default implementation behaves as
15300 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15301 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
15302 * the case of no accessibility delegate been set.
15303 * </p>
15304 *
15305 * @param host The View hosting the delegate.
15306 * @param event The event.
15307 * @return True if the event population was completed.
15308 *
15309 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15310 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
15311 */
15312 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15313 return host.dispatchPopulateAccessibilityEventInternal(event);
15314 }
15315
15316 /**
15317 * Gives a chance to the host View to populate the accessibility event with its
15318 * text content.
15319 * <p>
15320 * The default implementation behaves as
15321 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
15322 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
15323 * the case of no accessibility delegate been set.
15324 * </p>
15325 *
15326 * @param host The View hosting the delegate.
15327 * @param event The accessibility event which to populate.
15328 *
15329 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
15330 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
15331 */
15332 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
15333 host.onPopulateAccessibilityEventInternal(event);
15334 }
15335
15336 /**
15337 * Initializes an {@link AccessibilityEvent} with information about the
15338 * the host View which is the event source.
15339 * <p>
15340 * The default implementation behaves as
15341 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
15342 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
15343 * the case of no accessibility delegate been set.
15344 * </p>
15345 *
15346 * @param host The View hosting the delegate.
15347 * @param event The event to initialize.
15348 *
15349 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
15350 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
15351 */
15352 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
15353 host.onInitializeAccessibilityEventInternal(event);
15354 }
15355
15356 /**
15357 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
15358 * <p>
15359 * The default implementation behaves as
15360 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15361 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
15362 * the case of no accessibility delegate been set.
15363 * </p>
15364 *
15365 * @param host The View hosting the delegate.
15366 * @param info The instance to initialize.
15367 *
15368 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15369 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
15370 */
15371 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
15372 host.onInitializeAccessibilityNodeInfoInternal(info);
15373 }
15374
15375 /**
15376 * Called when a child of the host View has requested sending an
15377 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
15378 * to augment the event.
15379 * <p>
15380 * The default implementation behaves as
15381 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15382 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
15383 * the case of no accessibility delegate been set.
15384 * </p>
15385 *
15386 * @param host The View hosting the delegate.
15387 * @param child The child which requests sending the event.
15388 * @param event The event to be sent.
15389 * @return True if the event should be sent
15390 *
15391 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15392 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
15393 */
15394 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
15395 AccessibilityEvent event) {
15396 return host.onRequestSendAccessibilityEventInternal(child, event);
15397 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070015398
15399 /**
15400 * Gets the provider for managing a virtual view hierarchy rooted at this View
15401 * and reported to {@link android.accessibilityservice.AccessibilityService}s
15402 * that explore the window content.
15403 * <p>
15404 * The default implementation behaves as
15405 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
15406 * the case of no accessibility delegate been set.
15407 * </p>
15408 *
15409 * @return The provider.
15410 *
15411 * @see AccessibilityNodeProvider
15412 */
15413 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
15414 return null;
15415 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070015416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417}