blob: 49efea36b20816770456b8eb593fa76ba43219a1 [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;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * 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 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <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 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * 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 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700669 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001599 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002131 /**
2132 * Flag indicating that view has an animation set on it. This is used to track whether an
2133 * animation is cleared between successive frames, in order to tell the associated
2134 * DisplayList to clear its animation matrix.
2135 */
2136 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2137
Chet Haase1a3ab172012-05-11 08:41:20 -07002138 /**
2139 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2140 * is used to check whether later changes to the view's transform should invalidate the
2141 * view to force the quickReject test to run again.
2142 */
2143 static final int VIEW_QUICK_REJECTED = 0x20000000;
2144
Christopher Tate3d4bf172011-03-28 16:16:46 -07002145 /* End of masks for mPrivateFlags2 */
2146
2147 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2148
Chet Haasedaf98e92011-01-10 14:10:36 -08002149 /**
Adam Powell637d3372010-08-25 14:37:03 -07002150 * Always allow a user to over-scroll this view, provided it is a
2151 * view that can scroll.
2152 *
2153 * @see #getOverScrollMode()
2154 * @see #setOverScrollMode(int)
2155 */
2156 public static final int OVER_SCROLL_ALWAYS = 0;
2157
2158 /**
2159 * Allow a user to over-scroll this view only if the content is large
2160 * enough to meaningfully scroll, provided it is a view that can scroll.
2161 *
2162 * @see #getOverScrollMode()
2163 * @see #setOverScrollMode(int)
2164 */
2165 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2166
2167 /**
2168 * Never allow a user to over-scroll this view.
2169 *
2170 * @see #getOverScrollMode()
2171 * @see #setOverScrollMode(int)
2172 */
2173 public static final int OVER_SCROLL_NEVER = 2;
2174
2175 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002176 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2177 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002178 *
Joe Malin32736f02011-01-19 16:14:20 -08002179 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002180 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002181 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002182
2183 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002184 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2185 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002186 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002187 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002188 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002189 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002190 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002191 *
Joe Malin32736f02011-01-19 16:14:20 -08002192 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002193 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002194 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2195
2196 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002197 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2198 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002199 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002200 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002201 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2202 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2203 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002204 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002205 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2206 * window flags) for displaying content using every last pixel on the display.
2207 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002208 * <p>There is a limitation: because navigation controls are so important, the least user
2209 * interaction will cause them to reappear immediately. When this happens, both
2210 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2211 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002212 *
2213 * @see #setSystemUiVisibility(int)
2214 */
2215 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2216
2217 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002218 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2219 * into the normal fullscreen mode so that its content can take over the screen
2220 * while still allowing the user to interact with the application.
2221 *
2222 * <p>This has the same visual effect as
2223 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2224 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2225 * meaning that non-critical screen decorations (such as the status bar) will be
2226 * hidden while the user is in the View's window, focusing the experience on
2227 * that content. Unlike the window flag, if you are using ActionBar in
2228 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2229 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2230 * hide the action bar.
2231 *
2232 * <p>This approach to going fullscreen is best used over the window flag when
2233 * it is a transient state -- that is, the application does this at certain
2234 * points in its user interaction where it wants to allow the user to focus
2235 * on content, but not as a continuous state. For situations where the application
2236 * would like to simply stay full screen the entire time (such as a game that
2237 * wants to take over the screen), the
2238 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2239 * is usually a better approach. The state set here will be removed by the system
2240 * in various situations (such as the user moving to another application) like
2241 * the other system UI states.
2242 *
2243 * <p>When using this flag, the application should provide some easy facility
2244 * for the user to go out of it. A common example would be in an e-book
2245 * reader, where tapping on the screen brings back whatever screen and UI
2246 * decorations that had been hidden while the user was immersed in reading
2247 * the book.
2248 *
2249 * @see #setSystemUiVisibility(int)
2250 */
2251 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2252
2253 /**
2254 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2255 * flags, we would like a stable view of the content insets given to
2256 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2257 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002258 * as a continuous state. In the stock Android UI this is the space for
2259 * the system bar, nav bar, and status bar, but not more transient elements
2260 * such as an input method.
2261 *
2262 * The stable layout your UI sees is based on the system UI modes you can
2263 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2264 * then you will get a stable layout for changes of the
2265 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2266 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2267 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2268 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2269 * with a stable layout. (Note that you should avoid using
2270 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2271 *
2272 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2273 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2274 * then a hidden status bar will be considered a "stable" state for purposes
2275 * here. This allows your UI to continually hide the status bar, while still
2276 * using the system UI flags to hide the action bar while still retaining
2277 * a stable layout. Note that changing the window fullscreen flag will never
2278 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002279 *
2280 * <p>If you are using ActionBar in
2281 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2282 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2283 * insets it adds to those given to the application.
2284 */
2285 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2286
2287 /**
2288 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2289 * to be layed out as if it has requested
2290 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2291 * allows it to avoid artifacts when switching in and out of that mode, at
2292 * the expense that some of its user interface may be covered by screen
2293 * decorations when they are shown. You can perform layout of your inner
2294 * UI elements to account for the navagation system UI through the
2295 * {@link #fitSystemWindows(Rect)} method.
2296 */
2297 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2298
2299 /**
2300 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2301 * to be layed out as if it has requested
2302 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2303 * allows it to avoid artifacts when switching in and out of that mode, at
2304 * the expense that some of its user interface may be covered by screen
2305 * decorations when they are shown. You can perform layout of your inner
2306 * UI elements to account for non-fullscreen system UI through the
2307 * {@link #fitSystemWindows(Rect)} method.
2308 */
2309 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2310
2311 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002312 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2313 */
2314 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2315
2316 /**
2317 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2318 */
2319 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002320
2321 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002322 * @hide
2323 *
2324 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2325 * out of the public fields to keep the undefined bits out of the developer's way.
2326 *
2327 * Flag to make the status bar not expandable. Unless you also
2328 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2329 */
2330 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2331
2332 /**
2333 * @hide
2334 *
2335 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2336 * out of the public fields to keep the undefined bits out of the developer's way.
2337 *
2338 * Flag to hide notification icons and scrolling ticker text.
2339 */
2340 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2341
2342 /**
2343 * @hide
2344 *
2345 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2346 * out of the public fields to keep the undefined bits out of the developer's way.
2347 *
2348 * Flag to disable incoming notification alerts. This will not block
2349 * icons, but it will block sound, vibrating and other visual or aural notifications.
2350 */
2351 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2352
2353 /**
2354 * @hide
2355 *
2356 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2357 * out of the public fields to keep the undefined bits out of the developer's way.
2358 *
2359 * Flag to hide only the scrolling ticker. Note that
2360 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2361 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2362 */
2363 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
2371 * Flag to hide the center system info area.
2372 */
2373 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2374
2375 /**
2376 * @hide
2377 *
2378 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2379 * out of the public fields to keep the undefined bits out of the developer's way.
2380 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002381 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002382 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2383 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002384 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002385
2386 /**
2387 * @hide
2388 *
2389 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2390 * out of the public fields to keep the undefined bits out of the developer's way.
2391 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002392 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002393 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2394 */
2395 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2396
2397 /**
2398 * @hide
2399 *
2400 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2401 * out of the public fields to keep the undefined bits out of the developer's way.
2402 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002403 * Flag to hide only the clock. You might use this if your activity has
2404 * its own clock making the status bar's clock redundant.
2405 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002406 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2407
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002408 /**
2409 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002410 *
2411 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2412 * out of the public fields to keep the undefined bits out of the developer's way.
2413 *
2414 * Flag to hide only the recent apps button. Don't use this
2415 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2416 */
2417 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2418
2419 /**
2420 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002421 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002422 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002423
2424 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002425 * These are the system UI flags that can be cleared by events outside
2426 * of an application. Currently this is just the ability to tap on the
2427 * screen while hiding the navigation bar to have it return.
2428 * @hide
2429 */
2430 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002431 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2432 | SYSTEM_UI_FLAG_FULLSCREEN;
2433
2434 /**
2435 * Flags that can impact the layout in relation to system UI.
2436 */
2437 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2438 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2439 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002440
2441 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002442 * Find views that render the specified text.
2443 *
2444 * @see #findViewsWithText(ArrayList, CharSequence, int)
2445 */
2446 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2447
2448 /**
2449 * Find find views that contain the specified content description.
2450 *
2451 * @see #findViewsWithText(ArrayList, CharSequence, int)
2452 */
2453 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2454
2455 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002456 * Find views that contain {@link AccessibilityNodeProvider}. Such
2457 * a View is a root of virtual view hierarchy and may contain the searched
2458 * text. If this flag is set Views with providers are automatically
2459 * added and it is a responsibility of the client to call the APIs of
2460 * the provider to determine whether the virtual tree rooted at this View
2461 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2462 * represeting the virtual views with this text.
2463 *
2464 * @see #findViewsWithText(ArrayList, CharSequence, int)
2465 *
2466 * @hide
2467 */
2468 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2469
2470 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002471 * The undefined cursor position.
2472 */
2473 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2474
2475 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002476 * Indicates that the screen has changed state and is now off.
2477 *
2478 * @see #onScreenStateChanged(int)
2479 */
2480 public static final int SCREEN_STATE_OFF = 0x0;
2481
2482 /**
2483 * Indicates that the screen has changed state and is now on.
2484 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002485 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002486 */
2487 public static final int SCREEN_STATE_ON = 0x1;
2488
2489 /**
Adam Powell637d3372010-08-25 14:37:03 -07002490 * Controls the over-scroll mode for this view.
2491 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2492 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2493 * and {@link #OVER_SCROLL_NEVER}.
2494 */
2495 private int mOverScrollMode;
2496
2497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 * The parent this view is attached to.
2499 * {@hide}
2500 *
2501 * @see #getParent()
2502 */
2503 protected ViewParent mParent;
2504
2505 /**
2506 * {@hide}
2507 */
2508 AttachInfo mAttachInfo;
2509
2510 /**
2511 * {@hide}
2512 */
Romain Guy809a7f62009-05-14 15:44:42 -07002513 @ViewDebug.ExportedProperty(flagMapping = {
2514 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2515 name = "FORCE_LAYOUT"),
2516 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2517 name = "LAYOUT_REQUIRED"),
2518 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002519 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002520 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2521 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2522 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2523 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2524 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002526 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527
2528 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002529 * This view's request for the visibility of the status bar.
2530 * @hide
2531 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002532 @ViewDebug.ExportedProperty(flagMapping = {
2533 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2534 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2535 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2536 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2537 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2538 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2539 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2540 equals = SYSTEM_UI_FLAG_VISIBLE,
2541 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2542 })
Joe Onorato664644d2011-01-23 17:53:23 -08002543 int mSystemUiVisibility;
2544
2545 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002546 * Reference count for transient state.
2547 * @see #setHasTransientState(boolean)
2548 */
2549 int mTransientStateCount = 0;
2550
2551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 * Count of how many windows this view has been attached to.
2553 */
2554 int mWindowAttachCount;
2555
2556 /**
2557 * The layout parameters associated with this view and used by the parent
2558 * {@link android.view.ViewGroup} to determine how this view should be
2559 * laid out.
2560 * {@hide}
2561 */
2562 protected ViewGroup.LayoutParams mLayoutParams;
2563
2564 /**
2565 * The view flags hold various views states.
2566 * {@hide}
2567 */
2568 @ViewDebug.ExportedProperty
2569 int mViewFlags;
2570
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002571 static class TransformationInfo {
2572 /**
2573 * The transform matrix for the View. This transform is calculated internally
2574 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2575 * is used by default. Do *not* use this variable directly; instead call
2576 * getMatrix(), which will automatically recalculate the matrix if necessary
2577 * to get the correct matrix based on the latest rotation and scale properties.
2578 */
2579 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002580
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002581 /**
2582 * The transform matrix for the View. This transform is calculated internally
2583 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2584 * is used by default. Do *not* use this variable directly; instead call
2585 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2586 * to get the correct matrix based on the latest rotation and scale properties.
2587 */
2588 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002589
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002590 /**
2591 * An internal variable that tracks whether we need to recalculate the
2592 * transform matrix, based on whether the rotation or scaleX/Y properties
2593 * have changed since the matrix was last calculated.
2594 */
2595 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002596
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002597 /**
2598 * An internal variable that tracks whether we need to recalculate the
2599 * transform matrix, based on whether the rotation or scaleX/Y properties
2600 * have changed since the matrix was last calculated.
2601 */
2602 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002603
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002604 /**
2605 * A variable that tracks whether we need to recalculate the
2606 * transform matrix, based on whether the rotation or scaleX/Y properties
2607 * have changed since the matrix was last calculated. This variable
2608 * is only valid after a call to updateMatrix() or to a function that
2609 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2610 */
2611 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002612
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002613 /**
2614 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2615 */
2616 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002618 /**
2619 * This matrix is used when computing the matrix for 3D rotations.
2620 */
2621 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002622
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002623 /**
2624 * These prev values are used to recalculate a centered pivot point when necessary. The
2625 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2626 * set), so thes values are only used then as well.
2627 */
2628 private int mPrevWidth = -1;
2629 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002630
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002631 /**
2632 * The degrees rotation around the vertical axis through the pivot point.
2633 */
2634 @ViewDebug.ExportedProperty
2635 float mRotationY = 0f;
2636
2637 /**
2638 * The degrees rotation around the horizontal axis through the pivot point.
2639 */
2640 @ViewDebug.ExportedProperty
2641 float mRotationX = 0f;
2642
2643 /**
2644 * The degrees rotation around the pivot point.
2645 */
2646 @ViewDebug.ExportedProperty
2647 float mRotation = 0f;
2648
2649 /**
2650 * The amount of translation of the object away from its left property (post-layout).
2651 */
2652 @ViewDebug.ExportedProperty
2653 float mTranslationX = 0f;
2654
2655 /**
2656 * The amount of translation of the object away from its top property (post-layout).
2657 */
2658 @ViewDebug.ExportedProperty
2659 float mTranslationY = 0f;
2660
2661 /**
2662 * The amount of scale in the x direction around the pivot point. A
2663 * value of 1 means no scaling is applied.
2664 */
2665 @ViewDebug.ExportedProperty
2666 float mScaleX = 1f;
2667
2668 /**
2669 * The amount of scale in the y direction around the pivot point. A
2670 * value of 1 means no scaling is applied.
2671 */
2672 @ViewDebug.ExportedProperty
2673 float mScaleY = 1f;
2674
2675 /**
Chet Haasea33de552012-02-03 16:28:24 -08002676 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002677 */
2678 @ViewDebug.ExportedProperty
2679 float mPivotX = 0f;
2680
2681 /**
Chet Haasea33de552012-02-03 16:28:24 -08002682 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002683 */
2684 @ViewDebug.ExportedProperty
2685 float mPivotY = 0f;
2686
2687 /**
2688 * The opacity of the View. This is a value from 0 to 1, where 0 means
2689 * completely transparent and 1 means completely opaque.
2690 */
2691 @ViewDebug.ExportedProperty
2692 float mAlpha = 1f;
2693 }
2694
2695 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002696
Joe Malin32736f02011-01-19 16:14:20 -08002697 private boolean mLastIsOpaque;
2698
Chet Haasefd2b0022010-08-06 13:08:56 -07002699 /**
2700 * Convenience value to check for float values that are close enough to zero to be considered
2701 * zero.
2702 */
Romain Guy2542d192010-08-18 11:47:12 -07002703 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002704
2705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 * The distance in pixels from the left edge of this view's parent
2707 * to the left edge of this view.
2708 * {@hide}
2709 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002710 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 protected int mLeft;
2712 /**
2713 * The distance in pixels from the left edge of this view's parent
2714 * to the right edge of this view.
2715 * {@hide}
2716 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002717 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 protected int mRight;
2719 /**
2720 * The distance in pixels from the top edge of this view's parent
2721 * to the top edge of this view.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mTop;
2726 /**
2727 * The distance in pixels from the top edge of this view's parent
2728 * to the bottom edge of this view.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mBottom;
2733
2734 /**
2735 * The offset, in pixels, by which the content of this view is scrolled
2736 * horizontally.
2737 * {@hide}
2738 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002739 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 protected int mScrollX;
2741 /**
2742 * The offset, in pixels, by which the content of this view is scrolled
2743 * vertically.
2744 * {@hide}
2745 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002746 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 protected int mScrollY;
2748
2749 /**
2750 * The left padding in pixels, that is the distance in pixels between the
2751 * left edge of this view and the left edge of its content.
2752 * {@hide}
2753 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002754 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 protected int mPaddingLeft;
2756 /**
2757 * The right padding in pixels, that is the distance in pixels between the
2758 * right edge of this view and the right edge of its content.
2759 * {@hide}
2760 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002761 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 protected int mPaddingRight;
2763 /**
2764 * The top padding in pixels, that is the distance in pixels between the
2765 * top edge of this view and the top edge of its content.
2766 * {@hide}
2767 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002768 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769 protected int mPaddingTop;
2770 /**
2771 * The bottom padding in pixels, that is the distance in pixels between the
2772 * bottom edge of this view and the bottom edge of its content.
2773 * {@hide}
2774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002775 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 protected int mPaddingBottom;
2777
2778 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002779 * The layout insets in pixels, that is the distance in pixels between the
2780 * visible edges of this view its bounds.
2781 */
2782 private Insets mLayoutInsets;
2783
2784 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002785 * Briefly describes the view and is primarily used for accessibility support.
2786 */
2787 private CharSequence mContentDescription;
2788
2789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002791 *
2792 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002794 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002795 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
2797 /**
2798 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002799 *
2800 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002802 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002803 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002805 /**
Adam Powell20232d02010-12-08 21:08:53 -08002806 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002807 *
2808 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002809 */
2810 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002811 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002812
2813 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002814 * Cache if the user padding is relative.
2815 *
2816 */
2817 @ViewDebug.ExportedProperty(category = "padding")
2818 boolean mUserPaddingRelative;
2819
2820 /**
2821 * Cache the paddingStart set by the user to append to the scrollbar's size.
2822 *
2823 */
2824 @ViewDebug.ExportedProperty(category = "padding")
2825 int mUserPaddingStart;
2826
2827 /**
2828 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2829 *
2830 */
2831 @ViewDebug.ExportedProperty(category = "padding")
2832 int mUserPaddingEnd;
2833
2834 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002835 * @hide
2836 */
2837 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2838 /**
2839 * @hide
2840 */
2841 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Philip Milne6c8ea062012-04-03 17:38:43 -07002843 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844
2845 private int mBackgroundResource;
2846 private boolean mBackgroundSizeChanged;
2847
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002848 static class ListenerInfo {
2849 /**
2850 * Listener used to dispatch focus change events.
2851 * This field should be made private, so it is hidden from the SDK.
2852 * {@hide}
2853 */
2854 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002856 /**
2857 * Listeners for layout change events.
2858 */
2859 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002860
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002861 /**
2862 * Listeners for attach events.
2863 */
2864 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002865
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002866 /**
2867 * Listener used to dispatch click events.
2868 * This field should be made private, so it is hidden from the SDK.
2869 * {@hide}
2870 */
2871 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002873 /**
2874 * Listener used to dispatch long click events.
2875 * This field should be made private, so it is hidden from the SDK.
2876 * {@hide}
2877 */
2878 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002880 /**
2881 * Listener used to build the context menu.
2882 * This field should be made private, so it is hidden from the SDK.
2883 * {@hide}
2884 */
2885 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002887 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002889 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002891 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002892
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002893 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002894
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002895 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002896
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002897 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2898 }
2899
2900 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 /**
2903 * The application environment this view lives in.
2904 * This field should be made private, so it is hidden from the SDK.
2905 * {@hide}
2906 */
2907 protected Context mContext;
2908
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002909 private final Resources mResources;
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 private ScrollabilityCache mScrollCache;
2912
2913 private int[] mDrawableState = null;
2914
Romain Guy0211a0a2011-02-14 16:34:59 -08002915 /**
2916 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002917 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002918 * @hide
2919 */
2920 public boolean mCachingFailed;
2921
Romain Guy02890fd2010-08-06 17:58:44 -07002922 private Bitmap mDrawingCache;
2923 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002924 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002925 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926
2927 /**
2928 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2929 * the user may specify which view to go to next.
2930 */
2931 private int mNextFocusLeftId = View.NO_ID;
2932
2933 /**
2934 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2935 * the user may specify which view to go to next.
2936 */
2937 private int mNextFocusRightId = View.NO_ID;
2938
2939 /**
2940 * When this view has focus and the next focus is {@link #FOCUS_UP},
2941 * the user may specify which view to go to next.
2942 */
2943 private int mNextFocusUpId = View.NO_ID;
2944
2945 /**
2946 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2947 * the user may specify which view to go to next.
2948 */
2949 private int mNextFocusDownId = View.NO_ID;
2950
Jeff Brown4e6319b2010-12-13 10:36:51 -08002951 /**
2952 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2953 * the user may specify which view to go to next.
2954 */
2955 int mNextFocusForwardId = View.NO_ID;
2956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002958 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002959 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002960 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 private UnsetPressedState mUnsetPressedState;
2963
2964 /**
2965 * Whether the long press's action has been invoked. The tap's action is invoked on the
2966 * up event while a long press is invoked as soon as the long press duration is reached, so
2967 * a long press could be performed before the tap is checked, in which case the tap's action
2968 * should not be invoked.
2969 */
2970 private boolean mHasPerformedLongPress;
2971
2972 /**
2973 * The minimum height of the view. We'll try our best to have the height
2974 * of this view to at least this amount.
2975 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002976 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 private int mMinHeight;
2978
2979 /**
2980 * The minimum width of the view. We'll try our best to have the width
2981 * of this view to at least this amount.
2982 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002983 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 private int mMinWidth;
2985
2986 /**
2987 * The delegate to handle touch events that are physically in this view
2988 * but should be handled by another view.
2989 */
2990 private TouchDelegate mTouchDelegate = null;
2991
2992 /**
2993 * Solid color to use as a background when creating the drawing cache. Enables
2994 * the cache to use 16 bit bitmaps instead of 32 bit.
2995 */
2996 private int mDrawingCacheBackgroundColor = 0;
2997
2998 /**
2999 * Special tree observer used when mAttachInfo is null.
3000 */
3001 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003002
Adam Powelle14579b2009-12-16 18:39:52 -08003003 /**
3004 * Cache the touch slop from the context that created the view.
3005 */
3006 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003009 * Object that handles automatic animation of view properties.
3010 */
3011 private ViewPropertyAnimator mAnimator = null;
3012
3013 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003014 * Flag indicating that a drag can cross window boundaries. When
3015 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3016 * with this flag set, all visible applications will be able to participate
3017 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003018 *
3019 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003020 */
3021 public static final int DRAG_FLAG_GLOBAL = 1;
3022
3023 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003024 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3025 */
3026 private float mVerticalScrollFactor;
3027
3028 /**
Adam Powell20232d02010-12-08 21:08:53 -08003029 * Position of the vertical scroll bar.
3030 */
3031 private int mVerticalScrollbarPosition;
3032
3033 /**
3034 * Position the scroll bar at the default position as determined by the system.
3035 */
3036 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3037
3038 /**
3039 * Position the scroll bar along the left edge.
3040 */
3041 public static final int SCROLLBAR_POSITION_LEFT = 1;
3042
3043 /**
3044 * Position the scroll bar along the right edge.
3045 */
3046 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3047
3048 /**
Romain Guy171c5922011-01-06 10:04:23 -08003049 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003050 *
3051 * @see #getLayerType()
3052 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003053 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003054 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003055 */
3056 public static final int LAYER_TYPE_NONE = 0;
3057
3058 /**
3059 * <p>Indicates that the view has a software layer. A software layer is backed
3060 * by a bitmap and causes the view to be rendered using Android's software
3061 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003062 *
Romain Guy171c5922011-01-06 10:04:23 -08003063 * <p>Software layers have various usages:</p>
3064 * <p>When the application is not using hardware acceleration, a software layer
3065 * is useful to apply a specific color filter and/or blending mode and/or
3066 * translucency to a view and all its children.</p>
3067 * <p>When the application is using hardware acceleration, a software layer
3068 * is useful to render drawing primitives not supported by the hardware
3069 * accelerated pipeline. It can also be used to cache a complex view tree
3070 * into a texture and reduce the complexity of drawing operations. For instance,
3071 * when animating a complex view tree with a translation, a software layer can
3072 * be used to render the view tree only once.</p>
3073 * <p>Software layers should be avoided when the affected view tree updates
3074 * often. Every update will require to re-render the software layer, which can
3075 * potentially be slow (particularly when hardware acceleration is turned on
3076 * since the layer will have to be uploaded into a hardware texture after every
3077 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003078 *
3079 * @see #getLayerType()
3080 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003081 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003082 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003083 */
3084 public static final int LAYER_TYPE_SOFTWARE = 1;
3085
3086 /**
3087 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3088 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3089 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3090 * rendering pipeline, but only if hardware acceleration is turned on for the
3091 * view hierarchy. When hardware acceleration is turned off, hardware layers
3092 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003093 *
Romain Guy171c5922011-01-06 10:04:23 -08003094 * <p>A hardware layer is useful to apply a specific color filter and/or
3095 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003096 * <p>A hardware layer can be used to cache a complex view tree into a
3097 * texture and reduce the complexity of drawing operations. For instance,
3098 * when animating a complex view tree with a translation, a hardware layer can
3099 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003100 * <p>A hardware layer can also be used to increase the rendering quality when
3101 * rotation transformations are applied on a view. It can also be used to
3102 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003103 *
3104 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003105 * @see #setLayerType(int, android.graphics.Paint)
3106 * @see #LAYER_TYPE_NONE
3107 * @see #LAYER_TYPE_SOFTWARE
3108 */
3109 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003110
Romain Guy3aaff3a2011-01-12 14:18:47 -08003111 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3112 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3113 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3114 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3115 })
Romain Guy171c5922011-01-06 10:04:23 -08003116 int mLayerType = LAYER_TYPE_NONE;
3117 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003118 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003119
3120 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003121 * Set to true when the view is sending hover accessibility events because it
3122 * is the innermost hovered view.
3123 */
3124 private boolean mSendingHoverAccessibilityEvents;
3125
3126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 * Simple constructor to use when creating a view from code.
3128 *
3129 * @param context The Context the view is running in, through which it can
3130 * access the current theme, resources, etc.
3131 */
3132 public View(Context context) {
3133 mContext = context;
3134 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003135 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003136 // Set layout and text direction defaults
3137 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003138 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003139 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3140 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003141 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003142 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003143 mUserPaddingStart = -1;
3144 mUserPaddingEnd = -1;
3145 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 }
3147
3148 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003149 * Delegate for injecting accessiblity functionality.
3150 */
3151 AccessibilityDelegate mAccessibilityDelegate;
3152
3153 /**
3154 * Consistency verifier for debugging purposes.
3155 * @hide
3156 */
3157 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3158 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3159 new InputEventConsistencyVerifier(this, 0) : null;
3160
3161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 * Constructor that is called when inflating a view from XML. This is called
3163 * when a view is being constructed from an XML file, supplying attributes
3164 * that were specified in the XML file. This version uses a default style of
3165 * 0, so the only attribute values applied are those in the Context's Theme
3166 * and the given AttributeSet.
3167 *
3168 * <p>
3169 * The method onFinishInflate() will be called after all children have been
3170 * added.
3171 *
3172 * @param context The Context the view is running in, through which it can
3173 * access the current theme, resources, etc.
3174 * @param attrs The attributes of the XML tag that is inflating the view.
3175 * @see #View(Context, AttributeSet, int)
3176 */
3177 public View(Context context, AttributeSet attrs) {
3178 this(context, attrs, 0);
3179 }
3180
3181 /**
3182 * Perform inflation from XML and apply a class-specific base style. This
3183 * constructor of View allows subclasses to use their own base style when
3184 * they are inflating. For example, a Button class's constructor would call
3185 * this version of the super class constructor and supply
3186 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3187 * the theme's button style to modify all of the base view attributes (in
3188 * particular its background) as well as the Button class's attributes.
3189 *
3190 * @param context The Context the view is running in, through which it can
3191 * access the current theme, resources, etc.
3192 * @param attrs The attributes of the XML tag that is inflating the view.
3193 * @param defStyle The default style to apply to this view. If 0, no style
3194 * will be applied (beyond what is included in the theme). This may
3195 * either be an attribute resource, whose value will be retrieved
3196 * from the current theme, or an explicit style resource.
3197 * @see #View(Context, AttributeSet)
3198 */
3199 public View(Context context, AttributeSet attrs, int defStyle) {
3200 this(context);
3201
3202 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3203 defStyle, 0);
3204
3205 Drawable background = null;
3206
3207 int leftPadding = -1;
3208 int topPadding = -1;
3209 int rightPadding = -1;
3210 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003211 int startPadding = -1;
3212 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213
3214 int padding = -1;
3215
3216 int viewFlagValues = 0;
3217 int viewFlagMasks = 0;
3218
3219 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 int x = 0;
3222 int y = 0;
3223
Chet Haase73066682010-11-29 15:55:32 -08003224 float tx = 0;
3225 float ty = 0;
3226 float rotation = 0;
3227 float rotationX = 0;
3228 float rotationY = 0;
3229 float sx = 1f;
3230 float sy = 1f;
3231 boolean transformSet = false;
3232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3234
Adam Powell637d3372010-08-25 14:37:03 -07003235 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 final int N = a.getIndexCount();
3237 for (int i = 0; i < N; i++) {
3238 int attr = a.getIndex(i);
3239 switch (attr) {
3240 case com.android.internal.R.styleable.View_background:
3241 background = a.getDrawable(attr);
3242 break;
3243 case com.android.internal.R.styleable.View_padding:
3244 padding = a.getDimensionPixelSize(attr, -1);
3245 break;
3246 case com.android.internal.R.styleable.View_paddingLeft:
3247 leftPadding = a.getDimensionPixelSize(attr, -1);
3248 break;
3249 case com.android.internal.R.styleable.View_paddingTop:
3250 topPadding = a.getDimensionPixelSize(attr, -1);
3251 break;
3252 case com.android.internal.R.styleable.View_paddingRight:
3253 rightPadding = a.getDimensionPixelSize(attr, -1);
3254 break;
3255 case com.android.internal.R.styleable.View_paddingBottom:
3256 bottomPadding = a.getDimensionPixelSize(attr, -1);
3257 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003258 case com.android.internal.R.styleable.View_paddingStart:
3259 startPadding = a.getDimensionPixelSize(attr, -1);
3260 break;
3261 case com.android.internal.R.styleable.View_paddingEnd:
3262 endPadding = a.getDimensionPixelSize(attr, -1);
3263 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 case com.android.internal.R.styleable.View_scrollX:
3265 x = a.getDimensionPixelOffset(attr, 0);
3266 break;
3267 case com.android.internal.R.styleable.View_scrollY:
3268 y = a.getDimensionPixelOffset(attr, 0);
3269 break;
Chet Haase73066682010-11-29 15:55:32 -08003270 case com.android.internal.R.styleable.View_alpha:
3271 setAlpha(a.getFloat(attr, 1f));
3272 break;
3273 case com.android.internal.R.styleable.View_transformPivotX:
3274 setPivotX(a.getDimensionPixelOffset(attr, 0));
3275 break;
3276 case com.android.internal.R.styleable.View_transformPivotY:
3277 setPivotY(a.getDimensionPixelOffset(attr, 0));
3278 break;
3279 case com.android.internal.R.styleable.View_translationX:
3280 tx = a.getDimensionPixelOffset(attr, 0);
3281 transformSet = true;
3282 break;
3283 case com.android.internal.R.styleable.View_translationY:
3284 ty = a.getDimensionPixelOffset(attr, 0);
3285 transformSet = true;
3286 break;
3287 case com.android.internal.R.styleable.View_rotation:
3288 rotation = a.getFloat(attr, 0);
3289 transformSet = true;
3290 break;
3291 case com.android.internal.R.styleable.View_rotationX:
3292 rotationX = a.getFloat(attr, 0);
3293 transformSet = true;
3294 break;
3295 case com.android.internal.R.styleable.View_rotationY:
3296 rotationY = a.getFloat(attr, 0);
3297 transformSet = true;
3298 break;
3299 case com.android.internal.R.styleable.View_scaleX:
3300 sx = a.getFloat(attr, 1f);
3301 transformSet = true;
3302 break;
3303 case com.android.internal.R.styleable.View_scaleY:
3304 sy = a.getFloat(attr, 1f);
3305 transformSet = true;
3306 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 case com.android.internal.R.styleable.View_id:
3308 mID = a.getResourceId(attr, NO_ID);
3309 break;
3310 case com.android.internal.R.styleable.View_tag:
3311 mTag = a.getText(attr);
3312 break;
3313 case com.android.internal.R.styleable.View_fitsSystemWindows:
3314 if (a.getBoolean(attr, false)) {
3315 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3316 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3317 }
3318 break;
3319 case com.android.internal.R.styleable.View_focusable:
3320 if (a.getBoolean(attr, false)) {
3321 viewFlagValues |= FOCUSABLE;
3322 viewFlagMasks |= FOCUSABLE_MASK;
3323 }
3324 break;
3325 case com.android.internal.R.styleable.View_focusableInTouchMode:
3326 if (a.getBoolean(attr, false)) {
3327 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3328 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3329 }
3330 break;
3331 case com.android.internal.R.styleable.View_clickable:
3332 if (a.getBoolean(attr, false)) {
3333 viewFlagValues |= CLICKABLE;
3334 viewFlagMasks |= CLICKABLE;
3335 }
3336 break;
3337 case com.android.internal.R.styleable.View_longClickable:
3338 if (a.getBoolean(attr, false)) {
3339 viewFlagValues |= LONG_CLICKABLE;
3340 viewFlagMasks |= LONG_CLICKABLE;
3341 }
3342 break;
3343 case com.android.internal.R.styleable.View_saveEnabled:
3344 if (!a.getBoolean(attr, true)) {
3345 viewFlagValues |= SAVE_DISABLED;
3346 viewFlagMasks |= SAVE_DISABLED_MASK;
3347 }
3348 break;
3349 case com.android.internal.R.styleable.View_duplicateParentState:
3350 if (a.getBoolean(attr, false)) {
3351 viewFlagValues |= DUPLICATE_PARENT_STATE;
3352 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3353 }
3354 break;
3355 case com.android.internal.R.styleable.View_visibility:
3356 final int visibility = a.getInt(attr, 0);
3357 if (visibility != 0) {
3358 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3359 viewFlagMasks |= VISIBILITY_MASK;
3360 }
3361 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003362 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003363 // Clear any layout direction flags (included resolved bits) already set
3364 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3365 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003366 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003367 final int value = (layoutDirection != -1) ?
3368 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3369 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003370 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 case com.android.internal.R.styleable.View_drawingCacheQuality:
3372 final int cacheQuality = a.getInt(attr, 0);
3373 if (cacheQuality != 0) {
3374 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3375 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3376 }
3377 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003378 case com.android.internal.R.styleable.View_contentDescription:
3379 mContentDescription = a.getString(attr);
3380 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3382 if (!a.getBoolean(attr, true)) {
3383 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3384 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3385 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003386 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3388 if (!a.getBoolean(attr, true)) {
3389 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3390 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3391 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003392 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 case R.styleable.View_scrollbars:
3394 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3395 if (scrollbars != SCROLLBARS_NONE) {
3396 viewFlagValues |= scrollbars;
3397 viewFlagMasks |= SCROLLBARS_MASK;
3398 initializeScrollbars(a);
3399 }
3400 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003401 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003403 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3404 // Ignore the attribute starting with ICS
3405 break;
3406 }
3407 // With builds < ICS, fall through and apply fading edges
3408 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3410 if (fadingEdge != FADING_EDGE_NONE) {
3411 viewFlagValues |= fadingEdge;
3412 viewFlagMasks |= FADING_EDGE_MASK;
3413 initializeFadingEdge(a);
3414 }
3415 break;
3416 case R.styleable.View_scrollbarStyle:
3417 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3418 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3419 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3420 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3421 }
3422 break;
3423 case R.styleable.View_isScrollContainer:
3424 setScrollContainer = true;
3425 if (a.getBoolean(attr, false)) {
3426 setScrollContainer(true);
3427 }
3428 break;
3429 case com.android.internal.R.styleable.View_keepScreenOn:
3430 if (a.getBoolean(attr, false)) {
3431 viewFlagValues |= KEEP_SCREEN_ON;
3432 viewFlagMasks |= KEEP_SCREEN_ON;
3433 }
3434 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003435 case R.styleable.View_filterTouchesWhenObscured:
3436 if (a.getBoolean(attr, false)) {
3437 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3438 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3439 }
3440 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 case R.styleable.View_nextFocusLeft:
3442 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3443 break;
3444 case R.styleable.View_nextFocusRight:
3445 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3446 break;
3447 case R.styleable.View_nextFocusUp:
3448 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3449 break;
3450 case R.styleable.View_nextFocusDown:
3451 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3452 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003453 case R.styleable.View_nextFocusForward:
3454 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3455 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 case R.styleable.View_minWidth:
3457 mMinWidth = a.getDimensionPixelSize(attr, 0);
3458 break;
3459 case R.styleable.View_minHeight:
3460 mMinHeight = a.getDimensionPixelSize(attr, 0);
3461 break;
Romain Guy9a817362009-05-01 10:57:14 -07003462 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003463 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003464 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003465 + "be used within a restricted context");
3466 }
3467
Romain Guy9a817362009-05-01 10:57:14 -07003468 final String handlerName = a.getString(attr);
3469 if (handlerName != null) {
3470 setOnClickListener(new OnClickListener() {
3471 private Method mHandler;
3472
3473 public void onClick(View v) {
3474 if (mHandler == null) {
3475 try {
3476 mHandler = getContext().getClass().getMethod(handlerName,
3477 View.class);
3478 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003479 int id = getId();
3480 String idText = id == NO_ID ? "" : " with id '"
3481 + getContext().getResources().getResourceEntryName(
3482 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003483 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003484 handlerName + "(View) in the activity "
3485 + getContext().getClass() + " for onClick handler"
3486 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003487 }
3488 }
3489
3490 try {
3491 mHandler.invoke(getContext(), View.this);
3492 } catch (IllegalAccessException e) {
3493 throw new IllegalStateException("Could not execute non "
3494 + "public method of the activity", e);
3495 } catch (InvocationTargetException e) {
3496 throw new IllegalStateException("Could not execute "
3497 + "method of the activity", e);
3498 }
3499 }
3500 });
3501 }
3502 break;
Adam Powell637d3372010-08-25 14:37:03 -07003503 case R.styleable.View_overScrollMode:
3504 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3505 break;
Adam Powell20232d02010-12-08 21:08:53 -08003506 case R.styleable.View_verticalScrollbarPosition:
3507 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3508 break;
Romain Guy171c5922011-01-06 10:04:23 -08003509 case R.styleable.View_layerType:
3510 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3511 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003512 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003513 // Clear any text direction flag already set
3514 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3515 // Set the text direction flags depending on the value of the attribute
3516 final int textDirection = a.getInt(attr, -1);
3517 if (textDirection != -1) {
3518 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3519 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003520 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003521 case R.styleable.View_textAlignment:
3522 // Clear any text alignment flag already set
3523 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3524 // Set the text alignment flag depending on the value of the attribute
3525 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3526 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3527 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003528 case R.styleable.View_importantForAccessibility:
3529 setImportantForAccessibility(a.getInt(attr,
3530 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 }
3532 }
3533
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003534 a.recycle();
3535
Adam Powell637d3372010-08-25 14:37:03 -07003536 setOverScrollMode(overScrollMode);
3537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003539 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 }
3541
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003542 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3543 // layout direction). Those cached values will be used later during padding resolution.
3544 mUserPaddingStart = startPadding;
3545 mUserPaddingEnd = endPadding;
3546
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003547 updateUserPaddingRelative();
3548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 if (padding >= 0) {
3550 leftPadding = padding;
3551 topPadding = padding;
3552 rightPadding = padding;
3553 bottomPadding = padding;
3554 }
3555
3556 // If the user specified the padding (either with android:padding or
3557 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3558 // use the default padding or the padding from the background drawable
3559 // (stored at this point in mPadding*)
3560 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3561 topPadding >= 0 ? topPadding : mPaddingTop,
3562 rightPadding >= 0 ? rightPadding : mPaddingRight,
3563 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3564
3565 if (viewFlagMasks != 0) {
3566 setFlags(viewFlagValues, viewFlagMasks);
3567 }
3568
3569 // Needs to be called after mViewFlags is set
3570 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3571 recomputePadding();
3572 }
3573
3574 if (x != 0 || y != 0) {
3575 scrollTo(x, y);
3576 }
3577
Chet Haase73066682010-11-29 15:55:32 -08003578 if (transformSet) {
3579 setTranslationX(tx);
3580 setTranslationY(ty);
3581 setRotation(rotation);
3582 setRotationX(rotationX);
3583 setRotationY(rotationY);
3584 setScaleX(sx);
3585 setScaleY(sy);
3586 }
3587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3589 setScrollContainer(true);
3590 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003591
3592 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 }
3594
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003595 private void updateUserPaddingRelative() {
3596 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3597 }
3598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 /**
3600 * Non-public constructor for use in testing
3601 */
3602 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003603 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 }
3605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 /**
3607 * <p>
3608 * Initializes the fading edges from a given set of styled attributes. This
3609 * method should be called by subclasses that need fading edges and when an
3610 * instance of these subclasses is created programmatically rather than
3611 * being inflated from XML. This method is automatically called when the XML
3612 * is inflated.
3613 * </p>
3614 *
3615 * @param a the styled attributes set to initialize the fading edges from
3616 */
3617 protected void initializeFadingEdge(TypedArray a) {
3618 initScrollCache();
3619
3620 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3621 R.styleable.View_fadingEdgeLength,
3622 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3623 }
3624
3625 /**
3626 * Returns the size of the vertical faded edges used to indicate that more
3627 * content in this view is visible.
3628 *
3629 * @return The size in pixels of the vertical faded edge or 0 if vertical
3630 * faded edges are not enabled for this view.
3631 * @attr ref android.R.styleable#View_fadingEdgeLength
3632 */
3633 public int getVerticalFadingEdgeLength() {
3634 if (isVerticalFadingEdgeEnabled()) {
3635 ScrollabilityCache cache = mScrollCache;
3636 if (cache != null) {
3637 return cache.fadingEdgeLength;
3638 }
3639 }
3640 return 0;
3641 }
3642
3643 /**
3644 * Set the size of the faded edge used to indicate that more content in this
3645 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003646 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3647 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3648 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 *
3650 * @param length The size in pixels of the faded edge used to indicate that more
3651 * content in this view is visible.
3652 */
3653 public void setFadingEdgeLength(int length) {
3654 initScrollCache();
3655 mScrollCache.fadingEdgeLength = length;
3656 }
3657
3658 /**
3659 * Returns the size of the horizontal faded edges used to indicate that more
3660 * content in this view is visible.
3661 *
3662 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3663 * faded edges are not enabled for this view.
3664 * @attr ref android.R.styleable#View_fadingEdgeLength
3665 */
3666 public int getHorizontalFadingEdgeLength() {
3667 if (isHorizontalFadingEdgeEnabled()) {
3668 ScrollabilityCache cache = mScrollCache;
3669 if (cache != null) {
3670 return cache.fadingEdgeLength;
3671 }
3672 }
3673 return 0;
3674 }
3675
3676 /**
3677 * Returns the width of the vertical scrollbar.
3678 *
3679 * @return The width in pixels of the vertical scrollbar or 0 if there
3680 * is no vertical scrollbar.
3681 */
3682 public int getVerticalScrollbarWidth() {
3683 ScrollabilityCache cache = mScrollCache;
3684 if (cache != null) {
3685 ScrollBarDrawable scrollBar = cache.scrollBar;
3686 if (scrollBar != null) {
3687 int size = scrollBar.getSize(true);
3688 if (size <= 0) {
3689 size = cache.scrollBarSize;
3690 }
3691 return size;
3692 }
3693 return 0;
3694 }
3695 return 0;
3696 }
3697
3698 /**
3699 * Returns the height of the horizontal scrollbar.
3700 *
3701 * @return The height in pixels of the horizontal scrollbar or 0 if
3702 * there is no horizontal scrollbar.
3703 */
3704 protected int getHorizontalScrollbarHeight() {
3705 ScrollabilityCache cache = mScrollCache;
3706 if (cache != null) {
3707 ScrollBarDrawable scrollBar = cache.scrollBar;
3708 if (scrollBar != null) {
3709 int size = scrollBar.getSize(false);
3710 if (size <= 0) {
3711 size = cache.scrollBarSize;
3712 }
3713 return size;
3714 }
3715 return 0;
3716 }
3717 return 0;
3718 }
3719
3720 /**
3721 * <p>
3722 * Initializes the scrollbars from a given set of styled attributes. This
3723 * method should be called by subclasses that need scrollbars and when an
3724 * instance of these subclasses is created programmatically rather than
3725 * being inflated from XML. This method is automatically called when the XML
3726 * is inflated.
3727 * </p>
3728 *
3729 * @param a the styled attributes set to initialize the scrollbars from
3730 */
3731 protected void initializeScrollbars(TypedArray a) {
3732 initScrollCache();
3733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003735
Mike Cleronf116bf82009-09-27 19:14:12 -07003736 if (scrollabilityCache.scrollBar == null) {
3737 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3738 }
Joe Malin32736f02011-01-19 16:14:20 -08003739
Romain Guy8bda2482010-03-02 11:42:11 -08003740 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741
Mike Cleronf116bf82009-09-27 19:14:12 -07003742 if (!fadeScrollbars) {
3743 scrollabilityCache.state = ScrollabilityCache.ON;
3744 }
3745 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003746
3747
Mike Cleronf116bf82009-09-27 19:14:12 -07003748 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3749 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3750 .getScrollBarFadeDuration());
3751 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3752 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3753 ViewConfiguration.getScrollDefaultDelay());
3754
Joe Malin32736f02011-01-19 16:14:20 -08003755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3757 com.android.internal.R.styleable.View_scrollbarSize,
3758 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3759
3760 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3761 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3762
3763 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3764 if (thumb != null) {
3765 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3766 }
3767
3768 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3769 false);
3770 if (alwaysDraw) {
3771 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3772 }
3773
3774 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3775 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3776
3777 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3778 if (thumb != null) {
3779 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3780 }
3781
3782 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3783 false);
3784 if (alwaysDraw) {
3785 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3786 }
3787
3788 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003789 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 }
3791
3792 /**
3793 * <p>
3794 * Initalizes the scrollability cache if necessary.
3795 * </p>
3796 */
3797 private void initScrollCache() {
3798 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003799 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 }
3801 }
3802
Philip Milne6c8ea062012-04-03 17:38:43 -07003803 private ScrollabilityCache getScrollCache() {
3804 initScrollCache();
3805 return mScrollCache;
3806 }
3807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 /**
Adam Powell20232d02010-12-08 21:08:53 -08003809 * Set the position of the vertical scroll bar. Should be one of
3810 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3811 * {@link #SCROLLBAR_POSITION_RIGHT}.
3812 *
3813 * @param position Where the vertical scroll bar should be positioned.
3814 */
3815 public void setVerticalScrollbarPosition(int position) {
3816 if (mVerticalScrollbarPosition != position) {
3817 mVerticalScrollbarPosition = position;
3818 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003819 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003820 }
3821 }
3822
3823 /**
3824 * @return The position where the vertical scroll bar will show, if applicable.
3825 * @see #setVerticalScrollbarPosition(int)
3826 */
3827 public int getVerticalScrollbarPosition() {
3828 return mVerticalScrollbarPosition;
3829 }
3830
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003831 ListenerInfo getListenerInfo() {
3832 if (mListenerInfo != null) {
3833 return mListenerInfo;
3834 }
3835 mListenerInfo = new ListenerInfo();
3836 return mListenerInfo;
3837 }
3838
Adam Powell20232d02010-12-08 21:08:53 -08003839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 * Register a callback to be invoked when focus of this view changed.
3841 *
3842 * @param l The callback that will run.
3843 */
3844 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003845 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 }
3847
3848 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003849 * Add a listener that will be called when the bounds of the view change due to
3850 * layout processing.
3851 *
3852 * @param listener The listener that will be called when layout bounds change.
3853 */
3854 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003855 ListenerInfo li = getListenerInfo();
3856 if (li.mOnLayoutChangeListeners == null) {
3857 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003858 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003859 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3860 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003861 }
Chet Haase21cd1382010-09-01 17:42:29 -07003862 }
3863
3864 /**
3865 * Remove a listener for layout changes.
3866 *
3867 * @param listener The listener for layout bounds change.
3868 */
3869 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003870 ListenerInfo li = mListenerInfo;
3871 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003872 return;
3873 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003874 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003875 }
3876
3877 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003878 * Add a listener for attach state changes.
3879 *
3880 * This listener will be called whenever this view is attached or detached
3881 * from a window. Remove the listener using
3882 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3883 *
3884 * @param listener Listener to attach
3885 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3886 */
3887 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003888 ListenerInfo li = getListenerInfo();
3889 if (li.mOnAttachStateChangeListeners == null) {
3890 li.mOnAttachStateChangeListeners
3891 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003892 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003893 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003894 }
3895
3896 /**
3897 * Remove a listener for attach state changes. The listener will receive no further
3898 * notification of window attach/detach events.
3899 *
3900 * @param listener Listener to remove
3901 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3902 */
3903 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003904 ListenerInfo li = mListenerInfo;
3905 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003906 return;
3907 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003908 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003909 }
3910
3911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 * Returns the focus-change callback registered for this view.
3913 *
3914 * @return The callback, or null if one is not registered.
3915 */
3916 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003917 ListenerInfo li = mListenerInfo;
3918 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 }
3920
3921 /**
3922 * Register a callback to be invoked when this view is clicked. If this view is not
3923 * clickable, it becomes clickable.
3924 *
3925 * @param l The callback that will run
3926 *
3927 * @see #setClickable(boolean)
3928 */
3929 public void setOnClickListener(OnClickListener l) {
3930 if (!isClickable()) {
3931 setClickable(true);
3932 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003933 getListenerInfo().mOnClickListener = l;
3934 }
3935
3936 /**
3937 * Return whether this view has an attached OnClickListener. Returns
3938 * true if there is a listener, false if there is none.
3939 */
3940 public boolean hasOnClickListeners() {
3941 ListenerInfo li = mListenerInfo;
3942 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 }
3944
3945 /**
3946 * Register a callback to be invoked when this view is clicked and held. If this view is not
3947 * long clickable, it becomes long clickable.
3948 *
3949 * @param l The callback that will run
3950 *
3951 * @see #setLongClickable(boolean)
3952 */
3953 public void setOnLongClickListener(OnLongClickListener l) {
3954 if (!isLongClickable()) {
3955 setLongClickable(true);
3956 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003957 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 }
3959
3960 /**
3961 * Register a callback to be invoked when the context menu for this view is
3962 * being built. If this view is not long clickable, it becomes long clickable.
3963 *
3964 * @param l The callback that will run
3965 *
3966 */
3967 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3968 if (!isLongClickable()) {
3969 setLongClickable(true);
3970 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003971 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 }
3973
3974 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003975 * Call this view's OnClickListener, if it is defined. Performs all normal
3976 * actions associated with clicking: reporting accessibility event, playing
3977 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 *
3979 * @return True there was an assigned OnClickListener that was called, false
3980 * otherwise is returned.
3981 */
3982 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003983 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3984
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003988 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 return true;
3990 }
3991
3992 return false;
3993 }
3994
3995 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003996 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3997 * this only calls the listener, and does not do any associated clicking
3998 * actions like reporting an accessibility event.
3999 *
4000 * @return True there was an assigned OnClickListener that was called, false
4001 * otherwise is returned.
4002 */
4003 public boolean callOnClick() {
4004 ListenerInfo li = mListenerInfo;
4005 if (li != null && li.mOnClickListener != null) {
4006 li.mOnClickListener.onClick(this);
4007 return true;
4008 }
4009 return false;
4010 }
4011
4012 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004013 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4014 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004016 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 */
4018 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004019 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004022 ListenerInfo li = mListenerInfo;
4023 if (li != null && li.mOnLongClickListener != null) {
4024 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 }
4026 if (!handled) {
4027 handled = showContextMenu();
4028 }
4029 if (handled) {
4030 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4031 }
4032 return handled;
4033 }
4034
4035 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004036 * Performs button-related actions during a touch down event.
4037 *
4038 * @param event The event.
4039 * @return True if the down was consumed.
4040 *
4041 * @hide
4042 */
4043 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4044 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4045 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4046 return true;
4047 }
4048 }
4049 return false;
4050 }
4051
4052 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 * Bring up the context menu for this view.
4054 *
4055 * @return Whether a context menu was displayed.
4056 */
4057 public boolean showContextMenu() {
4058 return getParent().showContextMenuForChild(this);
4059 }
4060
4061 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004062 * Bring up the context menu for this view, referring to the item under the specified point.
4063 *
4064 * @param x The referenced x coordinate.
4065 * @param y The referenced y coordinate.
4066 * @param metaState The keyboard modifiers that were pressed.
4067 * @return Whether a context menu was displayed.
4068 *
4069 * @hide
4070 */
4071 public boolean showContextMenu(float x, float y, int metaState) {
4072 return showContextMenu();
4073 }
4074
4075 /**
Adam Powell6e346362010-07-23 10:18:23 -07004076 * Start an action mode.
4077 *
4078 * @param callback Callback that will control the lifecycle of the action mode
4079 * @return The new action mode if it is started, null otherwise
4080 *
4081 * @see ActionMode
4082 */
4083 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004084 ViewParent parent = getParent();
4085 if (parent == null) return null;
4086 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004087 }
4088
4089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 * Register a callback to be invoked when a key is pressed in this view.
4091 * @param l the key listener to attach to this view
4092 */
4093 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004094 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 }
4096
4097 /**
4098 * Register a callback to be invoked when a touch event is sent to this view.
4099 * @param l the touch listener to attach to this view
4100 */
4101 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004102 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 }
4104
4105 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004106 * Register a callback to be invoked when a generic motion event is sent to this view.
4107 * @param l the generic motion listener to attach to this view
4108 */
4109 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004110 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004111 }
4112
4113 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004114 * Register a callback to be invoked when a hover event is sent to this view.
4115 * @param l the hover listener to attach to this view
4116 */
4117 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004118 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004119 }
4120
4121 /**
Joe Malin32736f02011-01-19 16:14:20 -08004122 * Register a drag event listener callback object for this View. The parameter is
4123 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4124 * View, the system calls the
4125 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4126 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004127 */
4128 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004129 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004130 }
4131
4132 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004133 * Give this view focus. This will cause
4134 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 *
4136 * Note: this does not check whether this {@link View} should get focus, it just
4137 * gives it focus no matter what. It should only be called internally by framework
4138 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4139 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004140 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4141 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 * focus moved when requestFocus() is called. It may not always
4143 * apply, in which case use the default View.FOCUS_DOWN.
4144 * @param previouslyFocusedRect The rectangle of the view that had focus
4145 * prior in this View's coordinate system.
4146 */
4147 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4148 if (DBG) {
4149 System.out.println(this + " requestFocus()");
4150 }
4151
4152 if ((mPrivateFlags & FOCUSED) == 0) {
4153 mPrivateFlags |= FOCUSED;
4154
4155 if (mParent != null) {
4156 mParent.requestChildFocus(this, this);
4157 }
4158
4159 onFocusChanged(true, direction, previouslyFocusedRect);
4160 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004161
4162 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4163 notifyAccessibilityStateChanged();
4164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166 }
4167
4168 /**
4169 * Request that a rectangle of this view be visible on the screen,
4170 * scrolling if necessary just enough.
4171 *
4172 * <p>A View should call this if it maintains some notion of which part
4173 * of its content is interesting. For example, a text editing view
4174 * should call this when its cursor moves.
4175 *
4176 * @param rectangle The rectangle.
4177 * @return Whether any parent scrolled.
4178 */
4179 public boolean requestRectangleOnScreen(Rect rectangle) {
4180 return requestRectangleOnScreen(rectangle, false);
4181 }
4182
4183 /**
4184 * Request that a rectangle of this view be visible on the screen,
4185 * scrolling if necessary just enough.
4186 *
4187 * <p>A View should call this if it maintains some notion of which part
4188 * of its content is interesting. For example, a text editing view
4189 * should call this when its cursor moves.
4190 *
4191 * <p>When <code>immediate</code> is set to true, scrolling will not be
4192 * animated.
4193 *
4194 * @param rectangle The rectangle.
4195 * @param immediate True to forbid animated scrolling, false otherwise
4196 * @return Whether any parent scrolled.
4197 */
4198 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4199 View child = this;
4200 ViewParent parent = mParent;
4201 boolean scrolled = false;
4202 while (parent != null) {
4203 scrolled |= parent.requestChildRectangleOnScreen(child,
4204 rectangle, immediate);
4205
4206 // offset rect so next call has the rectangle in the
4207 // coordinate system of its direct child.
4208 rectangle.offset(child.getLeft(), child.getTop());
4209 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4210
4211 if (!(parent instanceof View)) {
4212 break;
4213 }
Romain Guy8506ab42009-06-11 17:35:47 -07004214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 child = (View) parent;
4216 parent = child.getParent();
4217 }
4218 return scrolled;
4219 }
4220
4221 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004222 * Called when this view wants to give up focus. If focus is cleared
4223 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4224 * <p>
4225 * <strong>Note:</strong> When a View clears focus the framework is trying
4226 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004227 * View is the first from the top that can take focus, then all callbacks
4228 * related to clearing focus will be invoked after wich the framework will
4229 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004230 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 */
4232 public void clearFocus() {
4233 if (DBG) {
4234 System.out.println(this + " clearFocus()");
4235 }
4236
4237 if ((mPrivateFlags & FOCUSED) != 0) {
4238 mPrivateFlags &= ~FOCUSED;
4239
4240 if (mParent != null) {
4241 mParent.clearChildFocus(this);
4242 }
4243
4244 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004247
4248 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004249
4250 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4251 notifyAccessibilityStateChanged();
4252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 }
4254 }
4255
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004256 void ensureInputFocusOnFirstFocusable() {
4257 View root = getRootView();
4258 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004259 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
4261 }
4262
4263 /**
4264 * Called internally by the view system when a new view is getting focus.
4265 * This is what clears the old focus.
4266 */
4267 void unFocus() {
4268 if (DBG) {
4269 System.out.println(this + " unFocus()");
4270 }
4271
4272 if ((mPrivateFlags & FOCUSED) != 0) {
4273 mPrivateFlags &= ~FOCUSED;
4274
4275 onFocusChanged(false, 0, null);
4276 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004277
4278 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4279 notifyAccessibilityStateChanged();
4280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 }
4282 }
4283
4284 /**
4285 * Returns true if this view has focus iteself, or is the ancestor of the
4286 * view that has focus.
4287 *
4288 * @return True if this view has or contains focus, false otherwise.
4289 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004290 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 public boolean hasFocus() {
4292 return (mPrivateFlags & FOCUSED) != 0;
4293 }
4294
4295 /**
4296 * Returns true if this view is focusable or if it contains a reachable View
4297 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4298 * is a View whose parents do not block descendants focus.
4299 *
4300 * Only {@link #VISIBLE} views are considered focusable.
4301 *
4302 * @return True if the view is focusable or if the view contains a focusable
4303 * View, false otherwise.
4304 *
4305 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4306 */
4307 public boolean hasFocusable() {
4308 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4309 }
4310
4311 /**
4312 * Called by the view system when the focus state of this view changes.
4313 * When the focus change event is caused by directional navigation, direction
4314 * and previouslyFocusedRect provide insight into where the focus is coming from.
4315 * When overriding, be sure to call up through to the super class so that
4316 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004317 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 * @param gainFocus True if the View has focus; false otherwise.
4319 * @param direction The direction focus has moved when requestFocus()
4320 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004321 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4322 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4323 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4325 * system, of the previously focused view. If applicable, this will be
4326 * passed in as finer grained information about where the focus is coming
4327 * from (in addition to direction). Will be <code>null</code> otherwise.
4328 */
4329 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004330 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004331 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4332 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004333 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004334 }
4335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 InputMethodManager imm = InputMethodManager.peekInstance();
4337 if (!gainFocus) {
4338 if (isPressed()) {
4339 setPressed(false);
4340 }
4341 if (imm != null && mAttachInfo != null
4342 && mAttachInfo.mHasWindowFocus) {
4343 imm.focusOut(this);
4344 }
Romain Guya2431d02009-04-30 16:30:00 -07004345 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 } else if (imm != null && mAttachInfo != null
4347 && mAttachInfo.mHasWindowFocus) {
4348 imm.focusIn(this);
4349 }
Romain Guy8506ab42009-06-11 17:35:47 -07004350
Romain Guy0fd89bf2011-01-26 15:41:30 -08004351 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004352 ListenerInfo li = mListenerInfo;
4353 if (li != null && li.mOnFocusChangeListener != null) {
4354 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 }
Joe Malin32736f02011-01-19 16:14:20 -08004356
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004357 if (mAttachInfo != null) {
4358 mAttachInfo.mKeyDispatchState.reset(this);
4359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 }
4361
4362 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004363 * Sends an accessibility event of the given type. If accessiiblity is
4364 * not enabled this method has no effect. The default implementation calls
4365 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4366 * to populate information about the event source (this View), then calls
4367 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4368 * populate the text content of the event source including its descendants,
4369 * and last calls
4370 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4371 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004372 * <p>
4373 * If an {@link AccessibilityDelegate} has been specified via calling
4374 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4375 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4376 * responsible for handling this call.
4377 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004378 *
Scott Mainb303d832011-10-12 16:45:18 -07004379 * @param eventType The type of the event to send, as defined by several types from
4380 * {@link android.view.accessibility.AccessibilityEvent}, such as
4381 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4382 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004383 *
4384 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4385 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4386 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004387 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004388 */
4389 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004390 if (mAccessibilityDelegate != null) {
4391 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4392 } else {
4393 sendAccessibilityEventInternal(eventType);
4394 }
4395 }
4396
4397 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004398 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4399 * {@link AccessibilityEvent} to make an announcement which is related to some
4400 * sort of a context change for which none of the events representing UI transitions
4401 * is a good fit. For example, announcing a new page in a book. If accessibility
4402 * is not enabled this method does nothing.
4403 *
4404 * @param text The announcement text.
4405 */
4406 public void announceForAccessibility(CharSequence text) {
4407 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4408 AccessibilityEvent event = AccessibilityEvent.obtain(
4409 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4410 event.getText().add(text);
4411 sendAccessibilityEventUnchecked(event);
4412 }
4413 }
4414
4415 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004416 * @see #sendAccessibilityEvent(int)
4417 *
4418 * Note: Called from the default {@link AccessibilityDelegate}.
4419 */
4420 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004421 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4422 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4423 }
4424 }
4425
4426 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004427 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4428 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004429 * perform a check whether accessibility is enabled.
4430 * <p>
4431 * If an {@link AccessibilityDelegate} has been specified via calling
4432 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4433 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4434 * is responsible for handling this call.
4435 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004436 *
4437 * @param event The event to send.
4438 *
4439 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004440 */
4441 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004442 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004443 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004444 } else {
4445 sendAccessibilityEventUncheckedInternal(event);
4446 }
4447 }
4448
4449 /**
4450 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4451 *
4452 * Note: Called from the default {@link AccessibilityDelegate}.
4453 */
4454 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004455 if (!isShown()) {
4456 return;
4457 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004458 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004459 // Only a subset of accessibility events populates text content.
4460 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4461 dispatchPopulateAccessibilityEvent(event);
4462 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004463 // Intercept accessibility focus events fired by virtual nodes to keep
4464 // track of accessibility focus position in such nodes.
4465 final int eventType = event.getEventType();
4466 switch (eventType) {
4467 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4468 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4469 event.getSourceNodeId());
4470 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4471 ViewRootImpl viewRootImpl = getViewRootImpl();
4472 if (viewRootImpl != null) {
4473 viewRootImpl.setAccessibilityFocusedHost(this);
4474 }
4475 }
4476 } break;
4477 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4478 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4479 event.getSourceNodeId());
4480 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4481 ViewRootImpl viewRootImpl = getViewRootImpl();
4482 if (viewRootImpl != null) {
4483 viewRootImpl.setAccessibilityFocusedHost(null);
4484 }
4485 }
4486 } break;
4487 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004488 // In the beginning we called #isShown(), so we know that getParent() is not null.
4489 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004490 }
4491
4492 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004493 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4494 * to its children for adding their text content to the event. Note that the
4495 * event text is populated in a separate dispatch path since we add to the
4496 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004497 * A typical implementation will call
4498 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4499 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4500 * on each child. Override this method if custom population of the event text
4501 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004502 * <p>
4503 * If an {@link AccessibilityDelegate} has been specified via calling
4504 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4505 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4506 * is responsible for handling this call.
4507 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004508 * <p>
4509 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4510 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4511 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004512 *
4513 * @param event The event.
4514 *
4515 * @return True if the event population was completed.
4516 */
4517 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004518 if (mAccessibilityDelegate != null) {
4519 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4520 } else {
4521 return dispatchPopulateAccessibilityEventInternal(event);
4522 }
4523 }
4524
4525 /**
4526 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4527 *
4528 * Note: Called from the default {@link AccessibilityDelegate}.
4529 */
4530 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004531 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004532 return false;
4533 }
4534
4535 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004536 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004537 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004538 * text content. While this method is free to modify event
4539 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004540 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4541 * <p>
4542 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004543 * to the text added by the super implementation:
4544 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004545 * super.onPopulateAccessibilityEvent(event);
4546 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4547 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4548 * mCurrentDate.getTimeInMillis(), flags);
4549 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004550 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004551 * <p>
4552 * If an {@link AccessibilityDelegate} has been specified via calling
4553 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4554 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4555 * is responsible for handling this call.
4556 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004557 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4558 * information to the event, in case the default implementation has basic information to add.
4559 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004560 *
4561 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004562 *
4563 * @see #sendAccessibilityEvent(int)
4564 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004565 */
4566 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004567 if (mAccessibilityDelegate != null) {
4568 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4569 } else {
4570 onPopulateAccessibilityEventInternal(event);
4571 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004572 }
4573
4574 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004575 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4576 *
4577 * Note: Called from the default {@link AccessibilityDelegate}.
4578 */
4579 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4580
4581 }
4582
4583 /**
4584 * Initializes an {@link AccessibilityEvent} with information about
4585 * this View which is the event source. In other words, the source of
4586 * an accessibility event is the view whose state change triggered firing
4587 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004588 * <p>
4589 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004590 * to properties set by the super implementation:
4591 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4592 * super.onInitializeAccessibilityEvent(event);
4593 * event.setPassword(true);
4594 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004595 * <p>
4596 * If an {@link AccessibilityDelegate} has been specified via calling
4597 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4598 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4599 * is responsible for handling this call.
4600 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004601 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4602 * information to the event, in case the default implementation has basic information to add.
4603 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004604 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004605 *
4606 * @see #sendAccessibilityEvent(int)
4607 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4608 */
4609 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004610 if (mAccessibilityDelegate != null) {
4611 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4612 } else {
4613 onInitializeAccessibilityEventInternal(event);
4614 }
4615 }
4616
4617 /**
4618 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4619 *
4620 * Note: Called from the default {@link AccessibilityDelegate}.
4621 */
4622 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004623 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004624 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004625 event.setPackageName(getContext().getPackageName());
4626 event.setEnabled(isEnabled());
4627 event.setContentDescription(mContentDescription);
4628
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004629 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004630 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004631 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4632 FOCUSABLES_ALL);
4633 event.setItemCount(focusablesTempList.size());
4634 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4635 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004636 }
4637 }
4638
4639 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004640 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4641 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4642 * This method is responsible for obtaining an accessibility node info from a
4643 * pool of reusable instances and calling
4644 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4645 * initialize the former.
4646 * <p>
4647 * Note: The client is responsible for recycling the obtained instance by calling
4648 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4649 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004650 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004651 * @return A populated {@link AccessibilityNodeInfo}.
4652 *
4653 * @see AccessibilityNodeInfo
4654 */
4655 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004656 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4657 if (provider != null) {
4658 return provider.createAccessibilityNodeInfo(View.NO_ID);
4659 } else {
4660 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4661 onInitializeAccessibilityNodeInfo(info);
4662 return info;
4663 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004664 }
4665
4666 /**
4667 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4668 * The base implementation sets:
4669 * <ul>
4670 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004671 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4672 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004673 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4674 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4675 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4676 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4677 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4678 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4679 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4680 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4681 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4682 * </ul>
4683 * <p>
4684 * Subclasses should override this method, call the super implementation,
4685 * and set additional attributes.
4686 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 * <p>
4688 * If an {@link AccessibilityDelegate} has been specified via calling
4689 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4690 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4691 * is responsible for handling this call.
4692 * </p>
4693 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004694 * @param info The instance to initialize.
4695 */
4696 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004697 if (mAccessibilityDelegate != null) {
4698 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4699 } else {
4700 onInitializeAccessibilityNodeInfoInternal(info);
4701 }
4702 }
4703
4704 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004705 * Gets the location of this view in screen coordintates.
4706 *
4707 * @param outRect The output location
4708 */
4709 private void getBoundsOnScreen(Rect outRect) {
4710 if (mAttachInfo == null) {
4711 return;
4712 }
4713
4714 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004715 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004716
4717 if (!hasIdentityMatrix()) {
4718 getMatrix().mapRect(position);
4719 }
4720
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004721 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004722
4723 ViewParent parent = mParent;
4724 while (parent instanceof View) {
4725 View parentView = (View) parent;
4726
4727 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4728
4729 if (!parentView.hasIdentityMatrix()) {
4730 parentView.getMatrix().mapRect(position);
4731 }
4732
4733 position.offset(parentView.mLeft, parentView.mTop);
4734
4735 parent = parentView.mParent;
4736 }
4737
4738 if (parent instanceof ViewRootImpl) {
4739 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4740 position.offset(0, -viewRootImpl.mCurScrollY);
4741 }
4742
4743 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4744
4745 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4746 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4747 }
4748
4749 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004750 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4751 *
4752 * Note: Called from the default {@link AccessibilityDelegate}.
4753 */
4754 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004755 Rect bounds = mAttachInfo.mTmpInvalRect;
4756 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004757 info.setBoundsInParent(bounds);
4758
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004759 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004760 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004761
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004762 ViewParent parent = getParentForAccessibility();
4763 if (parent instanceof View) {
4764 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004765 }
4766
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004767 info.setVisibleToUser(isVisibleToUser());
4768
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004769 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004770 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004771 info.setContentDescription(getContentDescription());
4772
4773 info.setEnabled(isEnabled());
4774 info.setClickable(isClickable());
4775 info.setFocusable(isFocusable());
4776 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004777 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004778 info.setSelected(isSelected());
4779 info.setLongClickable(isLongClickable());
4780
4781 // TODO: These make sense only if we are in an AdapterView but all
4782 // views can be selected. Maybe from accessiiblity perspective
4783 // we should report as selectable view in an AdapterView.
4784 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4785 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4786
4787 if (isFocusable()) {
4788 if (isFocused()) {
4789 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4790 } else {
4791 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4792 }
4793 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004794
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004795 if (!isAccessibilityFocused()) {
4796 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4797 } else {
4798 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4799 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004800
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004801 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004802 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4803 }
4804
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004805 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004806 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4807 }
4808
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004809 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004810 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4811 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4812 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004813 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4814 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004815 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004816 }
4817
4818 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004819 * Computes whether this view is visible to the user. Such a view is
4820 * attached, visible, all its predecessors are visible, it is not clipped
4821 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004822 *
4823 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004824 *
4825 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004826 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004827 protected boolean isVisibleToUser() {
4828 return isVisibleToUser(null);
4829 }
4830
4831 /**
4832 * Computes whether the given portion of this view is visible to the user. Such a view is
4833 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4834 * the specified portion is not clipped entirely by its predecessors.
4835 *
4836 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4837 * <code>null</code>, and the entire view will be tested in this case.
4838 * When <code>true</code> is returned by the function, the actual visible
4839 * region will be stored in this parameter; that is, if boundInView is fully
4840 * contained within the view, no modification will be made, otherwise regions
4841 * outside of the visible area of the view will be clipped.
4842 *
4843 * @return Whether the specified portion of the view is visible on the screen.
4844 *
4845 * @hide
4846 */
4847 protected boolean isVisibleToUser(Rect boundInView) {
4848 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4849 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004850 // The first two checks are made also made by isShown() which
4851 // however traverses the tree up to the parent to catch that.
4852 // Therefore, we do some fail fast check to minimize the up
4853 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004854 boolean isVisible = mAttachInfo != null
4855 && mAttachInfo.mWindowVisibility == View.VISIBLE
4856 && getAlpha() > 0
4857 && isShown()
4858 && getGlobalVisibleRect(visibleRect, offset);
4859 if (isVisible && boundInView != null) {
4860 visibleRect.offset(-offset.x, -offset.y);
4861 isVisible &= boundInView.intersect(visibleRect);
4862 }
4863 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004864 }
4865
4866 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004867 * Sets a delegate for implementing accessibility support via compositon as
4868 * opposed to inheritance. The delegate's primary use is for implementing
4869 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4870 *
4871 * @param delegate The delegate instance.
4872 *
4873 * @see AccessibilityDelegate
4874 */
4875 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4876 mAccessibilityDelegate = delegate;
4877 }
4878
4879 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004880 * Gets the provider for managing a virtual view hierarchy rooted at this View
4881 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4882 * that explore the window content.
4883 * <p>
4884 * If this method returns an instance, this instance is responsible for managing
4885 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4886 * View including the one representing the View itself. Similarly the returned
4887 * instance is responsible for performing accessibility actions on any virtual
4888 * view or the root view itself.
4889 * </p>
4890 * <p>
4891 * If an {@link AccessibilityDelegate} has been specified via calling
4892 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4893 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4894 * is responsible for handling this call.
4895 * </p>
4896 *
4897 * @return The provider.
4898 *
4899 * @see AccessibilityNodeProvider
4900 */
4901 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4902 if (mAccessibilityDelegate != null) {
4903 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4904 } else {
4905 return null;
4906 }
4907 }
4908
4909 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004910 * Gets the unique identifier of this view on the screen for accessibility purposes.
4911 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4912 *
4913 * @return The view accessibility id.
4914 *
4915 * @hide
4916 */
4917 public int getAccessibilityViewId() {
4918 if (mAccessibilityViewId == NO_ID) {
4919 mAccessibilityViewId = sNextAccessibilityViewId++;
4920 }
4921 return mAccessibilityViewId;
4922 }
4923
4924 /**
4925 * Gets the unique identifier of the window in which this View reseides.
4926 *
4927 * @return The window accessibility id.
4928 *
4929 * @hide
4930 */
4931 public int getAccessibilityWindowId() {
4932 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4933 }
4934
4935 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004936 * Gets the {@link View} description. It briefly describes the view and is
4937 * primarily used for accessibility support. Set this property to enable
4938 * better accessibility support for your application. This is especially
4939 * true for views that do not have textual representation (For example,
4940 * ImageButton).
4941 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004942 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004943 *
4944 * @attr ref android.R.styleable#View_contentDescription
4945 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004946 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004947 public CharSequence getContentDescription() {
4948 return mContentDescription;
4949 }
4950
4951 /**
4952 * Sets the {@link View} description. It briefly describes the view and is
4953 * primarily used for accessibility support. Set this property to enable
4954 * better accessibility support for your application. This is especially
4955 * true for views that do not have textual representation (For example,
4956 * ImageButton).
4957 *
4958 * @param contentDescription The content description.
4959 *
4960 * @attr ref android.R.styleable#View_contentDescription
4961 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004962 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004963 public void setContentDescription(CharSequence contentDescription) {
4964 mContentDescription = contentDescription;
4965 }
4966
4967 /**
Romain Guya2431d02009-04-30 16:30:00 -07004968 * Invoked whenever this view loses focus, either by losing window focus or by losing
4969 * focus within its window. This method can be used to clear any state tied to the
4970 * focus. For instance, if a button is held pressed with the trackball and the window
4971 * loses focus, this method can be used to cancel the press.
4972 *
4973 * Subclasses of View overriding this method should always call super.onFocusLost().
4974 *
4975 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004976 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004977 *
4978 * @hide pending API council approval
4979 */
4980 protected void onFocusLost() {
4981 resetPressedState();
4982 }
4983
4984 private void resetPressedState() {
4985 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4986 return;
4987 }
4988
4989 if (isPressed()) {
4990 setPressed(false);
4991
4992 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004993 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004994 }
4995 }
4996 }
4997
4998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 * Returns true if this view has focus
5000 *
5001 * @return True if this view has focus, false otherwise.
5002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005003 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 public boolean isFocused() {
5005 return (mPrivateFlags & FOCUSED) != 0;
5006 }
5007
5008 /**
5009 * Find the view in the hierarchy rooted at this view that currently has
5010 * focus.
5011 *
5012 * @return The view that currently has focus, or null if no focused view can
5013 * be found.
5014 */
5015 public View findFocus() {
5016 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5017 }
5018
5019 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005020 * Indicates whether this view is one of the set of scrollable containers in
5021 * its window.
5022 *
5023 * @return whether this view is one of the set of scrollable containers in
5024 * its window
5025 *
5026 * @attr ref android.R.styleable#View_isScrollContainer
5027 */
5028 public boolean isScrollContainer() {
5029 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5030 }
5031
5032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 * Change whether this view is one of the set of scrollable containers in
5034 * its window. This will be used to determine whether the window can
5035 * resize or must pan when a soft input area is open -- scrollable
5036 * containers allow the window to use resize mode since the container
5037 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005038 *
5039 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 */
5041 public void setScrollContainer(boolean isScrollContainer) {
5042 if (isScrollContainer) {
5043 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5044 mAttachInfo.mScrollContainers.add(this);
5045 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5046 }
5047 mPrivateFlags |= SCROLL_CONTAINER;
5048 } else {
5049 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5050 mAttachInfo.mScrollContainers.remove(this);
5051 }
5052 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5053 }
5054 }
5055
5056 /**
5057 * Returns the quality of the drawing cache.
5058 *
5059 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5060 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5061 *
5062 * @see #setDrawingCacheQuality(int)
5063 * @see #setDrawingCacheEnabled(boolean)
5064 * @see #isDrawingCacheEnabled()
5065 *
5066 * @attr ref android.R.styleable#View_drawingCacheQuality
5067 */
5068 public int getDrawingCacheQuality() {
5069 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5070 }
5071
5072 /**
5073 * Set the drawing cache quality of this view. This value is used only when the
5074 * drawing cache is enabled
5075 *
5076 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5077 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5078 *
5079 * @see #getDrawingCacheQuality()
5080 * @see #setDrawingCacheEnabled(boolean)
5081 * @see #isDrawingCacheEnabled()
5082 *
5083 * @attr ref android.R.styleable#View_drawingCacheQuality
5084 */
5085 public void setDrawingCacheQuality(int quality) {
5086 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5087 }
5088
5089 /**
5090 * Returns whether the screen should remain on, corresponding to the current
5091 * value of {@link #KEEP_SCREEN_ON}.
5092 *
5093 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5094 *
5095 * @see #setKeepScreenOn(boolean)
5096 *
5097 * @attr ref android.R.styleable#View_keepScreenOn
5098 */
5099 public boolean getKeepScreenOn() {
5100 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5101 }
5102
5103 /**
5104 * Controls whether the screen should remain on, modifying the
5105 * value of {@link #KEEP_SCREEN_ON}.
5106 *
5107 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5108 *
5109 * @see #getKeepScreenOn()
5110 *
5111 * @attr ref android.R.styleable#View_keepScreenOn
5112 */
5113 public void setKeepScreenOn(boolean keepScreenOn) {
5114 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5115 }
5116
5117 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005118 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5119 * @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 -08005120 *
5121 * @attr ref android.R.styleable#View_nextFocusLeft
5122 */
5123 public int getNextFocusLeftId() {
5124 return mNextFocusLeftId;
5125 }
5126
5127 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005128 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5129 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5130 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005131 *
5132 * @attr ref android.R.styleable#View_nextFocusLeft
5133 */
5134 public void setNextFocusLeftId(int nextFocusLeftId) {
5135 mNextFocusLeftId = nextFocusLeftId;
5136 }
5137
5138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005139 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5140 * @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 -08005141 *
5142 * @attr ref android.R.styleable#View_nextFocusRight
5143 */
5144 public int getNextFocusRightId() {
5145 return mNextFocusRightId;
5146 }
5147
5148 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005149 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5150 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5151 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 *
5153 * @attr ref android.R.styleable#View_nextFocusRight
5154 */
5155 public void setNextFocusRightId(int nextFocusRightId) {
5156 mNextFocusRightId = nextFocusRightId;
5157 }
5158
5159 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005160 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5161 * @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 -08005162 *
5163 * @attr ref android.R.styleable#View_nextFocusUp
5164 */
5165 public int getNextFocusUpId() {
5166 return mNextFocusUpId;
5167 }
5168
5169 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005170 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5171 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5172 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 *
5174 * @attr ref android.R.styleable#View_nextFocusUp
5175 */
5176 public void setNextFocusUpId(int nextFocusUpId) {
5177 mNextFocusUpId = nextFocusUpId;
5178 }
5179
5180 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005181 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5182 * @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 -08005183 *
5184 * @attr ref android.R.styleable#View_nextFocusDown
5185 */
5186 public int getNextFocusDownId() {
5187 return mNextFocusDownId;
5188 }
5189
5190 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005191 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5192 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5193 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194 *
5195 * @attr ref android.R.styleable#View_nextFocusDown
5196 */
5197 public void setNextFocusDownId(int nextFocusDownId) {
5198 mNextFocusDownId = nextFocusDownId;
5199 }
5200
5201 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005202 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5203 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5204 *
5205 * @attr ref android.R.styleable#View_nextFocusForward
5206 */
5207 public int getNextFocusForwardId() {
5208 return mNextFocusForwardId;
5209 }
5210
5211 /**
5212 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5213 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5214 * decide automatically.
5215 *
5216 * @attr ref android.R.styleable#View_nextFocusForward
5217 */
5218 public void setNextFocusForwardId(int nextFocusForwardId) {
5219 mNextFocusForwardId = nextFocusForwardId;
5220 }
5221
5222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 * Returns the visibility of this view and all of its ancestors
5224 *
5225 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5226 */
5227 public boolean isShown() {
5228 View current = this;
5229 //noinspection ConstantConditions
5230 do {
5231 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5232 return false;
5233 }
5234 ViewParent parent = current.mParent;
5235 if (parent == null) {
5236 return false; // We are not attached to the view root
5237 }
5238 if (!(parent instanceof View)) {
5239 return true;
5240 }
5241 current = (View) parent;
5242 } while (current != null);
5243
5244 return false;
5245 }
5246
5247 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005248 * Called by the view hierarchy when the content insets for a window have
5249 * changed, to allow it to adjust its content to fit within those windows.
5250 * The content insets tell you the space that the status bar, input method,
5251 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005253 * <p>You do not normally need to deal with this function, since the default
5254 * window decoration given to applications takes care of applying it to the
5255 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5256 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5257 * and your content can be placed under those system elements. You can then
5258 * use this method within your view hierarchy if you have parts of your UI
5259 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005261 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005262 * inset's to the view's padding, consuming that content (modifying the
5263 * insets to be 0), and returning true. This behavior is off by default, but can
5264 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5265 *
5266 * <p>This function's traversal down the hierarchy is depth-first. The same content
5267 * insets object is propagated down the hierarchy, so any changes made to it will
5268 * be seen by all following views (including potentially ones above in
5269 * the hierarchy since this is a depth-first traversal). The first view
5270 * that returns true will abort the entire traversal.
5271 *
5272 * <p>The default implementation works well for a situation where it is
5273 * used with a container that covers the entire window, allowing it to
5274 * apply the appropriate insets to its content on all edges. If you need
5275 * a more complicated layout (such as two different views fitting system
5276 * windows, one on the top of the window, and one on the bottom),
5277 * you can override the method and handle the insets however you would like.
5278 * Note that the insets provided by the framework are always relative to the
5279 * far edges of the window, not accounting for the location of the called view
5280 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005281 * where the layout will place the view, as it is done before layout happens.)
5282 *
5283 * <p>Note: unlike many View methods, there is no dispatch phase to this
5284 * call. If you are overriding it in a ViewGroup and want to allow the
5285 * call to continue to your children, you must be sure to call the super
5286 * implementation.
5287 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005288 * <p>Here is a sample layout that makes use of fitting system windows
5289 * to have controls for a video view placed inside of the window decorations
5290 * that it hides and shows. This can be used with code like the second
5291 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5292 *
5293 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5294 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005295 * @param insets Current content insets of the window. Prior to
5296 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5297 * the insets or else you and Android will be unhappy.
5298 *
5299 * @return Return true if this view applied the insets and it should not
5300 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005301 * @see #getFitsSystemWindows()
5302 * @see #setFitsSystemWindows()
5303 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 */
5305 protected boolean fitSystemWindows(Rect insets) {
5306 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005307 mUserPaddingStart = -1;
5308 mUserPaddingEnd = -1;
5309 mUserPaddingRelative = false;
5310 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5311 || mAttachInfo == null
5312 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5313 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5314 return true;
5315 } else {
5316 internalSetPadding(0, 0, 0, 0);
5317 return false;
5318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 }
5320 return false;
5321 }
5322
5323 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005324 * Sets whether or not this view should account for system screen decorations
5325 * such as the status bar and inset its content; that is, controlling whether
5326 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5327 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005328 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005329 * <p>Note that if you are providing your own implementation of
5330 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5331 * flag to true -- your implementation will be overriding the default
5332 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005333 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005334 * @param fitSystemWindows If true, then the default implementation of
5335 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005336 *
5337 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005338 * @see #getFitsSystemWindows()
5339 * @see #fitSystemWindows(Rect)
5340 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005341 */
5342 public void setFitsSystemWindows(boolean fitSystemWindows) {
5343 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5344 }
5345
5346 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005347 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005348 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5349 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005350 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005351 * @return Returns true if the default implementation of
5352 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005353 *
5354 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005355 * @see #setFitsSystemWindows()
5356 * @see #fitSystemWindows(Rect)
5357 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005358 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005359 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005360 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5361 }
5362
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005363 /** @hide */
5364 public boolean fitsSystemWindows() {
5365 return getFitsSystemWindows();
5366 }
5367
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005368 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005369 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5370 */
5371 public void requestFitSystemWindows() {
5372 if (mParent != null) {
5373 mParent.requestFitSystemWindows();
5374 }
5375 }
5376
5377 /**
5378 * For use by PhoneWindow to make its own system window fitting optional.
5379 * @hide
5380 */
5381 public void makeOptionalFitsSystemWindows() {
5382 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5383 }
5384
5385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 * Returns the visibility status for this view.
5387 *
5388 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5389 * @attr ref android.R.styleable#View_visibility
5390 */
5391 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005392 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5393 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5394 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005395 })
5396 public int getVisibility() {
5397 return mViewFlags & VISIBILITY_MASK;
5398 }
5399
5400 /**
5401 * Set the enabled state of this view.
5402 *
5403 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5404 * @attr ref android.R.styleable#View_visibility
5405 */
5406 @RemotableViewMethod
5407 public void setVisibility(int visibility) {
5408 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005409 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 }
5411
5412 /**
5413 * Returns the enabled status for this view. The interpretation of the
5414 * enabled state varies by subclass.
5415 *
5416 * @return True if this view is enabled, false otherwise.
5417 */
5418 @ViewDebug.ExportedProperty
5419 public boolean isEnabled() {
5420 return (mViewFlags & ENABLED_MASK) == ENABLED;
5421 }
5422
5423 /**
5424 * Set the enabled state of this view. The interpretation of the enabled
5425 * state varies by subclass.
5426 *
5427 * @param enabled True if this view is enabled, false otherwise.
5428 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005429 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005431 if (enabled == isEnabled()) return;
5432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5434
5435 /*
5436 * The View most likely has to change its appearance, so refresh
5437 * the drawable state.
5438 */
5439 refreshDrawableState();
5440
5441 // Invalidate too, since the default behavior for views is to be
5442 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005443 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 }
5445
5446 /**
5447 * Set whether this view can receive the focus.
5448 *
5449 * Setting this to false will also ensure that this view is not focusable
5450 * in touch mode.
5451 *
5452 * @param focusable If true, this view can receive the focus.
5453 *
5454 * @see #setFocusableInTouchMode(boolean)
5455 * @attr ref android.R.styleable#View_focusable
5456 */
5457 public void setFocusable(boolean focusable) {
5458 if (!focusable) {
5459 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5460 }
5461 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5462 }
5463
5464 /**
5465 * Set whether this view can receive focus while in touch mode.
5466 *
5467 * Setting this to true will also ensure that this view is focusable.
5468 *
5469 * @param focusableInTouchMode If true, this view can receive the focus while
5470 * in touch mode.
5471 *
5472 * @see #setFocusable(boolean)
5473 * @attr ref android.R.styleable#View_focusableInTouchMode
5474 */
5475 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5476 // Focusable in touch mode should always be set before the focusable flag
5477 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5478 // which, in touch mode, will not successfully request focus on this view
5479 // because the focusable in touch mode flag is not set
5480 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5481 if (focusableInTouchMode) {
5482 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5483 }
5484 }
5485
5486 /**
5487 * Set whether this view should have sound effects enabled for events such as
5488 * clicking and touching.
5489 *
5490 * <p>You may wish to disable sound effects for a view if you already play sounds,
5491 * for instance, a dial key that plays dtmf tones.
5492 *
5493 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5494 * @see #isSoundEffectsEnabled()
5495 * @see #playSoundEffect(int)
5496 * @attr ref android.R.styleable#View_soundEffectsEnabled
5497 */
5498 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5499 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5500 }
5501
5502 /**
5503 * @return whether this view should have sound effects enabled for events such as
5504 * clicking and touching.
5505 *
5506 * @see #setSoundEffectsEnabled(boolean)
5507 * @see #playSoundEffect(int)
5508 * @attr ref android.R.styleable#View_soundEffectsEnabled
5509 */
5510 @ViewDebug.ExportedProperty
5511 public boolean isSoundEffectsEnabled() {
5512 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5513 }
5514
5515 /**
5516 * Set whether this view should have haptic feedback for events such as
5517 * long presses.
5518 *
5519 * <p>You may wish to disable haptic feedback if your view already controls
5520 * its own haptic feedback.
5521 *
5522 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5523 * @see #isHapticFeedbackEnabled()
5524 * @see #performHapticFeedback(int)
5525 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5526 */
5527 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5528 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5529 }
5530
5531 /**
5532 * @return whether this view should have haptic feedback enabled for events
5533 * long presses.
5534 *
5535 * @see #setHapticFeedbackEnabled(boolean)
5536 * @see #performHapticFeedback(int)
5537 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5538 */
5539 @ViewDebug.ExportedProperty
5540 public boolean isHapticFeedbackEnabled() {
5541 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5542 }
5543
5544 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005545 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005546 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005547 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5548 * {@link #LAYOUT_DIRECTION_RTL},
5549 * {@link #LAYOUT_DIRECTION_INHERIT} or
5550 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005551 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005552 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005553 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005554 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005555 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005556 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5557 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5558 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5559 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005560 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005561 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005562 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005563 }
5564
5565 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005566 * Set the layout direction for this view. This will propagate a reset of layout direction
5567 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005568 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005569 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5570 * {@link #LAYOUT_DIRECTION_RTL},
5571 * {@link #LAYOUT_DIRECTION_INHERIT} or
5572 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005573 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005574 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005575 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005576 */
5577 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005578 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005579 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005580 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005581 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005582 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005583 // Set the new layout direction (filtered) and ask for a layout pass
5584 mPrivateFlags2 |=
5585 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5586 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005587 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005588 }
5589
5590 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005591 * Returns the resolved layout direction for this view.
5592 *
5593 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005594 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005595 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005596 */
5597 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005598 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5599 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005600 })
5601 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005602 // The layout diretion will be resolved only if needed
5603 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5604 resolveLayoutDirection();
5605 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005606 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005607 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5608 }
5609
5610 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005611 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5612 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005613 *
5614 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005615 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005616 */
5617 @ViewDebug.ExportedProperty(category = "layout")
5618 public boolean isLayoutRtl() {
5619 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5620 }
5621
5622 /**
Adam Powell539ee872012-02-03 19:00:49 -08005623 * Indicates whether the view is currently tracking transient state that the
5624 * app should not need to concern itself with saving and restoring, but that
5625 * the framework should take special note to preserve when possible.
5626 *
Adam Powell785c4472012-05-02 21:25:39 -07005627 * <p>A view with transient state cannot be trivially rebound from an external
5628 * data source, such as an adapter binding item views in a list. This may be
5629 * because the view is performing an animation, tracking user selection
5630 * of content, or similar.</p>
5631 *
Adam Powell539ee872012-02-03 19:00:49 -08005632 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005633 */
5634 @ViewDebug.ExportedProperty(category = "layout")
5635 public boolean hasTransientState() {
5636 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5637 }
5638
5639 /**
5640 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005641 * framework should attempt to preserve when possible. This flag is reference counted,
5642 * so every call to setHasTransientState(true) should be paired with a later call
5643 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005644 *
Adam Powell785c4472012-05-02 21:25:39 -07005645 * <p>A view with transient state cannot be trivially rebound from an external
5646 * data source, such as an adapter binding item views in a list. This may be
5647 * because the view is performing an animation, tracking user selection
5648 * of content, or similar.</p>
5649 *
Adam Powell539ee872012-02-03 19:00:49 -08005650 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005651 */
5652 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005653 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5654 mTransientStateCount - 1;
5655 if (mTransientStateCount < 0) {
5656 mTransientStateCount = 0;
5657 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5658 "unmatched pair of setHasTransientState calls");
5659 }
5660 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005661 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005662 // update flag if we've just incremented up from 0 or decremented down to 0
5663 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5664 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5665 if (mParent != null) {
5666 try {
5667 mParent.childHasTransientStateChanged(this, hasTransientState);
5668 } catch (AbstractMethodError e) {
5669 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5670 " does not fully implement ViewParent", e);
5671 }
Adam Powell539ee872012-02-03 19:00:49 -08005672 }
5673 }
5674 }
5675
5676 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 * If this view doesn't do any drawing on its own, set this flag to
5678 * allow further optimizations. By default, this flag is not set on
5679 * View, but could be set on some View subclasses such as ViewGroup.
5680 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005681 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5682 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005683 *
5684 * @param willNotDraw whether or not this View draw on its own
5685 */
5686 public void setWillNotDraw(boolean willNotDraw) {
5687 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5688 }
5689
5690 /**
5691 * Returns whether or not this View draws on its own.
5692 *
5693 * @return true if this view has nothing to draw, false otherwise
5694 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005695 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 public boolean willNotDraw() {
5697 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5698 }
5699
5700 /**
5701 * When a View's drawing cache is enabled, drawing is redirected to an
5702 * offscreen bitmap. Some views, like an ImageView, must be able to
5703 * bypass this mechanism if they already draw a single bitmap, to avoid
5704 * unnecessary usage of the memory.
5705 *
5706 * @param willNotCacheDrawing true if this view does not cache its
5707 * drawing, false otherwise
5708 */
5709 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5710 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5711 }
5712
5713 /**
5714 * Returns whether or not this View can cache its drawing or not.
5715 *
5716 * @return true if this view does not cache its drawing, false otherwise
5717 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005718 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005719 public boolean willNotCacheDrawing() {
5720 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5721 }
5722
5723 /**
5724 * Indicates whether this view reacts to click events or not.
5725 *
5726 * @return true if the view is clickable, false otherwise
5727 *
5728 * @see #setClickable(boolean)
5729 * @attr ref android.R.styleable#View_clickable
5730 */
5731 @ViewDebug.ExportedProperty
5732 public boolean isClickable() {
5733 return (mViewFlags & CLICKABLE) == CLICKABLE;
5734 }
5735
5736 /**
5737 * Enables or disables click events for this view. When a view
5738 * is clickable it will change its state to "pressed" on every click.
5739 * Subclasses should set the view clickable to visually react to
5740 * user's clicks.
5741 *
5742 * @param clickable true to make the view clickable, false otherwise
5743 *
5744 * @see #isClickable()
5745 * @attr ref android.R.styleable#View_clickable
5746 */
5747 public void setClickable(boolean clickable) {
5748 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5749 }
5750
5751 /**
5752 * Indicates whether this view reacts to long click events or not.
5753 *
5754 * @return true if the view is long clickable, false otherwise
5755 *
5756 * @see #setLongClickable(boolean)
5757 * @attr ref android.R.styleable#View_longClickable
5758 */
5759 public boolean isLongClickable() {
5760 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5761 }
5762
5763 /**
5764 * Enables or disables long click events for this view. When a view is long
5765 * clickable it reacts to the user holding down the button for a longer
5766 * duration than a tap. This event can either launch the listener or a
5767 * context menu.
5768 *
5769 * @param longClickable true to make the view long clickable, false otherwise
5770 * @see #isLongClickable()
5771 * @attr ref android.R.styleable#View_longClickable
5772 */
5773 public void setLongClickable(boolean longClickable) {
5774 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5775 }
5776
5777 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005778 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 *
5780 * @see #isClickable()
5781 * @see #setClickable(boolean)
5782 *
5783 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5784 * the View's internal state from a previously set "pressed" state.
5785 */
5786 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005787 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 if (pressed) {
5790 mPrivateFlags |= PRESSED;
5791 } else {
5792 mPrivateFlags &= ~PRESSED;
5793 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005794
5795 if (needsRefresh) {
5796 refreshDrawableState();
5797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 dispatchSetPressed(pressed);
5799 }
5800
5801 /**
5802 * Dispatch setPressed to all of this View's children.
5803 *
5804 * @see #setPressed(boolean)
5805 *
5806 * @param pressed The new pressed state
5807 */
5808 protected void dispatchSetPressed(boolean pressed) {
5809 }
5810
5811 /**
5812 * Indicates whether the view is currently in pressed state. Unless
5813 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5814 * the pressed state.
5815 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005816 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 * @see #isClickable()
5818 * @see #setClickable(boolean)
5819 *
5820 * @return true if the view is currently pressed, false otherwise
5821 */
5822 public boolean isPressed() {
5823 return (mPrivateFlags & PRESSED) == PRESSED;
5824 }
5825
5826 /**
5827 * Indicates whether this view will save its state (that is,
5828 * whether its {@link #onSaveInstanceState} method will be called).
5829 *
5830 * @return Returns true if the view state saving is enabled, else false.
5831 *
5832 * @see #setSaveEnabled(boolean)
5833 * @attr ref android.R.styleable#View_saveEnabled
5834 */
5835 public boolean isSaveEnabled() {
5836 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5837 }
5838
5839 /**
5840 * Controls whether the saving of this view's state is
5841 * enabled (that is, whether its {@link #onSaveInstanceState} method
5842 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005843 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 * for its state to be saved. This flag can only disable the
5845 * saving of this view; any child views may still have their state saved.
5846 *
5847 * @param enabled Set to false to <em>disable</em> state saving, or true
5848 * (the default) to allow it.
5849 *
5850 * @see #isSaveEnabled()
5851 * @see #setId(int)
5852 * @see #onSaveInstanceState()
5853 * @attr ref android.R.styleable#View_saveEnabled
5854 */
5855 public void setSaveEnabled(boolean enabled) {
5856 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5857 }
5858
Jeff Brown85a31762010-09-01 17:01:00 -07005859 /**
5860 * Gets whether the framework should discard touches when the view's
5861 * window is obscured by another visible window.
5862 * Refer to the {@link View} security documentation for more details.
5863 *
5864 * @return True if touch filtering is enabled.
5865 *
5866 * @see #setFilterTouchesWhenObscured(boolean)
5867 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5868 */
5869 @ViewDebug.ExportedProperty
5870 public boolean getFilterTouchesWhenObscured() {
5871 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5872 }
5873
5874 /**
5875 * Sets whether the framework should discard touches when the view's
5876 * window is obscured by another visible window.
5877 * Refer to the {@link View} security documentation for more details.
5878 *
5879 * @param enabled True if touch filtering should be enabled.
5880 *
5881 * @see #getFilterTouchesWhenObscured
5882 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5883 */
5884 public void setFilterTouchesWhenObscured(boolean enabled) {
5885 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5886 FILTER_TOUCHES_WHEN_OBSCURED);
5887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888
5889 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005890 * Indicates whether the entire hierarchy under this view will save its
5891 * state when a state saving traversal occurs from its parent. The default
5892 * is true; if false, these views will not be saved unless
5893 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5894 *
5895 * @return Returns true if the view state saving from parent is enabled, else false.
5896 *
5897 * @see #setSaveFromParentEnabled(boolean)
5898 */
5899 public boolean isSaveFromParentEnabled() {
5900 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5901 }
5902
5903 /**
5904 * Controls whether the entire hierarchy under this view will save its
5905 * state when a state saving traversal occurs from its parent. The default
5906 * is true; if false, these views will not be saved unless
5907 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5908 *
5909 * @param enabled Set to false to <em>disable</em> state saving, or true
5910 * (the default) to allow it.
5911 *
5912 * @see #isSaveFromParentEnabled()
5913 * @see #setId(int)
5914 * @see #onSaveInstanceState()
5915 */
5916 public void setSaveFromParentEnabled(boolean enabled) {
5917 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5918 }
5919
5920
5921 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 * Returns whether this View is able to take focus.
5923 *
5924 * @return True if this view can take focus, or false otherwise.
5925 * @attr ref android.R.styleable#View_focusable
5926 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005927 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005928 public final boolean isFocusable() {
5929 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5930 }
5931
5932 /**
5933 * When a view is focusable, it may not want to take focus when in touch mode.
5934 * For example, a button would like focus when the user is navigating via a D-pad
5935 * so that the user can click on it, but once the user starts touching the screen,
5936 * the button shouldn't take focus
5937 * @return Whether the view is focusable in touch mode.
5938 * @attr ref android.R.styleable#View_focusableInTouchMode
5939 */
5940 @ViewDebug.ExportedProperty
5941 public final boolean isFocusableInTouchMode() {
5942 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5943 }
5944
5945 /**
5946 * Find the nearest view in the specified direction that can take focus.
5947 * This does not actually give focus to that view.
5948 *
5949 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5950 *
5951 * @return The nearest focusable in the specified direction, or null if none
5952 * can be found.
5953 */
5954 public View focusSearch(int direction) {
5955 if (mParent != null) {
5956 return mParent.focusSearch(this, direction);
5957 } else {
5958 return null;
5959 }
5960 }
5961
5962 /**
5963 * This method is the last chance for the focused view and its ancestors to
5964 * respond to an arrow key. This is called when the focused view did not
5965 * consume the key internally, nor could the view system find a new view in
5966 * the requested direction to give focus to.
5967 *
5968 * @param focused The currently focused view.
5969 * @param direction The direction focus wants to move. One of FOCUS_UP,
5970 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5971 * @return True if the this view consumed this unhandled move.
5972 */
5973 public boolean dispatchUnhandledMove(View focused, int direction) {
5974 return false;
5975 }
5976
5977 /**
5978 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005979 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005981 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5982 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 * @return The user specified next view, or null if there is none.
5984 */
5985 View findUserSetNextFocus(View root, int direction) {
5986 switch (direction) {
5987 case FOCUS_LEFT:
5988 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005989 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 case FOCUS_RIGHT:
5991 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005992 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 case FOCUS_UP:
5994 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005995 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 case FOCUS_DOWN:
5997 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005998 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005999 case FOCUS_FORWARD:
6000 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006001 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006002 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006003 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006004 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006005 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006006 @Override
6007 public boolean apply(View t) {
6008 return t.mNextFocusForwardId == id;
6009 }
6010 });
6011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 }
6013 return null;
6014 }
6015
Jeff Brown4dfbec22011-08-15 14:55:37 -07006016 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6017 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6018 @Override
6019 public boolean apply(View t) {
6020 return t.mID == childViewId;
6021 }
6022 });
6023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 if (result == null) {
6025 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6026 + "by user for id " + childViewId);
6027 }
6028 return result;
6029 }
6030
6031 /**
6032 * Find and return all focusable views that are descendants of this view,
6033 * possibly including this view if it is focusable itself.
6034 *
6035 * @param direction The direction of the focus
6036 * @return A list of focusable views
6037 */
6038 public ArrayList<View> getFocusables(int direction) {
6039 ArrayList<View> result = new ArrayList<View>(24);
6040 addFocusables(result, direction);
6041 return result;
6042 }
6043
6044 /**
6045 * Add any focusable views that are descendants of this view (possibly
6046 * including this view if it is focusable itself) to views. If we are in touch mode,
6047 * only add views that are also focusable in touch mode.
6048 *
6049 * @param views Focusable views found so far
6050 * @param direction The direction of the focus
6051 */
6052 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006053 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055
svetoslavganov75986cf2009-05-14 22:28:01 -07006056 /**
6057 * Adds any focusable views that are descendants of this view (possibly
6058 * including this view if it is focusable itself) to views. This method
6059 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006060 * only views focusable in touch mode if we are in touch mode or
6061 * only views that can take accessibility focus if accessibility is enabeld
6062 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006063 *
6064 * @param views Focusable views found so far or null if all we are interested is
6065 * the number of focusables.
6066 * @param direction The direction of the focus.
6067 * @param focusableMode The type of focusables to be added.
6068 *
6069 * @see #FOCUSABLES_ALL
6070 * @see #FOCUSABLES_TOUCH_MODE
6071 */
6072 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006073 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006074 return;
6075 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006076 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006077 if (canTakeAccessibilityFocusFromHover() || getAccessibilityNodeProvider() != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006078 views.add(this);
6079 return;
6080 }
6081 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006082 if (!isFocusable()) {
6083 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006084 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006085 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6086 && isInTouchMode() && !isFocusableInTouchMode()) {
6087 return;
6088 }
6089 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 }
6091
6092 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006093 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006094 * The search is performed by either the text that the View renders or the content
6095 * description that describes the view for accessibility purposes and the view does
6096 * not render or both. Clients can specify how the search is to be performed via
6097 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6098 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006099 *
6100 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006101 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006102 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006103 * @see #FIND_VIEWS_WITH_TEXT
6104 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6105 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006106 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006107 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006108 if (getAccessibilityNodeProvider() != null) {
6109 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6110 outViews.add(this);
6111 }
6112 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006113 && (searched != null && searched.length() > 0)
6114 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006115 String searchedLowerCase = searched.toString().toLowerCase();
6116 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6117 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6118 outViews.add(this);
6119 }
6120 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006121 }
6122
6123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 * Find and return all touchable views that are descendants of this view,
6125 * possibly including this view if it is touchable itself.
6126 *
6127 * @return A list of touchable views
6128 */
6129 public ArrayList<View> getTouchables() {
6130 ArrayList<View> result = new ArrayList<View>();
6131 addTouchables(result);
6132 return result;
6133 }
6134
6135 /**
6136 * Add any touchable views that are descendants of this view (possibly
6137 * including this view if it is touchable itself) to views.
6138 *
6139 * @param views Touchable views found so far
6140 */
6141 public void addTouchables(ArrayList<View> views) {
6142 final int viewFlags = mViewFlags;
6143
6144 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6145 && (viewFlags & ENABLED_MASK) == ENABLED) {
6146 views.add(this);
6147 }
6148 }
6149
6150 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006151 * Returns whether this View is accessibility focused.
6152 *
6153 * @return True if this View is accessibility focused.
6154 */
6155 boolean isAccessibilityFocused() {
6156 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6157 }
6158
6159 /**
6160 * Call this to try to give accessibility focus to this view.
6161 *
6162 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6163 * returns false or the view is no visible or the view already has accessibility
6164 * focus.
6165 *
6166 * See also {@link #focusSearch(int)}, which is what you call to say that you
6167 * have focus, and you want your parent to look for the next one.
6168 *
6169 * @return Whether this view actually took accessibility focus.
6170 *
6171 * @hide
6172 */
6173 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006174 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6175 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006176 return false;
6177 }
6178 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6179 return false;
6180 }
6181 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6182 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6183 ViewRootImpl viewRootImpl = getViewRootImpl();
6184 if (viewRootImpl != null) {
6185 viewRootImpl.setAccessibilityFocusedHost(this);
6186 }
6187 invalidate();
6188 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6189 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006190 return true;
6191 }
6192 return false;
6193 }
6194
6195 /**
6196 * Call this to try to clear accessibility focus of this view.
6197 *
6198 * See also {@link #focusSearch(int)}, which is what you call to say that you
6199 * have focus, and you want your parent to look for the next one.
6200 *
6201 * @hide
6202 */
6203 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006204 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6205 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006206 invalidate();
6207 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6208 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006209 // Clear the text navigation state.
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006210 setAccessibilityCursorPosition(ACCESSIBILITY_CURSOR_POSITION_UNDEFINED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006211 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006212 // Clear the global reference of accessibility focus if this
6213 // view or any of its descendants had accessibility focus.
6214 ViewRootImpl viewRootImpl = getViewRootImpl();
6215 if (viewRootImpl != null) {
6216 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6217 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6218 viewRootImpl.setAccessibilityFocusedHost(null);
6219 }
6220 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006221 }
6222
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006223 private void requestAccessibilityFocusFromHover() {
6224 if (includeForAccessibility() && isActionableForAccessibility()) {
6225 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006226 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006227 } else {
6228 if (mParent != null) {
6229 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6230 if (nextFocus != null) {
6231 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006232 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006233 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006234 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006235 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006236 }
6237
6238 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006239 * @hide
6240 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006241 public boolean canTakeAccessibilityFocusFromHover() {
6242 if (includeForAccessibility() && isActionableForAccessibility()) {
6243 return true;
6244 }
6245 if (mParent != null) {
6246 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006247 }
6248 return false;
6249 }
6250
6251 /**
6252 * Clears accessibility focus without calling any callback methods
6253 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6254 * is used for clearing accessibility focus when giving this focus to
6255 * another view.
6256 */
6257 void clearAccessibilityFocusNoCallbacks() {
6258 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6259 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006260 setAccessibilityCursorPosition(ACCESSIBILITY_CURSOR_POSITION_UNDEFINED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006261 invalidate();
6262 }
6263 }
6264
6265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 * Call this to try to give focus to a specific view or to one of its
6267 * descendants.
6268 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006269 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6270 * false), or if it is focusable and it is not focusable in touch mode
6271 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006273 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 * have focus, and you want your parent to look for the next one.
6275 *
6276 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6277 * {@link #FOCUS_DOWN} and <code>null</code>.
6278 *
6279 * @return Whether this view or one of its descendants actually took focus.
6280 */
6281 public final boolean requestFocus() {
6282 return requestFocus(View.FOCUS_DOWN);
6283 }
6284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 /**
6286 * Call this to try to give focus to a specific view or to one of its
6287 * descendants and give it a hint about what direction focus is heading.
6288 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006289 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6290 * false), or if it is focusable and it is not focusable in touch mode
6291 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006293 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006294 * have focus, and you want your parent to look for the next one.
6295 *
6296 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6297 * <code>null</code> set for the previously focused rectangle.
6298 *
6299 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6300 * @return Whether this view or one of its descendants actually took focus.
6301 */
6302 public final boolean requestFocus(int direction) {
6303 return requestFocus(direction, null);
6304 }
6305
6306 /**
6307 * Call this to try to give focus to a specific view or to one of its descendants
6308 * and give it hints about the direction and a specific rectangle that the focus
6309 * is coming from. The rectangle can help give larger views a finer grained hint
6310 * about where focus is coming from, and therefore, where to show selection, or
6311 * forward focus change internally.
6312 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006313 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6314 * false), or if it is focusable and it is not focusable in touch mode
6315 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006316 *
6317 * A View will not take focus if it is not visible.
6318 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006319 * A View will not take focus if one of its parents has
6320 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6321 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006323 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 * have focus, and you want your parent to look for the next one.
6325 *
6326 * You may wish to override this method if your custom {@link View} has an internal
6327 * {@link View} that it wishes to forward the request to.
6328 *
6329 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6330 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6331 * to give a finer grained hint about where focus is coming from. May be null
6332 * if there is no hint.
6333 * @return Whether this view or one of its descendants actually took focus.
6334 */
6335 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006336 return requestFocusNoSearch(direction, previouslyFocusedRect);
6337 }
6338
6339 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 // need to be focusable
6341 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6342 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6343 return false;
6344 }
6345
6346 // need to be focusable in touch mode if in touch mode
6347 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006348 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6349 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 }
6351
6352 // need to not have any parents blocking us
6353 if (hasAncestorThatBlocksDescendantFocus()) {
6354 return false;
6355 }
6356
6357 handleFocusGainInternal(direction, previouslyFocusedRect);
6358 return true;
6359 }
6360
6361 /**
6362 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6363 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6364 * touch mode to request focus when they are touched.
6365 *
6366 * @return Whether this view or one of its descendants actually took focus.
6367 *
6368 * @see #isInTouchMode()
6369 *
6370 */
6371 public final boolean requestFocusFromTouch() {
6372 // Leave touch mode if we need to
6373 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006374 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006375 if (viewRoot != null) {
6376 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 }
6378 }
6379 return requestFocus(View.FOCUS_DOWN);
6380 }
6381
6382 /**
6383 * @return Whether any ancestor of this view blocks descendant focus.
6384 */
6385 private boolean hasAncestorThatBlocksDescendantFocus() {
6386 ViewParent ancestor = mParent;
6387 while (ancestor instanceof ViewGroup) {
6388 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6389 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6390 return true;
6391 } else {
6392 ancestor = vgAncestor.getParent();
6393 }
6394 }
6395 return false;
6396 }
6397
6398 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006399 * Gets the mode for determining whether this View is important for accessibility
6400 * which is if it fires accessibility events and if it is reported to
6401 * accessibility services that query the screen.
6402 *
6403 * @return The mode for determining whether a View is important for accessibility.
6404 *
6405 * @attr ref android.R.styleable#View_importantForAccessibility
6406 *
6407 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6408 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6409 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6410 */
6411 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6412 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6413 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6414 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6415 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6416 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6417 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6418 })
6419 public int getImportantForAccessibility() {
6420 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6421 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6422 }
6423
6424 /**
6425 * Sets how to determine whether this view is important for accessibility
6426 * which is if it fires accessibility events and if it is reported to
6427 * accessibility services that query the screen.
6428 *
6429 * @param mode How to determine whether this view is important for accessibility.
6430 *
6431 * @attr ref android.R.styleable#View_importantForAccessibility
6432 *
6433 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6434 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6435 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6436 */
6437 public void setImportantForAccessibility(int mode) {
6438 if (mode != getImportantForAccessibility()) {
6439 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6440 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6441 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6442 notifyAccessibilityStateChanged();
6443 }
6444 }
6445
6446 /**
6447 * Gets whether this view should be exposed for accessibility.
6448 *
6449 * @return Whether the view is exposed for accessibility.
6450 *
6451 * @hide
6452 */
6453 public boolean isImportantForAccessibility() {
6454 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6455 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6456 switch (mode) {
6457 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6458 return true;
6459 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6460 return false;
6461 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6462 return isActionableForAccessibility() || hasListenersForAccessibility();
6463 default:
6464 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6465 + mode);
6466 }
6467 }
6468
6469 /**
6470 * Gets the parent for accessibility purposes. Note that the parent for
6471 * accessibility is not necessary the immediate parent. It is the first
6472 * predecessor that is important for accessibility.
6473 *
6474 * @return The parent for accessibility purposes.
6475 */
6476 public ViewParent getParentForAccessibility() {
6477 if (mParent instanceof View) {
6478 View parentView = (View) mParent;
6479 if (parentView.includeForAccessibility()) {
6480 return mParent;
6481 } else {
6482 return mParent.getParentForAccessibility();
6483 }
6484 }
6485 return null;
6486 }
6487
6488 /**
6489 * Adds the children of a given View for accessibility. Since some Views are
6490 * not important for accessibility the children for accessibility are not
6491 * necessarily direct children of the riew, rather they are the first level of
6492 * descendants important for accessibility.
6493 *
6494 * @param children The list of children for accessibility.
6495 */
6496 public void addChildrenForAccessibility(ArrayList<View> children) {
6497 if (includeForAccessibility()) {
6498 children.add(this);
6499 }
6500 }
6501
6502 /**
6503 * Whether to regard this view for accessibility. A view is regarded for
6504 * accessibility if it is important for accessibility or the querying
6505 * accessibility service has explicitly requested that view not
6506 * important for accessibility are regarded.
6507 *
6508 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006509 *
6510 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006511 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006512 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006513 if (mAttachInfo != null) {
6514 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006515 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006516 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006517 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006518 }
6519 return false;
6520 }
6521
6522 /**
6523 * Returns whether the View is considered actionable from
6524 * accessibility perspective. Such view are important for
6525 * accessiiblity.
6526 *
6527 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006528 *
6529 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006530 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006531 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006532 return (isClickable() || isLongClickable() || isFocusable());
6533 }
6534
6535 /**
6536 * Returns whether the View has registered callbacks wich makes it
6537 * important for accessiiblity.
6538 *
6539 * @return True if the view is actionable for accessibility.
6540 */
6541 private boolean hasListenersForAccessibility() {
6542 ListenerInfo info = getListenerInfo();
6543 return mTouchDelegate != null || info.mOnKeyListener != null
6544 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6545 || info.mOnHoverListener != null || info.mOnDragListener != null;
6546 }
6547
6548 /**
6549 * Notifies accessibility services that some view's important for
6550 * accessibility state has changed. Note that such notifications
6551 * are made at most once every
6552 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6553 * to avoid unnecessary load to the system. Also once a view has
6554 * made a notifucation this method is a NOP until the notification has
6555 * been sent to clients.
6556 *
6557 * @hide
6558 *
6559 * TODO: Makse sure this method is called for any view state change
6560 * that is interesting for accessilility purposes.
6561 */
6562 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006563 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6564 return;
6565 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6567 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6568 if (mParent != null) {
6569 mParent.childAccessibilityStateChanged(this);
6570 }
6571 }
6572 }
6573
6574 /**
6575 * Reset the state indicating the this view has requested clients
6576 * interested in its accessiblity state to be notified.
6577 *
6578 * @hide
6579 */
6580 public void resetAccessibilityStateChanged() {
6581 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6582 }
6583
6584 /**
6585 * Performs the specified accessibility action on the view. For
6586 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006587 * <p>
6588 * If an {@link AccessibilityDelegate} has been specified via calling
6589 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6590 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6591 * is responsible for handling this call.
6592 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006593 *
6594 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006595 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006596 * @return Whether the action was performed.
6597 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006598 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006599 if (mAccessibilityDelegate != null) {
6600 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6601 } else {
6602 return performAccessibilityActionInternal(action, arguments);
6603 }
6604 }
6605
6606 /**
6607 * @see #performAccessibilityAction(int, Bundle)
6608 *
6609 * Note: Called from the default {@link AccessibilityDelegate}.
6610 */
6611 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006612 switch (action) {
6613 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006614 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006615 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006616 }
6617 } break;
6618 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6619 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006620 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006621 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006622 } break;
6623 case AccessibilityNodeInfo.ACTION_FOCUS: {
6624 if (!hasFocus()) {
6625 // Get out of touch mode since accessibility
6626 // wants to move focus around.
6627 getViewRootImpl().ensureTouchMode(false);
6628 return requestFocus();
6629 }
6630 } break;
6631 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6632 if (hasFocus()) {
6633 clearFocus();
6634 return !isFocused();
6635 }
6636 } break;
6637 case AccessibilityNodeInfo.ACTION_SELECT: {
6638 if (!isSelected()) {
6639 setSelected(true);
6640 return isSelected();
6641 }
6642 } break;
6643 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6644 if (isSelected()) {
6645 setSelected(false);
6646 return !isSelected();
6647 }
6648 } break;
6649 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6650 if (!isAccessibilityFocused()) {
6651 return requestAccessibilityFocus();
6652 }
6653 } break;
6654 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6655 if (isAccessibilityFocused()) {
6656 clearAccessibilityFocus();
6657 return true;
6658 }
6659 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006660 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6661 if (arguments != null) {
6662 final int granularity = arguments.getInt(
6663 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6664 return nextAtGranularity(granularity);
6665 }
6666 } break;
6667 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6668 if (arguments != null) {
6669 final int granularity = arguments.getInt(
6670 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6671 return previousAtGranularity(granularity);
6672 }
6673 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006674 }
6675 return false;
6676 }
6677
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006678 private boolean nextAtGranularity(int granularity) {
6679 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006680 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006681 return false;
6682 }
6683 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6684 if (iterator == null) {
6685 return false;
6686 }
6687 final int current = getAccessibilityCursorPosition();
6688 final int[] range = iterator.following(current);
6689 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006690 return false;
6691 }
6692 final int start = range[0];
6693 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006694 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006695 sendViewTextTraversedAtGranularityEvent(
6696 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6697 granularity, start, end);
6698 return true;
6699 }
6700
6701 private boolean previousAtGranularity(int granularity) {
6702 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006703 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006704 return false;
6705 }
6706 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6707 if (iterator == null) {
6708 return false;
6709 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006710 int current = getAccessibilityCursorPosition();
6711 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6712 current = text.length();
6713 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6714 // When traversing by character we always put the cursor after the character
6715 // to ease edit and have to compensate before asking the for previous segment.
6716 current--;
6717 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006718 final int[] range = iterator.preceding(current);
6719 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006720 return false;
6721 }
6722 final int start = range[0];
6723 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006724 // Always put the cursor after the character to ease edit.
6725 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6726 setAccessibilityCursorPosition(end);
6727 } else {
6728 setAccessibilityCursorPosition(start);
6729 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006730 sendViewTextTraversedAtGranularityEvent(
6731 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6732 granularity, start, end);
6733 return true;
6734 }
6735
6736 /**
6737 * Gets the text reported for accessibility purposes.
6738 *
6739 * @return The accessibility text.
6740 *
6741 * @hide
6742 */
6743 public CharSequence getIterableTextForAccessibility() {
6744 return mContentDescription;
6745 }
6746
6747 /**
6748 * @hide
6749 */
6750 public int getAccessibilityCursorPosition() {
6751 return mAccessibilityCursorPosition;
6752 }
6753
6754 /**
6755 * @hide
6756 */
6757 public void setAccessibilityCursorPosition(int position) {
6758 mAccessibilityCursorPosition = position;
6759 }
6760
6761 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6762 int fromIndex, int toIndex) {
6763 if (mParent == null) {
6764 return;
6765 }
6766 AccessibilityEvent event = AccessibilityEvent.obtain(
6767 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6768 onInitializeAccessibilityEvent(event);
6769 onPopulateAccessibilityEvent(event);
6770 event.setFromIndex(fromIndex);
6771 event.setToIndex(toIndex);
6772 event.setAction(action);
6773 event.setMovementGranularity(granularity);
6774 mParent.requestSendAccessibilityEvent(this, event);
6775 }
6776
6777 /**
6778 * @hide
6779 */
6780 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6781 switch (granularity) {
6782 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6783 CharSequence text = getIterableTextForAccessibility();
6784 if (text != null && text.length() > 0) {
6785 CharacterTextSegmentIterator iterator =
6786 CharacterTextSegmentIterator.getInstance(mContext);
6787 iterator.initialize(text.toString());
6788 return iterator;
6789 }
6790 } break;
6791 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6792 CharSequence text = getIterableTextForAccessibility();
6793 if (text != null && text.length() > 0) {
6794 WordTextSegmentIterator iterator =
6795 WordTextSegmentIterator.getInstance(mContext);
6796 iterator.initialize(text.toString());
6797 return iterator;
6798 }
6799 } break;
6800 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6801 CharSequence text = getIterableTextForAccessibility();
6802 if (text != null && text.length() > 0) {
6803 ParagraphTextSegmentIterator iterator =
6804 ParagraphTextSegmentIterator.getInstance();
6805 iterator.initialize(text.toString());
6806 return iterator;
6807 }
6808 } break;
6809 }
6810 return null;
6811 }
6812
Svetoslav Ganov42138042012-03-20 11:51:39 -07006813 /**
Romain Guya440b002010-02-24 15:57:54 -08006814 * @hide
6815 */
6816 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006817 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006818 onStartTemporaryDetach();
6819 }
6820
6821 /**
6822 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6824 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006825 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826 */
6827 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006828 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006829 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006830 }
6831
6832 /**
6833 * @hide
6834 */
6835 public void dispatchFinishTemporaryDetach() {
6836 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837 }
Romain Guy8506ab42009-06-11 17:35:47 -07006838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 /**
6840 * Called after {@link #onStartTemporaryDetach} when the container is done
6841 * changing the view.
6842 */
6843 public void onFinishTemporaryDetach() {
6844 }
Romain Guy8506ab42009-06-11 17:35:47 -07006845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006847 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6848 * for this view's window. Returns null if the view is not currently attached
6849 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006850 * just use the standard high-level event callbacks like
6851 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006852 */
6853 public KeyEvent.DispatcherState getKeyDispatcherState() {
6854 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6855 }
Joe Malin32736f02011-01-19 16:14:20 -08006856
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858 * Dispatch a key event before it is processed by any input method
6859 * associated with the view hierarchy. This can be used to intercept
6860 * key events in special situations before the IME consumes them; a
6861 * typical example would be handling the BACK key to update the application's
6862 * UI instead of allowing the IME to see it and close itself.
6863 *
6864 * @param event The key event to be dispatched.
6865 * @return True if the event was handled, false otherwise.
6866 */
6867 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6868 return onKeyPreIme(event.getKeyCode(), event);
6869 }
6870
6871 /**
6872 * Dispatch a key event to the next view on the focus path. This path runs
6873 * from the top of the view tree down to the currently focused view. If this
6874 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6875 * the next node down the focus path. This method also fires any key
6876 * listeners.
6877 *
6878 * @param event The key event to be dispatched.
6879 * @return True if the event was handled, false otherwise.
6880 */
6881 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006882 if (mInputEventConsistencyVerifier != null) {
6883 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885
Jeff Brown21bc5c92011-02-28 18:27:14 -08006886 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006887 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006888 ListenerInfo li = mListenerInfo;
6889 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6890 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891 return true;
6892 }
6893
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006894 if (event.dispatch(this, mAttachInfo != null
6895 ? mAttachInfo.mKeyDispatchState : null, this)) {
6896 return true;
6897 }
6898
6899 if (mInputEventConsistencyVerifier != null) {
6900 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6901 }
6902 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 }
6904
6905 /**
6906 * Dispatches a key shortcut event.
6907 *
6908 * @param event The key event to be dispatched.
6909 * @return True if the event was handled by the view, false otherwise.
6910 */
6911 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6912 return onKeyShortcut(event.getKeyCode(), event);
6913 }
6914
6915 /**
6916 * Pass the touch screen motion event down to the target view, or this
6917 * view if it is the target.
6918 *
6919 * @param event The motion event to be dispatched.
6920 * @return True if the event was handled by the view, false otherwise.
6921 */
6922 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006923 if (mInputEventConsistencyVerifier != null) {
6924 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6925 }
6926
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006927 if (onFilterTouchEventForSecurity(event)) {
6928 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006929 ListenerInfo li = mListenerInfo;
6930 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6931 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006932 return true;
6933 }
6934
6935 if (onTouchEvent(event)) {
6936 return true;
6937 }
Jeff Brown85a31762010-09-01 17:01:00 -07006938 }
6939
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006940 if (mInputEventConsistencyVerifier != null) {
6941 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006942 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006943 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 }
6945
6946 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006947 * Filter the touch event to apply security policies.
6948 *
6949 * @param event The motion event to be filtered.
6950 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006951 *
Jeff Brown85a31762010-09-01 17:01:00 -07006952 * @see #getFilterTouchesWhenObscured
6953 */
6954 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006955 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006956 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6957 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6958 // Window is obscured, drop this touch.
6959 return false;
6960 }
6961 return true;
6962 }
6963
6964 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 * Pass a trackball motion event down to the focused view.
6966 *
6967 * @param event The motion event to be dispatched.
6968 * @return True if the event was handled by the view, false otherwise.
6969 */
6970 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006971 if (mInputEventConsistencyVerifier != null) {
6972 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6973 }
6974
Romain Guy02ccac62011-06-24 13:20:23 -07006975 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 }
6977
6978 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006979 * Dispatch a generic motion event.
6980 * <p>
6981 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6982 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006983 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006984 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006985 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006986 *
6987 * @param event The motion event to be dispatched.
6988 * @return True if the event was handled by the view, false otherwise.
6989 */
6990 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006991 if (mInputEventConsistencyVerifier != null) {
6992 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6993 }
6994
Jeff Browna032cc02011-03-07 16:56:21 -08006995 final int source = event.getSource();
6996 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6997 final int action = event.getAction();
6998 if (action == MotionEvent.ACTION_HOVER_ENTER
6999 || action == MotionEvent.ACTION_HOVER_MOVE
7000 || action == MotionEvent.ACTION_HOVER_EXIT) {
7001 if (dispatchHoverEvent(event)) {
7002 return true;
7003 }
7004 } else if (dispatchGenericPointerEvent(event)) {
7005 return true;
7006 }
7007 } else if (dispatchGenericFocusedEvent(event)) {
7008 return true;
7009 }
7010
Jeff Brown10b62902011-06-20 16:40:37 -07007011 if (dispatchGenericMotionEventInternal(event)) {
7012 return true;
7013 }
7014
7015 if (mInputEventConsistencyVerifier != null) {
7016 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7017 }
7018 return false;
7019 }
7020
7021 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007022 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007023 ListenerInfo li = mListenerInfo;
7024 if (li != null && li.mOnGenericMotionListener != null
7025 && (mViewFlags & ENABLED_MASK) == ENABLED
7026 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007027 return true;
7028 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007029
7030 if (onGenericMotionEvent(event)) {
7031 return true;
7032 }
7033
7034 if (mInputEventConsistencyVerifier != null) {
7035 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7036 }
7037 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007038 }
7039
7040 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007041 * Dispatch a hover event.
7042 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007043 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007044 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007045 * </p>
7046 *
7047 * @param event The motion event to be dispatched.
7048 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007049 */
7050 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007051 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007052 ListenerInfo li = mListenerInfo;
7053 if (li != null && li.mOnHoverListener != null
7054 && (mViewFlags & ENABLED_MASK) == ENABLED
7055 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007056 return true;
7057 }
7058
Jeff Browna032cc02011-03-07 16:56:21 -08007059 return onHoverEvent(event);
7060 }
7061
7062 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007063 * Returns true if the view has a child to which it has recently sent
7064 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7065 * it does not have a hovered child, then it must be the innermost hovered view.
7066 * @hide
7067 */
7068 protected boolean hasHoveredChild() {
7069 return false;
7070 }
7071
7072 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007073 * Dispatch a generic motion event to the view under the first pointer.
7074 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007075 * Do not call this method directly.
7076 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007077 * </p>
7078 *
7079 * @param event The motion event to be dispatched.
7080 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007081 */
7082 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7083 return false;
7084 }
7085
7086 /**
7087 * Dispatch a generic motion event to the currently focused view.
7088 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007089 * Do not call this method directly.
7090 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007091 * </p>
7092 *
7093 * @param event The motion event to be dispatched.
7094 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007095 */
7096 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7097 return false;
7098 }
7099
7100 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007101 * Dispatch a pointer event.
7102 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007103 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7104 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7105 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007106 * and should not be expected to handle other pointing device features.
7107 * </p>
7108 *
7109 * @param event The motion event to be dispatched.
7110 * @return True if the event was handled by the view, false otherwise.
7111 * @hide
7112 */
7113 public final boolean dispatchPointerEvent(MotionEvent event) {
7114 if (event.isTouchEvent()) {
7115 return dispatchTouchEvent(event);
7116 } else {
7117 return dispatchGenericMotionEvent(event);
7118 }
7119 }
7120
7121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 * Called when the window containing this view gains or loses window focus.
7123 * ViewGroups should override to route to their children.
7124 *
7125 * @param hasFocus True if the window containing this view now has focus,
7126 * false otherwise.
7127 */
7128 public void dispatchWindowFocusChanged(boolean hasFocus) {
7129 onWindowFocusChanged(hasFocus);
7130 }
7131
7132 /**
7133 * Called when the window containing this view gains or loses focus. Note
7134 * that this is separate from view focus: to receive key events, both
7135 * your view and its window must have focus. If a window is displayed
7136 * on top of yours that takes input focus, then your own window will lose
7137 * focus but the view focus will remain unchanged.
7138 *
7139 * @param hasWindowFocus True if the window containing this view now has
7140 * focus, false otherwise.
7141 */
7142 public void onWindowFocusChanged(boolean hasWindowFocus) {
7143 InputMethodManager imm = InputMethodManager.peekInstance();
7144 if (!hasWindowFocus) {
7145 if (isPressed()) {
7146 setPressed(false);
7147 }
7148 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7149 imm.focusOut(this);
7150 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007151 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007152 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007153 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7155 imm.focusIn(this);
7156 }
7157 refreshDrawableState();
7158 }
7159
7160 /**
7161 * Returns true if this view is in a window that currently has window focus.
7162 * Note that this is not the same as the view itself having focus.
7163 *
7164 * @return True if this view is in a window that currently has window focus.
7165 */
7166 public boolean hasWindowFocus() {
7167 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7168 }
7169
7170 /**
Adam Powell326d8082009-12-09 15:10:07 -08007171 * Dispatch a view visibility change down the view hierarchy.
7172 * ViewGroups should override to route to their children.
7173 * @param changedView The view whose visibility changed. Could be 'this' or
7174 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007175 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7176 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007177 */
7178 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7179 onVisibilityChanged(changedView, visibility);
7180 }
7181
7182 /**
7183 * Called when the visibility of the view or an ancestor of the view is changed.
7184 * @param changedView The view whose visibility changed. Could be 'this' or
7185 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007186 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7187 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007188 */
7189 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007190 if (visibility == VISIBLE) {
7191 if (mAttachInfo != null) {
7192 initialAwakenScrollBars();
7193 } else {
7194 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7195 }
7196 }
Adam Powell326d8082009-12-09 15:10:07 -08007197 }
7198
7199 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007200 * Dispatch a hint about whether this view is displayed. For instance, when
7201 * a View moves out of the screen, it might receives a display hint indicating
7202 * the view is not displayed. Applications should not <em>rely</em> on this hint
7203 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007204 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007205 * @param hint A hint about whether or not this view is displayed:
7206 * {@link #VISIBLE} or {@link #INVISIBLE}.
7207 */
7208 public void dispatchDisplayHint(int hint) {
7209 onDisplayHint(hint);
7210 }
7211
7212 /**
7213 * Gives this view a hint about whether is displayed or not. For instance, when
7214 * a View moves out of the screen, it might receives a display hint indicating
7215 * the view is not displayed. Applications should not <em>rely</em> on this hint
7216 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007217 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007218 * @param hint A hint about whether or not this view is displayed:
7219 * {@link #VISIBLE} or {@link #INVISIBLE}.
7220 */
7221 protected void onDisplayHint(int hint) {
7222 }
7223
7224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 * Dispatch a window visibility change down the view hierarchy.
7226 * ViewGroups should override to route to their children.
7227 *
7228 * @param visibility The new visibility of the window.
7229 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007230 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 */
7232 public void dispatchWindowVisibilityChanged(int visibility) {
7233 onWindowVisibilityChanged(visibility);
7234 }
7235
7236 /**
7237 * Called when the window containing has change its visibility
7238 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7239 * that this tells you whether or not your window is being made visible
7240 * to the window manager; this does <em>not</em> tell you whether or not
7241 * your window is obscured by other windows on the screen, even if it
7242 * is itself visible.
7243 *
7244 * @param visibility The new visibility of the window.
7245 */
7246 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007247 if (visibility == VISIBLE) {
7248 initialAwakenScrollBars();
7249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 }
7251
7252 /**
7253 * Returns the current visibility of the window this view is attached to
7254 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7255 *
7256 * @return Returns the current visibility of the view's window.
7257 */
7258 public int getWindowVisibility() {
7259 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7260 }
7261
7262 /**
7263 * Retrieve the overall visible display size in which the window this view is
7264 * attached to has been positioned in. This takes into account screen
7265 * decorations above the window, for both cases where the window itself
7266 * is being position inside of them or the window is being placed under
7267 * then and covered insets are used for the window to position its content
7268 * inside. In effect, this tells you the available area where content can
7269 * be placed and remain visible to users.
7270 *
7271 * <p>This function requires an IPC back to the window manager to retrieve
7272 * the requested information, so should not be used in performance critical
7273 * code like drawing.
7274 *
7275 * @param outRect Filled in with the visible display frame. If the view
7276 * is not attached to a window, this is simply the raw display size.
7277 */
7278 public void getWindowVisibleDisplayFrame(Rect outRect) {
7279 if (mAttachInfo != null) {
7280 try {
7281 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7282 } catch (RemoteException e) {
7283 return;
7284 }
7285 // XXX This is really broken, and probably all needs to be done
7286 // in the window manager, and we need to know more about whether
7287 // we want the area behind or in front of the IME.
7288 final Rect insets = mAttachInfo.mVisibleInsets;
7289 outRect.left += insets.left;
7290 outRect.top += insets.top;
7291 outRect.right -= insets.right;
7292 outRect.bottom -= insets.bottom;
7293 return;
7294 }
7295 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007296 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 }
7298
7299 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007300 * Dispatch a notification about a resource configuration change down
7301 * the view hierarchy.
7302 * ViewGroups should override to route to their children.
7303 *
7304 * @param newConfig The new resource configuration.
7305 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007306 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007307 */
7308 public void dispatchConfigurationChanged(Configuration newConfig) {
7309 onConfigurationChanged(newConfig);
7310 }
7311
7312 /**
7313 * Called when the current configuration of the resources being used
7314 * by the application have changed. You can use this to decide when
7315 * to reload resources that can changed based on orientation and other
7316 * configuration characterstics. You only need to use this if you are
7317 * not relying on the normal {@link android.app.Activity} mechanism of
7318 * recreating the activity instance upon a configuration change.
7319 *
7320 * @param newConfig The new resource configuration.
7321 */
7322 protected void onConfigurationChanged(Configuration newConfig) {
7323 }
7324
7325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 * Private function to aggregate all per-view attributes in to the view
7327 * root.
7328 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007329 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7330 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 }
7332
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007333 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7334 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007335 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007336 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007337 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007338 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007339 ListenerInfo li = mListenerInfo;
7340 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007341 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007343 }
7344 }
7345
7346 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007347 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007348 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007349 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7350 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 ai.mRecomputeGlobalAttributes = true;
7352 }
7353 }
7354 }
7355
7356 /**
7357 * Returns whether the device is currently in touch mode. Touch mode is entered
7358 * once the user begins interacting with the device by touch, and affects various
7359 * things like whether focus is always visible to the user.
7360 *
7361 * @return Whether the device is in touch mode.
7362 */
7363 @ViewDebug.ExportedProperty
7364 public boolean isInTouchMode() {
7365 if (mAttachInfo != null) {
7366 return mAttachInfo.mInTouchMode;
7367 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007368 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 }
7370 }
7371
7372 /**
7373 * Returns the context the view is running in, through which it can
7374 * access the current theme, resources, etc.
7375 *
7376 * @return The view's Context.
7377 */
7378 @ViewDebug.CapturedViewProperty
7379 public final Context getContext() {
7380 return mContext;
7381 }
7382
7383 /**
7384 * Handle a key event before it is processed by any input method
7385 * associated with the view hierarchy. This can be used to intercept
7386 * key events in special situations before the IME consumes them; a
7387 * typical example would be handling the BACK key to update the application's
7388 * UI instead of allowing the IME to see it and close itself.
7389 *
7390 * @param keyCode The value in event.getKeyCode().
7391 * @param event Description of the key event.
7392 * @return If you handled the event, return true. If you want to allow the
7393 * event to be handled by the next receiver, return false.
7394 */
7395 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7396 return false;
7397 }
7398
7399 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007400 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7401 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7403 * is released, if the view is enabled and clickable.
7404 *
7405 * @param keyCode A key code that represents the button pressed, from
7406 * {@link android.view.KeyEvent}.
7407 * @param event The KeyEvent object that defines the button action.
7408 */
7409 public boolean onKeyDown(int keyCode, KeyEvent event) {
7410 boolean result = false;
7411
7412 switch (keyCode) {
7413 case KeyEvent.KEYCODE_DPAD_CENTER:
7414 case KeyEvent.KEYCODE_ENTER: {
7415 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7416 return true;
7417 }
7418 // Long clickable items don't necessarily have to be clickable
7419 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7420 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7421 (event.getRepeatCount() == 0)) {
7422 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007423 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 return true;
7425 }
7426 break;
7427 }
7428 }
7429 return result;
7430 }
7431
7432 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007433 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7434 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7435 * the event).
7436 */
7437 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7438 return false;
7439 }
7440
7441 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007442 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7443 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7445 * {@link KeyEvent#KEYCODE_ENTER} is released.
7446 *
7447 * @param keyCode A key code that represents the button pressed, from
7448 * {@link android.view.KeyEvent}.
7449 * @param event The KeyEvent object that defines the button action.
7450 */
7451 public boolean onKeyUp(int keyCode, KeyEvent event) {
7452 boolean result = false;
7453
7454 switch (keyCode) {
7455 case KeyEvent.KEYCODE_DPAD_CENTER:
7456 case KeyEvent.KEYCODE_ENTER: {
7457 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7458 return true;
7459 }
7460 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7461 setPressed(false);
7462
7463 if (!mHasPerformedLongPress) {
7464 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007465 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007466
7467 result = performClick();
7468 }
7469 }
7470 break;
7471 }
7472 }
7473 return result;
7474 }
7475
7476 /**
7477 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7478 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7479 * the event).
7480 *
7481 * @param keyCode A key code that represents the button pressed, from
7482 * {@link android.view.KeyEvent}.
7483 * @param repeatCount The number of times the action was made.
7484 * @param event The KeyEvent object that defines the button action.
7485 */
7486 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7487 return false;
7488 }
7489
7490 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007491 * Called on the focused view when a key shortcut event is not handled.
7492 * Override this method to implement local key shortcuts for the View.
7493 * Key shortcuts can also be implemented by setting the
7494 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 *
7496 * @param keyCode The value in event.getKeyCode().
7497 * @param event Description of the key event.
7498 * @return If you handled the event, return true. If you want to allow the
7499 * event to be handled by the next receiver, return false.
7500 */
7501 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7502 return false;
7503 }
7504
7505 /**
7506 * Check whether the called view is a text editor, in which case it
7507 * would make sense to automatically display a soft input window for
7508 * it. Subclasses should override this if they implement
7509 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007510 * a call on that method would return a non-null InputConnection, and
7511 * they are really a first-class editor that the user would normally
7512 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007513 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007514 * <p>The default implementation always returns false. This does
7515 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7516 * will not be called or the user can not otherwise perform edits on your
7517 * view; it is just a hint to the system that this is not the primary
7518 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007519 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 * @return Returns true if this view is a text editor, else false.
7521 */
7522 public boolean onCheckIsTextEditor() {
7523 return false;
7524 }
Romain Guy8506ab42009-06-11 17:35:47 -07007525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 /**
7527 * Create a new InputConnection for an InputMethod to interact
7528 * with the view. The default implementation returns null, since it doesn't
7529 * support input methods. You can override this to implement such support.
7530 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007531 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532 * <p>When implementing this, you probably also want to implement
7533 * {@link #onCheckIsTextEditor()} to indicate you will return a
7534 * non-null InputConnection.
7535 *
7536 * @param outAttrs Fill in with attribute information about the connection.
7537 */
7538 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7539 return null;
7540 }
7541
7542 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007543 * Called by the {@link android.view.inputmethod.InputMethodManager}
7544 * when a view who is not the current
7545 * input connection target is trying to make a call on the manager. The
7546 * default implementation returns false; you can override this to return
7547 * true for certain views if you are performing InputConnection proxying
7548 * to them.
7549 * @param view The View that is making the InputMethodManager call.
7550 * @return Return true to allow the call, false to reject.
7551 */
7552 public boolean checkInputConnectionProxy(View view) {
7553 return false;
7554 }
Romain Guy8506ab42009-06-11 17:35:47 -07007555
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 * Show the context menu for this view. It is not safe to hold on to the
7558 * menu after returning from this method.
7559 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007560 * You should normally not overload this method. Overload
7561 * {@link #onCreateContextMenu(ContextMenu)} or define an
7562 * {@link OnCreateContextMenuListener} to add items to the context menu.
7563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 * @param menu The context menu to populate
7565 */
7566 public void createContextMenu(ContextMenu menu) {
7567 ContextMenuInfo menuInfo = getContextMenuInfo();
7568
7569 // Sets the current menu info so all items added to menu will have
7570 // my extra info set.
7571 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7572
7573 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007574 ListenerInfo li = mListenerInfo;
7575 if (li != null && li.mOnCreateContextMenuListener != null) {
7576 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
7578
7579 // Clear the extra information so subsequent items that aren't mine don't
7580 // have my extra info.
7581 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7582
7583 if (mParent != null) {
7584 mParent.createContextMenu(menu);
7585 }
7586 }
7587
7588 /**
7589 * Views should implement this if they have extra information to associate
7590 * with the context menu. The return result is supplied as a parameter to
7591 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7592 * callback.
7593 *
7594 * @return Extra information about the item for which the context menu
7595 * should be shown. This information will vary across different
7596 * subclasses of View.
7597 */
7598 protected ContextMenuInfo getContextMenuInfo() {
7599 return null;
7600 }
7601
7602 /**
7603 * Views should implement this if the view itself is going to add items to
7604 * the context menu.
7605 *
7606 * @param menu the context menu to populate
7607 */
7608 protected void onCreateContextMenu(ContextMenu menu) {
7609 }
7610
7611 /**
7612 * Implement this method to handle trackball motion events. The
7613 * <em>relative</em> movement of the trackball since the last event
7614 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7615 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7616 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7617 * they will often be fractional values, representing the more fine-grained
7618 * movement information available from a trackball).
7619 *
7620 * @param event The motion event.
7621 * @return True if the event was handled, false otherwise.
7622 */
7623 public boolean onTrackballEvent(MotionEvent event) {
7624 return false;
7625 }
7626
7627 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007628 * Implement this method to handle generic motion events.
7629 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007630 * Generic motion events describe joystick movements, mouse hovers, track pad
7631 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007632 * {@link MotionEvent#getSource() source} of the motion event specifies
7633 * the class of input that was received. Implementations of this method
7634 * must examine the bits in the source before processing the event.
7635 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007636 * </p><p>
7637 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7638 * are delivered to the view under the pointer. All other generic motion events are
7639 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007640 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007641 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007642 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007643 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7644 * // process the joystick movement...
7645 * return true;
7646 * }
7647 * }
7648 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7649 * switch (event.getAction()) {
7650 * case MotionEvent.ACTION_HOVER_MOVE:
7651 * // process the mouse hover movement...
7652 * return true;
7653 * case MotionEvent.ACTION_SCROLL:
7654 * // process the scroll wheel movement...
7655 * return true;
7656 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007657 * }
7658 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007659 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007660 *
7661 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007662 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007663 */
7664 public boolean onGenericMotionEvent(MotionEvent event) {
7665 return false;
7666 }
7667
7668 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007669 * Implement this method to handle hover events.
7670 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007671 * This method is called whenever a pointer is hovering into, over, or out of the
7672 * bounds of a view and the view is not currently being touched.
7673 * Hover events are represented as pointer events with action
7674 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7675 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7676 * </p>
7677 * <ul>
7678 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7679 * when the pointer enters the bounds of the view.</li>
7680 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7681 * when the pointer has already entered the bounds of the view and has moved.</li>
7682 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7683 * when the pointer has exited the bounds of the view or when the pointer is
7684 * about to go down due to a button click, tap, or similar user action that
7685 * causes the view to be touched.</li>
7686 * </ul>
7687 * <p>
7688 * The view should implement this method to return true to indicate that it is
7689 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007690 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007691 * The default implementation calls {@link #setHovered} to update the hovered state
7692 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007693 * is enabled and is clickable. The default implementation also sends hover
7694 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007695 * </p>
7696 *
7697 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007698 * @return True if the view handled the hover event.
7699 *
7700 * @see #isHovered
7701 * @see #setHovered
7702 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007703 */
7704 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007705 // The root view may receive hover (or touch) events that are outside the bounds of
7706 // the window. This code ensures that we only send accessibility events for
7707 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007708 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007709 if (!mSendingHoverAccessibilityEvents) {
7710 if ((action == MotionEvent.ACTION_HOVER_ENTER
7711 || action == MotionEvent.ACTION_HOVER_MOVE)
7712 && !hasHoveredChild()
7713 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007714 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007715 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007716 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007717 }
7718 } else {
7719 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007720 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007721 && !pointInView(event.getX(), event.getY()))) {
7722 mSendingHoverAccessibilityEvents = false;
7723 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007724 // If the window does not have input focus we take away accessibility
7725 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007726 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007727 getViewRootImpl().setAccessibilityFocusedHost(null);
7728 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007729 }
Jeff Browna1b24182011-07-28 13:38:24 -07007730 }
7731
Jeff Brown87b7f802011-06-21 18:35:45 -07007732 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007733 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007734 case MotionEvent.ACTION_HOVER_ENTER:
7735 setHovered(true);
7736 break;
7737 case MotionEvent.ACTION_HOVER_EXIT:
7738 setHovered(false);
7739 break;
7740 }
Jeff Browna1b24182011-07-28 13:38:24 -07007741
7742 // Dispatch the event to onGenericMotionEvent before returning true.
7743 // This is to provide compatibility with existing applications that
7744 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7745 // break because of the new default handling for hoverable views
7746 // in onHoverEvent.
7747 // Note that onGenericMotionEvent will be called by default when
7748 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7749 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007750 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007751 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007752
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007753 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007754 }
7755
7756 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007757 * Returns true if the view should handle {@link #onHoverEvent}
7758 * by calling {@link #setHovered} to change its hovered state.
7759 *
7760 * @return True if the view is hoverable.
7761 */
7762 private boolean isHoverable() {
7763 final int viewFlags = mViewFlags;
7764 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7765 return false;
7766 }
7767
7768 return (viewFlags & CLICKABLE) == CLICKABLE
7769 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7770 }
7771
7772 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007773 * Returns true if the view is currently hovered.
7774 *
7775 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007776 *
7777 * @see #setHovered
7778 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007779 */
Jeff Brown10b62902011-06-20 16:40:37 -07007780 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007781 public boolean isHovered() {
7782 return (mPrivateFlags & HOVERED) != 0;
7783 }
7784
7785 /**
7786 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007787 * <p>
7788 * Calling this method also changes the drawable state of the view. This
7789 * enables the view to react to hover by using different drawable resources
7790 * to change its appearance.
7791 * </p><p>
7792 * The {@link #onHoverChanged} method is called when the hovered state changes.
7793 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007794 *
7795 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007796 *
7797 * @see #isHovered
7798 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007799 */
7800 public void setHovered(boolean hovered) {
7801 if (hovered) {
7802 if ((mPrivateFlags & HOVERED) == 0) {
7803 mPrivateFlags |= HOVERED;
7804 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007805 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007806 }
7807 } else {
7808 if ((mPrivateFlags & HOVERED) != 0) {
7809 mPrivateFlags &= ~HOVERED;
7810 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007811 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007812 }
7813 }
7814 }
7815
7816 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007817 * Implement this method to handle hover state changes.
7818 * <p>
7819 * This method is called whenever the hover state changes as a result of a
7820 * call to {@link #setHovered}.
7821 * </p>
7822 *
7823 * @param hovered The current hover state, as returned by {@link #isHovered}.
7824 *
7825 * @see #isHovered
7826 * @see #setHovered
7827 */
7828 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007829 }
7830
7831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 * Implement this method to handle touch screen motion events.
7833 *
7834 * @param event The motion event.
7835 * @return True if the event was handled, false otherwise.
7836 */
7837 public boolean onTouchEvent(MotionEvent event) {
7838 final int viewFlags = mViewFlags;
7839
7840 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007841 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007842 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 // A disabled view that is clickable still consumes the touch
7845 // events, it just doesn't respond to them.
7846 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7847 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7848 }
7849
7850 if (mTouchDelegate != null) {
7851 if (mTouchDelegate.onTouchEvent(event)) {
7852 return true;
7853 }
7854 }
7855
7856 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7857 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7858 switch (event.getAction()) {
7859 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007860 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7861 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 // take focus if we don't have it already and we should in
7863 // touch mode.
7864 boolean focusTaken = false;
7865 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7866 focusTaken = requestFocus();
7867 }
7868
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007869 if (prepressed) {
7870 // The button is being released before we actually
7871 // showed it as pressed. Make it show the pressed
7872 // state now (before scheduling the click) to ensure
7873 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007874 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007875 }
Joe Malin32736f02011-01-19 16:14:20 -08007876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 if (!mHasPerformedLongPress) {
7878 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007879 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880
7881 // Only perform take click actions if we were in the pressed state
7882 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007883 // Use a Runnable and post this rather than calling
7884 // performClick directly. This lets other visual state
7885 // of the view update before click actions start.
7886 if (mPerformClick == null) {
7887 mPerformClick = new PerformClick();
7888 }
7889 if (!post(mPerformClick)) {
7890 performClick();
7891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 }
7893 }
7894
7895 if (mUnsetPressedState == null) {
7896 mUnsetPressedState = new UnsetPressedState();
7897 }
7898
Adam Powelle14579b2009-12-16 18:39:52 -08007899 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007900 postDelayed(mUnsetPressedState,
7901 ViewConfiguration.getPressedStateDuration());
7902 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 // If the post failed, unpress right now
7904 mUnsetPressedState.run();
7905 }
Adam Powelle14579b2009-12-16 18:39:52 -08007906 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 }
7908 break;
7909
7910 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007911 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007912
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007913 if (performButtonActionOnTouchDown(event)) {
7914 break;
7915 }
7916
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007917 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007918 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007919
7920 // For views inside a scrolling container, delay the pressed feedback for
7921 // a short period in case this is a scroll.
7922 if (isInScrollingContainer) {
7923 mPrivateFlags |= PREPRESSED;
7924 if (mPendingCheckForTap == null) {
7925 mPendingCheckForTap = new CheckForTap();
7926 }
7927 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7928 } else {
7929 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007930 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007931 checkForLongClick(0);
7932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007933 break;
7934
7935 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007936 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007937 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 break;
7939
7940 case MotionEvent.ACTION_MOVE:
7941 final int x = (int) event.getX();
7942 final int y = (int) event.getY();
7943
7944 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007945 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007947 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007949 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007950 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951
Adam Powell4d6f0662012-02-21 15:11:11 -08007952 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 }
7955 break;
7956 }
7957 return true;
7958 }
7959
7960 return false;
7961 }
7962
7963 /**
Adam Powell10298662011-08-14 18:26:30 -07007964 * @hide
7965 */
7966 public boolean isInScrollingContainer() {
7967 ViewParent p = getParent();
7968 while (p != null && p instanceof ViewGroup) {
7969 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7970 return true;
7971 }
7972 p = p.getParent();
7973 }
7974 return false;
7975 }
7976
7977 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007978 * Remove the longpress detection timer.
7979 */
7980 private void removeLongPressCallback() {
7981 if (mPendingCheckForLongPress != null) {
7982 removeCallbacks(mPendingCheckForLongPress);
7983 }
7984 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007985
7986 /**
7987 * Remove the pending click action
7988 */
7989 private void removePerformClickCallback() {
7990 if (mPerformClick != null) {
7991 removeCallbacks(mPerformClick);
7992 }
7993 }
7994
Adam Powelle14579b2009-12-16 18:39:52 -08007995 /**
Romain Guya440b002010-02-24 15:57:54 -08007996 * Remove the prepress detection timer.
7997 */
7998 private void removeUnsetPressCallback() {
7999 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8000 setPressed(false);
8001 removeCallbacks(mUnsetPressedState);
8002 }
8003 }
8004
8005 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008006 * Remove the tap detection timer.
8007 */
8008 private void removeTapCallback() {
8009 if (mPendingCheckForTap != null) {
8010 mPrivateFlags &= ~PREPRESSED;
8011 removeCallbacks(mPendingCheckForTap);
8012 }
8013 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008014
8015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 * Cancels a pending long press. Your subclass can use this if you
8017 * want the context menu to come up if the user presses and holds
8018 * at the same place, but you don't want it to come up if they press
8019 * and then move around enough to cause scrolling.
8020 */
8021 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008022 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008023
8024 /*
8025 * The prepressed state handled by the tap callback is a display
8026 * construct, but the tap callback will post a long press callback
8027 * less its own timeout. Remove it here.
8028 */
8029 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030 }
8031
8032 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008033 * Remove the pending callback for sending a
8034 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8035 */
8036 private void removeSendViewScrolledAccessibilityEventCallback() {
8037 if (mSendViewScrolledAccessibilityEvent != null) {
8038 removeCallbacks(mSendViewScrolledAccessibilityEvent);
8039 }
8040 }
8041
8042 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 * Sets the TouchDelegate for this View.
8044 */
8045 public void setTouchDelegate(TouchDelegate delegate) {
8046 mTouchDelegate = delegate;
8047 }
8048
8049 /**
8050 * Gets the TouchDelegate for this View.
8051 */
8052 public TouchDelegate getTouchDelegate() {
8053 return mTouchDelegate;
8054 }
8055
8056 /**
8057 * Set flags controlling behavior of this view.
8058 *
8059 * @param flags Constant indicating the value which should be set
8060 * @param mask Constant indicating the bit range that should be changed
8061 */
8062 void setFlags(int flags, int mask) {
8063 int old = mViewFlags;
8064 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8065
8066 int changed = mViewFlags ^ old;
8067 if (changed == 0) {
8068 return;
8069 }
8070 int privateFlags = mPrivateFlags;
8071
8072 /* Check if the FOCUSABLE bit has changed */
8073 if (((changed & FOCUSABLE_MASK) != 0) &&
8074 ((privateFlags & HAS_BOUNDS) !=0)) {
8075 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8076 && ((privateFlags & FOCUSED) != 0)) {
8077 /* Give up focus if we are no longer focusable */
8078 clearFocus();
8079 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8080 && ((privateFlags & FOCUSED) == 0)) {
8081 /*
8082 * Tell the view system that we are now available to take focus
8083 * if no one else already has it.
8084 */
8085 if (mParent != null) mParent.focusableViewAvailable(this);
8086 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008087 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8088 notifyAccessibilityStateChanged();
8089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 }
8091
8092 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8093 if ((changed & VISIBILITY_MASK) != 0) {
8094 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008095 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008096 * it was not visible. Marking it drawn ensures that the invalidation will
8097 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008099 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008100 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101
8102 needGlobalAttributesUpdate(true);
8103
8104 // a view becoming visible is worth notifying the parent
8105 // about in case nothing has focus. even if this specific view
8106 // isn't focusable, it may contain something that is, so let
8107 // the root view try to give this focus if nothing else does.
8108 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8109 mParent.focusableViewAvailable(this);
8110 }
8111 }
8112 }
8113
8114 /* Check if the GONE bit has changed */
8115 if ((changed & GONE) != 0) {
8116 needGlobalAttributesUpdate(false);
8117 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118
Romain Guyecd80ee2009-12-03 17:13:02 -08008119 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8120 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008121 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008122 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008123 if (mParent instanceof View) {
8124 // GONE views noop invalidation, so invalidate the parent
8125 ((View) mParent).invalidate(true);
8126 }
8127 // Mark the view drawn to ensure that it gets invalidated properly the next
8128 // time it is visible and gets invalidated
8129 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 }
8131 if (mAttachInfo != null) {
8132 mAttachInfo.mViewVisibilityChanged = true;
8133 }
8134 }
8135
8136 /* Check if the VISIBLE bit has changed */
8137 if ((changed & INVISIBLE) != 0) {
8138 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008139 /*
8140 * If this view is becoming invisible, set the DRAWN flag so that
8141 * the next invalidate() will not be skipped.
8142 */
8143 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144
8145 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008146 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 if (getRootView() != this) {
8148 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008149 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 }
8151 }
8152 if (mAttachInfo != null) {
8153 mAttachInfo.mViewVisibilityChanged = true;
8154 }
8155 }
8156
Adam Powell326d8082009-12-09 15:10:07 -08008157 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008158 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008159 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8160 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008161 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008162 } else if (mParent != null) {
8163 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008164 }
Adam Powell326d8082009-12-09 15:10:07 -08008165 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8166 }
8167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8169 destroyDrawingCache();
8170 }
8171
8172 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8173 destroyDrawingCache();
8174 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008175 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 }
8177
8178 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8179 destroyDrawingCache();
8180 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8181 }
8182
8183 if ((changed & DRAW_MASK) != 0) {
8184 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008185 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 mPrivateFlags &= ~SKIP_DRAW;
8187 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8188 } else {
8189 mPrivateFlags |= SKIP_DRAW;
8190 }
8191 } else {
8192 mPrivateFlags &= ~SKIP_DRAW;
8193 }
8194 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008195 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 }
8197
8198 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008199 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 mParent.recomputeViewAttributes(this);
8201 }
8202 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008203
8204 if (AccessibilityManager.getInstance(mContext).isEnabled()
8205 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8206 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8207 notifyAccessibilityStateChanged();
8208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 }
8210
8211 /**
8212 * Change the view's z order in the tree, so it's on top of other sibling
8213 * views
8214 */
8215 public void bringToFront() {
8216 if (mParent != null) {
8217 mParent.bringChildToFront(this);
8218 }
8219 }
8220
8221 /**
8222 * This is called in response to an internal scroll in this view (i.e., the
8223 * view scrolled its own contents). This is typically as a result of
8224 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8225 * called.
8226 *
8227 * @param l Current horizontal scroll origin.
8228 * @param t Current vertical scroll origin.
8229 * @param oldl Previous horizontal scroll origin.
8230 * @param oldt Previous vertical scroll origin.
8231 */
8232 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008233 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8234 postSendViewScrolledAccessibilityEventCallback();
8235 }
8236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008237 mBackgroundSizeChanged = true;
8238
8239 final AttachInfo ai = mAttachInfo;
8240 if (ai != null) {
8241 ai.mViewScrollChanged = true;
8242 }
8243 }
8244
8245 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008246 * Interface definition for a callback to be invoked when the layout bounds of a view
8247 * changes due to layout processing.
8248 */
8249 public interface OnLayoutChangeListener {
8250 /**
8251 * Called when the focus state of a view has changed.
8252 *
8253 * @param v The view whose state has changed.
8254 * @param left The new value of the view's left property.
8255 * @param top The new value of the view's top property.
8256 * @param right The new value of the view's right property.
8257 * @param bottom The new value of the view's bottom property.
8258 * @param oldLeft The previous value of the view's left property.
8259 * @param oldTop The previous value of the view's top property.
8260 * @param oldRight The previous value of the view's right property.
8261 * @param oldBottom The previous value of the view's bottom property.
8262 */
8263 void onLayoutChange(View v, int left, int top, int right, int bottom,
8264 int oldLeft, int oldTop, int oldRight, int oldBottom);
8265 }
8266
8267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 * This is called during layout when the size of this view has changed. If
8269 * you were just added to the view hierarchy, you're called with the old
8270 * values of 0.
8271 *
8272 * @param w Current width of this view.
8273 * @param h Current height of this view.
8274 * @param oldw Old width of this view.
8275 * @param oldh Old height of this view.
8276 */
8277 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8278 }
8279
8280 /**
8281 * Called by draw to draw the child views. This may be overridden
8282 * by derived classes to gain control just before its children are drawn
8283 * (but after its own view has been drawn).
8284 * @param canvas the canvas on which to draw the view
8285 */
8286 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 }
8289
8290 /**
8291 * Gets the parent of this view. Note that the parent is a
8292 * ViewParent and not necessarily a View.
8293 *
8294 * @return Parent of this view.
8295 */
8296 public final ViewParent getParent() {
8297 return mParent;
8298 }
8299
8300 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008301 * Set the horizontal scrolled position of your view. This will cause a call to
8302 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8303 * invalidated.
8304 * @param value the x position to scroll to
8305 */
8306 public void setScrollX(int value) {
8307 scrollTo(value, mScrollY);
8308 }
8309
8310 /**
8311 * Set the vertical scrolled position of your view. This will cause a call to
8312 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8313 * invalidated.
8314 * @param value the y position to scroll to
8315 */
8316 public void setScrollY(int value) {
8317 scrollTo(mScrollX, value);
8318 }
8319
8320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 * Return the scrolled left position of this view. This is the left edge of
8322 * the displayed part of your view. You do not need to draw any pixels
8323 * farther left, since those are outside of the frame of your view on
8324 * screen.
8325 *
8326 * @return The left edge of the displayed part of your view, in pixels.
8327 */
8328 public final int getScrollX() {
8329 return mScrollX;
8330 }
8331
8332 /**
8333 * Return the scrolled top position of this view. This is the top edge of
8334 * the displayed part of your view. You do not need to draw any pixels above
8335 * it, since those are outside of the frame of your view on screen.
8336 *
8337 * @return The top edge of the displayed part of your view, in pixels.
8338 */
8339 public final int getScrollY() {
8340 return mScrollY;
8341 }
8342
8343 /**
8344 * Return the width of the your view.
8345 *
8346 * @return The width of your view, in pixels.
8347 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008348 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 public final int getWidth() {
8350 return mRight - mLeft;
8351 }
8352
8353 /**
8354 * Return the height of your view.
8355 *
8356 * @return The height of your view, in pixels.
8357 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008358 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008359 public final int getHeight() {
8360 return mBottom - mTop;
8361 }
8362
8363 /**
8364 * Return the visible drawing bounds of your view. Fills in the output
8365 * rectangle with the values from getScrollX(), getScrollY(),
8366 * getWidth(), and getHeight().
8367 *
8368 * @param outRect The (scrolled) drawing bounds of the view.
8369 */
8370 public void getDrawingRect(Rect outRect) {
8371 outRect.left = mScrollX;
8372 outRect.top = mScrollY;
8373 outRect.right = mScrollX + (mRight - mLeft);
8374 outRect.bottom = mScrollY + (mBottom - mTop);
8375 }
8376
8377 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008378 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8379 * raw width component (that is the result is masked by
8380 * {@link #MEASURED_SIZE_MASK}).
8381 *
8382 * @return The raw measured width of this view.
8383 */
8384 public final int getMeasuredWidth() {
8385 return mMeasuredWidth & MEASURED_SIZE_MASK;
8386 }
8387
8388 /**
8389 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008390 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008391 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 * This should be used during measurement and layout calculations only. Use
8393 * {@link #getWidth()} to see how wide a view is after layout.
8394 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008395 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008397 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 return mMeasuredWidth;
8399 }
8400
8401 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008402 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8403 * raw width component (that is the result is masked by
8404 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008406 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 */
8408 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008409 return mMeasuredHeight & MEASURED_SIZE_MASK;
8410 }
8411
8412 /**
8413 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008414 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008415 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8416 * This should be used during measurement and layout calculations only. Use
8417 * {@link #getHeight()} to see how wide a view is after layout.
8418 *
8419 * @return The measured width of this view as a bit mask.
8420 */
8421 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 return mMeasuredHeight;
8423 }
8424
8425 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008426 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8427 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8428 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8429 * and the height component is at the shifted bits
8430 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8431 */
8432 public final int getMeasuredState() {
8433 return (mMeasuredWidth&MEASURED_STATE_MASK)
8434 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8435 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8436 }
8437
8438 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008439 * The transform matrix of this view, which is calculated based on the current
8440 * roation, scale, and pivot properties.
8441 *
8442 * @see #getRotation()
8443 * @see #getScaleX()
8444 * @see #getScaleY()
8445 * @see #getPivotX()
8446 * @see #getPivotY()
8447 * @return The current transform matrix for the view
8448 */
8449 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008450 if (mTransformationInfo != null) {
8451 updateMatrix();
8452 return mTransformationInfo.mMatrix;
8453 }
8454 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008455 }
8456
8457 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008458 * Utility function to determine if the value is far enough away from zero to be
8459 * considered non-zero.
8460 * @param value A floating point value to check for zero-ness
8461 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8462 */
8463 private static boolean nonzero(float value) {
8464 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8465 }
8466
8467 /**
Jeff Brown86671742010-09-30 20:00:15 -07008468 * Returns true if the transform matrix is the identity matrix.
8469 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008470 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008471 * @return True if the transform matrix is the identity matrix, false otherwise.
8472 */
Jeff Brown86671742010-09-30 20:00:15 -07008473 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008474 if (mTransformationInfo != null) {
8475 updateMatrix();
8476 return mTransformationInfo.mMatrixIsIdentity;
8477 }
8478 return true;
8479 }
8480
8481 void ensureTransformationInfo() {
8482 if (mTransformationInfo == null) {
8483 mTransformationInfo = new TransformationInfo();
8484 }
Jeff Brown86671742010-09-30 20:00:15 -07008485 }
8486
8487 /**
8488 * Recomputes the transform matrix if necessary.
8489 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008490 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008491 final TransformationInfo info = mTransformationInfo;
8492 if (info == null) {
8493 return;
8494 }
8495 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008496 // transform-related properties have changed since the last time someone
8497 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008498
8499 // Figure out if we need to update the pivot point
8500 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008501 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8502 info.mPrevWidth = mRight - mLeft;
8503 info.mPrevHeight = mBottom - mTop;
8504 info.mPivotX = info.mPrevWidth / 2f;
8505 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008506 }
8507 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008508 info.mMatrix.reset();
8509 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8510 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8511 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8512 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008513 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008514 if (info.mCamera == null) {
8515 info.mCamera = new Camera();
8516 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008517 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008518 info.mCamera.save();
8519 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8520 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8521 info.mCamera.getMatrix(info.matrix3D);
8522 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8523 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8524 info.mPivotY + info.mTranslationY);
8525 info.mMatrix.postConcat(info.matrix3D);
8526 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008527 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008528 info.mMatrixDirty = false;
8529 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8530 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008531 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008532 }
8533
8534 /**
8535 * Utility method to retrieve the inverse of the current mMatrix property.
8536 * We cache the matrix to avoid recalculating it when transform properties
8537 * have not changed.
8538 *
8539 * @return The inverse of the current matrix of this view.
8540 */
Jeff Brown86671742010-09-30 20:00:15 -07008541 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008542 final TransformationInfo info = mTransformationInfo;
8543 if (info != null) {
8544 updateMatrix();
8545 if (info.mInverseMatrixDirty) {
8546 if (info.mInverseMatrix == null) {
8547 info.mInverseMatrix = new Matrix();
8548 }
8549 info.mMatrix.invert(info.mInverseMatrix);
8550 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008551 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008552 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008553 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008554 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008555 }
8556
8557 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008558 * Gets the distance along the Z axis from the camera to this view.
8559 *
8560 * @see #setCameraDistance(float)
8561 *
8562 * @return The distance along the Z axis.
8563 */
8564 public float getCameraDistance() {
8565 ensureTransformationInfo();
8566 final float dpi = mResources.getDisplayMetrics().densityDpi;
8567 final TransformationInfo info = mTransformationInfo;
8568 if (info.mCamera == null) {
8569 info.mCamera = new Camera();
8570 info.matrix3D = new Matrix();
8571 }
8572 return -(info.mCamera.getLocationZ() * dpi);
8573 }
8574
8575 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008576 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8577 * views are drawn) from the camera to this view. The camera's distance
8578 * affects 3D transformations, for instance rotations around the X and Y
8579 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008580 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008581 * use a camera distance that's greater than the height (X axis rotation) or
8582 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008583 *
Romain Guya5364ee2011-02-24 14:46:04 -08008584 * <p>The distance of the camera from the view plane can have an affect on the
8585 * perspective distortion of the view when it is rotated around the x or y axis.
8586 * For example, a large distance will result in a large viewing angle, and there
8587 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008588 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008589 * also result in some drawing artifacts if the rotated view ends up partially
8590 * behind the camera (which is why the recommendation is to use a distance at
8591 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008592 *
Romain Guya5364ee2011-02-24 14:46:04 -08008593 * <p>The distance is expressed in "depth pixels." The default distance depends
8594 * on the screen density. For instance, on a medium density display, the
8595 * default distance is 1280. On a high density display, the default distance
8596 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008597 *
Romain Guya5364ee2011-02-24 14:46:04 -08008598 * <p>If you want to specify a distance that leads to visually consistent
8599 * results across various densities, use the following formula:</p>
8600 * <pre>
8601 * float scale = context.getResources().getDisplayMetrics().density;
8602 * view.setCameraDistance(distance * scale);
8603 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008604 *
Romain Guya5364ee2011-02-24 14:46:04 -08008605 * <p>The density scale factor of a high density display is 1.5,
8606 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008607 *
Romain Guya5364ee2011-02-24 14:46:04 -08008608 * @param distance The distance in "depth pixels", if negative the opposite
8609 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008610 *
8611 * @see #setRotationX(float)
8612 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008613 */
8614 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008615 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008616
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008617 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008618 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008619 final TransformationInfo info = mTransformationInfo;
8620 if (info.mCamera == null) {
8621 info.mCamera = new Camera();
8622 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008623 }
8624
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008625 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8626 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008627
Chet Haase9d1992d2012-03-13 11:03:25 -07008628 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008629 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008630 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008631 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008632 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8633 // View was rejected last time it was drawn by its parent; this may have changed
8634 invalidateParentIfNeeded();
8635 }
Romain Guya5364ee2011-02-24 14:46:04 -08008636 }
8637
8638 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008639 * The degrees that the view is rotated around the pivot point.
8640 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008641 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008642 * @see #getPivotX()
8643 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008644 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008645 * @return The degrees of rotation.
8646 */
Chet Haasea5531132012-02-02 13:41:44 -08008647 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008648 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008649 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008650 }
8651
8652 /**
Chet Haase897247b2010-09-09 14:54:47 -07008653 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8654 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008655 *
8656 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008657 *
8658 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008659 * @see #getPivotX()
8660 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008661 * @see #setRotationX(float)
8662 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008663 *
8664 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008665 */
8666 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008667 ensureTransformationInfo();
8668 final TransformationInfo info = mTransformationInfo;
8669 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008670 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008671 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 info.mRotation = rotation;
8673 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008674 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008675 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008676 mDisplayList.setRotation(rotation);
8677 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008678 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8679 // View was rejected last time it was drawn by its parent; this may have changed
8680 invalidateParentIfNeeded();
8681 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008682 }
8683 }
8684
8685 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008686 * The degrees that the view is rotated around the vertical axis through the pivot point.
8687 *
8688 * @see #getPivotX()
8689 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008690 * @see #setRotationY(float)
8691 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008692 * @return The degrees of Y rotation.
8693 */
Chet Haasea5531132012-02-02 13:41:44 -08008694 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008695 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008696 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008697 }
8698
8699 /**
Chet Haase897247b2010-09-09 14:54:47 -07008700 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8701 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8702 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008703 *
Romain Guya5364ee2011-02-24 14:46:04 -08008704 * When rotating large views, it is recommended to adjust the camera distance
8705 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008706 *
8707 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008708 *
8709 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008710 * @see #getPivotX()
8711 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008712 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008713 * @see #setRotationX(float)
8714 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008715 *
8716 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008717 */
8718 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008719 ensureTransformationInfo();
8720 final TransformationInfo info = mTransformationInfo;
8721 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008722 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008723 info.mRotationY = rotationY;
8724 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008725 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008726 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008727 mDisplayList.setRotationY(rotationY);
8728 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008729 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8730 // View was rejected last time it was drawn by its parent; this may have changed
8731 invalidateParentIfNeeded();
8732 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008733 }
8734 }
8735
8736 /**
8737 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8738 *
8739 * @see #getPivotX()
8740 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008741 * @see #setRotationX(float)
8742 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008743 * @return The degrees of X rotation.
8744 */
Chet Haasea5531132012-02-02 13:41:44 -08008745 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008746 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008747 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008748 }
8749
8750 /**
Chet Haase897247b2010-09-09 14:54:47 -07008751 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8752 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8753 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008754 *
Romain Guya5364ee2011-02-24 14:46:04 -08008755 * When rotating large views, it is recommended to adjust the camera distance
8756 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008757 *
8758 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008759 *
8760 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008761 * @see #getPivotX()
8762 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008763 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008764 * @see #setRotationY(float)
8765 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008766 *
8767 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008768 */
8769 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008770 ensureTransformationInfo();
8771 final TransformationInfo info = mTransformationInfo;
8772 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008773 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008774 info.mRotationX = rotationX;
8775 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008776 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008777 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008778 mDisplayList.setRotationX(rotationX);
8779 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008780 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8781 // View was rejected last time it was drawn by its parent; this may have changed
8782 invalidateParentIfNeeded();
8783 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008784 }
8785 }
8786
8787 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008788 * The amount that the view is scaled in x around the pivot point, as a proportion of
8789 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8790 *
Joe Onorato93162322010-09-16 15:42:01 -04008791 * <p>By default, this is 1.0f.
8792 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008793 * @see #getPivotX()
8794 * @see #getPivotY()
8795 * @return The scaling factor.
8796 */
Chet Haasea5531132012-02-02 13:41:44 -08008797 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008798 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008799 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008800 }
8801
8802 /**
8803 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8804 * the view's unscaled width. A value of 1 means that no scaling is applied.
8805 *
8806 * @param scaleX The scaling factor.
8807 * @see #getPivotX()
8808 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008809 *
8810 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008811 */
8812 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008813 ensureTransformationInfo();
8814 final TransformationInfo info = mTransformationInfo;
8815 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008816 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008817 info.mScaleX = scaleX;
8818 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008819 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008820 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008821 mDisplayList.setScaleX(scaleX);
8822 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008823 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8824 // View was rejected last time it was drawn by its parent; this may have changed
8825 invalidateParentIfNeeded();
8826 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008827 }
8828 }
8829
8830 /**
8831 * The amount that the view is scaled in y around the pivot point, as a proportion of
8832 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8833 *
Joe Onorato93162322010-09-16 15:42:01 -04008834 * <p>By default, this is 1.0f.
8835 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008836 * @see #getPivotX()
8837 * @see #getPivotY()
8838 * @return The scaling factor.
8839 */
Chet Haasea5531132012-02-02 13:41:44 -08008840 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008841 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008843 }
8844
8845 /**
8846 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8847 * the view's unscaled width. A value of 1 means that no scaling is applied.
8848 *
8849 * @param scaleY The scaling factor.
8850 * @see #getPivotX()
8851 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008852 *
8853 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008854 */
8855 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008856 ensureTransformationInfo();
8857 final TransformationInfo info = mTransformationInfo;
8858 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008859 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008860 info.mScaleY = scaleY;
8861 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008862 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008863 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008864 mDisplayList.setScaleY(scaleY);
8865 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008866 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8867 // View was rejected last time it was drawn by its parent; this may have changed
8868 invalidateParentIfNeeded();
8869 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008870 }
8871 }
8872
8873 /**
8874 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8875 * and {@link #setScaleX(float) scaled}.
8876 *
8877 * @see #getRotation()
8878 * @see #getScaleX()
8879 * @see #getScaleY()
8880 * @see #getPivotY()
8881 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008882 *
8883 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008884 */
Chet Haasea5531132012-02-02 13:41:44 -08008885 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008886 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008887 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008888 }
8889
8890 /**
8891 * Sets the x location of the point around which the view is
8892 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008893 * By default, the pivot point is centered on the object.
8894 * Setting this property disables this behavior and causes the view to use only the
8895 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008896 *
8897 * @param pivotX The x location of the pivot point.
8898 * @see #getRotation()
8899 * @see #getScaleX()
8900 * @see #getScaleY()
8901 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008902 *
8903 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008904 */
8905 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008906 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008907 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008908 final TransformationInfo info = mTransformationInfo;
8909 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008910 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008911 info.mPivotX = pivotX;
8912 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008913 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008914 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008915 mDisplayList.setPivotX(pivotX);
8916 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008917 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8918 // View was rejected last time it was drawn by its parent; this may have changed
8919 invalidateParentIfNeeded();
8920 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 }
8922 }
8923
8924 /**
8925 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8926 * and {@link #setScaleY(float) scaled}.
8927 *
8928 * @see #getRotation()
8929 * @see #getScaleX()
8930 * @see #getScaleY()
8931 * @see #getPivotY()
8932 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008933 *
8934 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008935 */
Chet Haasea5531132012-02-02 13:41:44 -08008936 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008937 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008938 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 }
8940
8941 /**
8942 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008943 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8944 * Setting this property disables this behavior and causes the view to use only the
8945 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008946 *
8947 * @param pivotY The y location of the pivot point.
8948 * @see #getRotation()
8949 * @see #getScaleX()
8950 * @see #getScaleY()
8951 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008952 *
8953 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008954 */
8955 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008956 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008957 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008958 final TransformationInfo info = mTransformationInfo;
8959 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008960 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008961 info.mPivotY = pivotY;
8962 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008963 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008964 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008965 mDisplayList.setPivotY(pivotY);
8966 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008967 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8968 // View was rejected last time it was drawn by its parent; this may have changed
8969 invalidateParentIfNeeded();
8970 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008971 }
8972 }
8973
8974 /**
8975 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8976 * completely transparent and 1 means the view is completely opaque.
8977 *
Joe Onorato93162322010-09-16 15:42:01 -04008978 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008979 * @return The opacity of the view.
8980 */
Chet Haasea5531132012-02-02 13:41:44 -08008981 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008982 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008983 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008984 }
8985
8986 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008987 * Returns whether this View has content which overlaps. This function, intended to be
8988 * overridden by specific View types, is an optimization when alpha is set on a view. If
8989 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8990 * and then composited it into place, which can be expensive. If the view has no overlapping
8991 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8992 * An example of overlapping rendering is a TextView with a background image, such as a
8993 * Button. An example of non-overlapping rendering is a TextView with no background, or
8994 * an ImageView with only the foreground image. The default implementation returns true;
8995 * subclasses should override if they have cases which can be optimized.
8996 *
8997 * @return true if the content in this view might overlap, false otherwise.
8998 */
8999 public boolean hasOverlappingRendering() {
9000 return true;
9001 }
9002
9003 /**
Romain Guy171c5922011-01-06 10:04:23 -08009004 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9005 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009006 *
Romain Guy171c5922011-01-06 10:04:23 -08009007 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9008 * responsible for applying the opacity itself. Otherwise, calling this method is
9009 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009010 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009011 *
Chet Haasea5531132012-02-02 13:41:44 -08009012 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9013 * performance implications. It is generally best to use the alpha property sparingly and
9014 * transiently, as in the case of fading animations.</p>
9015 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009016 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009017 *
Joe Malin32736f02011-01-19 16:14:20 -08009018 * @see #setLayerType(int, android.graphics.Paint)
9019 *
Chet Haase73066682010-11-29 15:55:32 -08009020 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009021 */
9022 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009024 if (mTransformationInfo.mAlpha != alpha) {
9025 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009026 if (onSetAlpha((int) (alpha * 255))) {
9027 mPrivateFlags |= ALPHA_SET;
9028 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009029 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009030 invalidate(true);
9031 } else {
9032 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009033 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009034 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009035 mDisplayList.setAlpha(alpha);
9036 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009037 }
Chet Haaseed032702010-10-01 14:05:54 -07009038 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009039 }
9040
9041 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009042 * Faster version of setAlpha() which performs the same steps except there are
9043 * no calls to invalidate(). The caller of this function should perform proper invalidation
9044 * on the parent and this object. The return value indicates whether the subclass handles
9045 * alpha (the return value for onSetAlpha()).
9046 *
9047 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009048 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9049 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009050 */
9051 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009052 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009053 if (mTransformationInfo.mAlpha != alpha) {
9054 mTransformationInfo.mAlpha = alpha;
9055 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9056 if (subclassHandlesAlpha) {
9057 mPrivateFlags |= ALPHA_SET;
9058 return true;
9059 } else {
9060 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009061 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009062 mDisplayList.setAlpha(alpha);
9063 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009064 }
Chet Haasea00f3862011-02-22 06:34:40 -08009065 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009066 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009067 }
9068
9069 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009070 * Top position of this view relative to its parent.
9071 *
9072 * @return The top of this view, in pixels.
9073 */
9074 @ViewDebug.CapturedViewProperty
9075 public final int getTop() {
9076 return mTop;
9077 }
9078
9079 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009080 * Sets the top position of this view relative to its parent. This method is meant to be called
9081 * by the layout system and should not generally be called otherwise, because the property
9082 * may be changed at any time by the layout.
9083 *
9084 * @param top The top of this view, in pixels.
9085 */
9086 public final void setTop(int top) {
9087 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009088 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009089 final boolean matrixIsIdentity = mTransformationInfo == null
9090 || mTransformationInfo.mMatrixIsIdentity;
9091 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009092 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009093 int minTop;
9094 int yLoc;
9095 if (top < mTop) {
9096 minTop = top;
9097 yLoc = top - mTop;
9098 } else {
9099 minTop = mTop;
9100 yLoc = 0;
9101 }
Chet Haasee9140a72011-02-16 16:23:29 -08009102 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009103 }
9104 } else {
9105 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009106 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009107 }
9108
Chet Haaseed032702010-10-01 14:05:54 -07009109 int width = mRight - mLeft;
9110 int oldHeight = mBottom - mTop;
9111
Chet Haase21cd1382010-09-01 17:42:29 -07009112 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009113 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009114 mDisplayList.setTop(mTop);
9115 }
Chet Haase21cd1382010-09-01 17:42:29 -07009116
Chet Haaseed032702010-10-01 14:05:54 -07009117 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9118
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009119 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009120 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9121 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009122 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009123 }
Chet Haase21cd1382010-09-01 17:42:29 -07009124 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009125 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009126 }
Chet Haase55dbb652010-12-21 20:15:08 -08009127 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009128 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009129 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9130 // View was rejected last time it was drawn by its parent; this may have changed
9131 invalidateParentIfNeeded();
9132 }
Chet Haase21cd1382010-09-01 17:42:29 -07009133 }
9134 }
9135
9136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 * Bottom position of this view relative to its parent.
9138 *
9139 * @return The bottom of this view, in pixels.
9140 */
9141 @ViewDebug.CapturedViewProperty
9142 public final int getBottom() {
9143 return mBottom;
9144 }
9145
9146 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009147 * True if this view has changed since the last time being drawn.
9148 *
9149 * @return The dirty state of this view.
9150 */
9151 public boolean isDirty() {
9152 return (mPrivateFlags & DIRTY_MASK) != 0;
9153 }
9154
9155 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009156 * Sets the bottom position of this view relative to its parent. This method is meant to be
9157 * called by the layout system and should not generally be called otherwise, because the
9158 * property may be changed at any time by the layout.
9159 *
9160 * @param bottom The bottom of this view, in pixels.
9161 */
9162 public final void setBottom(int bottom) {
9163 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009164 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009165 final boolean matrixIsIdentity = mTransformationInfo == null
9166 || mTransformationInfo.mMatrixIsIdentity;
9167 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009168 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009169 int maxBottom;
9170 if (bottom < mBottom) {
9171 maxBottom = mBottom;
9172 } else {
9173 maxBottom = bottom;
9174 }
Chet Haasee9140a72011-02-16 16:23:29 -08009175 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009176 }
9177 } else {
9178 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009179 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009180 }
9181
Chet Haaseed032702010-10-01 14:05:54 -07009182 int width = mRight - mLeft;
9183 int oldHeight = mBottom - mTop;
9184
Chet Haase21cd1382010-09-01 17:42:29 -07009185 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009186 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009187 mDisplayList.setBottom(mBottom);
9188 }
Chet Haase21cd1382010-09-01 17:42:29 -07009189
Chet Haaseed032702010-10-01 14:05:54 -07009190 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9191
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009192 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009193 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9194 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009195 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009196 }
Chet Haase21cd1382010-09-01 17:42:29 -07009197 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009198 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009199 }
Chet Haase55dbb652010-12-21 20:15:08 -08009200 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009201 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009202 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9203 // View was rejected last time it was drawn by its parent; this may have changed
9204 invalidateParentIfNeeded();
9205 }
Chet Haase21cd1382010-09-01 17:42:29 -07009206 }
9207 }
9208
9209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 * Left position of this view relative to its parent.
9211 *
9212 * @return The left edge of this view, in pixels.
9213 */
9214 @ViewDebug.CapturedViewProperty
9215 public final int getLeft() {
9216 return mLeft;
9217 }
9218
9219 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009220 * Sets the left position of this view relative to its parent. This method is meant to be called
9221 * by the layout system and should not generally be called otherwise, because the property
9222 * may be changed at any time by the layout.
9223 *
9224 * @param left The bottom of this view, in pixels.
9225 */
9226 public final void setLeft(int left) {
9227 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009228 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009229 final boolean matrixIsIdentity = mTransformationInfo == null
9230 || mTransformationInfo.mMatrixIsIdentity;
9231 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009232 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009233 int minLeft;
9234 int xLoc;
9235 if (left < mLeft) {
9236 minLeft = left;
9237 xLoc = left - mLeft;
9238 } else {
9239 minLeft = mLeft;
9240 xLoc = 0;
9241 }
Chet Haasee9140a72011-02-16 16:23:29 -08009242 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009243 }
9244 } else {
9245 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009246 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009247 }
9248
Chet Haaseed032702010-10-01 14:05:54 -07009249 int oldWidth = mRight - mLeft;
9250 int height = mBottom - mTop;
9251
Chet Haase21cd1382010-09-01 17:42:29 -07009252 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009253 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009254 mDisplayList.setLeft(left);
9255 }
Chet Haase21cd1382010-09-01 17:42:29 -07009256
Chet Haaseed032702010-10-01 14:05:54 -07009257 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9258
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009259 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009260 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9261 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009262 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009263 }
Chet Haase21cd1382010-09-01 17:42:29 -07009264 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009265 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009266 }
Chet Haase55dbb652010-12-21 20:15:08 -08009267 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009268 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009269 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9270 // View was rejected last time it was drawn by its parent; this may have changed
9271 invalidateParentIfNeeded();
9272 }
Chet Haase21cd1382010-09-01 17:42:29 -07009273 }
9274 }
9275
9276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009277 * Right position of this view relative to its parent.
9278 *
9279 * @return The right edge of this view, in pixels.
9280 */
9281 @ViewDebug.CapturedViewProperty
9282 public final int getRight() {
9283 return mRight;
9284 }
9285
9286 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009287 * Sets the right position of this view relative to its parent. This method is meant to be called
9288 * by the layout system and should not generally be called otherwise, because the property
9289 * may be changed at any time by the layout.
9290 *
9291 * @param right The bottom of this view, in pixels.
9292 */
9293 public final void setRight(int right) {
9294 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009295 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009296 final boolean matrixIsIdentity = mTransformationInfo == null
9297 || mTransformationInfo.mMatrixIsIdentity;
9298 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009299 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009300 int maxRight;
9301 if (right < mRight) {
9302 maxRight = mRight;
9303 } else {
9304 maxRight = right;
9305 }
Chet Haasee9140a72011-02-16 16:23:29 -08009306 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009307 }
9308 } else {
9309 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009310 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009311 }
9312
Chet Haaseed032702010-10-01 14:05:54 -07009313 int oldWidth = mRight - mLeft;
9314 int height = mBottom - mTop;
9315
Chet Haase21cd1382010-09-01 17:42:29 -07009316 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009317 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009318 mDisplayList.setRight(mRight);
9319 }
Chet Haase21cd1382010-09-01 17:42:29 -07009320
Chet Haaseed032702010-10-01 14:05:54 -07009321 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9322
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009323 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009324 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9325 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009326 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009327 }
Chet Haase21cd1382010-09-01 17:42:29 -07009328 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009329 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009330 }
Chet Haase55dbb652010-12-21 20:15:08 -08009331 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009332 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009333 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9334 // View was rejected last time it was drawn by its parent; this may have changed
9335 invalidateParentIfNeeded();
9336 }
Chet Haase21cd1382010-09-01 17:42:29 -07009337 }
9338 }
9339
9340 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009341 * The visual x position of this view, in pixels. This is equivalent to the
9342 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009343 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009344 *
Chet Haasedf030d22010-07-30 17:22:38 -07009345 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009346 */
Chet Haasea5531132012-02-02 13:41:44 -08009347 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009348 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009349 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009350 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009351
Chet Haasedf030d22010-07-30 17:22:38 -07009352 /**
9353 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9354 * {@link #setTranslationX(float) translationX} property to be the difference between
9355 * the x value passed in and the current {@link #getLeft() left} property.
9356 *
9357 * @param x The visual x position of this view, in pixels.
9358 */
9359 public void setX(float x) {
9360 setTranslationX(x - mLeft);
9361 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009362
Chet Haasedf030d22010-07-30 17:22:38 -07009363 /**
9364 * The visual y position of this view, in pixels. This is equivalent to the
9365 * {@link #setTranslationY(float) translationY} property plus the current
9366 * {@link #getTop() top} property.
9367 *
9368 * @return The visual y position of this view, in pixels.
9369 */
Chet Haasea5531132012-02-02 13:41:44 -08009370 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009371 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009372 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009373 }
9374
9375 /**
9376 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9377 * {@link #setTranslationY(float) translationY} property to be the difference between
9378 * the y value passed in and the current {@link #getTop() top} property.
9379 *
9380 * @param y The visual y position of this view, in pixels.
9381 */
9382 public void setY(float y) {
9383 setTranslationY(y - mTop);
9384 }
9385
9386
9387 /**
9388 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9389 * This position is post-layout, in addition to wherever the object's
9390 * layout placed it.
9391 *
9392 * @return The horizontal position of this view relative to its left position, in pixels.
9393 */
Chet Haasea5531132012-02-02 13:41:44 -08009394 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009395 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009396 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009397 }
9398
9399 /**
9400 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9401 * This effectively positions the object post-layout, in addition to wherever the object's
9402 * layout placed it.
9403 *
9404 * @param translationX The horizontal position of this view relative to its left position,
9405 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009406 *
9407 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009408 */
9409 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009410 ensureTransformationInfo();
9411 final TransformationInfo info = mTransformationInfo;
9412 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009413 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009414 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009415 info.mTranslationX = translationX;
9416 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009417 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009418 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009419 mDisplayList.setTranslationX(translationX);
9420 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009421 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9422 // View was rejected last time it was drawn by its parent; this may have changed
9423 invalidateParentIfNeeded();
9424 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009425 }
9426 }
9427
9428 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009429 * The horizontal location of this view relative to its {@link #getTop() top} position.
9430 * This position is post-layout, in addition to wherever the object's
9431 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009432 *
Chet Haasedf030d22010-07-30 17:22:38 -07009433 * @return The vertical position of this view relative to its top position,
9434 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009435 */
Chet Haasea5531132012-02-02 13:41:44 -08009436 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009437 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009438 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009439 }
9440
9441 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009442 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9443 * This effectively positions the object post-layout, in addition to wherever the object's
9444 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009445 *
Chet Haasedf030d22010-07-30 17:22:38 -07009446 * @param translationY The vertical position of this view relative to its top position,
9447 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009448 *
9449 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009450 */
Chet Haasedf030d22010-07-30 17:22:38 -07009451 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009452 ensureTransformationInfo();
9453 final TransformationInfo info = mTransformationInfo;
9454 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009455 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009456 info.mTranslationY = translationY;
9457 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009458 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009459 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009460 mDisplayList.setTranslationY(translationY);
9461 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009462 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9463 // View was rejected last time it was drawn by its parent; this may have changed
9464 invalidateParentIfNeeded();
9465 }
Chet Haasedf030d22010-07-30 17:22:38 -07009466 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009467 }
9468
9469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 * Hit rectangle in parent's coordinates
9471 *
9472 * @param outRect The hit rectangle of the view.
9473 */
9474 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009475 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009476 final TransformationInfo info = mTransformationInfo;
9477 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009478 outRect.set(mLeft, mTop, mRight, mBottom);
9479 } else {
9480 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009481 tmpRect.set(-info.mPivotX, -info.mPivotY,
9482 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9483 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009484 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9485 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009486 }
9487 }
9488
9489 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009490 * Determines whether the given point, in local coordinates is inside the view.
9491 */
9492 /*package*/ final boolean pointInView(float localX, float localY) {
9493 return localX >= 0 && localX < (mRight - mLeft)
9494 && localY >= 0 && localY < (mBottom - mTop);
9495 }
9496
9497 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009498 * Utility method to determine whether the given point, in local coordinates,
9499 * is inside the view, where the area of the view is expanded by the slop factor.
9500 * This method is called while processing touch-move events to determine if the event
9501 * is still within the view.
9502 */
9503 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009504 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009505 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 }
9507
9508 /**
9509 * When a view has focus and the user navigates away from it, the next view is searched for
9510 * starting from the rectangle filled in by this method.
9511 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009512 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9513 * of the view. However, if your view maintains some idea of internal selection,
9514 * such as a cursor, or a selected row or column, you should override this method and
9515 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009516 *
9517 * @param r The rectangle to fill in, in this view's coordinates.
9518 */
9519 public void getFocusedRect(Rect r) {
9520 getDrawingRect(r);
9521 }
9522
9523 /**
9524 * If some part of this view is not clipped by any of its parents, then
9525 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009526 * coordinates (without taking possible View rotations into account), offset
9527 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9528 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 *
9530 * @param r If true is returned, r holds the global coordinates of the
9531 * visible portion of this view.
9532 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9533 * between this view and its root. globalOffet may be null.
9534 * @return true if r is non-empty (i.e. part of the view is visible at the
9535 * root level.
9536 */
9537 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9538 int width = mRight - mLeft;
9539 int height = mBottom - mTop;
9540 if (width > 0 && height > 0) {
9541 r.set(0, 0, width, height);
9542 if (globalOffset != null) {
9543 globalOffset.set(-mScrollX, -mScrollY);
9544 }
9545 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9546 }
9547 return false;
9548 }
9549
9550 public final boolean getGlobalVisibleRect(Rect r) {
9551 return getGlobalVisibleRect(r, null);
9552 }
9553
9554 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009555 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 if (getGlobalVisibleRect(r, offset)) {
9557 r.offset(-offset.x, -offset.y); // make r local
9558 return true;
9559 }
9560 return false;
9561 }
9562
9563 /**
9564 * Offset this view's vertical location by the specified number of pixels.
9565 *
9566 * @param offset the number of pixels to offset the view by
9567 */
9568 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009569 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009570 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009571 final boolean matrixIsIdentity = mTransformationInfo == null
9572 || mTransformationInfo.mMatrixIsIdentity;
9573 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009574 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009575 invalidateViewProperty(false, false);
9576 } else {
9577 final ViewParent p = mParent;
9578 if (p != null && mAttachInfo != null) {
9579 final Rect r = mAttachInfo.mTmpInvalRect;
9580 int minTop;
9581 int maxBottom;
9582 int yLoc;
9583 if (offset < 0) {
9584 minTop = mTop + offset;
9585 maxBottom = mBottom;
9586 yLoc = offset;
9587 } else {
9588 minTop = mTop;
9589 maxBottom = mBottom + offset;
9590 yLoc = 0;
9591 }
9592 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9593 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009594 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009595 }
9596 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009597 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009598 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009599
Chet Haasec3aa3612010-06-17 08:50:37 -07009600 mTop += offset;
9601 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009602 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009603 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009604 invalidateViewProperty(false, false);
9605 } else {
9606 if (!matrixIsIdentity) {
9607 invalidateViewProperty(false, true);
9608 }
9609 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009610 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 }
9613
9614 /**
9615 * Offset this view's horizontal location by the specified amount of pixels.
9616 *
9617 * @param offset the numer of pixels to offset the view by
9618 */
9619 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009620 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009621 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009622 final boolean matrixIsIdentity = mTransformationInfo == null
9623 || mTransformationInfo.mMatrixIsIdentity;
9624 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009625 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009626 invalidateViewProperty(false, false);
9627 } else {
9628 final ViewParent p = mParent;
9629 if (p != null && mAttachInfo != null) {
9630 final Rect r = mAttachInfo.mTmpInvalRect;
9631 int minLeft;
9632 int maxRight;
9633 if (offset < 0) {
9634 minLeft = mLeft + offset;
9635 maxRight = mRight;
9636 } else {
9637 minLeft = mLeft;
9638 maxRight = mRight + offset;
9639 }
9640 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9641 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009642 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009643 }
9644 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009645 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009646 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009647
Chet Haasec3aa3612010-06-17 08:50:37 -07009648 mLeft += offset;
9649 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009650 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009651 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009652 invalidateViewProperty(false, false);
9653 } else {
9654 if (!matrixIsIdentity) {
9655 invalidateViewProperty(false, true);
9656 }
9657 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009658 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 }
9661
9662 /**
9663 * Get the LayoutParams associated with this view. All views should have
9664 * layout parameters. These supply parameters to the <i>parent</i> of this
9665 * view specifying how it should be arranged. There are many subclasses of
9666 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9667 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009668 *
9669 * This method may return null if this View is not attached to a parent
9670 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9671 * was not invoked successfully. When a View is attached to a parent
9672 * ViewGroup, this method must not return null.
9673 *
9674 * @return The LayoutParams associated with this view, or null if no
9675 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009677 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 public ViewGroup.LayoutParams getLayoutParams() {
9679 return mLayoutParams;
9680 }
9681
9682 /**
9683 * Set the layout parameters associated with this view. These supply
9684 * parameters to the <i>parent</i> of this view specifying how it should be
9685 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9686 * correspond to the different subclasses of ViewGroup that are responsible
9687 * for arranging their children.
9688 *
Romain Guy01c174b2011-02-22 11:51:06 -08009689 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009690 */
9691 public void setLayoutParams(ViewGroup.LayoutParams params) {
9692 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009693 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 }
9695 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009696 if (mParent instanceof ViewGroup) {
9697 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 requestLayout();
9700 }
9701
9702 /**
9703 * Set the scrolled position of your view. This will cause a call to
9704 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9705 * invalidated.
9706 * @param x the x position to scroll to
9707 * @param y the y position to scroll to
9708 */
9709 public void scrollTo(int x, int y) {
9710 if (mScrollX != x || mScrollY != y) {
9711 int oldX = mScrollX;
9712 int oldY = mScrollY;
9713 mScrollX = x;
9714 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009715 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009717 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009718 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009720 }
9721 }
9722
9723 /**
9724 * Move the scrolled position of your view. This will cause a call to
9725 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9726 * invalidated.
9727 * @param x the amount of pixels to scroll by horizontally
9728 * @param y the amount of pixels to scroll by vertically
9729 */
9730 public void scrollBy(int x, int y) {
9731 scrollTo(mScrollX + x, mScrollY + y);
9732 }
9733
9734 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009735 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9736 * animation to fade the scrollbars out after a default delay. If a subclass
9737 * provides animated scrolling, the start delay should equal the duration
9738 * of the scrolling animation.</p>
9739 *
9740 * <p>The animation starts only if at least one of the scrollbars is
9741 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9742 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9743 * this method returns true, and false otherwise. If the animation is
9744 * started, this method calls {@link #invalidate()}; in that case the
9745 * caller should not call {@link #invalidate()}.</p>
9746 *
9747 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009748 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009749 *
9750 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9751 * and {@link #scrollTo(int, int)}.</p>
9752 *
9753 * @return true if the animation is played, false otherwise
9754 *
9755 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009756 * @see #scrollBy(int, int)
9757 * @see #scrollTo(int, int)
9758 * @see #isHorizontalScrollBarEnabled()
9759 * @see #isVerticalScrollBarEnabled()
9760 * @see #setHorizontalScrollBarEnabled(boolean)
9761 * @see #setVerticalScrollBarEnabled(boolean)
9762 */
9763 protected boolean awakenScrollBars() {
9764 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009765 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009766 }
9767
9768 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009769 * Trigger the scrollbars to draw.
9770 * This method differs from awakenScrollBars() only in its default duration.
9771 * initialAwakenScrollBars() will show the scroll bars for longer than
9772 * usual to give the user more of a chance to notice them.
9773 *
9774 * @return true if the animation is played, false otherwise.
9775 */
9776 private boolean initialAwakenScrollBars() {
9777 return mScrollCache != null &&
9778 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9779 }
9780
9781 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009782 * <p>
9783 * Trigger the scrollbars to draw. When invoked this method starts an
9784 * animation to fade the scrollbars out after a fixed delay. If a subclass
9785 * provides animated scrolling, the start delay should equal the duration of
9786 * the scrolling animation.
9787 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009788 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009789 * <p>
9790 * The animation starts only if at least one of the scrollbars is enabled,
9791 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9792 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9793 * this method returns true, and false otherwise. If the animation is
9794 * started, this method calls {@link #invalidate()}; in that case the caller
9795 * should not call {@link #invalidate()}.
9796 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009797 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009798 * <p>
9799 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009800 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009801 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009802 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009803 * @param startDelay the delay, in milliseconds, after which the animation
9804 * should start; when the delay is 0, the animation starts
9805 * immediately
9806 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009807 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009808 * @see #scrollBy(int, int)
9809 * @see #scrollTo(int, int)
9810 * @see #isHorizontalScrollBarEnabled()
9811 * @see #isVerticalScrollBarEnabled()
9812 * @see #setHorizontalScrollBarEnabled(boolean)
9813 * @see #setVerticalScrollBarEnabled(boolean)
9814 */
9815 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009816 return awakenScrollBars(startDelay, true);
9817 }
Joe Malin32736f02011-01-19 16:14:20 -08009818
Mike Cleron290947b2009-09-29 18:34:32 -07009819 /**
9820 * <p>
9821 * Trigger the scrollbars to draw. When invoked this method starts an
9822 * animation to fade the scrollbars out after a fixed delay. If a subclass
9823 * provides animated scrolling, the start delay should equal the duration of
9824 * the scrolling animation.
9825 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009826 *
Mike Cleron290947b2009-09-29 18:34:32 -07009827 * <p>
9828 * The animation starts only if at least one of the scrollbars is enabled,
9829 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9830 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9831 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009832 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009833 * is set to true; in that case the caller
9834 * should not call {@link #invalidate()}.
9835 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009836 *
Mike Cleron290947b2009-09-29 18:34:32 -07009837 * <p>
9838 * This method should be invoked everytime a subclass directly updates the
9839 * scroll parameters.
9840 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009841 *
Mike Cleron290947b2009-09-29 18:34:32 -07009842 * @param startDelay the delay, in milliseconds, after which the animation
9843 * should start; when the delay is 0, the animation starts
9844 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009845 *
Mike Cleron290947b2009-09-29 18:34:32 -07009846 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009847 *
Mike Cleron290947b2009-09-29 18:34:32 -07009848 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009849 *
Mike Cleron290947b2009-09-29 18:34:32 -07009850 * @see #scrollBy(int, int)
9851 * @see #scrollTo(int, int)
9852 * @see #isHorizontalScrollBarEnabled()
9853 * @see #isVerticalScrollBarEnabled()
9854 * @see #setHorizontalScrollBarEnabled(boolean)
9855 * @see #setVerticalScrollBarEnabled(boolean)
9856 */
9857 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009858 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009859
Mike Cleronf116bf82009-09-27 19:14:12 -07009860 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9861 return false;
9862 }
9863
9864 if (scrollCache.scrollBar == null) {
9865 scrollCache.scrollBar = new ScrollBarDrawable();
9866 }
9867
9868 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9869
Mike Cleron290947b2009-09-29 18:34:32 -07009870 if (invalidate) {
9871 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009872 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009873 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009874
9875 if (scrollCache.state == ScrollabilityCache.OFF) {
9876 // FIXME: this is copied from WindowManagerService.
9877 // We should get this value from the system when it
9878 // is possible to do so.
9879 final int KEY_REPEAT_FIRST_DELAY = 750;
9880 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9881 }
9882
9883 // Tell mScrollCache when we should start fading. This may
9884 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009885 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009886 scrollCache.fadeStartTime = fadeStartTime;
9887 scrollCache.state = ScrollabilityCache.ON;
9888
9889 // Schedule our fader to run, unscheduling any old ones first
9890 if (mAttachInfo != null) {
9891 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9892 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9893 }
9894
9895 return true;
9896 }
9897
9898 return false;
9899 }
9900
9901 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009902 * Do not invalidate views which are not visible and which are not running an animation. They
9903 * will not get drawn and they should not set dirty flags as if they will be drawn
9904 */
9905 private boolean skipInvalidate() {
9906 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9907 (!(mParent instanceof ViewGroup) ||
9908 !((ViewGroup) mParent).isViewTransitioning(this));
9909 }
9910 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009911 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009912 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9913 * in the future. This must be called from a UI thread. To call from a non-UI
9914 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 *
9916 * WARNING: This method is destructive to dirty.
9917 * @param dirty the rectangle representing the bounds of the dirty region
9918 */
9919 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009920 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009921 return;
9922 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009923 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009924 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9925 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009927 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009928 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009929 final ViewParent p = mParent;
9930 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009931 //noinspection PointlessBooleanExpression,ConstantConditions
9932 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9933 if (p != null && ai != null && ai.mHardwareAccelerated) {
9934 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009935 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009936 p.invalidateChild(this, null);
9937 return;
9938 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 if (p != null && ai != null) {
9941 final int scrollX = mScrollX;
9942 final int scrollY = mScrollY;
9943 final Rect r = ai.mTmpInvalRect;
9944 r.set(dirty.left - scrollX, dirty.top - scrollY,
9945 dirty.right - scrollX, dirty.bottom - scrollY);
9946 mParent.invalidateChild(this, r);
9947 }
9948 }
9949 }
9950
9951 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009952 * 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 -08009953 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009954 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9955 * will be called at some point in the future. This must be called from
9956 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 * @param l the left position of the dirty region
9958 * @param t the top position of the dirty region
9959 * @param r the right position of the dirty region
9960 * @param b the bottom position of the dirty region
9961 */
9962 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009963 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009964 return;
9965 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009966 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009967 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9968 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009970 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009971 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 final ViewParent p = mParent;
9973 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009974 //noinspection PointlessBooleanExpression,ConstantConditions
9975 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9976 if (p != null && ai != null && ai.mHardwareAccelerated) {
9977 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009978 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009979 p.invalidateChild(this, null);
9980 return;
9981 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009983 if (p != null && ai != null && l < r && t < b) {
9984 final int scrollX = mScrollX;
9985 final int scrollY = mScrollY;
9986 final Rect tmpr = ai.mTmpInvalRect;
9987 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9988 p.invalidateChild(this, tmpr);
9989 }
9990 }
9991 }
9992
9993 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009994 * Invalidate the whole view. If the view is visible,
9995 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9996 * the future. This must be called from a UI thread. To call from a non-UI thread,
9997 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998 */
9999 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010000 invalidate(true);
10001 }
Joe Malin32736f02011-01-19 16:14:20 -080010002
Chet Haaseed032702010-10-01 14:05:54 -070010003 /**
10004 * This is where the invalidate() work actually happens. A full invalidate()
10005 * causes the drawing cache to be invalidated, but this function can be called with
10006 * invalidateCache set to false to skip that invalidation step for cases that do not
10007 * need it (for example, a component that remains at the same dimensions with the same
10008 * content).
10009 *
10010 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10011 * well. This is usually true for a full invalidate, but may be set to false if the
10012 * View's contents or dimensions have not changed.
10013 */
Romain Guy849d0a32011-02-01 17:20:48 -080010014 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010015 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010016 return;
10017 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010018 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010019 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010020 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10021 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010022 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010023 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010024 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010025 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010026 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010029 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010030 //noinspection PointlessBooleanExpression,ConstantConditions
10031 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10032 if (p != null && ai != null && ai.mHardwareAccelerated) {
10033 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010034 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010035 p.invalidateChild(this, null);
10036 return;
10037 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010038 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010040 if (p != null && ai != null) {
10041 final Rect r = ai.mTmpInvalRect;
10042 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10043 // Don't call invalidate -- we don't want to internally scroll
10044 // our own bounds
10045 p.invalidateChild(this, r);
10046 }
10047 }
10048 }
10049
10050 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010051 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10052 * set any flags or handle all of the cases handled by the default invalidation methods.
10053 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10054 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10055 * walk up the hierarchy, transforming the dirty rect as necessary.
10056 *
10057 * The method also handles normal invalidation logic if display list properties are not
10058 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10059 * backup approach, to handle these cases used in the various property-setting methods.
10060 *
10061 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10062 * are not being used in this view
10063 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10064 * list properties are not being used in this view
10065 */
10066 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010067 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010068 if (invalidateParent) {
10069 invalidateParentCaches();
10070 }
10071 if (forceRedraw) {
10072 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10073 }
10074 invalidate(false);
10075 } else {
10076 final AttachInfo ai = mAttachInfo;
10077 final ViewParent p = mParent;
10078 if (p != null && ai != null) {
10079 final Rect r = ai.mTmpInvalRect;
10080 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10081 if (mParent instanceof ViewGroup) {
10082 ((ViewGroup) mParent).invalidateChildFast(this, r);
10083 } else {
10084 mParent.invalidateChild(this, r);
10085 }
10086 }
10087 }
10088 }
10089
10090 /**
10091 * Utility method to transform a given Rect by the current matrix of this view.
10092 */
10093 void transformRect(final Rect rect) {
10094 if (!getMatrix().isIdentity()) {
10095 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10096 boundingRect.set(rect);
10097 getMatrix().mapRect(boundingRect);
10098 rect.set((int) (boundingRect.left - 0.5f),
10099 (int) (boundingRect.top - 0.5f),
10100 (int) (boundingRect.right + 0.5f),
10101 (int) (boundingRect.bottom + 0.5f));
10102 }
10103 }
10104
10105 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010106 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010107 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10108 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010109 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10110 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010111 *
10112 * @hide
10113 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010114 protected void invalidateParentCaches() {
10115 if (mParent instanceof View) {
10116 ((View) mParent).mPrivateFlags |= INVALIDATED;
10117 }
10118 }
Joe Malin32736f02011-01-19 16:14:20 -080010119
Romain Guy0fd89bf2011-01-26 15:41:30 -080010120 /**
10121 * Used to indicate that the parent of this view should be invalidated. This functionality
10122 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10123 * which is necessary when various parent-managed properties of the view change, such as
10124 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10125 * an invalidation event to the parent.
10126 *
10127 * @hide
10128 */
10129 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010130 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010131 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010132 }
10133 }
10134
10135 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010136 * Indicates whether this View is opaque. An opaque View guarantees that it will
10137 * draw all the pixels overlapping its bounds using a fully opaque color.
10138 *
10139 * Subclasses of View should override this method whenever possible to indicate
10140 * whether an instance is opaque. Opaque Views are treated in a special way by
10141 * the View hierarchy, possibly allowing it to perform optimizations during
10142 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010143 *
Romain Guy24443ea2009-05-11 11:56:30 -070010144 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010145 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010146 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010147 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010148 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010149 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10150 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010151 }
10152
Adam Powell20232d02010-12-08 21:08:53 -080010153 /**
10154 * @hide
10155 */
10156 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010157 // Opaque if:
10158 // - Has a background
10159 // - Background is opaque
10160 // - Doesn't have scrollbars or scrollbars are inside overlay
10161
Philip Milne6c8ea062012-04-03 17:38:43 -070010162 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010163 mPrivateFlags |= OPAQUE_BACKGROUND;
10164 } else {
10165 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10166 }
10167
10168 final int flags = mViewFlags;
10169 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10170 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10171 mPrivateFlags |= OPAQUE_SCROLLBARS;
10172 } else {
10173 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10174 }
10175 }
10176
10177 /**
10178 * @hide
10179 */
10180 protected boolean hasOpaqueScrollbars() {
10181 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010182 }
10183
10184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 * @return A handler associated with the thread running the View. This
10186 * handler can be used to pump events in the UI events queue.
10187 */
10188 public Handler getHandler() {
10189 if (mAttachInfo != null) {
10190 return mAttachInfo.mHandler;
10191 }
10192 return null;
10193 }
10194
10195 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010196 * Gets the view root associated with the View.
10197 * @return The view root, or null if none.
10198 * @hide
10199 */
10200 public ViewRootImpl getViewRootImpl() {
10201 if (mAttachInfo != null) {
10202 return mAttachInfo.mViewRootImpl;
10203 }
10204 return null;
10205 }
10206
10207 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010208 * <p>Causes the Runnable to be added to the message queue.
10209 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010210 *
Romain Guye63a4f32011-08-11 11:33:31 -070010211 * <p>This method can be invoked from outside of the UI thread
10212 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 *
10214 * @param action The Runnable that will be executed.
10215 *
10216 * @return Returns true if the Runnable was successfully placed in to the
10217 * message queue. Returns false on failure, usually because the
10218 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010219 *
10220 * @see #postDelayed
10221 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 */
10223 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010224 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010225 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010226 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010228 // Assume that post will succeed later
10229 ViewRootImpl.getRunQueue().post(action);
10230 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 }
10232
10233 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010234 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010236 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010237 *
Romain Guye63a4f32011-08-11 11:33:31 -070010238 * <p>This method can be invoked from outside of the UI thread
10239 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 *
10241 * @param action The Runnable that will be executed.
10242 * @param delayMillis The delay (in milliseconds) until the Runnable
10243 * will be executed.
10244 *
10245 * @return true if the Runnable was successfully placed in to the
10246 * message queue. Returns false on failure, usually because the
10247 * looper processing the message queue is exiting. Note that a
10248 * result of true does not mean the Runnable will be processed --
10249 * if the looper is quit before the delivery time of the message
10250 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010251 *
10252 * @see #post
10253 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010254 */
10255 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010256 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010257 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010258 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010260 // Assume that post will succeed later
10261 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10262 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 }
10264
10265 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010266 * <p>Causes the Runnable to execute on the next animation time step.
10267 * The runnable will be run on the user interface thread.</p>
10268 *
10269 * <p>This method can be invoked from outside of the UI thread
10270 * only when this View is attached to a window.</p>
10271 *
10272 * @param action The Runnable that will be executed.
10273 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010274 * @see #postOnAnimationDelayed
10275 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010276 */
10277 public void postOnAnimation(Runnable action) {
10278 final AttachInfo attachInfo = mAttachInfo;
10279 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010280 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10281 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010282 } else {
10283 // Assume that post will succeed later
10284 ViewRootImpl.getRunQueue().post(action);
10285 }
10286 }
10287
10288 /**
10289 * <p>Causes the Runnable to execute on the next animation time step,
10290 * after the specified amount of time elapses.
10291 * The runnable will be run on the user interface thread.</p>
10292 *
10293 * <p>This method can be invoked from outside of the UI thread
10294 * only when this View is attached to a window.</p>
10295 *
10296 * @param action The Runnable that will be executed.
10297 * @param delayMillis The delay (in milliseconds) until the Runnable
10298 * will be executed.
10299 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010300 * @see #postOnAnimation
10301 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010302 */
10303 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10304 final AttachInfo attachInfo = mAttachInfo;
10305 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010306 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10307 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010308 } else {
10309 // Assume that post will succeed later
10310 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10311 }
10312 }
10313
10314 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010315 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010316 *
Romain Guye63a4f32011-08-11 11:33:31 -070010317 * <p>This method can be invoked from outside of the UI thread
10318 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010319 *
10320 * @param action The Runnable to remove from the message handling queue
10321 *
10322 * @return true if this view could ask the Handler to remove the Runnable,
10323 * false otherwise. When the returned value is true, the Runnable
10324 * may or may not have been actually removed from the message queue
10325 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010326 *
10327 * @see #post
10328 * @see #postDelayed
10329 * @see #postOnAnimation
10330 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 */
10332 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010333 if (action != null) {
10334 final AttachInfo attachInfo = mAttachInfo;
10335 if (attachInfo != null) {
10336 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010337 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10338 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010339 } else {
10340 // Assume that post will succeed later
10341 ViewRootImpl.getRunQueue().removeCallbacks(action);
10342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 return true;
10345 }
10346
10347 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010348 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10349 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010350 *
Romain Guye63a4f32011-08-11 11:33:31 -070010351 * <p>This method can be invoked from outside of the UI thread
10352 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010354 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010355 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356 */
10357 public void postInvalidate() {
10358 postInvalidateDelayed(0);
10359 }
10360
10361 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010362 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10363 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010364 *
Romain Guye63a4f32011-08-11 11:33:31 -070010365 * <p>This method can be invoked from outside of the UI thread
10366 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 *
10368 * @param left The left coordinate of the rectangle to invalidate.
10369 * @param top The top coordinate of the rectangle to invalidate.
10370 * @param right The right coordinate of the rectangle to invalidate.
10371 * @param bottom The bottom coordinate of the rectangle to invalidate.
10372 *
10373 * @see #invalidate(int, int, int, int)
10374 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010375 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 */
10377 public void postInvalidate(int left, int top, int right, int bottom) {
10378 postInvalidateDelayed(0, left, top, right, bottom);
10379 }
10380
10381 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010382 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10383 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010384 *
Romain Guye63a4f32011-08-11 11:33:31 -070010385 * <p>This method can be invoked from outside of the UI thread
10386 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010387 *
10388 * @param delayMilliseconds the duration in milliseconds to delay the
10389 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010390 *
10391 * @see #invalidate()
10392 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010393 */
10394 public void postInvalidateDelayed(long delayMilliseconds) {
10395 // We try only with the AttachInfo because there's no point in invalidating
10396 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010397 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010398 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010399 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 }
10401 }
10402
10403 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010404 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10405 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010406 *
Romain Guye63a4f32011-08-11 11:33:31 -070010407 * <p>This method can be invoked from outside of the UI thread
10408 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409 *
10410 * @param delayMilliseconds the duration in milliseconds to delay the
10411 * invalidation by
10412 * @param left The left coordinate of the rectangle to invalidate.
10413 * @param top The top coordinate of the rectangle to invalidate.
10414 * @param right The right coordinate of the rectangle to invalidate.
10415 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010416 *
10417 * @see #invalidate(int, int, int, int)
10418 * @see #invalidate(Rect)
10419 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 */
10421 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10422 int right, int bottom) {
10423
10424 // We try only with the AttachInfo because there's no point in invalidating
10425 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010426 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010427 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10429 info.target = this;
10430 info.left = left;
10431 info.top = top;
10432 info.right = right;
10433 info.bottom = bottom;
10434
Jeff Browna175a5b2012-02-15 19:18:31 -080010435 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 }
10437 }
10438
10439 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010440 * <p>Cause an invalidate to happen on the next animation time step, typically the
10441 * next display frame.</p>
10442 *
10443 * <p>This method can be invoked from outside of the UI thread
10444 * only when this View is attached to a window.</p>
10445 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010446 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010447 */
10448 public void postInvalidateOnAnimation() {
10449 // We try only with the AttachInfo because there's no point in invalidating
10450 // if we are not attached to our window
10451 final AttachInfo attachInfo = mAttachInfo;
10452 if (attachInfo != null) {
10453 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10454 }
10455 }
10456
10457 /**
10458 * <p>Cause an invalidate of the specified area to happen on the next animation
10459 * time step, typically the next display frame.</p>
10460 *
10461 * <p>This method can be invoked from outside of the UI thread
10462 * only when this View is attached to a window.</p>
10463 *
10464 * @param left The left coordinate of the rectangle to invalidate.
10465 * @param top The top coordinate of the rectangle to invalidate.
10466 * @param right The right coordinate of the rectangle to invalidate.
10467 * @param bottom The bottom coordinate of the rectangle to invalidate.
10468 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010469 * @see #invalidate(int, int, int, int)
10470 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010471 */
10472 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10473 // We try only with the AttachInfo because there's no point in invalidating
10474 // if we are not attached to our window
10475 final AttachInfo attachInfo = mAttachInfo;
10476 if (attachInfo != null) {
10477 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10478 info.target = this;
10479 info.left = left;
10480 info.top = top;
10481 info.right = right;
10482 info.bottom = bottom;
10483
10484 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10485 }
10486 }
10487
10488 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010489 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10490 * This event is sent at most once every
10491 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10492 */
10493 private void postSendViewScrolledAccessibilityEventCallback() {
10494 if (mSendViewScrolledAccessibilityEvent == null) {
10495 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10496 }
10497 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10498 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10499 postDelayed(mSendViewScrolledAccessibilityEvent,
10500 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10501 }
10502 }
10503
10504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 * Called by a parent to request that a child update its values for mScrollX
10506 * and mScrollY if necessary. This will typically be done if the child is
10507 * animating a scroll using a {@link android.widget.Scroller Scroller}
10508 * object.
10509 */
10510 public void computeScroll() {
10511 }
10512
10513 /**
10514 * <p>Indicate whether the horizontal edges are faded when the view is
10515 * scrolled horizontally.</p>
10516 *
10517 * @return true if the horizontal edges should are faded on scroll, false
10518 * otherwise
10519 *
10520 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010521 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010522 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523 */
10524 public boolean isHorizontalFadingEdgeEnabled() {
10525 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10526 }
10527
10528 /**
10529 * <p>Define whether the horizontal edges should be faded when this view
10530 * is scrolled horizontally.</p>
10531 *
10532 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10533 * be faded when the view is scrolled
10534 * horizontally
10535 *
10536 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010537 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010538 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 */
10540 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10541 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10542 if (horizontalFadingEdgeEnabled) {
10543 initScrollCache();
10544 }
10545
10546 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10547 }
10548 }
10549
10550 /**
10551 * <p>Indicate whether the vertical edges are faded when the view is
10552 * scrolled horizontally.</p>
10553 *
10554 * @return true if the vertical edges should are faded on scroll, false
10555 * otherwise
10556 *
10557 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010558 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010559 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 */
10561 public boolean isVerticalFadingEdgeEnabled() {
10562 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10563 }
10564
10565 /**
10566 * <p>Define whether the vertical edges should be faded when this view
10567 * is scrolled vertically.</p>
10568 *
10569 * @param verticalFadingEdgeEnabled true if the vertical edges should
10570 * be faded when the view is scrolled
10571 * vertically
10572 *
10573 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010574 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010575 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 */
10577 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10578 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10579 if (verticalFadingEdgeEnabled) {
10580 initScrollCache();
10581 }
10582
10583 mViewFlags ^= FADING_EDGE_VERTICAL;
10584 }
10585 }
10586
10587 /**
10588 * Returns the strength, or intensity, of the top faded edge. The strength is
10589 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10590 * returns 0.0 or 1.0 but no value in between.
10591 *
10592 * Subclasses should override this method to provide a smoother fade transition
10593 * when scrolling occurs.
10594 *
10595 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10596 */
10597 protected float getTopFadingEdgeStrength() {
10598 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10599 }
10600
10601 /**
10602 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10603 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10604 * returns 0.0 or 1.0 but no value in between.
10605 *
10606 * Subclasses should override this method to provide a smoother fade transition
10607 * when scrolling occurs.
10608 *
10609 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10610 */
10611 protected float getBottomFadingEdgeStrength() {
10612 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10613 computeVerticalScrollRange() ? 1.0f : 0.0f;
10614 }
10615
10616 /**
10617 * Returns the strength, or intensity, of the left faded edge. The strength is
10618 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10619 * returns 0.0 or 1.0 but no value in between.
10620 *
10621 * Subclasses should override this method to provide a smoother fade transition
10622 * when scrolling occurs.
10623 *
10624 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10625 */
10626 protected float getLeftFadingEdgeStrength() {
10627 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10628 }
10629
10630 /**
10631 * Returns the strength, or intensity, of the right faded edge. The strength is
10632 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10633 * returns 0.0 or 1.0 but no value in between.
10634 *
10635 * Subclasses should override this method to provide a smoother fade transition
10636 * when scrolling occurs.
10637 *
10638 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10639 */
10640 protected float getRightFadingEdgeStrength() {
10641 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10642 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10643 }
10644
10645 /**
10646 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10647 * scrollbar is not drawn by default.</p>
10648 *
10649 * @return true if the horizontal scrollbar should be painted, false
10650 * otherwise
10651 *
10652 * @see #setHorizontalScrollBarEnabled(boolean)
10653 */
10654 public boolean isHorizontalScrollBarEnabled() {
10655 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10656 }
10657
10658 /**
10659 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10660 * scrollbar is not drawn by default.</p>
10661 *
10662 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10663 * be painted
10664 *
10665 * @see #isHorizontalScrollBarEnabled()
10666 */
10667 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10668 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10669 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010670 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010671 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 }
10673 }
10674
10675 /**
10676 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10677 * scrollbar is not drawn by default.</p>
10678 *
10679 * @return true if the vertical scrollbar should be painted, false
10680 * otherwise
10681 *
10682 * @see #setVerticalScrollBarEnabled(boolean)
10683 */
10684 public boolean isVerticalScrollBarEnabled() {
10685 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10686 }
10687
10688 /**
10689 * <p>Define whether the vertical scrollbar should be drawn or not. The
10690 * scrollbar is not drawn by default.</p>
10691 *
10692 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10693 * be painted
10694 *
10695 * @see #isVerticalScrollBarEnabled()
10696 */
10697 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10698 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10699 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010700 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010701 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 }
10703 }
10704
Adam Powell20232d02010-12-08 21:08:53 -080010705 /**
10706 * @hide
10707 */
10708 protected void recomputePadding() {
10709 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 }
Joe Malin32736f02011-01-19 16:14:20 -080010711
Mike Cleronfe81d382009-09-28 14:22:16 -070010712 /**
10713 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010714 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010715 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010716 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010717 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010718 */
10719 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10720 initScrollCache();
10721 final ScrollabilityCache scrollabilityCache = mScrollCache;
10722 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010723 if (fadeScrollbars) {
10724 scrollabilityCache.state = ScrollabilityCache.OFF;
10725 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010726 scrollabilityCache.state = ScrollabilityCache.ON;
10727 }
10728 }
Joe Malin32736f02011-01-19 16:14:20 -080010729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010730 /**
Joe Malin32736f02011-01-19 16:14:20 -080010731 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010732 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010733 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010734 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010735 *
10736 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010737 */
10738 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010739 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010740 }
Joe Malin32736f02011-01-19 16:14:20 -080010741
Mike Cleron52f0a642009-09-28 18:21:37 -070010742 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010743 *
10744 * Returns the delay before scrollbars fade.
10745 *
10746 * @return the delay before scrollbars fade
10747 *
10748 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10749 */
10750 public int getScrollBarDefaultDelayBeforeFade() {
10751 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10752 mScrollCache.scrollBarDefaultDelayBeforeFade;
10753 }
10754
10755 /**
10756 * Define the delay before scrollbars fade.
10757 *
10758 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10759 *
10760 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10761 */
10762 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10763 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10764 }
10765
10766 /**
10767 *
10768 * Returns the scrollbar fade duration.
10769 *
10770 * @return the scrollbar fade duration
10771 *
10772 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10773 */
10774 public int getScrollBarFadeDuration() {
10775 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10776 mScrollCache.scrollBarFadeDuration;
10777 }
10778
10779 /**
10780 * Define the scrollbar fade duration.
10781 *
10782 * @param scrollBarFadeDuration - the scrollbar fade duration
10783 *
10784 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10785 */
10786 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10787 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10788 }
10789
10790 /**
10791 *
10792 * Returns the scrollbar size.
10793 *
10794 * @return the scrollbar size
10795 *
10796 * @attr ref android.R.styleable#View_scrollbarSize
10797 */
10798 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010799 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010800 mScrollCache.scrollBarSize;
10801 }
10802
10803 /**
10804 * Define the scrollbar size.
10805 *
10806 * @param scrollBarSize - the scrollbar size
10807 *
10808 * @attr ref android.R.styleable#View_scrollbarSize
10809 */
10810 public void setScrollBarSize(int scrollBarSize) {
10811 getScrollCache().scrollBarSize = scrollBarSize;
10812 }
10813
10814 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10816 * inset. When inset, they add to the padding of the view. And the scrollbars
10817 * can be drawn inside the padding area or on the edge of the view. For example,
10818 * if a view has a background drawable and you want to draw the scrollbars
10819 * inside the padding specified by the drawable, you can use
10820 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10821 * appear at the edge of the view, ignoring the padding, then you can use
10822 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10823 * @param style the style of the scrollbars. Should be one of
10824 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10825 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10826 * @see #SCROLLBARS_INSIDE_OVERLAY
10827 * @see #SCROLLBARS_INSIDE_INSET
10828 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10829 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010830 *
10831 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010832 */
10833 public void setScrollBarStyle(int style) {
10834 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10835 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010836 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010837 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 }
10839 }
10840
10841 /**
10842 * <p>Returns the current scrollbar style.</p>
10843 * @return the current scrollbar style
10844 * @see #SCROLLBARS_INSIDE_OVERLAY
10845 * @see #SCROLLBARS_INSIDE_INSET
10846 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10847 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010848 *
10849 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010851 @ViewDebug.ExportedProperty(mapping = {
10852 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10853 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10854 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10855 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10856 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010857 public int getScrollBarStyle() {
10858 return mViewFlags & SCROLLBARS_STYLE_MASK;
10859 }
10860
10861 /**
10862 * <p>Compute the horizontal range that the horizontal scrollbar
10863 * represents.</p>
10864 *
10865 * <p>The range is expressed in arbitrary units that must be the same as the
10866 * units used by {@link #computeHorizontalScrollExtent()} and
10867 * {@link #computeHorizontalScrollOffset()}.</p>
10868 *
10869 * <p>The default range is the drawing width of this view.</p>
10870 *
10871 * @return the total horizontal range represented by the horizontal
10872 * scrollbar
10873 *
10874 * @see #computeHorizontalScrollExtent()
10875 * @see #computeHorizontalScrollOffset()
10876 * @see android.widget.ScrollBarDrawable
10877 */
10878 protected int computeHorizontalScrollRange() {
10879 return getWidth();
10880 }
10881
10882 /**
10883 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10884 * within the horizontal range. This value is used to compute the position
10885 * of the thumb within the scrollbar's track.</p>
10886 *
10887 * <p>The range is expressed in arbitrary units that must be the same as the
10888 * units used by {@link #computeHorizontalScrollRange()} and
10889 * {@link #computeHorizontalScrollExtent()}.</p>
10890 *
10891 * <p>The default offset is the scroll offset of this view.</p>
10892 *
10893 * @return the horizontal offset of the scrollbar's thumb
10894 *
10895 * @see #computeHorizontalScrollRange()
10896 * @see #computeHorizontalScrollExtent()
10897 * @see android.widget.ScrollBarDrawable
10898 */
10899 protected int computeHorizontalScrollOffset() {
10900 return mScrollX;
10901 }
10902
10903 /**
10904 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10905 * within the horizontal range. This value is used to compute the length
10906 * of the thumb within the scrollbar's track.</p>
10907 *
10908 * <p>The range is expressed in arbitrary units that must be the same as the
10909 * units used by {@link #computeHorizontalScrollRange()} and
10910 * {@link #computeHorizontalScrollOffset()}.</p>
10911 *
10912 * <p>The default extent is the drawing width of this view.</p>
10913 *
10914 * @return the horizontal extent of the scrollbar's thumb
10915 *
10916 * @see #computeHorizontalScrollRange()
10917 * @see #computeHorizontalScrollOffset()
10918 * @see android.widget.ScrollBarDrawable
10919 */
10920 protected int computeHorizontalScrollExtent() {
10921 return getWidth();
10922 }
10923
10924 /**
10925 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10926 *
10927 * <p>The range is expressed in arbitrary units that must be the same as the
10928 * units used by {@link #computeVerticalScrollExtent()} and
10929 * {@link #computeVerticalScrollOffset()}.</p>
10930 *
10931 * @return the total vertical range represented by the vertical scrollbar
10932 *
10933 * <p>The default range is the drawing height of this view.</p>
10934 *
10935 * @see #computeVerticalScrollExtent()
10936 * @see #computeVerticalScrollOffset()
10937 * @see android.widget.ScrollBarDrawable
10938 */
10939 protected int computeVerticalScrollRange() {
10940 return getHeight();
10941 }
10942
10943 /**
10944 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10945 * within the horizontal range. This value is used to compute the position
10946 * of the thumb within the scrollbar's track.</p>
10947 *
10948 * <p>The range is expressed in arbitrary units that must be the same as the
10949 * units used by {@link #computeVerticalScrollRange()} and
10950 * {@link #computeVerticalScrollExtent()}.</p>
10951 *
10952 * <p>The default offset is the scroll offset of this view.</p>
10953 *
10954 * @return the vertical offset of the scrollbar's thumb
10955 *
10956 * @see #computeVerticalScrollRange()
10957 * @see #computeVerticalScrollExtent()
10958 * @see android.widget.ScrollBarDrawable
10959 */
10960 protected int computeVerticalScrollOffset() {
10961 return mScrollY;
10962 }
10963
10964 /**
10965 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10966 * within the vertical range. This value is used to compute the length
10967 * of the thumb within the scrollbar's track.</p>
10968 *
10969 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010970 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 * {@link #computeVerticalScrollOffset()}.</p>
10972 *
10973 * <p>The default extent is the drawing height of this view.</p>
10974 *
10975 * @return the vertical extent of the scrollbar's thumb
10976 *
10977 * @see #computeVerticalScrollRange()
10978 * @see #computeVerticalScrollOffset()
10979 * @see android.widget.ScrollBarDrawable
10980 */
10981 protected int computeVerticalScrollExtent() {
10982 return getHeight();
10983 }
10984
10985 /**
Adam Powell69159442011-06-13 17:53:06 -070010986 * Check if this view can be scrolled horizontally in a certain direction.
10987 *
10988 * @param direction Negative to check scrolling left, positive to check scrolling right.
10989 * @return true if this view can be scrolled in the specified direction, false otherwise.
10990 */
10991 public boolean canScrollHorizontally(int direction) {
10992 final int offset = computeHorizontalScrollOffset();
10993 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10994 if (range == 0) return false;
10995 if (direction < 0) {
10996 return offset > 0;
10997 } else {
10998 return offset < range - 1;
10999 }
11000 }
11001
11002 /**
11003 * Check if this view can be scrolled vertically in a certain direction.
11004 *
11005 * @param direction Negative to check scrolling up, positive to check scrolling down.
11006 * @return true if this view can be scrolled in the specified direction, false otherwise.
11007 */
11008 public boolean canScrollVertically(int direction) {
11009 final int offset = computeVerticalScrollOffset();
11010 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11011 if (range == 0) return false;
11012 if (direction < 0) {
11013 return offset > 0;
11014 } else {
11015 return offset < range - 1;
11016 }
11017 }
11018
11019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11021 * scrollbars are painted only if they have been awakened first.</p>
11022 *
11023 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011024 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011025 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011027 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 // scrollbars are drawn only when the animation is running
11029 final ScrollabilityCache cache = mScrollCache;
11030 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011031
Mike Cleronf116bf82009-09-27 19:14:12 -070011032 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011033
Mike Cleronf116bf82009-09-27 19:14:12 -070011034 if (state == ScrollabilityCache.OFF) {
11035 return;
11036 }
Joe Malin32736f02011-01-19 16:14:20 -080011037
Mike Cleronf116bf82009-09-27 19:14:12 -070011038 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011039
Mike Cleronf116bf82009-09-27 19:14:12 -070011040 if (state == ScrollabilityCache.FADING) {
11041 // We're fading -- get our fade interpolation
11042 if (cache.interpolatorValues == null) {
11043 cache.interpolatorValues = new float[1];
11044 }
Joe Malin32736f02011-01-19 16:14:20 -080011045
Mike Cleronf116bf82009-09-27 19:14:12 -070011046 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011047
Mike Cleronf116bf82009-09-27 19:14:12 -070011048 // Stops the animation if we're done
11049 if (cache.scrollBarInterpolator.timeToValues(values) ==
11050 Interpolator.Result.FREEZE_END) {
11051 cache.state = ScrollabilityCache.OFF;
11052 } else {
11053 cache.scrollBar.setAlpha(Math.round(values[0]));
11054 }
Joe Malin32736f02011-01-19 16:14:20 -080011055
11056 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011057 // drawing. We only want this when we're fading so that
11058 // we prevent excessive redraws
11059 invalidate = true;
11060 } else {
11061 // We're just on -- but we may have been fading before so
11062 // reset alpha
11063 cache.scrollBar.setAlpha(255);
11064 }
11065
Joe Malin32736f02011-01-19 16:14:20 -080011066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067 final int viewFlags = mViewFlags;
11068
11069 final boolean drawHorizontalScrollBar =
11070 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11071 final boolean drawVerticalScrollBar =
11072 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11073 && !isVerticalScrollBarHidden();
11074
11075 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11076 final int width = mRight - mLeft;
11077 final int height = mBottom - mTop;
11078
11079 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011080
Mike Reede8853fc2009-09-04 14:01:48 -040011081 final int scrollX = mScrollX;
11082 final int scrollY = mScrollY;
11083 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11084
Mike Cleronf116bf82009-09-27 19:14:12 -070011085 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011088 int size = scrollBar.getSize(false);
11089 if (size <= 0) {
11090 size = cache.scrollBarSize;
11091 }
11092
Mike Cleronf116bf82009-09-27 19:14:12 -070011093 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011094 computeHorizontalScrollOffset(),
11095 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011096 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011097 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011098 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011099 left = scrollX + (mPaddingLeft & inside);
11100 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11101 bottom = top + size;
11102 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11103 if (invalidate) {
11104 invalidate(left, top, right, bottom);
11105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011106 }
11107
11108 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011109 int size = scrollBar.getSize(true);
11110 if (size <= 0) {
11111 size = cache.scrollBarSize;
11112 }
11113
Mike Reede8853fc2009-09-04 14:01:48 -040011114 scrollBar.setParameters(computeVerticalScrollRange(),
11115 computeVerticalScrollOffset(),
11116 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011117 switch (mVerticalScrollbarPosition) {
11118 default:
11119 case SCROLLBAR_POSITION_DEFAULT:
11120 case SCROLLBAR_POSITION_RIGHT:
11121 left = scrollX + width - size - (mUserPaddingRight & inside);
11122 break;
11123 case SCROLLBAR_POSITION_LEFT:
11124 left = scrollX + (mUserPaddingLeft & inside);
11125 break;
11126 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011127 top = scrollY + (mPaddingTop & inside);
11128 right = left + size;
11129 bottom = scrollY + height - (mUserPaddingBottom & inside);
11130 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11131 if (invalidate) {
11132 invalidate(left, top, right, bottom);
11133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 }
11135 }
11136 }
11137 }
Romain Guy8506ab42009-06-11 17:35:47 -070011138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011140 * 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 -080011141 * FastScroller is visible.
11142 * @return whether to temporarily hide the vertical scrollbar
11143 * @hide
11144 */
11145 protected boolean isVerticalScrollBarHidden() {
11146 return false;
11147 }
11148
11149 /**
11150 * <p>Draw the horizontal scrollbar if
11151 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 * @param canvas the canvas on which to draw the scrollbar
11154 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 *
11156 * @see #isHorizontalScrollBarEnabled()
11157 * @see #computeHorizontalScrollRange()
11158 * @see #computeHorizontalScrollExtent()
11159 * @see #computeHorizontalScrollOffset()
11160 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011161 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011162 */
Romain Guy8fb95422010-08-17 18:38:51 -070011163 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11164 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011165 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011166 scrollBar.draw(canvas);
11167 }
Mike Reede8853fc2009-09-04 14:01:48 -040011168
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11171 * returns true.</p>
11172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011173 * @param canvas the canvas on which to draw the scrollbar
11174 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 *
11176 * @see #isVerticalScrollBarEnabled()
11177 * @see #computeVerticalScrollRange()
11178 * @see #computeVerticalScrollExtent()
11179 * @see #computeVerticalScrollOffset()
11180 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011181 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011182 */
Romain Guy8fb95422010-08-17 18:38:51 -070011183 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11184 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011185 scrollBar.setBounds(l, t, r, b);
11186 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 }
11188
11189 /**
11190 * Implement this to do your drawing.
11191 *
11192 * @param canvas the canvas on which the background will be drawn
11193 */
11194 protected void onDraw(Canvas canvas) {
11195 }
11196
11197 /*
11198 * Caller is responsible for calling requestLayout if necessary.
11199 * (This allows addViewInLayout to not request a new layout.)
11200 */
11201 void assignParent(ViewParent parent) {
11202 if (mParent == null) {
11203 mParent = parent;
11204 } else if (parent == null) {
11205 mParent = null;
11206 } else {
11207 throw new RuntimeException("view " + this + " being added, but"
11208 + " it already has a parent");
11209 }
11210 }
11211
11212 /**
11213 * This is called when the view is attached to a window. At this point it
11214 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011215 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11216 * however it may be called any time before the first onDraw -- including
11217 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 *
11219 * @see #onDetachedFromWindow()
11220 */
11221 protected void onAttachedToWindow() {
11222 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11223 mParent.requestTransparentRegion(this);
11224 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011225
Adam Powell8568c3a2010-04-19 14:26:11 -070011226 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11227 initialAwakenScrollBars();
11228 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11229 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011230
Chet Haasea9b61ac2010-12-20 07:40:25 -080011231 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011232
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011233 // Order is important here: LayoutDirection MUST be resolved before Padding
11234 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011235 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011236 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011237 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011238 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011239
Svetoslav Ganov42138042012-03-20 11:51:39 -070011240 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011241 if (isFocused()) {
11242 InputMethodManager imm = InputMethodManager.peekInstance();
11243 imm.focusIn(this);
11244 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011245
11246 if (mAttachInfo != null && mDisplayList != null) {
11247 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11248 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011249 }
Cibu Johny86666632010-02-22 13:01:02 -080011250
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011251 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011252 * @see #onScreenStateChanged(int)
11253 */
11254 void dispatchScreenStateChanged(int screenState) {
11255 onScreenStateChanged(screenState);
11256 }
11257
11258 /**
11259 * This method is called whenever the state of the screen this view is
11260 * attached to changes. A state change will usually occurs when the screen
11261 * turns on or off (whether it happens automatically or the user does it
11262 * manually.)
11263 *
11264 * @param screenState The new state of the screen. Can be either
11265 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11266 */
11267 public void onScreenStateChanged(int screenState) {
11268 }
11269
11270 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011271 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11272 */
11273 private boolean hasRtlSupport() {
11274 return mContext.getApplicationInfo().hasRtlSupport();
11275 }
11276
11277 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011278 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11279 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011280 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011281 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011282 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011283 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011284 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011285 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011286
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011287 if (hasRtlSupport()) {
11288 // Set resolved depending on layout direction
11289 switch (getLayoutDirection()) {
11290 case LAYOUT_DIRECTION_INHERIT:
11291 // If this is root view, no need to look at parent's layout dir.
11292 if (canResolveLayoutDirection()) {
11293 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011294
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011295 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11296 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11297 }
11298 } else {
11299 // Nothing to do, LTR by default
11300 }
11301 break;
11302 case LAYOUT_DIRECTION_RTL:
11303 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11304 break;
11305 case LAYOUT_DIRECTION_LOCALE:
11306 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011307 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11308 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011309 break;
11310 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011311 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011312 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011313 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011314
11315 // Set to resolved
11316 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011317 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011318 // Resolve padding
11319 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011320 }
11321
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011322 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011323 * Called when layout direction has been resolved.
11324 *
11325 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011326 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011327 */
11328 public void onResolvedLayoutDirectionChanged() {
11329 }
11330
11331 /**
11332 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011333 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011334 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011335 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011336 // If the user specified the absolute padding (either with android:padding or
11337 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11338 // use the default padding or the padding from the background drawable
11339 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011340 int resolvedLayoutDirection = getResolvedLayoutDirection();
11341 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011342 case LAYOUT_DIRECTION_RTL:
11343 // Start user padding override Right user padding. Otherwise, if Right user
11344 // padding is not defined, use the default Right padding. If Right user padding
11345 // is defined, just use it.
11346 if (mUserPaddingStart >= 0) {
11347 mUserPaddingRight = mUserPaddingStart;
11348 } else if (mUserPaddingRight < 0) {
11349 mUserPaddingRight = mPaddingRight;
11350 }
11351 if (mUserPaddingEnd >= 0) {
11352 mUserPaddingLeft = mUserPaddingEnd;
11353 } else if (mUserPaddingLeft < 0) {
11354 mUserPaddingLeft = mPaddingLeft;
11355 }
11356 break;
11357 case LAYOUT_DIRECTION_LTR:
11358 default:
11359 // Start user padding override Left user padding. Otherwise, if Left user
11360 // padding is not defined, use the default left padding. If Left user padding
11361 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011362 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011363 mUserPaddingLeft = mUserPaddingStart;
11364 } else if (mUserPaddingLeft < 0) {
11365 mUserPaddingLeft = mPaddingLeft;
11366 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011367 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011368 mUserPaddingRight = mUserPaddingEnd;
11369 } else if (mUserPaddingRight < 0) {
11370 mUserPaddingRight = mPaddingRight;
11371 }
11372 }
11373
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011374 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11375
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011376 if(isPaddingRelative()) {
11377 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11378 } else {
11379 recomputePadding();
11380 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011381 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011382 }
11383
11384 /**
11385 * Resolve padding depending on the layout direction. Subclasses that care about
11386 * padding resolution should override this method. The default implementation does
11387 * nothing.
11388 *
11389 * @param layoutDirection the direction of the layout
11390 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011391 * @see {@link #LAYOUT_DIRECTION_LTR}
11392 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011393 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011394 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011395 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011396 }
11397
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011398 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011399 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011400 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011401 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011402 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011403 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011404 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011405 switch (getLayoutDirection()) {
11406 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011407 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011408 default:
11409 return true;
11410 }
11411 }
11412
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011413 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011414 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11415 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011416 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011417 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011418 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011419 // Reset the current resolved bits
11420 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011421 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011422 // Reset also the text direction
11423 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011424 }
11425
11426 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011427 * Called during reset of resolved layout direction.
11428 *
11429 * Subclasses need to override this method to clear cached information that depends on the
11430 * resolved layout direction, or to inform child views that inherit their layout direction.
11431 *
11432 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011433 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011434 */
11435 public void onResolvedLayoutDirectionReset() {
11436 }
11437
11438 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011439 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011440 *
11441 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011442 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011443 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011444 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011445 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011446 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011447 }
11448
11449 /**
11450 * This is called when the view is detached from a window. At this point it
11451 * no longer has a surface for drawing.
11452 *
11453 * @see #onAttachedToWindow()
11454 */
11455 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011456 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011457
Romain Guya440b002010-02-24 15:57:54 -080011458 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011459 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011460 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011461 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011464
Romain Guya998dff2012-03-23 18:58:36 -070011465 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011466
11467 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011468 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011469 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011470 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011471 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011472 } else {
11473 if (mDisplayList != null) {
11474 // Should never happen
11475 mDisplayList.invalidate();
11476 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011477 }
11478
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011479 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011480
11481 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011482 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011483 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 }
11485
11486 /**
11487 * @return The number of times this view has been attached to a window
11488 */
11489 protected int getWindowAttachCount() {
11490 return mWindowAttachCount;
11491 }
11492
11493 /**
11494 * Retrieve a unique token identifying the window this view is attached to.
11495 * @return Return the window's token for use in
11496 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11497 */
11498 public IBinder getWindowToken() {
11499 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11500 }
11501
11502 /**
11503 * Retrieve a unique token identifying the top-level "real" window of
11504 * the window that this view is attached to. That is, this is like
11505 * {@link #getWindowToken}, except if the window this view in is a panel
11506 * window (attached to another containing window), then the token of
11507 * the containing window is returned instead.
11508 *
11509 * @return Returns the associated window token, either
11510 * {@link #getWindowToken()} or the containing window's token.
11511 */
11512 public IBinder getApplicationWindowToken() {
11513 AttachInfo ai = mAttachInfo;
11514 if (ai != null) {
11515 IBinder appWindowToken = ai.mPanelParentWindowToken;
11516 if (appWindowToken == null) {
11517 appWindowToken = ai.mWindowToken;
11518 }
11519 return appWindowToken;
11520 }
11521 return null;
11522 }
11523
11524 /**
11525 * Retrieve private session object this view hierarchy is using to
11526 * communicate with the window manager.
11527 * @return the session object to communicate with the window manager
11528 */
11529 /*package*/ IWindowSession getWindowSession() {
11530 return mAttachInfo != null ? mAttachInfo.mSession : null;
11531 }
11532
11533 /**
11534 * @param info the {@link android.view.View.AttachInfo} to associated with
11535 * this view
11536 */
11537 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11538 //System.out.println("Attached! " + this);
11539 mAttachInfo = info;
11540 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011541 // We will need to evaluate the drawable state at least once.
11542 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 if (mFloatingTreeObserver != null) {
11544 info.mTreeObserver.merge(mFloatingTreeObserver);
11545 mFloatingTreeObserver = null;
11546 }
11547 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11548 mAttachInfo.mScrollContainers.add(this);
11549 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11550 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011551 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011553
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011554 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011555 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011556 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011557 if (listeners != null && listeners.size() > 0) {
11558 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11559 // perform the dispatching. The iterator is a safe guard against listeners that
11560 // could mutate the list by calling the various add/remove methods. This prevents
11561 // the array from being modified while we iterate it.
11562 for (OnAttachStateChangeListener listener : listeners) {
11563 listener.onViewAttachedToWindow(this);
11564 }
11565 }
11566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011567 int vis = info.mWindowVisibility;
11568 if (vis != GONE) {
11569 onWindowVisibilityChanged(vis);
11570 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011571 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11572 // If nobody has evaluated the drawable state yet, then do it now.
11573 refreshDrawableState();
11574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 }
11576
11577 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 AttachInfo info = mAttachInfo;
11579 if (info != null) {
11580 int vis = info.mWindowVisibility;
11581 if (vis != GONE) {
11582 onWindowVisibilityChanged(GONE);
11583 }
11584 }
11585
11586 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011587
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011588 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011589 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011590 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011591 if (listeners != null && listeners.size() > 0) {
11592 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11593 // perform the dispatching. The iterator is a safe guard against listeners that
11594 // could mutate the list by calling the various add/remove methods. This prevents
11595 // the array from being modified while we iterate it.
11596 for (OnAttachStateChangeListener listener : listeners) {
11597 listener.onViewDetachedFromWindow(this);
11598 }
11599 }
11600
Romain Guy01d5edc2011-01-28 11:28:53 -080011601 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 mAttachInfo.mScrollContainers.remove(this);
11603 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11604 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011606 mAttachInfo = null;
11607 }
11608
11609 /**
11610 * Store this view hierarchy's frozen state into the given container.
11611 *
11612 * @param container The SparseArray in which to save the view's state.
11613 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011614 * @see #restoreHierarchyState(android.util.SparseArray)
11615 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11616 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 */
11618 public void saveHierarchyState(SparseArray<Parcelable> container) {
11619 dispatchSaveInstanceState(container);
11620 }
11621
11622 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011623 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11624 * this view and its children. May be overridden to modify how freezing happens to a
11625 * 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 -080011626 *
11627 * @param container The SparseArray in which to save the view's state.
11628 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011629 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11630 * @see #saveHierarchyState(android.util.SparseArray)
11631 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 */
11633 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11634 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11635 mPrivateFlags &= ~SAVE_STATE_CALLED;
11636 Parcelable state = onSaveInstanceState();
11637 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11638 throw new IllegalStateException(
11639 "Derived class did not call super.onSaveInstanceState()");
11640 }
11641 if (state != null) {
11642 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11643 // + ": " + state);
11644 container.put(mID, state);
11645 }
11646 }
11647 }
11648
11649 /**
11650 * Hook allowing a view to generate a representation of its internal state
11651 * that can later be used to create a new instance with that same state.
11652 * This state should only contain information that is not persistent or can
11653 * not be reconstructed later. For example, you will never store your
11654 * current position on screen because that will be computed again when a
11655 * new instance of the view is placed in its view hierarchy.
11656 * <p>
11657 * Some examples of things you may store here: the current cursor position
11658 * in a text view (but usually not the text itself since that is stored in a
11659 * content provider or other persistent storage), the currently selected
11660 * item in a list view.
11661 *
11662 * @return Returns a Parcelable object containing the view's current dynamic
11663 * state, or null if there is nothing interesting to save. The
11664 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011665 * @see #onRestoreInstanceState(android.os.Parcelable)
11666 * @see #saveHierarchyState(android.util.SparseArray)
11667 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 * @see #setSaveEnabled(boolean)
11669 */
11670 protected Parcelable onSaveInstanceState() {
11671 mPrivateFlags |= SAVE_STATE_CALLED;
11672 return BaseSavedState.EMPTY_STATE;
11673 }
11674
11675 /**
11676 * Restore this view hierarchy's frozen state from the given container.
11677 *
11678 * @param container The SparseArray which holds previously frozen states.
11679 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011680 * @see #saveHierarchyState(android.util.SparseArray)
11681 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11682 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011683 */
11684 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11685 dispatchRestoreInstanceState(container);
11686 }
11687
11688 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011689 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11690 * state for this view and its children. May be overridden to modify how restoring
11691 * happens to a view's children; for example, some views may want to not store state
11692 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 *
11694 * @param container The SparseArray which holds previously saved state.
11695 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011696 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11697 * @see #restoreHierarchyState(android.util.SparseArray)
11698 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011699 */
11700 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11701 if (mID != NO_ID) {
11702 Parcelable state = container.get(mID);
11703 if (state != null) {
11704 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11705 // + ": " + state);
11706 mPrivateFlags &= ~SAVE_STATE_CALLED;
11707 onRestoreInstanceState(state);
11708 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11709 throw new IllegalStateException(
11710 "Derived class did not call super.onRestoreInstanceState()");
11711 }
11712 }
11713 }
11714 }
11715
11716 /**
11717 * Hook allowing a view to re-apply a representation of its internal state that had previously
11718 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11719 * null state.
11720 *
11721 * @param state The frozen state that had previously been returned by
11722 * {@link #onSaveInstanceState}.
11723 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011724 * @see #onSaveInstanceState()
11725 * @see #restoreHierarchyState(android.util.SparseArray)
11726 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011727 */
11728 protected void onRestoreInstanceState(Parcelable state) {
11729 mPrivateFlags |= SAVE_STATE_CALLED;
11730 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011731 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11732 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011733 + "when two views of different type have the same id in the same hierarchy. "
11734 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011735 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 }
11737 }
11738
11739 /**
11740 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11741 *
11742 * @return the drawing start time in milliseconds
11743 */
11744 public long getDrawingTime() {
11745 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11746 }
11747
11748 /**
11749 * <p>Enables or disables the duplication of the parent's state into this view. When
11750 * duplication is enabled, this view gets its drawable state from its parent rather
11751 * than from its own internal properties.</p>
11752 *
11753 * <p>Note: in the current implementation, setting this property to true after the
11754 * view was added to a ViewGroup might have no effect at all. This property should
11755 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11756 *
11757 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11758 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011759 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011760 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11761 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 *
11763 * @param enabled True to enable duplication of the parent's drawable state, false
11764 * to disable it.
11765 *
11766 * @see #getDrawableState()
11767 * @see #isDuplicateParentStateEnabled()
11768 */
11769 public void setDuplicateParentStateEnabled(boolean enabled) {
11770 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11771 }
11772
11773 /**
11774 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11775 *
11776 * @return True if this view's drawable state is duplicated from the parent,
11777 * false otherwise
11778 *
11779 * @see #getDrawableState()
11780 * @see #setDuplicateParentStateEnabled(boolean)
11781 */
11782 public boolean isDuplicateParentStateEnabled() {
11783 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11784 }
11785
11786 /**
Romain Guy171c5922011-01-06 10:04:23 -080011787 * <p>Specifies the type of layer backing this view. The layer can be
11788 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11789 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011790 *
Romain Guy171c5922011-01-06 10:04:23 -080011791 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11792 * instance that controls how the layer is composed on screen. The following
11793 * properties of the paint are taken into account when composing the layer:</p>
11794 * <ul>
11795 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11796 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11797 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11798 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011799 *
Romain Guy171c5922011-01-06 10:04:23 -080011800 * <p>If this view has an alpha value set to < 1.0 by calling
11801 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11802 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11803 * equivalent to setting a hardware layer on this view and providing a paint with
11804 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011805 *
Romain Guy171c5922011-01-06 10:04:23 -080011806 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11807 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11808 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011809 *
Romain Guy171c5922011-01-06 10:04:23 -080011810 * @param layerType The ype of layer to use with this view, must be one of
11811 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11812 * {@link #LAYER_TYPE_HARDWARE}
11813 * @param paint The paint used to compose the layer. This argument is optional
11814 * and can be null. It is ignored when the layer type is
11815 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011816 *
11817 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011818 * @see #LAYER_TYPE_NONE
11819 * @see #LAYER_TYPE_SOFTWARE
11820 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011821 * @see #setAlpha(float)
11822 *
Romain Guy171c5922011-01-06 10:04:23 -080011823 * @attr ref android.R.styleable#View_layerType
11824 */
11825 public void setLayerType(int layerType, Paint paint) {
11826 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011827 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011828 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11829 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011830
Romain Guyd6cd5722011-01-17 14:42:41 -080011831 if (layerType == mLayerType) {
11832 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11833 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011834 invalidateParentCaches();
11835 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011836 }
11837 return;
11838 }
Romain Guy171c5922011-01-06 10:04:23 -080011839
11840 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011841 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011842 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011843 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011844 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011845 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011846 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011847 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011848 default:
11849 break;
Romain Guy171c5922011-01-06 10:04:23 -080011850 }
11851
11852 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011853 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11854 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11855 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011856
Romain Guy0fd89bf2011-01-26 15:41:30 -080011857 invalidateParentCaches();
11858 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011859 }
11860
11861 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011862 * Indicates whether this view has a static layer. A view with layer type
11863 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11864 * dynamic.
11865 */
11866 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011867 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011868 }
11869
11870 /**
Romain Guy171c5922011-01-06 10:04:23 -080011871 * Indicates what type of layer is currently associated with this view. By default
11872 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11873 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11874 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011875 *
Romain Guy171c5922011-01-06 10:04:23 -080011876 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11877 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011878 *
11879 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011880 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011881 * @see #LAYER_TYPE_NONE
11882 * @see #LAYER_TYPE_SOFTWARE
11883 * @see #LAYER_TYPE_HARDWARE
11884 */
11885 public int getLayerType() {
11886 return mLayerType;
11887 }
Joe Malin32736f02011-01-19 16:14:20 -080011888
Romain Guy6c319ca2011-01-11 14:29:25 -080011889 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011890 * Forces this view's layer to be created and this view to be rendered
11891 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11892 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011893 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011894 * This method can for instance be used to render a view into its layer before
11895 * starting an animation. If this view is complex, rendering into the layer
11896 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011897 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011898 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011899 *
11900 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011901 */
11902 public void buildLayer() {
11903 if (mLayerType == LAYER_TYPE_NONE) return;
11904
11905 if (mAttachInfo == null) {
11906 throw new IllegalStateException("This view must be attached to a window first");
11907 }
11908
11909 switch (mLayerType) {
11910 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011911 if (mAttachInfo.mHardwareRenderer != null &&
11912 mAttachInfo.mHardwareRenderer.isEnabled() &&
11913 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011914 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011915 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011916 break;
11917 case LAYER_TYPE_SOFTWARE:
11918 buildDrawingCache(true);
11919 break;
11920 }
11921 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011922
Romain Guy9c4b79a2011-11-10 19:23:58 -080011923 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11924 void flushLayer() {
11925 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11926 mHardwareLayer.flush();
11927 }
11928 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011929
11930 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011931 * <p>Returns a hardware layer that can be used to draw this view again
11932 * without executing its draw method.</p>
11933 *
11934 * @return A HardwareLayer ready to render, or null if an error occurred.
11935 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011936 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011937 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11938 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011939 return null;
11940 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011941
Romain Guy9c4b79a2011-11-10 19:23:58 -080011942 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011943
11944 final int width = mRight - mLeft;
11945 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011946
Romain Guy6c319ca2011-01-11 14:29:25 -080011947 if (width == 0 || height == 0) {
11948 return null;
11949 }
11950
11951 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11952 if (mHardwareLayer == null) {
11953 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11954 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011955 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011956 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11957 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011958 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011959 }
11960
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011961 // The layer is not valid if the underlying GPU resources cannot be allocated
11962 if (!mHardwareLayer.isValid()) {
11963 return null;
11964 }
11965
Chet Haasea1cff502012-02-21 13:43:44 -080011966 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011967 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011968 }
11969
11970 return mHardwareLayer;
11971 }
Romain Guy171c5922011-01-06 10:04:23 -080011972
Romain Guy589b0bb2011-10-10 13:57:47 -070011973 /**
11974 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011975 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011976 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011977 *
11978 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011979 * @see #LAYER_TYPE_HARDWARE
11980 */
Romain Guya998dff2012-03-23 18:58:36 -070011981 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011982 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011983 AttachInfo info = mAttachInfo;
11984 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011985 info.mHardwareRenderer.isEnabled() &&
11986 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011987 mHardwareLayer.destroy();
11988 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011989
Romain Guy9c4b79a2011-11-10 19:23:58 -080011990 invalidate(true);
11991 invalidateParentCaches();
11992 }
Romain Guy65b345f2011-07-27 18:51:50 -070011993 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011994 }
Romain Guy65b345f2011-07-27 18:51:50 -070011995 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011996 }
11997
Romain Guy171c5922011-01-06 10:04:23 -080011998 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011999 * Destroys all hardware rendering resources. This method is invoked
12000 * when the system needs to reclaim resources. Upon execution of this
12001 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012002 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012003 * Note: you <strong>must</strong> call
12004 * <code>super.destroyHardwareResources()</code> when overriding
12005 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012006 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012007 * @hide
12008 */
12009 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012010 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012011 }
12012
12013 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12015 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12016 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12017 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12018 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12019 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012020 *
Romain Guy171c5922011-01-06 10:04:23 -080012021 * <p>Enabling the drawing cache is similar to
12022 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012023 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12024 * drawing cache has no effect on rendering because the system uses a different mechanism
12025 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12026 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12027 * for information on how to enable software and hardware layers.</p>
12028 *
12029 * <p>This API can be used to manually generate
12030 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12031 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012032 *
12033 * @param enabled true to enable the drawing cache, false otherwise
12034 *
12035 * @see #isDrawingCacheEnabled()
12036 * @see #getDrawingCache()
12037 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012038 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012039 */
12040 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012041 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12043 }
12044
12045 /**
12046 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12047 *
12048 * @return true if the drawing cache is enabled
12049 *
12050 * @see #setDrawingCacheEnabled(boolean)
12051 * @see #getDrawingCache()
12052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012053 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 public boolean isDrawingCacheEnabled() {
12055 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12056 }
12057
12058 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012059 * Debugging utility which recursively outputs the dirty state of a view and its
12060 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012061 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012062 * @hide
12063 */
Romain Guy676b1732011-02-14 14:45:33 -080012064 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012065 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12066 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12067 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12068 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12069 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12070 if (clear) {
12071 mPrivateFlags &= clearMask;
12072 }
12073 if (this instanceof ViewGroup) {
12074 ViewGroup parent = (ViewGroup) this;
12075 final int count = parent.getChildCount();
12076 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012077 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012078 child.outputDirtyFlags(indent + " ", clear, clearMask);
12079 }
12080 }
12081 }
12082
12083 /**
12084 * This method is used by ViewGroup to cause its children to restore or recreate their
12085 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12086 * to recreate its own display list, which would happen if it went through the normal
12087 * draw/dispatchDraw mechanisms.
12088 *
12089 * @hide
12090 */
12091 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012092
12093 /**
12094 * A view that is not attached or hardware accelerated cannot create a display list.
12095 * This method checks these conditions and returns the appropriate result.
12096 *
12097 * @return true if view has the ability to create a display list, false otherwise.
12098 *
12099 * @hide
12100 */
12101 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012102 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012103 }
Joe Malin32736f02011-01-19 16:14:20 -080012104
Chet Haasedaf98e92011-01-10 14:10:36 -080012105 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012106 * @return The HardwareRenderer associated with that view or null if hardware rendering
12107 * is not supported or this this has not been attached to a window.
12108 *
12109 * @hide
12110 */
12111 public HardwareRenderer getHardwareRenderer() {
12112 if (mAttachInfo != null) {
12113 return mAttachInfo.mHardwareRenderer;
12114 }
12115 return null;
12116 }
12117
12118 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012119 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12120 * Otherwise, the same display list will be returned (after having been rendered into
12121 * along the way, depending on the invalidation state of the view).
12122 *
12123 * @param displayList The previous version of this displayList, could be null.
12124 * @param isLayer Whether the requester of the display list is a layer. If so,
12125 * the view will avoid creating a layer inside the resulting display list.
12126 * @return A new or reused DisplayList object.
12127 */
12128 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12129 if (!canHaveDisplayList()) {
12130 return null;
12131 }
12132
12133 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12134 displayList == null || !displayList.isValid() ||
12135 (!isLayer && mRecreateDisplayList))) {
12136 // Don't need to recreate the display list, just need to tell our
12137 // children to restore/recreate theirs
12138 if (displayList != null && displayList.isValid() &&
12139 !isLayer && !mRecreateDisplayList) {
12140 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12141 mPrivateFlags &= ~DIRTY_MASK;
12142 dispatchGetDisplayList();
12143
12144 return displayList;
12145 }
12146
12147 if (!isLayer) {
12148 // If we got here, we're recreating it. Mark it as such to ensure that
12149 // we copy in child display lists into ours in drawChild()
12150 mRecreateDisplayList = true;
12151 }
12152 if (displayList == null) {
12153 final String name = getClass().getSimpleName();
12154 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12155 // If we're creating a new display list, make sure our parent gets invalidated
12156 // since they will need to recreate their display list to account for this
12157 // new child display list.
12158 invalidateParentCaches();
12159 }
12160
12161 boolean caching = false;
12162 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012163 int width = mRight - mLeft;
12164 int height = mBottom - mTop;
12165
12166 try {
12167 canvas.setViewport(width, height);
12168 // The dirty rect should always be null for a display list
12169 canvas.onPreDraw(null);
12170 int layerType = (
12171 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12172 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012173 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012174 if (layerType == LAYER_TYPE_HARDWARE) {
12175 final HardwareLayer layer = getHardwareLayer();
12176 if (layer != null && layer.isValid()) {
12177 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12178 } else {
12179 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12180 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12181 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12182 }
12183 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012184 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012185 buildDrawingCache(true);
12186 Bitmap cache = getDrawingCache(true);
12187 if (cache != null) {
12188 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12189 caching = true;
12190 }
Chet Haasea1cff502012-02-21 13:43:44 -080012191 }
Chet Haasea1cff502012-02-21 13:43:44 -080012192 } else {
12193
12194 computeScroll();
12195
Chet Haasea1cff502012-02-21 13:43:44 -080012196 canvas.translate(-mScrollX, -mScrollY);
12197 if (!isLayer) {
12198 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12199 mPrivateFlags &= ~DIRTY_MASK;
12200 }
12201
12202 // Fast path for layouts with no backgrounds
12203 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12204 dispatchDraw(canvas);
12205 } else {
12206 draw(canvas);
12207 }
12208 }
12209 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012210 canvas.onPostDraw();
12211
12212 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012213 displayList.setCaching(caching);
12214 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012215 displayList.setLeftTopRightBottom(0, 0, width, height);
12216 } else {
12217 setDisplayListProperties(displayList);
12218 }
12219 }
12220 } else if (!isLayer) {
12221 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12222 mPrivateFlags &= ~DIRTY_MASK;
12223 }
12224
12225 return displayList;
12226 }
12227
12228 /**
12229 * Get the DisplayList for the HardwareLayer
12230 *
12231 * @param layer The HardwareLayer whose DisplayList we want
12232 * @return A DisplayList fopr the specified HardwareLayer
12233 */
12234 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12235 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12236 layer.setDisplayList(displayList);
12237 return displayList;
12238 }
12239
12240
12241 /**
Romain Guyb051e892010-09-28 19:09:36 -070012242 * <p>Returns a display list that can be used to draw this view again
12243 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012244 *
Romain Guyb051e892010-09-28 19:09:36 -070012245 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012246 *
12247 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012248 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012249 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012250 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012251 return mDisplayList;
12252 }
12253
12254 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012255 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012256 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012257 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012258 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012259 * @see #getDrawingCache(boolean)
12260 */
12261 public Bitmap getDrawingCache() {
12262 return getDrawingCache(false);
12263 }
12264
12265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012266 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12267 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12268 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12269 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12270 * request the drawing cache by calling this method and draw it on screen if the
12271 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012272 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012273 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12274 * this method will create a bitmap of the same size as this view. Because this bitmap
12275 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12276 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12277 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12278 * size than the view. This implies that your application must be able to handle this
12279 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012280 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012281 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12282 * the current density of the screen when the application is in compatibility
12283 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012285 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012286 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287 * @see #setDrawingCacheEnabled(boolean)
12288 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012289 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012290 * @see #destroyDrawingCache()
12291 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012292 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012293 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12294 return null;
12295 }
12296 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012297 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012298 }
Romain Guy02890fd2010-08-06 17:58:44 -070012299 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 }
12301
12302 /**
12303 * <p>Frees the resources used by the drawing cache. If you call
12304 * {@link #buildDrawingCache()} manually without calling
12305 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12306 * should cleanup the cache with this method afterwards.</p>
12307 *
12308 * @see #setDrawingCacheEnabled(boolean)
12309 * @see #buildDrawingCache()
12310 * @see #getDrawingCache()
12311 */
12312 public void destroyDrawingCache() {
12313 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012314 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012315 mDrawingCache = null;
12316 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012317 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012318 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012319 mUnscaledDrawingCache = null;
12320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321 }
12322
12323 /**
12324 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012325 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012326 * view will always be drawn on top of a solid color.
12327 *
12328 * @param color The background color to use for the drawing cache's bitmap
12329 *
12330 * @see #setDrawingCacheEnabled(boolean)
12331 * @see #buildDrawingCache()
12332 * @see #getDrawingCache()
12333 */
12334 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012335 if (color != mDrawingCacheBackgroundColor) {
12336 mDrawingCacheBackgroundColor = color;
12337 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012339 }
12340
12341 /**
12342 * @see #setDrawingCacheBackgroundColor(int)
12343 *
12344 * @return The background color to used for the drawing cache's bitmap
12345 */
12346 public int getDrawingCacheBackgroundColor() {
12347 return mDrawingCacheBackgroundColor;
12348 }
12349
12350 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012351 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012352 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012353 * @see #buildDrawingCache(boolean)
12354 */
12355 public void buildDrawingCache() {
12356 buildDrawingCache(false);
12357 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012358
Romain Guyfbd8f692009-06-26 14:51:58 -070012359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012360 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12361 *
12362 * <p>If you call {@link #buildDrawingCache()} manually without calling
12363 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12364 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012365 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012366 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12367 * this method will create a bitmap of the same size as this view. Because this bitmap
12368 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12369 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12370 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12371 * size than the view. This implies that your application must be able to handle this
12372 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012373 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012374 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12375 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012376 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012377 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012378 *
12379 * @see #getDrawingCache()
12380 * @see #destroyDrawingCache()
12381 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012382 public void buildDrawingCache(boolean autoScale) {
12383 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012384 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012385 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012386
Romain Guy8506ab42009-06-11 17:35:47 -070012387 int width = mRight - mLeft;
12388 int height = mBottom - mTop;
12389
12390 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012391 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012392
Romain Guye1123222009-06-29 14:24:56 -070012393 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012394 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12395 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012397
12398 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012399 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012400 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012401
12402 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012403 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012404 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12406 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012407 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012408 return;
12409 }
12410
12411 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012412 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413
12414 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012415 Bitmap.Config quality;
12416 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012417 // Never pick ARGB_4444 because it looks awful
12418 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012419 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12420 case DRAWING_CACHE_QUALITY_AUTO:
12421 quality = Bitmap.Config.ARGB_8888;
12422 break;
12423 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012424 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 break;
12426 case DRAWING_CACHE_QUALITY_HIGH:
12427 quality = Bitmap.Config.ARGB_8888;
12428 break;
12429 default:
12430 quality = Bitmap.Config.ARGB_8888;
12431 break;
12432 }
12433 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012434 // Optimization for translucent windows
12435 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012436 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012437 }
12438
12439 // Try to cleanup memory
12440 if (bitmap != null) bitmap.recycle();
12441
12442 try {
12443 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012444 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012445 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012446 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012447 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012448 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012449 }
Adam Powell26153a32010-11-08 15:22:27 -080012450 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451 } catch (OutOfMemoryError e) {
12452 // If there is not enough memory to create the bitmap cache, just
12453 // ignore the issue as bitmap caches are not required to draw the
12454 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012455 if (autoScale) {
12456 mDrawingCache = null;
12457 } else {
12458 mUnscaledDrawingCache = null;
12459 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012460 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 return;
12462 }
12463
12464 clear = drawingCacheBackgroundColor != 0;
12465 }
12466
12467 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 if (attachInfo != null) {
12469 canvas = attachInfo.mCanvas;
12470 if (canvas == null) {
12471 canvas = new Canvas();
12472 }
12473 canvas.setBitmap(bitmap);
12474 // Temporarily clobber the cached Canvas in case one of our children
12475 // is also using a drawing cache. Without this, the children would
12476 // steal the canvas by attaching their own bitmap to it and bad, bad
12477 // thing would happen (invisible views, corrupted drawings, etc.)
12478 attachInfo.mCanvas = null;
12479 } else {
12480 // This case should hopefully never or seldom happen
12481 canvas = new Canvas(bitmap);
12482 }
12483
12484 if (clear) {
12485 bitmap.eraseColor(drawingCacheBackgroundColor);
12486 }
12487
12488 computeScroll();
12489 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012490
Romain Guye1123222009-06-29 14:24:56 -070012491 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012492 final float scale = attachInfo.mApplicationScale;
12493 canvas.scale(scale, scale);
12494 }
Joe Malin32736f02011-01-19 16:14:20 -080012495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012496 canvas.translate(-mScrollX, -mScrollY);
12497
Romain Guy5bcdff42009-05-14 21:27:18 -070012498 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012499 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12500 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012501 mPrivateFlags |= DRAWING_CACHE_VALID;
12502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503
12504 // Fast path for layouts with no backgrounds
12505 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012506 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 dispatchDraw(canvas);
12508 } else {
12509 draw(canvas);
12510 }
12511
12512 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012513 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514
12515 if (attachInfo != null) {
12516 // Restore the cached Canvas for our siblings
12517 attachInfo.mCanvas = canvas;
12518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 }
12520 }
12521
12522 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012523 * Create a snapshot of the view into a bitmap. We should probably make
12524 * some form of this public, but should think about the API.
12525 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012526 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012527 int width = mRight - mLeft;
12528 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012529
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012530 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012531 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012532 width = (int) ((width * scale) + 0.5f);
12533 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012534
Romain Guy8c11e312009-09-14 15:15:30 -070012535 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012536 if (bitmap == null) {
12537 throw new OutOfMemoryError();
12538 }
12539
Romain Guyc529d8d2011-09-06 15:01:39 -070012540 Resources resources = getResources();
12541 if (resources != null) {
12542 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12543 }
Joe Malin32736f02011-01-19 16:14:20 -080012544
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012545 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012546 if (attachInfo != null) {
12547 canvas = attachInfo.mCanvas;
12548 if (canvas == null) {
12549 canvas = new Canvas();
12550 }
12551 canvas.setBitmap(bitmap);
12552 // Temporarily clobber the cached Canvas in case one of our children
12553 // is also using a drawing cache. Without this, the children would
12554 // steal the canvas by attaching their own bitmap to it and bad, bad
12555 // things would happen (invisible views, corrupted drawings, etc.)
12556 attachInfo.mCanvas = null;
12557 } else {
12558 // This case should hopefully never or seldom happen
12559 canvas = new Canvas(bitmap);
12560 }
12561
Romain Guy5bcdff42009-05-14 21:27:18 -070012562 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012563 bitmap.eraseColor(backgroundColor);
12564 }
12565
12566 computeScroll();
12567 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012568 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012569 canvas.translate(-mScrollX, -mScrollY);
12570
Romain Guy5bcdff42009-05-14 21:27:18 -070012571 // Temporarily remove the dirty mask
12572 int flags = mPrivateFlags;
12573 mPrivateFlags &= ~DIRTY_MASK;
12574
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012575 // Fast path for layouts with no backgrounds
12576 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12577 dispatchDraw(canvas);
12578 } else {
12579 draw(canvas);
12580 }
12581
Romain Guy5bcdff42009-05-14 21:27:18 -070012582 mPrivateFlags = flags;
12583
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012584 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012585 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012586
12587 if (attachInfo != null) {
12588 // Restore the cached Canvas for our siblings
12589 attachInfo.mCanvas = canvas;
12590 }
Romain Guy8506ab42009-06-11 17:35:47 -070012591
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012592 return bitmap;
12593 }
12594
12595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012596 * Indicates whether this View is currently in edit mode. A View is usually
12597 * in edit mode when displayed within a developer tool. For instance, if
12598 * this View is being drawn by a visual user interface builder, this method
12599 * should return true.
12600 *
12601 * Subclasses should check the return value of this method to provide
12602 * different behaviors if their normal behavior might interfere with the
12603 * host environment. For instance: the class spawns a thread in its
12604 * constructor, the drawing code relies on device-specific features, etc.
12605 *
12606 * This method is usually checked in the drawing code of custom widgets.
12607 *
12608 * @return True if this View is in edit mode, false otherwise.
12609 */
12610 public boolean isInEditMode() {
12611 return false;
12612 }
12613
12614 /**
12615 * If the View draws content inside its padding and enables fading edges,
12616 * it needs to support padding offsets. Padding offsets are added to the
12617 * fading edges to extend the length of the fade so that it covers pixels
12618 * drawn inside the padding.
12619 *
12620 * Subclasses of this class should override this method if they need
12621 * to draw content inside the padding.
12622 *
12623 * @return True if padding offset must be applied, false otherwise.
12624 *
12625 * @see #getLeftPaddingOffset()
12626 * @see #getRightPaddingOffset()
12627 * @see #getTopPaddingOffset()
12628 * @see #getBottomPaddingOffset()
12629 *
12630 * @since CURRENT
12631 */
12632 protected boolean isPaddingOffsetRequired() {
12633 return false;
12634 }
12635
12636 /**
12637 * Amount by which to extend the left fading region. Called only when
12638 * {@link #isPaddingOffsetRequired()} returns true.
12639 *
12640 * @return The left padding offset in pixels.
12641 *
12642 * @see #isPaddingOffsetRequired()
12643 *
12644 * @since CURRENT
12645 */
12646 protected int getLeftPaddingOffset() {
12647 return 0;
12648 }
12649
12650 /**
12651 * Amount by which to extend the right fading region. Called only when
12652 * {@link #isPaddingOffsetRequired()} returns true.
12653 *
12654 * @return The right padding offset in pixels.
12655 *
12656 * @see #isPaddingOffsetRequired()
12657 *
12658 * @since CURRENT
12659 */
12660 protected int getRightPaddingOffset() {
12661 return 0;
12662 }
12663
12664 /**
12665 * Amount by which to extend the top fading region. Called only when
12666 * {@link #isPaddingOffsetRequired()} returns true.
12667 *
12668 * @return The top padding offset in pixels.
12669 *
12670 * @see #isPaddingOffsetRequired()
12671 *
12672 * @since CURRENT
12673 */
12674 protected int getTopPaddingOffset() {
12675 return 0;
12676 }
12677
12678 /**
12679 * Amount by which to extend the bottom fading region. Called only when
12680 * {@link #isPaddingOffsetRequired()} returns true.
12681 *
12682 * @return The bottom padding offset in pixels.
12683 *
12684 * @see #isPaddingOffsetRequired()
12685 *
12686 * @since CURRENT
12687 */
12688 protected int getBottomPaddingOffset() {
12689 return 0;
12690 }
12691
12692 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012693 * @hide
12694 * @param offsetRequired
12695 */
12696 protected int getFadeTop(boolean offsetRequired) {
12697 int top = mPaddingTop;
12698 if (offsetRequired) top += getTopPaddingOffset();
12699 return top;
12700 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012701
Romain Guyf2fc4602011-07-19 15:20:03 -070012702 /**
12703 * @hide
12704 * @param offsetRequired
12705 */
12706 protected int getFadeHeight(boolean offsetRequired) {
12707 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012708 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012709 return mBottom - mTop - mPaddingBottom - padding;
12710 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012711
Romain Guyf2fc4602011-07-19 15:20:03 -070012712 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012713 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012714 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012715 *
Romain Guy2bffd262010-09-12 17:40:02 -070012716 * <p>Even if this method returns true, it does not mean that every call
12717 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12718 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012719 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012720 * window is hardware accelerated,
12721 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12722 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012723 *
Romain Guy2bffd262010-09-12 17:40:02 -070012724 * @return True if the view is attached to a window and the window is
12725 * hardware accelerated; false in any other case.
12726 */
12727 public boolean isHardwareAccelerated() {
12728 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12729 }
Joe Malin32736f02011-01-19 16:14:20 -080012730
Romain Guy2bffd262010-09-12 17:40:02 -070012731 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012732 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12733 * case of an active Animation being run on the view.
12734 */
12735 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12736 Animation a, boolean scalingRequired) {
12737 Transformation invalidationTransform;
12738 final int flags = parent.mGroupFlags;
12739 final boolean initialized = a.isInitialized();
12740 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012741 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012742 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012743 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012744 onAnimationStart();
12745 }
12746
12747 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12748 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12749 if (parent.mInvalidationTransformation == null) {
12750 parent.mInvalidationTransformation = new Transformation();
12751 }
12752 invalidationTransform = parent.mInvalidationTransformation;
12753 a.getTransformation(drawingTime, invalidationTransform, 1f);
12754 } else {
12755 invalidationTransform = parent.mChildTransformation;
12756 }
Romain Guy393a52c2012-05-22 20:21:08 -070012757
Chet Haasebcca79a2012-02-14 08:45:14 -080012758 if (more) {
12759 if (!a.willChangeBounds()) {
12760 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12761 parent.FLAG_OPTIMIZE_INVALIDATE) {
12762 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12763 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12764 // The child need to draw an animation, potentially offscreen, so
12765 // make sure we do not cancel invalidate requests
12766 parent.mPrivateFlags |= DRAW_ANIMATION;
12767 parent.invalidate(mLeft, mTop, mRight, mBottom);
12768 }
12769 } else {
12770 if (parent.mInvalidateRegion == null) {
12771 parent.mInvalidateRegion = new RectF();
12772 }
12773 final RectF region = parent.mInvalidateRegion;
12774 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12775 invalidationTransform);
12776
12777 // The child need to draw an animation, potentially offscreen, so
12778 // make sure we do not cancel invalidate requests
12779 parent.mPrivateFlags |= DRAW_ANIMATION;
12780
12781 final int left = mLeft + (int) region.left;
12782 final int top = mTop + (int) region.top;
12783 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12784 top + (int) (region.height() + .5f));
12785 }
12786 }
12787 return more;
12788 }
12789
Chet Haasea1cff502012-02-21 13:43:44 -080012790 /**
12791 * This method is called by getDisplayList() when a display list is created or re-rendered.
12792 * It sets or resets the current value of all properties on that display list (resetting is
12793 * necessary when a display list is being re-created, because we need to make sure that
12794 * previously-set transform values
12795 */
12796 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012797 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012798 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012799 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012800 if (mParent instanceof ViewGroup) {
12801 displayList.setClipChildren(
12802 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12803 }
Chet Haase9420abd2012-03-29 16:28:32 -070012804 float alpha = 1;
12805 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12806 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12807 ViewGroup parentVG = (ViewGroup) mParent;
12808 final boolean hasTransform =
12809 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12810 if (hasTransform) {
12811 Transformation transform = parentVG.mChildTransformation;
12812 final int transformType = parentVG.mChildTransformation.getTransformationType();
12813 if (transformType != Transformation.TYPE_IDENTITY) {
12814 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12815 alpha = transform.getAlpha();
12816 }
12817 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12818 displayList.setStaticMatrix(transform.getMatrix());
12819 }
12820 }
12821 }
Chet Haasea1cff502012-02-21 13:43:44 -080012822 }
12823 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012824 alpha *= mTransformationInfo.mAlpha;
12825 if (alpha < 1) {
12826 final int multipliedAlpha = (int) (255 * alpha);
12827 if (onSetAlpha(multipliedAlpha)) {
12828 alpha = 1;
12829 }
12830 }
12831 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012832 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12833 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12834 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12835 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012836 if (mTransformationInfo.mCamera == null) {
12837 mTransformationInfo.mCamera = new Camera();
12838 mTransformationInfo.matrix3D = new Matrix();
12839 }
12840 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012841 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12842 displayList.setPivotX(getPivotX());
12843 displayList.setPivotY(getPivotY());
12844 }
Chet Haase9420abd2012-03-29 16:28:32 -070012845 } else if (alpha < 1) {
12846 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012847 }
12848 }
12849 }
12850
Chet Haasebcca79a2012-02-14 08:45:14 -080012851 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012852 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12853 * This draw() method is an implementation detail and is not intended to be overridden or
12854 * to be called from anywhere else other than ViewGroup.drawChild().
12855 */
12856 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012857 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012858 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012859 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012860 final int flags = parent.mGroupFlags;
12861
Chet Haasea1cff502012-02-21 13:43:44 -080012862 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012863 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012864 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012865 }
12866
12867 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012868 boolean concatMatrix = false;
12869
12870 boolean scalingRequired = false;
12871 boolean caching;
12872 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12873
12874 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012875 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12876 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012877 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012878 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012879 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12880 } else {
12881 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12882 }
12883
Chet Haasebcca79a2012-02-14 08:45:14 -080012884 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012885 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012886 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012887 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012888 if (concatMatrix) {
12889 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12890 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012891 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012892 } else {
12893 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12894 mDisplayList != null) {
12895 // No longer animating: clear out old animation matrix
12896 mDisplayList.setAnimationMatrix(null);
12897 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12898 }
12899 if (!useDisplayListProperties &&
12900 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12901 final boolean hasTransform =
12902 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12903 if (hasTransform) {
12904 final int transformType = parent.mChildTransformation.getTransformationType();
12905 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12906 parent.mChildTransformation : null;
12907 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12908 }
Chet Haase64a48c12012-02-13 16:33:29 -080012909 }
12910 }
12911
12912 concatMatrix |= !childHasIdentityMatrix;
12913
12914 // Sets the flag as early as possible to allow draw() implementations
12915 // to call invalidate() successfully when doing animations
12916 mPrivateFlags |= DRAWN;
12917
Chet Haasebcca79a2012-02-14 08:45:14 -080012918 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012919 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012920 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012921 return more;
12922 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012923 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012924
12925 if (hardwareAccelerated) {
12926 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12927 // retain the flag's value temporarily in the mRecreateDisplayList flag
12928 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12929 mPrivateFlags &= ~INVALIDATED;
12930 }
12931
12932 computeScroll();
12933
12934 final int sx = mScrollX;
12935 final int sy = mScrollY;
12936
12937 DisplayList displayList = null;
12938 Bitmap cache = null;
12939 boolean hasDisplayList = false;
12940 if (caching) {
12941 if (!hardwareAccelerated) {
12942 if (layerType != LAYER_TYPE_NONE) {
12943 layerType = LAYER_TYPE_SOFTWARE;
12944 buildDrawingCache(true);
12945 }
12946 cache = getDrawingCache(true);
12947 } else {
12948 switch (layerType) {
12949 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012950 if (useDisplayListProperties) {
12951 hasDisplayList = canHaveDisplayList();
12952 } else {
12953 buildDrawingCache(true);
12954 cache = getDrawingCache(true);
12955 }
Chet Haase64a48c12012-02-13 16:33:29 -080012956 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012957 case LAYER_TYPE_HARDWARE:
12958 if (useDisplayListProperties) {
12959 hasDisplayList = canHaveDisplayList();
12960 }
12961 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012962 case LAYER_TYPE_NONE:
12963 // Delay getting the display list until animation-driven alpha values are
12964 // set up and possibly passed on to the view
12965 hasDisplayList = canHaveDisplayList();
12966 break;
12967 }
12968 }
12969 }
Chet Haasea1cff502012-02-21 13:43:44 -080012970 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012971 if (useDisplayListProperties) {
12972 displayList = getDisplayList();
12973 if (!displayList.isValid()) {
12974 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12975 // to getDisplayList(), the display list will be marked invalid and we should not
12976 // try to use it again.
12977 displayList = null;
12978 hasDisplayList = false;
12979 useDisplayListProperties = false;
12980 }
12981 }
Chet Haase64a48c12012-02-13 16:33:29 -080012982
12983 final boolean hasNoCache = cache == null || hasDisplayList;
12984 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12985 layerType != LAYER_TYPE_HARDWARE;
12986
Chet Haasea1cff502012-02-21 13:43:44 -080012987 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012988 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012989 restoreTo = canvas.save();
12990 }
Chet Haase64a48c12012-02-13 16:33:29 -080012991 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012992 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012993 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012994 if (!useDisplayListProperties) {
12995 canvas.translate(mLeft, mTop);
12996 }
Chet Haase64a48c12012-02-13 16:33:29 -080012997 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012998 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012999 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013000 restoreTo = canvas.save();
13001 }
Chet Haase64a48c12012-02-13 16:33:29 -080013002 // mAttachInfo cannot be null, otherwise scalingRequired == false
13003 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13004 canvas.scale(scale, scale);
13005 }
13006 }
13007
Chet Haasea1cff502012-02-21 13:43:44 -080013008 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013009 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080013010 if (transformToApply != null || !childHasIdentityMatrix) {
13011 int transX = 0;
13012 int transY = 0;
13013
13014 if (offsetForScroll) {
13015 transX = -sx;
13016 transY = -sy;
13017 }
13018
13019 if (transformToApply != null) {
13020 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013021 if (useDisplayListProperties) {
13022 displayList.setAnimationMatrix(transformToApply.getMatrix());
13023 } else {
13024 // Undo the scroll translation, apply the transformation matrix,
13025 // then redo the scroll translate to get the correct result.
13026 canvas.translate(-transX, -transY);
13027 canvas.concat(transformToApply.getMatrix());
13028 canvas.translate(transX, transY);
13029 }
Chet Haasea1cff502012-02-21 13:43:44 -080013030 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013031 }
13032
13033 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013034 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013035 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013036 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013037 }
13038 }
13039
Chet Haasea1cff502012-02-21 13:43:44 -080013040 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013041 canvas.translate(-transX, -transY);
13042 canvas.concat(getMatrix());
13043 canvas.translate(transX, transY);
13044 }
13045 }
13046
Chet Haase9420abd2012-03-29 16:28:32 -070013047 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013048 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013049 if (hasNoCache) {
13050 final int multipliedAlpha = (int) (255 * alpha);
13051 if (!onSetAlpha(multipliedAlpha)) {
13052 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013053 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013054 layerType != LAYER_TYPE_NONE) {
13055 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13056 }
Chet Haase9420abd2012-03-29 16:28:32 -070013057 if (useDisplayListProperties) {
13058 displayList.setAlpha(alpha * getAlpha());
13059 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013060 final int scrollX = hasDisplayList ? 0 : sx;
13061 final int scrollY = hasDisplayList ? 0 : sy;
13062 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13063 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013064 }
13065 } else {
13066 // Alpha is handled by the child directly, clobber the layer's alpha
13067 mPrivateFlags |= ALPHA_SET;
13068 }
13069 }
13070 }
13071 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13072 onSetAlpha(255);
13073 mPrivateFlags &= ~ALPHA_SET;
13074 }
13075
Chet Haasea1cff502012-02-21 13:43:44 -080013076 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13077 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013078 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013079 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013080 } else {
13081 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013082 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013083 } else {
13084 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13085 }
13086 }
13087 }
13088
Chet Haase9420abd2012-03-29 16:28:32 -070013089 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013090 displayList = getDisplayList();
13091 if (!displayList.isValid()) {
13092 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13093 // to getDisplayList(), the display list will be marked invalid and we should not
13094 // try to use it again.
13095 displayList = null;
13096 hasDisplayList = false;
13097 }
13098 }
13099
13100 if (hasNoCache) {
13101 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013102 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013103 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013104 if (layer != null && layer.isValid()) {
13105 mLayerPaint.setAlpha((int) (alpha * 255));
13106 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13107 layerRendered = true;
13108 } else {
13109 final int scrollX = hasDisplayList ? 0 : sx;
13110 final int scrollY = hasDisplayList ? 0 : sy;
13111 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013112 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013113 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13114 }
13115 }
13116
13117 if (!layerRendered) {
13118 if (!hasDisplayList) {
13119 // Fast path for layouts with no backgrounds
13120 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013121 mPrivateFlags &= ~DIRTY_MASK;
13122 dispatchDraw(canvas);
13123 } else {
13124 draw(canvas);
13125 }
13126 } else {
13127 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013128 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013129 }
13130 }
13131 } else if (cache != null) {
13132 mPrivateFlags &= ~DIRTY_MASK;
13133 Paint cachePaint;
13134
13135 if (layerType == LAYER_TYPE_NONE) {
13136 cachePaint = parent.mCachePaint;
13137 if (cachePaint == null) {
13138 cachePaint = new Paint();
13139 cachePaint.setDither(false);
13140 parent.mCachePaint = cachePaint;
13141 }
Chet Haase9420abd2012-03-29 16:28:32 -070013142 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013143 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013144 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13145 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013146 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013147 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013148 }
13149 } else {
13150 cachePaint = mLayerPaint;
13151 cachePaint.setAlpha((int) (alpha * 255));
13152 }
13153 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13154 }
13155
Chet Haasea1cff502012-02-21 13:43:44 -080013156 if (restoreTo >= 0) {
13157 canvas.restoreToCount(restoreTo);
13158 }
Chet Haase64a48c12012-02-13 16:33:29 -080013159
13160 if (a != null && !more) {
13161 if (!hardwareAccelerated && !a.getFillAfter()) {
13162 onSetAlpha(255);
13163 }
13164 parent.finishAnimatingView(this, a);
13165 }
13166
13167 if (more && hardwareAccelerated) {
13168 // invalidation is the trigger to recreate display lists, so if we're using
13169 // display lists to render, force an invalidate to allow the animation to
13170 // continue drawing another frame
13171 parent.invalidate(true);
13172 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13173 // alpha animations should cause the child to recreate its display list
13174 invalidate(true);
13175 }
13176 }
13177
13178 mRecreateDisplayList = false;
13179
13180 return more;
13181 }
13182
13183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013184 * Manually render this view (and all of its children) to the given Canvas.
13185 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013186 * called. When implementing a view, implement
13187 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13188 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013189 *
13190 * @param canvas The Canvas to which the View is rendered.
13191 */
13192 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013193 final int privateFlags = mPrivateFlags;
13194 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13195 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13196 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198 /*
13199 * Draw traversal performs several drawing steps which must be executed
13200 * in the appropriate order:
13201 *
13202 * 1. Draw the background
13203 * 2. If necessary, save the canvas' layers to prepare for fading
13204 * 3. Draw view's content
13205 * 4. Draw children
13206 * 5. If necessary, draw the fading edges and restore layers
13207 * 6. Draw decorations (scrollbars for instance)
13208 */
13209
13210 // Step 1, draw the background, if needed
13211 int saveCount;
13212
Romain Guy24443ea2009-05-11 11:56:30 -070013213 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013214 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013215 if (background != null) {
13216 final int scrollX = mScrollX;
13217 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013218
Romain Guy24443ea2009-05-11 11:56:30 -070013219 if (mBackgroundSizeChanged) {
13220 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13221 mBackgroundSizeChanged = false;
13222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013223
Romain Guy24443ea2009-05-11 11:56:30 -070013224 if ((scrollX | scrollY) == 0) {
13225 background.draw(canvas);
13226 } else {
13227 canvas.translate(scrollX, scrollY);
13228 background.draw(canvas);
13229 canvas.translate(-scrollX, -scrollY);
13230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013231 }
13232 }
13233
13234 // skip step 2 & 5 if possible (common case)
13235 final int viewFlags = mViewFlags;
13236 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13237 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13238 if (!verticalEdges && !horizontalEdges) {
13239 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013240 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013241
13242 // Step 4, draw the children
13243 dispatchDraw(canvas);
13244
13245 // Step 6, draw decorations (scrollbars)
13246 onDrawScrollBars(canvas);
13247
13248 // we're done...
13249 return;
13250 }
13251
13252 /*
13253 * Here we do the full fledged routine...
13254 * (this is an uncommon case where speed matters less,
13255 * this is why we repeat some of the tests that have been
13256 * done above)
13257 */
13258
13259 boolean drawTop = false;
13260 boolean drawBottom = false;
13261 boolean drawLeft = false;
13262 boolean drawRight = false;
13263
13264 float topFadeStrength = 0.0f;
13265 float bottomFadeStrength = 0.0f;
13266 float leftFadeStrength = 0.0f;
13267 float rightFadeStrength = 0.0f;
13268
13269 // Step 2, save the canvas' layers
13270 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013271
13272 final boolean offsetRequired = isPaddingOffsetRequired();
13273 if (offsetRequired) {
13274 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013275 }
13276
13277 int left = mScrollX + paddingLeft;
13278 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013279 int top = mScrollY + getFadeTop(offsetRequired);
13280 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281
13282 if (offsetRequired) {
13283 right += getRightPaddingOffset();
13284 bottom += getBottomPaddingOffset();
13285 }
13286
13287 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013288 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013289 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013290
13291 // clip the fade length if top and bottom fades overlap
13292 // overlapping fades produce odd-looking artifacts
13293 if (verticalEdges && (top + length > bottom - length)) {
13294 length = (bottom - top) / 2;
13295 }
13296
13297 // also clip horizontal fades if necessary
13298 if (horizontalEdges && (left + length > right - length)) {
13299 length = (right - left) / 2;
13300 }
13301
13302 if (verticalEdges) {
13303 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013304 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013305 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013306 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013307 }
13308
13309 if (horizontalEdges) {
13310 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013311 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013312 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013313 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013314 }
13315
13316 saveCount = canvas.getSaveCount();
13317
13318 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013319 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013320 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13321
13322 if (drawTop) {
13323 canvas.saveLayer(left, top, right, top + length, null, flags);
13324 }
13325
13326 if (drawBottom) {
13327 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13328 }
13329
13330 if (drawLeft) {
13331 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13332 }
13333
13334 if (drawRight) {
13335 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13336 }
13337 } else {
13338 scrollabilityCache.setFadeColor(solidColor);
13339 }
13340
13341 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013342 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013343
13344 // Step 4, draw the children
13345 dispatchDraw(canvas);
13346
13347 // Step 5, draw the fade effect and restore layers
13348 final Paint p = scrollabilityCache.paint;
13349 final Matrix matrix = scrollabilityCache.matrix;
13350 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351
13352 if (drawTop) {
13353 matrix.setScale(1, fadeHeight * topFadeStrength);
13354 matrix.postTranslate(left, top);
13355 fade.setLocalMatrix(matrix);
13356 canvas.drawRect(left, top, right, top + length, p);
13357 }
13358
13359 if (drawBottom) {
13360 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13361 matrix.postRotate(180);
13362 matrix.postTranslate(left, bottom);
13363 fade.setLocalMatrix(matrix);
13364 canvas.drawRect(left, bottom - length, right, bottom, p);
13365 }
13366
13367 if (drawLeft) {
13368 matrix.setScale(1, fadeHeight * leftFadeStrength);
13369 matrix.postRotate(-90);
13370 matrix.postTranslate(left, top);
13371 fade.setLocalMatrix(matrix);
13372 canvas.drawRect(left, top, left + length, bottom, p);
13373 }
13374
13375 if (drawRight) {
13376 matrix.setScale(1, fadeHeight * rightFadeStrength);
13377 matrix.postRotate(90);
13378 matrix.postTranslate(right, top);
13379 fade.setLocalMatrix(matrix);
13380 canvas.drawRect(right - length, top, right, bottom, p);
13381 }
13382
13383 canvas.restoreToCount(saveCount);
13384
13385 // Step 6, draw decorations (scrollbars)
13386 onDrawScrollBars(canvas);
13387 }
13388
13389 /**
13390 * Override this if your view is known to always be drawn on top of a solid color background,
13391 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13392 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13393 * should be set to 0xFF.
13394 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013395 * @see #setVerticalFadingEdgeEnabled(boolean)
13396 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013397 *
13398 * @return The known solid color background for this view, or 0 if the color may vary
13399 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013400 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013401 public int getSolidColor() {
13402 return 0;
13403 }
13404
13405 /**
13406 * Build a human readable string representation of the specified view flags.
13407 *
13408 * @param flags the view flags to convert to a string
13409 * @return a String representing the supplied flags
13410 */
13411 private static String printFlags(int flags) {
13412 String output = "";
13413 int numFlags = 0;
13414 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13415 output += "TAKES_FOCUS";
13416 numFlags++;
13417 }
13418
13419 switch (flags & VISIBILITY_MASK) {
13420 case INVISIBLE:
13421 if (numFlags > 0) {
13422 output += " ";
13423 }
13424 output += "INVISIBLE";
13425 // USELESS HERE numFlags++;
13426 break;
13427 case GONE:
13428 if (numFlags > 0) {
13429 output += " ";
13430 }
13431 output += "GONE";
13432 // USELESS HERE numFlags++;
13433 break;
13434 default:
13435 break;
13436 }
13437 return output;
13438 }
13439
13440 /**
13441 * Build a human readable string representation of the specified private
13442 * view flags.
13443 *
13444 * @param privateFlags the private view flags to convert to a string
13445 * @return a String representing the supplied flags
13446 */
13447 private static String printPrivateFlags(int privateFlags) {
13448 String output = "";
13449 int numFlags = 0;
13450
13451 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13452 output += "WANTS_FOCUS";
13453 numFlags++;
13454 }
13455
13456 if ((privateFlags & FOCUSED) == FOCUSED) {
13457 if (numFlags > 0) {
13458 output += " ";
13459 }
13460 output += "FOCUSED";
13461 numFlags++;
13462 }
13463
13464 if ((privateFlags & SELECTED) == SELECTED) {
13465 if (numFlags > 0) {
13466 output += " ";
13467 }
13468 output += "SELECTED";
13469 numFlags++;
13470 }
13471
13472 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13473 if (numFlags > 0) {
13474 output += " ";
13475 }
13476 output += "IS_ROOT_NAMESPACE";
13477 numFlags++;
13478 }
13479
13480 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13481 if (numFlags > 0) {
13482 output += " ";
13483 }
13484 output += "HAS_BOUNDS";
13485 numFlags++;
13486 }
13487
13488 if ((privateFlags & DRAWN) == DRAWN) {
13489 if (numFlags > 0) {
13490 output += " ";
13491 }
13492 output += "DRAWN";
13493 // USELESS HERE numFlags++;
13494 }
13495 return output;
13496 }
13497
13498 /**
13499 * <p>Indicates whether or not this view's layout will be requested during
13500 * the next hierarchy layout pass.</p>
13501 *
13502 * @return true if the layout will be forced during next layout pass
13503 */
13504 public boolean isLayoutRequested() {
13505 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13506 }
13507
13508 /**
13509 * Assign a size and position to a view and all of its
13510 * descendants
13511 *
13512 * <p>This is the second phase of the layout mechanism.
13513 * (The first is measuring). In this phase, each parent calls
13514 * layout on all of its children to position them.
13515 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013516 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013517 *
Chet Haase9c087442011-01-12 16:20:16 -080013518 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519 * Derived classes with children should override
13520 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013521 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013522 *
13523 * @param l Left position, relative to parent
13524 * @param t Top position, relative to parent
13525 * @param r Right position, relative to parent
13526 * @param b Bottom position, relative to parent
13527 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013528 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013529 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013530 int oldL = mLeft;
13531 int oldT = mTop;
13532 int oldB = mBottom;
13533 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 boolean changed = setFrame(l, t, r, b);
13535 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013536 onLayout(changed, l, t, r, b);
13537 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013538
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013539 ListenerInfo li = mListenerInfo;
13540 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013541 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013542 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013543 int numListeners = listenersCopy.size();
13544 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013545 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013546 }
13547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013548 }
13549 mPrivateFlags &= ~FORCE_LAYOUT;
13550 }
13551
13552 /**
13553 * Called from layout when this view should
13554 * assign a size and position to each of its children.
13555 *
13556 * Derived classes with children should override
13557 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013558 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013559 * @param changed This is a new size or position for this view
13560 * @param left Left position, relative to parent
13561 * @param top Top position, relative to parent
13562 * @param right Right position, relative to parent
13563 * @param bottom Bottom position, relative to parent
13564 */
13565 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13566 }
13567
13568 /**
13569 * Assign a size and position to this view.
13570 *
13571 * This is called from layout.
13572 *
13573 * @param left Left position, relative to parent
13574 * @param top Top position, relative to parent
13575 * @param right Right position, relative to parent
13576 * @param bottom Bottom position, relative to parent
13577 * @return true if the new size and position are different than the
13578 * previous ones
13579 * {@hide}
13580 */
13581 protected boolean setFrame(int left, int top, int right, int bottom) {
13582 boolean changed = false;
13583
13584 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013585 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 + right + "," + bottom + ")");
13587 }
13588
13589 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13590 changed = true;
13591
13592 // Remember our drawn bit
13593 int drawn = mPrivateFlags & DRAWN;
13594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013595 int oldWidth = mRight - mLeft;
13596 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013597 int newWidth = right - left;
13598 int newHeight = bottom - top;
13599 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13600
13601 // Invalidate our old position
13602 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013603
13604 mLeft = left;
13605 mTop = top;
13606 mRight = right;
13607 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013608 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013609 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013611
13612 mPrivateFlags |= HAS_BOUNDS;
13613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614
Chet Haase75755e22011-07-18 17:48:25 -070013615 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013616 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13617 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013618 if (mTransformationInfo != null) {
13619 mTransformationInfo.mMatrixDirty = true;
13620 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13623 }
13624
13625 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13626 // If we are visible, force the DRAWN bit to on so that
13627 // this invalidate will go through (at least to our parent).
13628 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013629 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630 // the DRAWN bit.
13631 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013632 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013633 // parent display list may need to be recreated based on a change in the bounds
13634 // of any child
13635 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013636 }
13637
13638 // Reset drawn bit to original value (invalidate turns it off)
13639 mPrivateFlags |= drawn;
13640
13641 mBackgroundSizeChanged = true;
13642 }
13643 return changed;
13644 }
13645
13646 /**
13647 * Finalize inflating a view from XML. This is called as the last phase
13648 * of inflation, after all child views have been added.
13649 *
13650 * <p>Even if the subclass overrides onFinishInflate, they should always be
13651 * sure to call the super method, so that we get called.
13652 */
13653 protected void onFinishInflate() {
13654 }
13655
13656 /**
13657 * Returns the resources associated with this view.
13658 *
13659 * @return Resources object.
13660 */
13661 public Resources getResources() {
13662 return mResources;
13663 }
13664
13665 /**
13666 * Invalidates the specified Drawable.
13667 *
13668 * @param drawable the drawable to invalidate
13669 */
13670 public void invalidateDrawable(Drawable drawable) {
13671 if (verifyDrawable(drawable)) {
13672 final Rect dirty = drawable.getBounds();
13673 final int scrollX = mScrollX;
13674 final int scrollY = mScrollY;
13675
13676 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13677 dirty.right + scrollX, dirty.bottom + scrollY);
13678 }
13679 }
13680
13681 /**
13682 * Schedules an action on a drawable to occur at a specified time.
13683 *
13684 * @param who the recipient of the action
13685 * @param what the action to run on the drawable
13686 * @param when the time at which the action must occur. Uses the
13687 * {@link SystemClock#uptimeMillis} timebase.
13688 */
13689 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013690 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013691 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013692 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013693 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13694 Choreographer.CALLBACK_ANIMATION, what, who,
13695 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013696 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013697 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013699 }
13700 }
13701
13702 /**
13703 * Cancels a scheduled action on a drawable.
13704 *
13705 * @param who the recipient of the action
13706 * @param what the action to cancel
13707 */
13708 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013709 if (verifyDrawable(who) && what != null) {
13710 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013711 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13712 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013713 } else {
13714 ViewRootImpl.getRunQueue().removeCallbacks(what);
13715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013716 }
13717 }
13718
13719 /**
13720 * Unschedule any events associated with the given Drawable. This can be
13721 * used when selecting a new Drawable into a view, so that the previous
13722 * one is completely unscheduled.
13723 *
13724 * @param who The Drawable to unschedule.
13725 *
13726 * @see #drawableStateChanged
13727 */
13728 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013729 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013730 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13731 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013732 }
13733 }
13734
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013735 /**
13736 * Return the layout direction of a given Drawable.
13737 *
13738 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013739 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013740 */
13741 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013742 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013743 }
13744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 /**
13746 * If your view subclass is displaying its own Drawable objects, it should
13747 * override this function and return true for any Drawable it is
13748 * displaying. This allows animations for those drawables to be
13749 * scheduled.
13750 *
13751 * <p>Be sure to call through to the super class when overriding this
13752 * function.
13753 *
13754 * @param who The Drawable to verify. Return true if it is one you are
13755 * displaying, else return the result of calling through to the
13756 * super class.
13757 *
13758 * @return boolean If true than the Drawable is being displayed in the
13759 * view; else false and it is not allowed to animate.
13760 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013761 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13762 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763 */
13764 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013765 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 }
13767
13768 /**
13769 * This function is called whenever the state of the view changes in such
13770 * a way that it impacts the state of drawables being shown.
13771 *
13772 * <p>Be sure to call through to the superclass when overriding this
13773 * function.
13774 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013775 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776 */
13777 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013778 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779 if (d != null && d.isStateful()) {
13780 d.setState(getDrawableState());
13781 }
13782 }
13783
13784 /**
13785 * Call this to force a view to update its drawable state. This will cause
13786 * drawableStateChanged to be called on this view. Views that are interested
13787 * in the new state should call getDrawableState.
13788 *
13789 * @see #drawableStateChanged
13790 * @see #getDrawableState
13791 */
13792 public void refreshDrawableState() {
13793 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13794 drawableStateChanged();
13795
13796 ViewParent parent = mParent;
13797 if (parent != null) {
13798 parent.childDrawableStateChanged(this);
13799 }
13800 }
13801
13802 /**
13803 * Return an array of resource IDs of the drawable states representing the
13804 * current state of the view.
13805 *
13806 * @return The current drawable state
13807 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013808 * @see Drawable#setState(int[])
13809 * @see #drawableStateChanged()
13810 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013811 */
13812 public final int[] getDrawableState() {
13813 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13814 return mDrawableState;
13815 } else {
13816 mDrawableState = onCreateDrawableState(0);
13817 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13818 return mDrawableState;
13819 }
13820 }
13821
13822 /**
13823 * Generate the new {@link android.graphics.drawable.Drawable} state for
13824 * this view. This is called by the view
13825 * system when the cached Drawable state is determined to be invalid. To
13826 * retrieve the current state, you should use {@link #getDrawableState}.
13827 *
13828 * @param extraSpace if non-zero, this is the number of extra entries you
13829 * would like in the returned array in which you can place your own
13830 * states.
13831 *
13832 * @return Returns an array holding the current {@link Drawable} state of
13833 * the view.
13834 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013835 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013836 */
13837 protected int[] onCreateDrawableState(int extraSpace) {
13838 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13839 mParent instanceof View) {
13840 return ((View) mParent).onCreateDrawableState(extraSpace);
13841 }
13842
13843 int[] drawableState;
13844
13845 int privateFlags = mPrivateFlags;
13846
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013847 int viewStateIndex = 0;
13848 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13849 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13850 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013851 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013852 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13853 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013854 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13855 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013856 // This is set if HW acceleration is requested, even if the current
13857 // process doesn't allow it. This is just to allow app preview
13858 // windows to better match their app.
13859 viewStateIndex |= VIEW_STATE_ACCELERATED;
13860 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013861 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013862
Christopher Tate3d4bf172011-03-28 16:16:46 -070013863 final int privateFlags2 = mPrivateFlags2;
13864 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13865 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013867 drawableState = VIEW_STATE_SETS[viewStateIndex];
13868
13869 //noinspection ConstantIfStatement
13870 if (false) {
13871 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13872 Log.i("View", toString()
13873 + " pressed=" + ((privateFlags & PRESSED) != 0)
13874 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13875 + " fo=" + hasFocus()
13876 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013877 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013878 + ": " + Arrays.toString(drawableState));
13879 }
13880
13881 if (extraSpace == 0) {
13882 return drawableState;
13883 }
13884
13885 final int[] fullState;
13886 if (drawableState != null) {
13887 fullState = new int[drawableState.length + extraSpace];
13888 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13889 } else {
13890 fullState = new int[extraSpace];
13891 }
13892
13893 return fullState;
13894 }
13895
13896 /**
13897 * Merge your own state values in <var>additionalState</var> into the base
13898 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013899 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 *
13901 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013902 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 * own additional state values.
13904 *
13905 * @param additionalState The additional state values you would like
13906 * added to <var>baseState</var>; this array is not modified.
13907 *
13908 * @return As a convenience, the <var>baseState</var> array you originally
13909 * passed into the function is returned.
13910 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013911 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 */
13913 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13914 final int N = baseState.length;
13915 int i = N - 1;
13916 while (i >= 0 && baseState[i] == 0) {
13917 i--;
13918 }
13919 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13920 return baseState;
13921 }
13922
13923 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013924 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13925 * on all Drawable objects associated with this view.
13926 */
13927 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013928 if (mBackground != null) {
13929 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013930 }
13931 }
13932
13933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 * Sets the background color for this view.
13935 * @param color the color of the background
13936 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013937 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013938 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013939 if (mBackground instanceof ColorDrawable) {
13940 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013941 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013942 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 }
13945
13946 /**
13947 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013948 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013949 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013950 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 * @attr ref android.R.styleable#View_background
13952 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013953 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 public void setBackgroundResource(int resid) {
13955 if (resid != 0 && resid == mBackgroundResource) {
13956 return;
13957 }
13958
13959 Drawable d= null;
13960 if (resid != 0) {
13961 d = mResources.getDrawable(resid);
13962 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013963 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013964
13965 mBackgroundResource = resid;
13966 }
13967
13968 /**
13969 * Set the background to a given Drawable, or remove the background. If the
13970 * background has padding, this View's padding is set to the background's
13971 * padding. However, when a background is removed, this View's padding isn't
13972 * touched. If setting the padding is desired, please use
13973 * {@link #setPadding(int, int, int, int)}.
13974 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013975 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013976 * background
13977 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013978 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013979 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013980 setBackgroundDrawable(background);
13981 }
13982
13983 /**
13984 * @deprecated use {@link #setBackground(Drawable)} instead
13985 */
13986 @Deprecated
13987 public void setBackgroundDrawable(Drawable background) {
13988 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013989 return;
13990 }
13991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 boolean requestLayout = false;
13993
13994 mBackgroundResource = 0;
13995
13996 /*
13997 * Regardless of whether we're setting a new background or not, we want
13998 * to clear the previous drawable.
13999 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014000 if (mBackground != null) {
14001 mBackground.setCallback(null);
14002 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014003 }
14004
Philip Milne6c8ea062012-04-03 17:38:43 -070014005 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 Rect padding = sThreadLocal.get();
14007 if (padding == null) {
14008 padding = new Rect();
14009 sThreadLocal.set(padding);
14010 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014011 if (background.getPadding(padding)) {
14012 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014013 case LAYOUT_DIRECTION_RTL:
14014 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14015 break;
14016 case LAYOUT_DIRECTION_LTR:
14017 default:
14018 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014020 }
14021
14022 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14023 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014024 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14025 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014026 requestLayout = true;
14027 }
14028
Philip Milne6c8ea062012-04-03 17:38:43 -070014029 background.setCallback(this);
14030 if (background.isStateful()) {
14031 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014033 background.setVisible(getVisibility() == VISIBLE, false);
14034 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014035
14036 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14037 mPrivateFlags &= ~SKIP_DRAW;
14038 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14039 requestLayout = true;
14040 }
14041 } else {
14042 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014043 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014044
14045 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14046 /*
14047 * This view ONLY drew the background before and we're removing
14048 * the background, so now it won't draw anything
14049 * (hence we SKIP_DRAW)
14050 */
14051 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14052 mPrivateFlags |= SKIP_DRAW;
14053 }
14054
14055 /*
14056 * When the background is set, we try to apply its padding to this
14057 * View. When the background is removed, we don't touch this View's
14058 * padding. This is noted in the Javadocs. Hence, we don't need to
14059 * requestLayout(), the invalidate() below is sufficient.
14060 */
14061
14062 // The old background's minimum size could have affected this
14063 // View's layout, so let's requestLayout
14064 requestLayout = true;
14065 }
14066
Romain Guy8f1344f52009-05-15 16:03:59 -070014067 computeOpaqueFlags();
14068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069 if (requestLayout) {
14070 requestLayout();
14071 }
14072
14073 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014074 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 }
14076
14077 /**
14078 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014079 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014081 *
14082 * @see #setBackground(Drawable)
14083 *
14084 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 */
14086 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014087 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014088 }
14089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014090 /**
14091 * Sets the padding. The view may add on the space required to display
14092 * the scrollbars, depending on the style and visibility of the scrollbars.
14093 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14094 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14095 * from the values set in this call.
14096 *
14097 * @attr ref android.R.styleable#View_padding
14098 * @attr ref android.R.styleable#View_paddingBottom
14099 * @attr ref android.R.styleable#View_paddingLeft
14100 * @attr ref android.R.styleable#View_paddingRight
14101 * @attr ref android.R.styleable#View_paddingTop
14102 * @param left the left padding in pixels
14103 * @param top the top padding in pixels
14104 * @param right the right padding in pixels
14105 * @param bottom the bottom padding in pixels
14106 */
14107 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014108 mUserPaddingStart = -1;
14109 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014110 mUserPaddingRelative = false;
14111
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014112 internalSetPadding(left, top, right, bottom);
14113 }
14114
14115 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014116 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117 mUserPaddingRight = right;
14118 mUserPaddingBottom = bottom;
14119
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014120 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014121 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014122
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014123 // Common case is there are no scroll bars.
14124 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014125 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014126 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014127 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014128 switch (mVerticalScrollbarPosition) {
14129 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014130 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14131 left += offset;
14132 } else {
14133 right += offset;
14134 }
14135 break;
Adam Powell20232d02010-12-08 21:08:53 -080014136 case SCROLLBAR_POSITION_RIGHT:
14137 right += offset;
14138 break;
14139 case SCROLLBAR_POSITION_LEFT:
14140 left += offset;
14141 break;
14142 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014143 }
Adam Powell20232d02010-12-08 21:08:53 -080014144 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014145 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14146 ? 0 : getHorizontalScrollbarHeight();
14147 }
14148 }
Romain Guy8506ab42009-06-11 17:35:47 -070014149
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014150 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014151 changed = true;
14152 mPaddingLeft = left;
14153 }
14154 if (mPaddingTop != top) {
14155 changed = true;
14156 mPaddingTop = top;
14157 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014158 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014159 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014160 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014162 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014163 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014164 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014165 }
14166
14167 if (changed) {
14168 requestLayout();
14169 }
14170 }
14171
14172 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014173 * Sets the relative padding. The view may add on the space required to display
14174 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014175 * from the values set in this call.
14176 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014177 * @param start the start padding in pixels
14178 * @param top the top padding in pixels
14179 * @param end the end padding in pixels
14180 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014181 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014182 */
14183 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014184 mUserPaddingStart = start;
14185 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014186 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014187
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014188 switch(getResolvedLayoutDirection()) {
14189 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014190 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014191 break;
14192 case LAYOUT_DIRECTION_LTR:
14193 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014194 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014195 }
14196 }
14197
14198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199 * Returns the top padding of this view.
14200 *
14201 * @return the top padding in pixels
14202 */
14203 public int getPaddingTop() {
14204 return mPaddingTop;
14205 }
14206
14207 /**
14208 * Returns the bottom padding of this view. If there are inset and enabled
14209 * scrollbars, this value may include the space required to display the
14210 * scrollbars as well.
14211 *
14212 * @return the bottom padding in pixels
14213 */
14214 public int getPaddingBottom() {
14215 return mPaddingBottom;
14216 }
14217
14218 /**
14219 * Returns the left padding of this view. If there are inset and enabled
14220 * scrollbars, this value may include the space required to display the
14221 * scrollbars as well.
14222 *
14223 * @return the left padding in pixels
14224 */
14225 public int getPaddingLeft() {
14226 return mPaddingLeft;
14227 }
14228
14229 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014230 * Returns the start padding of this view depending on its resolved layout direction.
14231 * If there are inset and enabled scrollbars, this value may include the space
14232 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014233 *
14234 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014235 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014236 */
14237 public int getPaddingStart() {
14238 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14239 mPaddingRight : mPaddingLeft;
14240 }
14241
14242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 * Returns the right padding of this view. If there are inset and enabled
14244 * scrollbars, this value may include the space required to display the
14245 * scrollbars as well.
14246 *
14247 * @return the right padding in pixels
14248 */
14249 public int getPaddingRight() {
14250 return mPaddingRight;
14251 }
14252
14253 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014254 * Returns the end padding of this view depending on its resolved layout direction.
14255 * If there are inset and enabled scrollbars, this value may include the space
14256 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014257 *
14258 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014259 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014260 */
14261 public int getPaddingEnd() {
14262 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14263 mPaddingLeft : mPaddingRight;
14264 }
14265
14266 /**
14267 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014268 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014269 *
14270 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014271 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014272 */
14273 public boolean isPaddingRelative() {
14274 return mUserPaddingRelative;
14275 }
14276
14277 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014278 * @hide
14279 */
Philip Milne7a23b492012-04-24 22:12:36 -070014280 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014281 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014282 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014283 }
14284 return mLayoutInsets;
14285 }
14286
14287 /**
14288 * @hide
14289 */
14290 public void setLayoutInsets(Insets layoutInsets) {
14291 mLayoutInsets = layoutInsets;
14292 }
14293
14294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 * Changes the selection state of this view. A view can be selected or not.
14296 * Note that selection is not the same as focus. Views are typically
14297 * selected in the context of an AdapterView like ListView or GridView;
14298 * the selected view is the view that is highlighted.
14299 *
14300 * @param selected true if the view must be selected, false otherwise
14301 */
14302 public void setSelected(boolean selected) {
14303 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14304 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014305 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014306 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014307 refreshDrawableState();
14308 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014309 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14310 notifyAccessibilityStateChanged();
14311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 }
14313 }
14314
14315 /**
14316 * Dispatch setSelected to all of this View's children.
14317 *
14318 * @see #setSelected(boolean)
14319 *
14320 * @param selected The new selected state
14321 */
14322 protected void dispatchSetSelected(boolean selected) {
14323 }
14324
14325 /**
14326 * Indicates the selection state of this view.
14327 *
14328 * @return true if the view is selected, false otherwise
14329 */
14330 @ViewDebug.ExportedProperty
14331 public boolean isSelected() {
14332 return (mPrivateFlags & SELECTED) != 0;
14333 }
14334
14335 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014336 * Changes the activated state of this view. A view can be activated or not.
14337 * Note that activation is not the same as selection. Selection is
14338 * a transient property, representing the view (hierarchy) the user is
14339 * currently interacting with. Activation is a longer-term state that the
14340 * user can move views in and out of. For example, in a list view with
14341 * single or multiple selection enabled, the views in the current selection
14342 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14343 * here.) The activated state is propagated down to children of the view it
14344 * is set on.
14345 *
14346 * @param activated true if the view must be activated, false otherwise
14347 */
14348 public void setActivated(boolean activated) {
14349 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14350 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014351 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014352 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014353 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014354 }
14355 }
14356
14357 /**
14358 * Dispatch setActivated to all of this View's children.
14359 *
14360 * @see #setActivated(boolean)
14361 *
14362 * @param activated The new activated state
14363 */
14364 protected void dispatchSetActivated(boolean activated) {
14365 }
14366
14367 /**
14368 * Indicates the activation state of this view.
14369 *
14370 * @return true if the view is activated, false otherwise
14371 */
14372 @ViewDebug.ExportedProperty
14373 public boolean isActivated() {
14374 return (mPrivateFlags & ACTIVATED) != 0;
14375 }
14376
14377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014378 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14379 * observer can be used to get notifications when global events, like
14380 * layout, happen.
14381 *
14382 * The returned ViewTreeObserver observer is not guaranteed to remain
14383 * valid for the lifetime of this View. If the caller of this method keeps
14384 * a long-lived reference to ViewTreeObserver, it should always check for
14385 * the return value of {@link ViewTreeObserver#isAlive()}.
14386 *
14387 * @return The ViewTreeObserver for this view's hierarchy.
14388 */
14389 public ViewTreeObserver getViewTreeObserver() {
14390 if (mAttachInfo != null) {
14391 return mAttachInfo.mTreeObserver;
14392 }
14393 if (mFloatingTreeObserver == null) {
14394 mFloatingTreeObserver = new ViewTreeObserver();
14395 }
14396 return mFloatingTreeObserver;
14397 }
14398
14399 /**
14400 * <p>Finds the topmost view in the current view hierarchy.</p>
14401 *
14402 * @return the topmost view containing this view
14403 */
14404 public View getRootView() {
14405 if (mAttachInfo != null) {
14406 final View v = mAttachInfo.mRootView;
14407 if (v != null) {
14408 return v;
14409 }
14410 }
Romain Guy8506ab42009-06-11 17:35:47 -070014411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412 View parent = this;
14413
14414 while (parent.mParent != null && parent.mParent instanceof View) {
14415 parent = (View) parent.mParent;
14416 }
14417
14418 return parent;
14419 }
14420
14421 /**
14422 * <p>Computes the coordinates of this view on the screen. The argument
14423 * must be an array of two integers. After the method returns, the array
14424 * contains the x and y location in that order.</p>
14425 *
14426 * @param location an array of two integers in which to hold the coordinates
14427 */
14428 public void getLocationOnScreen(int[] location) {
14429 getLocationInWindow(location);
14430
14431 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014432 if (info != null) {
14433 location[0] += info.mWindowLeft;
14434 location[1] += info.mWindowTop;
14435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014436 }
14437
14438 /**
14439 * <p>Computes the coordinates of this view in its window. The argument
14440 * must be an array of two integers. After the method returns, the array
14441 * contains the x and y location in that order.</p>
14442 *
14443 * @param location an array of two integers in which to hold the coordinates
14444 */
14445 public void getLocationInWindow(int[] location) {
14446 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014447 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 }
14449
Gilles Debunne6583ce52011-12-06 18:09:02 -080014450 if (mAttachInfo == null) {
14451 // When the view is not attached to a window, this method does not make sense
14452 location[0] = location[1] = 0;
14453 return;
14454 }
14455
Gilles Debunnecea45132011-11-24 02:19:27 +010014456 float[] position = mAttachInfo.mTmpTransformLocation;
14457 position[0] = position[1] = 0.0f;
14458
14459 if (!hasIdentityMatrix()) {
14460 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014462
Gilles Debunnecea45132011-11-24 02:19:27 +010014463 position[0] += mLeft;
14464 position[1] += mTop;
14465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014466 ViewParent viewParent = mParent;
14467 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014468 final View view = (View) viewParent;
14469
14470 position[0] -= view.mScrollX;
14471 position[1] -= view.mScrollY;
14472
14473 if (!view.hasIdentityMatrix()) {
14474 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014475 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014476
14477 position[0] += view.mLeft;
14478 position[1] += view.mTop;
14479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014480 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014481 }
Romain Guy8506ab42009-06-11 17:35:47 -070014482
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014483 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014484 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014485 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14486 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014488
14489 location[0] = (int) (position[0] + 0.5f);
14490 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014491 }
14492
14493 /**
14494 * {@hide}
14495 * @param id the id of the view to be found
14496 * @return the view of the specified id, null if cannot be found
14497 */
14498 protected View findViewTraversal(int id) {
14499 if (id == mID) {
14500 return this;
14501 }
14502 return null;
14503 }
14504
14505 /**
14506 * {@hide}
14507 * @param tag the tag of the view to be found
14508 * @return the view of specified tag, null if cannot be found
14509 */
14510 protected View findViewWithTagTraversal(Object tag) {
14511 if (tag != null && tag.equals(mTag)) {
14512 return this;
14513 }
14514 return null;
14515 }
14516
14517 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014518 * {@hide}
14519 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014520 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014521 * @return The first view that matches the predicate or null.
14522 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014523 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014524 if (predicate.apply(this)) {
14525 return this;
14526 }
14527 return null;
14528 }
14529
14530 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014531 * Look for a child view with the given id. If this view has the given
14532 * id, return this view.
14533 *
14534 * @param id The id to search for.
14535 * @return The view that has the given id in the hierarchy or null
14536 */
14537 public final View findViewById(int id) {
14538 if (id < 0) {
14539 return null;
14540 }
14541 return findViewTraversal(id);
14542 }
14543
14544 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014545 * Finds a view by its unuque and stable accessibility id.
14546 *
14547 * @param accessibilityId The searched accessibility id.
14548 * @return The found view.
14549 */
14550 final View findViewByAccessibilityId(int accessibilityId) {
14551 if (accessibilityId < 0) {
14552 return null;
14553 }
14554 return findViewByAccessibilityIdTraversal(accessibilityId);
14555 }
14556
14557 /**
14558 * Performs the traversal to find a view by its unuque and stable accessibility id.
14559 *
14560 * <strong>Note:</strong>This method does not stop at the root namespace
14561 * boundary since the user can touch the screen at an arbitrary location
14562 * potentially crossing the root namespace bounday which will send an
14563 * accessibility event to accessibility services and they should be able
14564 * to obtain the event source. Also accessibility ids are guaranteed to be
14565 * unique in the window.
14566 *
14567 * @param accessibilityId The accessibility id.
14568 * @return The found view.
14569 */
14570 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14571 if (getAccessibilityViewId() == accessibilityId) {
14572 return this;
14573 }
14574 return null;
14575 }
14576
14577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014578 * Look for a child view with the given tag. If this view has the given
14579 * tag, return this view.
14580 *
14581 * @param tag The tag to search for, using "tag.equals(getTag())".
14582 * @return The View that has the given tag in the hierarchy or null
14583 */
14584 public final View findViewWithTag(Object tag) {
14585 if (tag == null) {
14586 return null;
14587 }
14588 return findViewWithTagTraversal(tag);
14589 }
14590
14591 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014592 * {@hide}
14593 * Look for a child view that matches the specified predicate.
14594 * If this view matches the predicate, return this view.
14595 *
14596 * @param predicate The predicate to evaluate.
14597 * @return The first view that matches the predicate or null.
14598 */
14599 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014600 return findViewByPredicateTraversal(predicate, null);
14601 }
14602
14603 /**
14604 * {@hide}
14605 * Look for a child view that matches the specified predicate,
14606 * starting with the specified view and its descendents and then
14607 * recusively searching the ancestors and siblings of that view
14608 * until this view is reached.
14609 *
14610 * This method is useful in cases where the predicate does not match
14611 * a single unique view (perhaps multiple views use the same id)
14612 * and we are trying to find the view that is "closest" in scope to the
14613 * starting view.
14614 *
14615 * @param start The view to start from.
14616 * @param predicate The predicate to evaluate.
14617 * @return The first view that matches the predicate or null.
14618 */
14619 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14620 View childToSkip = null;
14621 for (;;) {
14622 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14623 if (view != null || start == this) {
14624 return view;
14625 }
14626
14627 ViewParent parent = start.getParent();
14628 if (parent == null || !(parent instanceof View)) {
14629 return null;
14630 }
14631
14632 childToSkip = start;
14633 start = (View) parent;
14634 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014635 }
14636
14637 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014638 * Sets the identifier for this view. The identifier does not have to be
14639 * unique in this view's hierarchy. The identifier should be a positive
14640 * number.
14641 *
14642 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014643 * @see #getId()
14644 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014645 *
14646 * @param id a number used to identify the view
14647 *
14648 * @attr ref android.R.styleable#View_id
14649 */
14650 public void setId(int id) {
14651 mID = id;
14652 }
14653
14654 /**
14655 * {@hide}
14656 *
14657 * @param isRoot true if the view belongs to the root namespace, false
14658 * otherwise
14659 */
14660 public void setIsRootNamespace(boolean isRoot) {
14661 if (isRoot) {
14662 mPrivateFlags |= IS_ROOT_NAMESPACE;
14663 } else {
14664 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14665 }
14666 }
14667
14668 /**
14669 * {@hide}
14670 *
14671 * @return true if the view belongs to the root namespace, false otherwise
14672 */
14673 public boolean isRootNamespace() {
14674 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14675 }
14676
14677 /**
14678 * Returns this view's identifier.
14679 *
14680 * @return a positive integer used to identify the view or {@link #NO_ID}
14681 * if the view has no ID
14682 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014683 * @see #setId(int)
14684 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014685 * @attr ref android.R.styleable#View_id
14686 */
14687 @ViewDebug.CapturedViewProperty
14688 public int getId() {
14689 return mID;
14690 }
14691
14692 /**
14693 * Returns this view's tag.
14694 *
14695 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014696 *
14697 * @see #setTag(Object)
14698 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014699 */
14700 @ViewDebug.ExportedProperty
14701 public Object getTag() {
14702 return mTag;
14703 }
14704
14705 /**
14706 * Sets the tag associated with this view. A tag can be used to mark
14707 * a view in its hierarchy and does not have to be unique within the
14708 * hierarchy. Tags can also be used to store data within a view without
14709 * resorting to another data structure.
14710 *
14711 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014712 *
14713 * @see #getTag()
14714 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014715 */
14716 public void setTag(final Object tag) {
14717 mTag = tag;
14718 }
14719
14720 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014721 * Returns the tag associated with this view and the specified key.
14722 *
14723 * @param key The key identifying the tag
14724 *
14725 * @return the Object stored in this view as a tag
14726 *
14727 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014728 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014729 */
14730 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014731 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014732 return null;
14733 }
14734
14735 /**
14736 * Sets a tag associated with this view and a key. A tag can be used
14737 * to mark a view in its hierarchy and does not have to be unique within
14738 * the hierarchy. Tags can also be used to store data within a view
14739 * without resorting to another data structure.
14740 *
14741 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014742 * application to ensure it is unique (see the <a
14743 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14744 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014745 * the Android framework or not associated with any package will cause
14746 * an {@link IllegalArgumentException} to be thrown.
14747 *
14748 * @param key The key identifying the tag
14749 * @param tag An Object to tag the view with
14750 *
14751 * @throws IllegalArgumentException If they specified key is not valid
14752 *
14753 * @see #setTag(Object)
14754 * @see #getTag(int)
14755 */
14756 public void setTag(int key, final Object tag) {
14757 // If the package id is 0x00 or 0x01, it's either an undefined package
14758 // or a framework id
14759 if ((key >>> 24) < 2) {
14760 throw new IllegalArgumentException("The key must be an application-specific "
14761 + "resource id.");
14762 }
14763
Adam Powell2b2f6d62011-09-23 11:15:39 -070014764 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014765 }
14766
14767 /**
14768 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14769 * framework id.
14770 *
14771 * @hide
14772 */
14773 public void setTagInternal(int key, Object tag) {
14774 if ((key >>> 24) != 0x1) {
14775 throw new IllegalArgumentException("The key must be a framework-specific "
14776 + "resource id.");
14777 }
14778
Adam Powell2b2f6d62011-09-23 11:15:39 -070014779 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014780 }
14781
Adam Powell2b2f6d62011-09-23 11:15:39 -070014782 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014783 if (mKeyedTags == null) {
14784 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014785 }
14786
Adam Powell7db82ac2011-09-22 19:44:04 -070014787 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014788 }
14789
14790 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014791 * Prints information about this view in the log output, with the tag
14792 * {@link #VIEW_LOG_TAG}.
14793 *
14794 * @hide
14795 */
14796 public void debug() {
14797 debug(0);
14798 }
14799
14800 /**
14801 * Prints information about this view in the log output, with the tag
14802 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14803 * indentation defined by the <code>depth</code>.
14804 *
14805 * @param depth the indentation level
14806 *
14807 * @hide
14808 */
14809 protected void debug(int depth) {
14810 String output = debugIndent(depth - 1);
14811
14812 output += "+ " + this;
14813 int id = getId();
14814 if (id != -1) {
14815 output += " (id=" + id + ")";
14816 }
14817 Object tag = getTag();
14818 if (tag != null) {
14819 output += " (tag=" + tag + ")";
14820 }
14821 Log.d(VIEW_LOG_TAG, output);
14822
14823 if ((mPrivateFlags & FOCUSED) != 0) {
14824 output = debugIndent(depth) + " FOCUSED";
14825 Log.d(VIEW_LOG_TAG, output);
14826 }
14827
14828 output = debugIndent(depth);
14829 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14830 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14831 + "} ";
14832 Log.d(VIEW_LOG_TAG, output);
14833
14834 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14835 || mPaddingBottom != 0) {
14836 output = debugIndent(depth);
14837 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14838 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14839 Log.d(VIEW_LOG_TAG, output);
14840 }
14841
14842 output = debugIndent(depth);
14843 output += "mMeasureWidth=" + mMeasuredWidth +
14844 " mMeasureHeight=" + mMeasuredHeight;
14845 Log.d(VIEW_LOG_TAG, output);
14846
14847 output = debugIndent(depth);
14848 if (mLayoutParams == null) {
14849 output += "BAD! no layout params";
14850 } else {
14851 output = mLayoutParams.debug(output);
14852 }
14853 Log.d(VIEW_LOG_TAG, output);
14854
14855 output = debugIndent(depth);
14856 output += "flags={";
14857 output += View.printFlags(mViewFlags);
14858 output += "}";
14859 Log.d(VIEW_LOG_TAG, output);
14860
14861 output = debugIndent(depth);
14862 output += "privateFlags={";
14863 output += View.printPrivateFlags(mPrivateFlags);
14864 output += "}";
14865 Log.d(VIEW_LOG_TAG, output);
14866 }
14867
14868 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014869 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014870 *
14871 * @param depth the indentation level
14872 * @return a String containing (depth * 2 + 3) * 2 white spaces
14873 *
14874 * @hide
14875 */
14876 protected static String debugIndent(int depth) {
14877 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14878 for (int i = 0; i < (depth * 2) + 3; i++) {
14879 spaces.append(' ').append(' ');
14880 }
14881 return spaces.toString();
14882 }
14883
14884 /**
14885 * <p>Return the offset of the widget's text baseline from the widget's top
14886 * boundary. If this widget does not support baseline alignment, this
14887 * method returns -1. </p>
14888 *
14889 * @return the offset of the baseline within the widget's bounds or -1
14890 * if baseline alignment is not supported
14891 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014892 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893 public int getBaseline() {
14894 return -1;
14895 }
14896
14897 /**
14898 * Call this when something has changed which has invalidated the
14899 * layout of this view. This will schedule a layout pass of the view
14900 * tree.
14901 */
14902 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014903 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014904 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014905
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014906 if (mLayoutParams != null) {
14907 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14908 }
14909
14910 if (mParent != null && !mParent.isLayoutRequested()) {
14911 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014912 }
14913 }
14914
14915 /**
14916 * Forces this view to be laid out during the next layout pass.
14917 * This method does not call requestLayout() or forceLayout()
14918 * on the parent.
14919 */
14920 public void forceLayout() {
14921 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014922 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014923 }
14924
14925 /**
14926 * <p>
14927 * This is called to find out how big a view should be. The parent
14928 * supplies constraint information in the width and height parameters.
14929 * </p>
14930 *
14931 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014932 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014933 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014934 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014935 * </p>
14936 *
14937 *
14938 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14939 * parent
14940 * @param heightMeasureSpec Vertical space requirements as imposed by the
14941 * parent
14942 *
14943 * @see #onMeasure(int, int)
14944 */
14945 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14946 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14947 widthMeasureSpec != mOldWidthMeasureSpec ||
14948 heightMeasureSpec != mOldHeightMeasureSpec) {
14949
14950 // first clears the measured dimension flag
14951 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014953 // measure ourselves, this should set the measured dimension flag back
14954 onMeasure(widthMeasureSpec, heightMeasureSpec);
14955
14956 // flag not set, setMeasuredDimension() was not invoked, we raise
14957 // an exception to warn the developer
14958 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14959 throw new IllegalStateException("onMeasure() did not set the"
14960 + " measured dimension by calling"
14961 + " setMeasuredDimension()");
14962 }
14963
14964 mPrivateFlags |= LAYOUT_REQUIRED;
14965 }
14966
14967 mOldWidthMeasureSpec = widthMeasureSpec;
14968 mOldHeightMeasureSpec = heightMeasureSpec;
14969 }
14970
14971 /**
14972 * <p>
14973 * Measure the view and its content to determine the measured width and the
14974 * measured height. This method is invoked by {@link #measure(int, int)} and
14975 * should be overriden by subclasses to provide accurate and efficient
14976 * measurement of their contents.
14977 * </p>
14978 *
14979 * <p>
14980 * <strong>CONTRACT:</strong> When overriding this method, you
14981 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14982 * measured width and height of this view. Failure to do so will trigger an
14983 * <code>IllegalStateException</code>, thrown by
14984 * {@link #measure(int, int)}. Calling the superclass'
14985 * {@link #onMeasure(int, int)} is a valid use.
14986 * </p>
14987 *
14988 * <p>
14989 * The base class implementation of measure defaults to the background size,
14990 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14991 * override {@link #onMeasure(int, int)} to provide better measurements of
14992 * their content.
14993 * </p>
14994 *
14995 * <p>
14996 * If this method is overridden, it is the subclass's responsibility to make
14997 * sure the measured height and width are at least the view's minimum height
14998 * and width ({@link #getSuggestedMinimumHeight()} and
14999 * {@link #getSuggestedMinimumWidth()}).
15000 * </p>
15001 *
15002 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15003 * The requirements are encoded with
15004 * {@link android.view.View.MeasureSpec}.
15005 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15006 * The requirements are encoded with
15007 * {@link android.view.View.MeasureSpec}.
15008 *
15009 * @see #getMeasuredWidth()
15010 * @see #getMeasuredHeight()
15011 * @see #setMeasuredDimension(int, int)
15012 * @see #getSuggestedMinimumHeight()
15013 * @see #getSuggestedMinimumWidth()
15014 * @see android.view.View.MeasureSpec#getMode(int)
15015 * @see android.view.View.MeasureSpec#getSize(int)
15016 */
15017 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15018 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15019 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15020 }
15021
15022 /**
15023 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15024 * measured width and measured height. Failing to do so will trigger an
15025 * exception at measurement time.</p>
15026 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015027 * @param measuredWidth The measured width of this view. May be a complex
15028 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15029 * {@link #MEASURED_STATE_TOO_SMALL}.
15030 * @param measuredHeight The measured height of this view. May be a complex
15031 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15032 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015033 */
15034 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15035 mMeasuredWidth = measuredWidth;
15036 mMeasuredHeight = measuredHeight;
15037
15038 mPrivateFlags |= MEASURED_DIMENSION_SET;
15039 }
15040
15041 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015042 * Merge two states as returned by {@link #getMeasuredState()}.
15043 * @param curState The current state as returned from a view or the result
15044 * of combining multiple views.
15045 * @param newState The new view state to combine.
15046 * @return Returns a new integer reflecting the combination of the two
15047 * states.
15048 */
15049 public static int combineMeasuredStates(int curState, int newState) {
15050 return curState | newState;
15051 }
15052
15053 /**
15054 * Version of {@link #resolveSizeAndState(int, int, int)}
15055 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15056 */
15057 public static int resolveSize(int size, int measureSpec) {
15058 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15059 }
15060
15061 /**
15062 * Utility to reconcile a desired size and state, with constraints imposed
15063 * by a MeasureSpec. Will take the desired size, unless a different size
15064 * is imposed by the constraints. The returned value is a compound integer,
15065 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15066 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15067 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015068 *
15069 * @param size How big the view wants to be
15070 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015071 * @return Size information bit mask as defined by
15072 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015073 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015074 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015075 int result = size;
15076 int specMode = MeasureSpec.getMode(measureSpec);
15077 int specSize = MeasureSpec.getSize(measureSpec);
15078 switch (specMode) {
15079 case MeasureSpec.UNSPECIFIED:
15080 result = size;
15081 break;
15082 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015083 if (specSize < size) {
15084 result = specSize | MEASURED_STATE_TOO_SMALL;
15085 } else {
15086 result = size;
15087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 break;
15089 case MeasureSpec.EXACTLY:
15090 result = specSize;
15091 break;
15092 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015093 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 }
15095
15096 /**
15097 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015098 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099 * by the MeasureSpec.
15100 *
15101 * @param size Default size for this view
15102 * @param measureSpec Constraints imposed by the parent
15103 * @return The size this view should be.
15104 */
15105 public static int getDefaultSize(int size, int measureSpec) {
15106 int result = size;
15107 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015108 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015109
15110 switch (specMode) {
15111 case MeasureSpec.UNSPECIFIED:
15112 result = size;
15113 break;
15114 case MeasureSpec.AT_MOST:
15115 case MeasureSpec.EXACTLY:
15116 result = specSize;
15117 break;
15118 }
15119 return result;
15120 }
15121
15122 /**
15123 * Returns the suggested minimum height that the view should use. This
15124 * returns the maximum of the view's minimum height
15125 * and the background's minimum height
15126 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15127 * <p>
15128 * When being used in {@link #onMeasure(int, int)}, the caller should still
15129 * ensure the returned height is within the requirements of the parent.
15130 *
15131 * @return The suggested minimum height of the view.
15132 */
15133 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015134 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015136 }
15137
15138 /**
15139 * Returns the suggested minimum width that the view should use. This
15140 * returns the maximum of the view's minimum width)
15141 * and the background's minimum width
15142 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15143 * <p>
15144 * When being used in {@link #onMeasure(int, int)}, the caller should still
15145 * ensure the returned width is within the requirements of the parent.
15146 *
15147 * @return The suggested minimum width of the view.
15148 */
15149 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015150 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015152
Philip Milne6c8ea062012-04-03 17:38:43 -070015153 /**
15154 * Returns the minimum height of the view.
15155 *
15156 * @return the minimum height the view will try to be.
15157 *
15158 * @see #setMinimumHeight(int)
15159 *
15160 * @attr ref android.R.styleable#View_minHeight
15161 */
15162 public int getMinimumHeight() {
15163 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015164 }
15165
15166 /**
15167 * Sets the minimum height of the view. It is not guaranteed the view will
15168 * be able to achieve this minimum height (for example, if its parent layout
15169 * constrains it with less available height).
15170 *
15171 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015172 *
15173 * @see #getMinimumHeight()
15174 *
15175 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015176 */
15177 public void setMinimumHeight(int minHeight) {
15178 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015179 requestLayout();
15180 }
15181
15182 /**
15183 * Returns the minimum width of the view.
15184 *
15185 * @return the minimum width the view will try to be.
15186 *
15187 * @see #setMinimumWidth(int)
15188 *
15189 * @attr ref android.R.styleable#View_minWidth
15190 */
15191 public int getMinimumWidth() {
15192 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015193 }
15194
15195 /**
15196 * Sets the minimum width of the view. It is not guaranteed the view will
15197 * be able to achieve this minimum width (for example, if its parent layout
15198 * constrains it with less available width).
15199 *
15200 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015201 *
15202 * @see #getMinimumWidth()
15203 *
15204 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015205 */
15206 public void setMinimumWidth(int minWidth) {
15207 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015208 requestLayout();
15209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015210 }
15211
15212 /**
15213 * Get the animation currently associated with this view.
15214 *
15215 * @return The animation that is currently playing or
15216 * scheduled to play for this view.
15217 */
15218 public Animation getAnimation() {
15219 return mCurrentAnimation;
15220 }
15221
15222 /**
15223 * Start the specified animation now.
15224 *
15225 * @param animation the animation to start now
15226 */
15227 public void startAnimation(Animation animation) {
15228 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15229 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015230 invalidateParentCaches();
15231 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015232 }
15233
15234 /**
15235 * Cancels any animations for this view.
15236 */
15237 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015238 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015239 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015241 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015242 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015243 }
15244
15245 /**
15246 * Sets the next animation to play for this view.
15247 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015248 * {@link #startAnimation(android.view.animation.Animation)} instead.
15249 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015250 * control over the start time and invalidation, but you
15251 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015252 * 2) the view's parent (which controls animations on its children)
15253 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 * start.
15255 *
15256 * @param animation The next animation, or null.
15257 */
15258 public void setAnimation(Animation animation) {
15259 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015261 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015262 // If the screen is off assume the animation start time is now instead of
15263 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15264 // would cause the animation to start when the screen turns back on
15265 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15266 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15267 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015269 animation.reset();
15270 }
15271 }
15272
15273 /**
15274 * Invoked by a parent ViewGroup to notify the start of the animation
15275 * currently associated with this view. If you override this method,
15276 * always call super.onAnimationStart();
15277 *
15278 * @see #setAnimation(android.view.animation.Animation)
15279 * @see #getAnimation()
15280 */
15281 protected void onAnimationStart() {
15282 mPrivateFlags |= ANIMATION_STARTED;
15283 }
15284
15285 /**
15286 * Invoked by a parent ViewGroup to notify the end of the animation
15287 * currently associated with this view. If you override this method,
15288 * always call super.onAnimationEnd();
15289 *
15290 * @see #setAnimation(android.view.animation.Animation)
15291 * @see #getAnimation()
15292 */
15293 protected void onAnimationEnd() {
15294 mPrivateFlags &= ~ANIMATION_STARTED;
15295 }
15296
15297 /**
15298 * Invoked if there is a Transform that involves alpha. Subclass that can
15299 * draw themselves with the specified alpha should return true, and then
15300 * respect that alpha when their onDraw() is called. If this returns false
15301 * then the view may be redirected to draw into an offscreen buffer to
15302 * fulfill the request, which will look fine, but may be slower than if the
15303 * subclass handles it internally. The default implementation returns false.
15304 *
15305 * @param alpha The alpha (0..255) to apply to the view's drawing
15306 * @return true if the view can draw with the specified alpha.
15307 */
15308 protected boolean onSetAlpha(int alpha) {
15309 return false;
15310 }
15311
15312 /**
15313 * This is used by the RootView to perform an optimization when
15314 * the view hierarchy contains one or several SurfaceView.
15315 * SurfaceView is always considered transparent, but its children are not,
15316 * therefore all View objects remove themselves from the global transparent
15317 * region (passed as a parameter to this function).
15318 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015319 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015320 *
15321 * @return Returns true if the effective visibility of the view at this
15322 * point is opaque, regardless of the transparent region; returns false
15323 * if it is possible for underlying windows to be seen behind the view.
15324 *
15325 * {@hide}
15326 */
15327 public boolean gatherTransparentRegion(Region region) {
15328 final AttachInfo attachInfo = mAttachInfo;
15329 if (region != null && attachInfo != null) {
15330 final int pflags = mPrivateFlags;
15331 if ((pflags & SKIP_DRAW) == 0) {
15332 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15333 // remove it from the transparent region.
15334 final int[] location = attachInfo.mTransparentLocation;
15335 getLocationInWindow(location);
15336 region.op(location[0], location[1], location[0] + mRight - mLeft,
15337 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015338 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015339 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15340 // exists, so we remove the background drawable's non-transparent
15341 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015342 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015343 }
15344 }
15345 return true;
15346 }
15347
15348 /**
15349 * Play a sound effect for this view.
15350 *
15351 * <p>The framework will play sound effects for some built in actions, such as
15352 * clicking, but you may wish to play these effects in your widget,
15353 * for instance, for internal navigation.
15354 *
15355 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15356 * {@link #isSoundEffectsEnabled()} is true.
15357 *
15358 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15359 */
15360 public void playSoundEffect(int soundConstant) {
15361 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15362 return;
15363 }
15364 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15365 }
15366
15367 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015368 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015369 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015370 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015371 *
15372 * <p>The framework will provide haptic feedback for some built in actions,
15373 * such as long presses, but you may wish to provide feedback for your
15374 * own widget.
15375 *
15376 * <p>The feedback will only be performed if
15377 * {@link #isHapticFeedbackEnabled()} is true.
15378 *
15379 * @param feedbackConstant One of the constants defined in
15380 * {@link HapticFeedbackConstants}
15381 */
15382 public boolean performHapticFeedback(int feedbackConstant) {
15383 return performHapticFeedback(feedbackConstant, 0);
15384 }
15385
15386 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015387 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015388 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015389 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015390 *
15391 * @param feedbackConstant One of the constants defined in
15392 * {@link HapticFeedbackConstants}
15393 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15394 */
15395 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15396 if (mAttachInfo == null) {
15397 return false;
15398 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015399 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015400 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015401 && !isHapticFeedbackEnabled()) {
15402 return false;
15403 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015404 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15405 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015406 }
15407
15408 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015409 * Request that the visibility of the status bar or other screen/window
15410 * decorations be changed.
15411 *
15412 * <p>This method is used to put the over device UI into temporary modes
15413 * where the user's attention is focused more on the application content,
15414 * by dimming or hiding surrounding system affordances. This is typically
15415 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15416 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15417 * to be placed behind the action bar (and with these flags other system
15418 * affordances) so that smooth transitions between hiding and showing them
15419 * can be done.
15420 *
15421 * <p>Two representative examples of the use of system UI visibility is
15422 * implementing a content browsing application (like a magazine reader)
15423 * and a video playing application.
15424 *
15425 * <p>The first code shows a typical implementation of a View in a content
15426 * browsing application. In this implementation, the application goes
15427 * into a content-oriented mode by hiding the status bar and action bar,
15428 * and putting the navigation elements into lights out mode. The user can
15429 * then interact with content while in this mode. Such an application should
15430 * provide an easy way for the user to toggle out of the mode (such as to
15431 * check information in the status bar or access notifications). In the
15432 * implementation here, this is done simply by tapping on the content.
15433 *
15434 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15435 * content}
15436 *
15437 * <p>This second code sample shows a typical implementation of a View
15438 * in a video playing application. In this situation, while the video is
15439 * playing the application would like to go into a complete full-screen mode,
15440 * to use as much of the display as possible for the video. When in this state
15441 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015442 * touching on the screen to pop the UI out of full screen mode. See
15443 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015444 *
15445 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15446 * content}
15447 *
15448 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15449 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15450 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15451 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015452 */
15453 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015454 if (visibility != mSystemUiVisibility) {
15455 mSystemUiVisibility = visibility;
15456 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15457 mParent.recomputeViewAttributes(this);
15458 }
Joe Onorato664644d2011-01-23 17:53:23 -080015459 }
15460 }
15461
15462 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015463 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15464 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15465 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15466 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15467 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015468 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015469 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015470 return mSystemUiVisibility;
15471 }
15472
Scott Mainec6331b2011-05-24 16:55:56 -070015473 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015474 * Returns the current system UI visibility that is currently set for
15475 * the entire window. This is the combination of the
15476 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15477 * views in the window.
15478 */
15479 public int getWindowSystemUiVisibility() {
15480 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15481 }
15482
15483 /**
15484 * Override to find out when the window's requested system UI visibility
15485 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15486 * This is different from the callbacks recieved through
15487 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15488 * in that this is only telling you about the local request of the window,
15489 * not the actual values applied by the system.
15490 */
15491 public void onWindowSystemUiVisibilityChanged(int visible) {
15492 }
15493
15494 /**
15495 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15496 * the view hierarchy.
15497 */
15498 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15499 onWindowSystemUiVisibilityChanged(visible);
15500 }
15501
15502 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015503 * Set a listener to receive callbacks when the visibility of the system bar changes.
15504 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15505 */
Joe Onorato664644d2011-01-23 17:53:23 -080015506 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015507 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015508 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15509 mParent.recomputeViewAttributes(this);
15510 }
15511 }
15512
15513 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015514 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15515 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015516 */
15517 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015518 ListenerInfo li = mListenerInfo;
15519 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15520 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015521 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015522 }
15523 }
15524
Dianne Hackborncf675782012-05-10 15:07:24 -070015525 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015526 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15527 if (val != mSystemUiVisibility) {
15528 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015529 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015530 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015531 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015532 }
15533
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015534 /** @hide */
15535 public void setDisabledSystemUiVisibility(int flags) {
15536 if (mAttachInfo != null) {
15537 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15538 mAttachInfo.mDisabledSystemUiVisibility = flags;
15539 if (mParent != null) {
15540 mParent.recomputeViewAttributes(this);
15541 }
15542 }
15543 }
15544 }
15545
Joe Onorato664644d2011-01-23 17:53:23 -080015546 /**
Joe Malin32736f02011-01-19 16:14:20 -080015547 * Creates an image that the system displays during the drag and drop
15548 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15549 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15550 * appearance as the given View. The default also positions the center of the drag shadow
15551 * directly under the touch point. If no View is provided (the constructor with no parameters
15552 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15553 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15554 * default is an invisible drag shadow.
15555 * <p>
15556 * You are not required to use the View you provide to the constructor as the basis of the
15557 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15558 * anything you want as the drag shadow.
15559 * </p>
15560 * <p>
15561 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15562 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15563 * size and position of the drag shadow. It uses this data to construct a
15564 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15565 * so that your application can draw the shadow image in the Canvas.
15566 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015567 *
15568 * <div class="special reference">
15569 * <h3>Developer Guides</h3>
15570 * <p>For a guide to implementing drag and drop features, read the
15571 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15572 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015573 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015574 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015575 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015576
15577 /**
Joe Malin32736f02011-01-19 16:14:20 -080015578 * Constructs a shadow image builder based on a View. By default, the resulting drag
15579 * shadow will have the same appearance and dimensions as the View, with the touch point
15580 * over the center of the View.
15581 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015582 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015583 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015584 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015585 }
15586
Christopher Tate17ed60c2011-01-18 12:50:26 -080015587 /**
15588 * Construct a shadow builder object with no associated View. This
15589 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15590 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15591 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015592 * reference to any View object. If they are not overridden, then the result is an
15593 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015594 */
15595 public DragShadowBuilder() {
15596 mView = new WeakReference<View>(null);
15597 }
15598
15599 /**
15600 * Returns the View object that had been passed to the
15601 * {@link #View.DragShadowBuilder(View)}
15602 * constructor. If that View parameter was {@code null} or if the
15603 * {@link #View.DragShadowBuilder()}
15604 * constructor was used to instantiate the builder object, this method will return
15605 * null.
15606 *
15607 * @return The View object associate with this builder object.
15608 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015609 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015610 final public View getView() {
15611 return mView.get();
15612 }
15613
Christopher Tate2c095f32010-10-04 14:13:40 -070015614 /**
Joe Malin32736f02011-01-19 16:14:20 -080015615 * Provides the metrics for the shadow image. These include the dimensions of
15616 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015617 * be centered under the touch location while dragging.
15618 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015619 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015620 * same as the dimensions of the View itself and centers the shadow under
15621 * the touch point.
15622 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015623 *
Joe Malin32736f02011-01-19 16:14:20 -080015624 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15625 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15626 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15627 * image.
15628 *
15629 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15630 * shadow image that should be underneath the touch point during the drag and drop
15631 * operation. Your application must set {@link android.graphics.Point#x} to the
15632 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015633 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015634 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015635 final View view = mView.get();
15636 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015637 shadowSize.set(view.getWidth(), view.getHeight());
15638 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015639 } else {
15640 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15641 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015642 }
15643
15644 /**
Joe Malin32736f02011-01-19 16:14:20 -080015645 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15646 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015647 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015648 *
Joe Malin32736f02011-01-19 16:14:20 -080015649 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015650 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015651 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015652 final View view = mView.get();
15653 if (view != null) {
15654 view.draw(canvas);
15655 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015656 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015657 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015658 }
15659 }
15660
15661 /**
Joe Malin32736f02011-01-19 16:14:20 -080015662 * Starts a drag and drop operation. When your application calls this method, it passes a
15663 * {@link android.view.View.DragShadowBuilder} object to the system. The
15664 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15665 * to get metrics for the drag shadow, and then calls the object's
15666 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15667 * <p>
15668 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15669 * drag events to all the View objects in your application that are currently visible. It does
15670 * this either by calling the View object's drag listener (an implementation of
15671 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15672 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15673 * Both are passed a {@link android.view.DragEvent} object that has a
15674 * {@link android.view.DragEvent#getAction()} value of
15675 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15676 * </p>
15677 * <p>
15678 * Your application can invoke startDrag() on any attached View object. The View object does not
15679 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15680 * be related to the View the user selected for dragging.
15681 * </p>
15682 * @param data A {@link android.content.ClipData} object pointing to the data to be
15683 * transferred by the drag and drop operation.
15684 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15685 * drag shadow.
15686 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15687 * drop operation. This Object is put into every DragEvent object sent by the system during the
15688 * current drag.
15689 * <p>
15690 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15691 * to the target Views. For example, it can contain flags that differentiate between a
15692 * a copy operation and a move operation.
15693 * </p>
15694 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15695 * so the parameter should be set to 0.
15696 * @return {@code true} if the method completes successfully, or
15697 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15698 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015699 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015700 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015701 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015702 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015703 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015704 }
15705 boolean okay = false;
15706
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015707 Point shadowSize = new Point();
15708 Point shadowTouchPoint = new Point();
15709 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015710
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015711 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15712 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15713 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015714 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015715
Chris Tatea32dcf72010-10-14 12:13:50 -070015716 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015717 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15718 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015719 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015720 Surface surface = new Surface();
15721 try {
15722 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015723 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015724 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015725 + " surface=" + surface);
15726 if (token != null) {
15727 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015728 try {
Chris Tate6b391282010-10-14 15:48:59 -070015729 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015730 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015731 } finally {
15732 surface.unlockCanvasAndPost(canvas);
15733 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015734
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015735 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015736
15737 // Cache the local state object for delivery with DragEvents
15738 root.setLocalDragState(myLocalState);
15739
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015740 // repurpose 'shadowSize' for the last touch point
15741 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015742
Christopher Tatea53146c2010-09-07 11:57:52 -070015743 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015744 shadowSize.x, shadowSize.y,
15745 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015746 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015747
15748 // Off and running! Release our local surface instance; the drag
15749 // shadow surface is now managed by the system process.
15750 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015751 }
15752 } catch (Exception e) {
15753 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15754 surface.destroy();
15755 }
15756
15757 return okay;
15758 }
15759
Christopher Tatea53146c2010-09-07 11:57:52 -070015760 /**
Joe Malin32736f02011-01-19 16:14:20 -080015761 * Handles drag events sent by the system following a call to
15762 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15763 *<p>
15764 * When the system calls this method, it passes a
15765 * {@link android.view.DragEvent} object. A call to
15766 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15767 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15768 * operation.
15769 * @param event The {@link android.view.DragEvent} sent by the system.
15770 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15771 * in DragEvent, indicating the type of drag event represented by this object.
15772 * @return {@code true} if the method was successful, otherwise {@code false}.
15773 * <p>
15774 * The method should return {@code true} in response to an action type of
15775 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15776 * operation.
15777 * </p>
15778 * <p>
15779 * The method should also return {@code true} in response to an action type of
15780 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15781 * {@code false} if it didn't.
15782 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015783 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015784 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015785 return false;
15786 }
15787
15788 /**
Joe Malin32736f02011-01-19 16:14:20 -080015789 * Detects if this View is enabled and has a drag event listener.
15790 * If both are true, then it calls the drag event listener with the
15791 * {@link android.view.DragEvent} it received. If the drag event listener returns
15792 * {@code true}, then dispatchDragEvent() returns {@code true}.
15793 * <p>
15794 * For all other cases, the method calls the
15795 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15796 * method and returns its result.
15797 * </p>
15798 * <p>
15799 * This ensures that a drag event is always consumed, even if the View does not have a drag
15800 * event listener. However, if the View has a listener and the listener returns true, then
15801 * onDragEvent() is not called.
15802 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015803 */
15804 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015805 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015806 ListenerInfo li = mListenerInfo;
15807 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15808 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015809 return true;
15810 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015811 return onDragEvent(event);
15812 }
15813
Christopher Tate3d4bf172011-03-28 16:16:46 -070015814 boolean canAcceptDrag() {
15815 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15816 }
15817
Christopher Tatea53146c2010-09-07 11:57:52 -070015818 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015819 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15820 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015821 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015822 */
15823 public void onCloseSystemDialogs(String reason) {
15824 }
Joe Malin32736f02011-01-19 16:14:20 -080015825
Dianne Hackbornffa42482009-09-23 22:20:11 -070015826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015827 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015828 * update a Region being computed for
15829 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 * that any non-transparent parts of the Drawable are removed from the
15831 * given transparent region.
15832 *
15833 * @param dr The Drawable whose transparency is to be applied to the region.
15834 * @param region A Region holding the current transparency information,
15835 * where any parts of the region that are set are considered to be
15836 * transparent. On return, this region will be modified to have the
15837 * transparency information reduced by the corresponding parts of the
15838 * Drawable that are not transparent.
15839 * {@hide}
15840 */
15841 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15842 if (DBG) {
15843 Log.i("View", "Getting transparent region for: " + this);
15844 }
15845 final Region r = dr.getTransparentRegion();
15846 final Rect db = dr.getBounds();
15847 final AttachInfo attachInfo = mAttachInfo;
15848 if (r != null && attachInfo != null) {
15849 final int w = getRight()-getLeft();
15850 final int h = getBottom()-getTop();
15851 if (db.left > 0) {
15852 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15853 r.op(0, 0, db.left, h, Region.Op.UNION);
15854 }
15855 if (db.right < w) {
15856 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15857 r.op(db.right, 0, w, h, Region.Op.UNION);
15858 }
15859 if (db.top > 0) {
15860 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15861 r.op(0, 0, w, db.top, Region.Op.UNION);
15862 }
15863 if (db.bottom < h) {
15864 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15865 r.op(0, db.bottom, w, h, Region.Op.UNION);
15866 }
15867 final int[] location = attachInfo.mTransparentLocation;
15868 getLocationInWindow(location);
15869 r.translate(location[0], location[1]);
15870 region.op(r, Region.Op.INTERSECT);
15871 } else {
15872 region.op(db, Region.Op.DIFFERENCE);
15873 }
15874 }
15875
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015876 private void checkForLongClick(int delayOffset) {
15877 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15878 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015879
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015880 if (mPendingCheckForLongPress == null) {
15881 mPendingCheckForLongPress = new CheckForLongPress();
15882 }
15883 mPendingCheckForLongPress.rememberWindowAttachCount();
15884 postDelayed(mPendingCheckForLongPress,
15885 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015887 }
15888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015889 /**
15890 * Inflate a view from an XML resource. This convenience method wraps the {@link
15891 * LayoutInflater} class, which provides a full range of options for view inflation.
15892 *
15893 * @param context The Context object for your activity or application.
15894 * @param resource The resource ID to inflate
15895 * @param root A view group that will be the parent. Used to properly inflate the
15896 * layout_* parameters.
15897 * @see LayoutInflater
15898 */
15899 public static View inflate(Context context, int resource, ViewGroup root) {
15900 LayoutInflater factory = LayoutInflater.from(context);
15901 return factory.inflate(resource, root);
15902 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015904 /**
Adam Powell637d3372010-08-25 14:37:03 -070015905 * Scroll the view with standard behavior for scrolling beyond the normal
15906 * content boundaries. Views that call this method should override
15907 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15908 * results of an over-scroll operation.
15909 *
15910 * Views can use this method to handle any touch or fling-based scrolling.
15911 *
15912 * @param deltaX Change in X in pixels
15913 * @param deltaY Change in Y in pixels
15914 * @param scrollX Current X scroll value in pixels before applying deltaX
15915 * @param scrollY Current Y scroll value in pixels before applying deltaY
15916 * @param scrollRangeX Maximum content scroll range along the X axis
15917 * @param scrollRangeY Maximum content scroll range along the Y axis
15918 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15919 * along the X axis.
15920 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15921 * along the Y axis.
15922 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15923 * @return true if scrolling was clamped to an over-scroll boundary along either
15924 * axis, false otherwise.
15925 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015926 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015927 protected boolean overScrollBy(int deltaX, int deltaY,
15928 int scrollX, int scrollY,
15929 int scrollRangeX, int scrollRangeY,
15930 int maxOverScrollX, int maxOverScrollY,
15931 boolean isTouchEvent) {
15932 final int overScrollMode = mOverScrollMode;
15933 final boolean canScrollHorizontal =
15934 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15935 final boolean canScrollVertical =
15936 computeVerticalScrollRange() > computeVerticalScrollExtent();
15937 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15938 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15939 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15940 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15941
15942 int newScrollX = scrollX + deltaX;
15943 if (!overScrollHorizontal) {
15944 maxOverScrollX = 0;
15945 }
15946
15947 int newScrollY = scrollY + deltaY;
15948 if (!overScrollVertical) {
15949 maxOverScrollY = 0;
15950 }
15951
15952 // Clamp values if at the limits and record
15953 final int left = -maxOverScrollX;
15954 final int right = maxOverScrollX + scrollRangeX;
15955 final int top = -maxOverScrollY;
15956 final int bottom = maxOverScrollY + scrollRangeY;
15957
15958 boolean clampedX = false;
15959 if (newScrollX > right) {
15960 newScrollX = right;
15961 clampedX = true;
15962 } else if (newScrollX < left) {
15963 newScrollX = left;
15964 clampedX = true;
15965 }
15966
15967 boolean clampedY = false;
15968 if (newScrollY > bottom) {
15969 newScrollY = bottom;
15970 clampedY = true;
15971 } else if (newScrollY < top) {
15972 newScrollY = top;
15973 clampedY = true;
15974 }
15975
15976 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15977
15978 return clampedX || clampedY;
15979 }
15980
15981 /**
15982 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15983 * respond to the results of an over-scroll operation.
15984 *
15985 * @param scrollX New X scroll value in pixels
15986 * @param scrollY New Y scroll value in pixels
15987 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15988 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15989 */
15990 protected void onOverScrolled(int scrollX, int scrollY,
15991 boolean clampedX, boolean clampedY) {
15992 // Intentionally empty.
15993 }
15994
15995 /**
15996 * Returns the over-scroll mode for this view. The result will be
15997 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15998 * (allow over-scrolling only if the view content is larger than the container),
15999 * or {@link #OVER_SCROLL_NEVER}.
16000 *
16001 * @return This view's over-scroll mode.
16002 */
16003 public int getOverScrollMode() {
16004 return mOverScrollMode;
16005 }
16006
16007 /**
16008 * Set the over-scroll mode for this view. Valid over-scroll modes are
16009 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16010 * (allow over-scrolling only if the view content is larger than the container),
16011 * or {@link #OVER_SCROLL_NEVER}.
16012 *
16013 * Setting the over-scroll mode of a view will have an effect only if the
16014 * view is capable of scrolling.
16015 *
16016 * @param overScrollMode The new over-scroll mode for this view.
16017 */
16018 public void setOverScrollMode(int overScrollMode) {
16019 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16020 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16021 overScrollMode != OVER_SCROLL_NEVER) {
16022 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16023 }
16024 mOverScrollMode = overScrollMode;
16025 }
16026
16027 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016028 * Gets a scale factor that determines the distance the view should scroll
16029 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16030 * @return The vertical scroll scale factor.
16031 * @hide
16032 */
16033 protected float getVerticalScrollFactor() {
16034 if (mVerticalScrollFactor == 0) {
16035 TypedValue outValue = new TypedValue();
16036 if (!mContext.getTheme().resolveAttribute(
16037 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16038 throw new IllegalStateException(
16039 "Expected theme to define listPreferredItemHeight.");
16040 }
16041 mVerticalScrollFactor = outValue.getDimension(
16042 mContext.getResources().getDisplayMetrics());
16043 }
16044 return mVerticalScrollFactor;
16045 }
16046
16047 /**
16048 * Gets a scale factor that determines the distance the view should scroll
16049 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16050 * @return The horizontal scroll scale factor.
16051 * @hide
16052 */
16053 protected float getHorizontalScrollFactor() {
16054 // TODO: Should use something else.
16055 return getVerticalScrollFactor();
16056 }
16057
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016058 /**
16059 * Return the value specifying the text direction or policy that was set with
16060 * {@link #setTextDirection(int)}.
16061 *
16062 * @return the defined text direction. It can be one of:
16063 *
16064 * {@link #TEXT_DIRECTION_INHERIT},
16065 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16066 * {@link #TEXT_DIRECTION_ANY_RTL},
16067 * {@link #TEXT_DIRECTION_LTR},
16068 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016069 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016070 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016071 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016072 @ViewDebug.ExportedProperty(category = "text", mapping = {
16073 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16074 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16075 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16076 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16077 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16078 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16079 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016080 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016081 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016082 }
16083
16084 /**
16085 * Set the text direction.
16086 *
16087 * @param textDirection the direction to set. Should be one of:
16088 *
16089 * {@link #TEXT_DIRECTION_INHERIT},
16090 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16091 * {@link #TEXT_DIRECTION_ANY_RTL},
16092 * {@link #TEXT_DIRECTION_LTR},
16093 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016094 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016095 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016096 */
16097 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016098 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016099 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016100 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016101 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016102 // Set the new text direction
16103 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016104 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016105 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016106 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016107 }
16108 }
16109
16110 /**
16111 * Return the resolved text direction.
16112 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016113 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16114 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16115 * up the parent chain of the view. if there is no parent, then it will return the default
16116 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16117 *
16118 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016119 *
Doug Feltcb3791202011-07-07 11:57:48 -070016120 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16121 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016122 * {@link #TEXT_DIRECTION_LTR},
16123 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016124 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016125 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016126 */
16127 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016128 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016129 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016130 resolveTextDirection();
16131 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016132 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016133 }
16134
16135 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016136 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16137 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016138 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016139 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016140 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016141 // Reset any previous text direction resolution
16142 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16143
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016144 if (hasRtlSupport()) {
16145 // Set resolved text direction flag depending on text direction flag
16146 final int textDirection = getTextDirection();
16147 switch(textDirection) {
16148 case TEXT_DIRECTION_INHERIT:
16149 if (canResolveTextDirection()) {
16150 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016151
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016152 // Set current resolved direction to the same value as the parent's one
16153 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16154 switch (parentResolvedDirection) {
16155 case TEXT_DIRECTION_FIRST_STRONG:
16156 case TEXT_DIRECTION_ANY_RTL:
16157 case TEXT_DIRECTION_LTR:
16158 case TEXT_DIRECTION_RTL:
16159 case TEXT_DIRECTION_LOCALE:
16160 mPrivateFlags2 |=
16161 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16162 break;
16163 default:
16164 // Default resolved direction is "first strong" heuristic
16165 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16166 }
16167 } else {
16168 // We cannot do the resolution if there is no parent, so use the default one
16169 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016170 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016171 break;
16172 case TEXT_DIRECTION_FIRST_STRONG:
16173 case TEXT_DIRECTION_ANY_RTL:
16174 case TEXT_DIRECTION_LTR:
16175 case TEXT_DIRECTION_RTL:
16176 case TEXT_DIRECTION_LOCALE:
16177 // Resolved direction is the same as text direction
16178 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16179 break;
16180 default:
16181 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016182 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016183 }
16184 } else {
16185 // Default resolved direction is "first strong" heuristic
16186 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016187 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016188
16189 // Set to resolved
16190 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016191 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016192 }
16193
16194 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016195 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016196 * resolution should override this method.
16197 *
16198 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016199 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016200 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016201 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016202 }
16203
16204 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016205 * Check if text direction resolution can be done.
16206 *
16207 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016208 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016209 */
16210 public boolean canResolveTextDirection() {
16211 switch (getTextDirection()) {
16212 case TEXT_DIRECTION_INHERIT:
16213 return (mParent != null) && (mParent instanceof ViewGroup);
16214 default:
16215 return true;
16216 }
16217 }
16218
16219 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016220 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016221 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016222 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016223 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016224 */
16225 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016226 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016227 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016228 }
16229
16230 /**
16231 * Called when text direction is reset. Subclasses that care about text direction reset should
16232 * override this method and do a reset of the text direction of their children. The default
16233 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016234 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016235 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016236 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016237 }
16238
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016239 /**
16240 * Return the value specifying the text alignment or policy that was set with
16241 * {@link #setTextAlignment(int)}.
16242 *
16243 * @return the defined text alignment. It can be one of:
16244 *
16245 * {@link #TEXT_ALIGNMENT_INHERIT},
16246 * {@link #TEXT_ALIGNMENT_GRAVITY},
16247 * {@link #TEXT_ALIGNMENT_CENTER},
16248 * {@link #TEXT_ALIGNMENT_TEXT_START},
16249 * {@link #TEXT_ALIGNMENT_TEXT_END},
16250 * {@link #TEXT_ALIGNMENT_VIEW_START},
16251 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016252 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016253 */
16254 @ViewDebug.ExportedProperty(category = "text", mapping = {
16255 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16256 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16257 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16258 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16259 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16260 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16261 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16262 })
16263 public int getTextAlignment() {
16264 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16265 }
16266
16267 /**
16268 * Set the text alignment.
16269 *
16270 * @param textAlignment The text alignment to set. Should be one of
16271 *
16272 * {@link #TEXT_ALIGNMENT_INHERIT},
16273 * {@link #TEXT_ALIGNMENT_GRAVITY},
16274 * {@link #TEXT_ALIGNMENT_CENTER},
16275 * {@link #TEXT_ALIGNMENT_TEXT_START},
16276 * {@link #TEXT_ALIGNMENT_TEXT_END},
16277 * {@link #TEXT_ALIGNMENT_VIEW_START},
16278 * {@link #TEXT_ALIGNMENT_VIEW_END}
16279 *
16280 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016281 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016282 */
16283 public void setTextAlignment(int textAlignment) {
16284 if (textAlignment != getTextAlignment()) {
16285 // Reset the current and resolved text alignment
16286 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16287 resetResolvedTextAlignment();
16288 // Set the new text alignment
16289 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16290 // Refresh
16291 requestLayout();
16292 invalidate(true);
16293 }
16294 }
16295
16296 /**
16297 * Return the resolved text alignment.
16298 *
16299 * The resolved text alignment. This needs resolution if the value is
16300 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16301 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16302 *
16303 * @return the resolved text alignment. Returns one of:
16304 *
16305 * {@link #TEXT_ALIGNMENT_GRAVITY},
16306 * {@link #TEXT_ALIGNMENT_CENTER},
16307 * {@link #TEXT_ALIGNMENT_TEXT_START},
16308 * {@link #TEXT_ALIGNMENT_TEXT_END},
16309 * {@link #TEXT_ALIGNMENT_VIEW_START},
16310 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016311 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016312 */
16313 @ViewDebug.ExportedProperty(category = "text", mapping = {
16314 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16315 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16316 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16317 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16318 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16319 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16320 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16321 })
16322 public int getResolvedTextAlignment() {
16323 // If text alignment is not resolved, then resolve it
16324 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16325 resolveTextAlignment();
16326 }
16327 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16328 }
16329
16330 /**
16331 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16332 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016333 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016334 */
16335 public void resolveTextAlignment() {
16336 // Reset any previous text alignment resolution
16337 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16338
16339 if (hasRtlSupport()) {
16340 // Set resolved text alignment flag depending on text alignment flag
16341 final int textAlignment = getTextAlignment();
16342 switch (textAlignment) {
16343 case TEXT_ALIGNMENT_INHERIT:
16344 // Check if we can resolve the text alignment
16345 if (canResolveLayoutDirection() && mParent instanceof View) {
16346 View view = (View) mParent;
16347
16348 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16349 switch (parentResolvedTextAlignment) {
16350 case TEXT_ALIGNMENT_GRAVITY:
16351 case TEXT_ALIGNMENT_TEXT_START:
16352 case TEXT_ALIGNMENT_TEXT_END:
16353 case TEXT_ALIGNMENT_CENTER:
16354 case TEXT_ALIGNMENT_VIEW_START:
16355 case TEXT_ALIGNMENT_VIEW_END:
16356 // Resolved text alignment is the same as the parent resolved
16357 // text alignment
16358 mPrivateFlags2 |=
16359 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16360 break;
16361 default:
16362 // Use default resolved text alignment
16363 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16364 }
16365 }
16366 else {
16367 // We cannot do the resolution if there is no parent so use the default
16368 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16369 }
16370 break;
16371 case TEXT_ALIGNMENT_GRAVITY:
16372 case TEXT_ALIGNMENT_TEXT_START:
16373 case TEXT_ALIGNMENT_TEXT_END:
16374 case TEXT_ALIGNMENT_CENTER:
16375 case TEXT_ALIGNMENT_VIEW_START:
16376 case TEXT_ALIGNMENT_VIEW_END:
16377 // Resolved text alignment is the same as text alignment
16378 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16379 break;
16380 default:
16381 // Use default resolved text alignment
16382 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16383 }
16384 } else {
16385 // Use default resolved text alignment
16386 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16387 }
16388
16389 // Set the resolved
16390 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16391 onResolvedTextAlignmentChanged();
16392 }
16393
16394 /**
16395 * Check if text alignment resolution can be done.
16396 *
16397 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016398 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016399 */
16400 public boolean canResolveTextAlignment() {
16401 switch (getTextAlignment()) {
16402 case TEXT_DIRECTION_INHERIT:
16403 return (mParent != null);
16404 default:
16405 return true;
16406 }
16407 }
16408
16409 /**
16410 * Called when text alignment has been resolved. Subclasses that care about text alignment
16411 * resolution should override this method.
16412 *
16413 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016414 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016415 */
16416 public void onResolvedTextAlignmentChanged() {
16417 }
16418
16419 /**
16420 * Reset resolved text alignment. Text alignment can be resolved with a call to
16421 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16422 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016423 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016424 */
16425 public void resetResolvedTextAlignment() {
16426 // Reset any previous text alignment resolution
16427 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16428 onResolvedTextAlignmentReset();
16429 }
16430
16431 /**
16432 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16433 * override this method and do a reset of the text alignment of their children. The default
16434 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016435 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016436 */
16437 public void onResolvedTextAlignmentReset() {
16438 }
16439
Chet Haaseb39f0512011-05-24 14:36:40 -070016440 //
16441 // Properties
16442 //
16443 /**
16444 * A Property wrapper around the <code>alpha</code> functionality handled by the
16445 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16446 */
Chet Haased47f1532011-12-16 11:18:52 -080016447 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016448 @Override
16449 public void setValue(View object, float value) {
16450 object.setAlpha(value);
16451 }
16452
16453 @Override
16454 public Float get(View object) {
16455 return object.getAlpha();
16456 }
16457 };
16458
16459 /**
16460 * A Property wrapper around the <code>translationX</code> functionality handled by the
16461 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16462 */
Chet Haased47f1532011-12-16 11:18:52 -080016463 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016464 @Override
16465 public void setValue(View object, float value) {
16466 object.setTranslationX(value);
16467 }
16468
16469 @Override
16470 public Float get(View object) {
16471 return object.getTranslationX();
16472 }
16473 };
16474
16475 /**
16476 * A Property wrapper around the <code>translationY</code> functionality handled by the
16477 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16478 */
Chet Haased47f1532011-12-16 11:18:52 -080016479 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016480 @Override
16481 public void setValue(View object, float value) {
16482 object.setTranslationY(value);
16483 }
16484
16485 @Override
16486 public Float get(View object) {
16487 return object.getTranslationY();
16488 }
16489 };
16490
16491 /**
16492 * A Property wrapper around the <code>x</code> functionality handled by the
16493 * {@link View#setX(float)} and {@link View#getX()} methods.
16494 */
Chet Haased47f1532011-12-16 11:18:52 -080016495 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016496 @Override
16497 public void setValue(View object, float value) {
16498 object.setX(value);
16499 }
16500
16501 @Override
16502 public Float get(View object) {
16503 return object.getX();
16504 }
16505 };
16506
16507 /**
16508 * A Property wrapper around the <code>y</code> functionality handled by the
16509 * {@link View#setY(float)} and {@link View#getY()} methods.
16510 */
Chet Haased47f1532011-12-16 11:18:52 -080016511 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016512 @Override
16513 public void setValue(View object, float value) {
16514 object.setY(value);
16515 }
16516
16517 @Override
16518 public Float get(View object) {
16519 return object.getY();
16520 }
16521 };
16522
16523 /**
16524 * A Property wrapper around the <code>rotation</code> functionality handled by the
16525 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16526 */
Chet Haased47f1532011-12-16 11:18:52 -080016527 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016528 @Override
16529 public void setValue(View object, float value) {
16530 object.setRotation(value);
16531 }
16532
16533 @Override
16534 public Float get(View object) {
16535 return object.getRotation();
16536 }
16537 };
16538
16539 /**
16540 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16541 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16542 */
Chet Haased47f1532011-12-16 11:18:52 -080016543 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016544 @Override
16545 public void setValue(View object, float value) {
16546 object.setRotationX(value);
16547 }
16548
16549 @Override
16550 public Float get(View object) {
16551 return object.getRotationX();
16552 }
16553 };
16554
16555 /**
16556 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16557 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16558 */
Chet Haased47f1532011-12-16 11:18:52 -080016559 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016560 @Override
16561 public void setValue(View object, float value) {
16562 object.setRotationY(value);
16563 }
16564
16565 @Override
16566 public Float get(View object) {
16567 return object.getRotationY();
16568 }
16569 };
16570
16571 /**
16572 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16573 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16574 */
Chet Haased47f1532011-12-16 11:18:52 -080016575 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016576 @Override
16577 public void setValue(View object, float value) {
16578 object.setScaleX(value);
16579 }
16580
16581 @Override
16582 public Float get(View object) {
16583 return object.getScaleX();
16584 }
16585 };
16586
16587 /**
16588 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16589 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16590 */
Chet Haased47f1532011-12-16 11:18:52 -080016591 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016592 @Override
16593 public void setValue(View object, float value) {
16594 object.setScaleY(value);
16595 }
16596
16597 @Override
16598 public Float get(View object) {
16599 return object.getScaleY();
16600 }
16601 };
16602
Jeff Brown33bbfd22011-02-24 20:55:35 -080016603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016604 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16605 * Each MeasureSpec represents a requirement for either the width or the height.
16606 * A MeasureSpec is comprised of a size and a mode. There are three possible
16607 * modes:
16608 * <dl>
16609 * <dt>UNSPECIFIED</dt>
16610 * <dd>
16611 * The parent has not imposed any constraint on the child. It can be whatever size
16612 * it wants.
16613 * </dd>
16614 *
16615 * <dt>EXACTLY</dt>
16616 * <dd>
16617 * The parent has determined an exact size for the child. The child is going to be
16618 * given those bounds regardless of how big it wants to be.
16619 * </dd>
16620 *
16621 * <dt>AT_MOST</dt>
16622 * <dd>
16623 * The child can be as large as it wants up to the specified size.
16624 * </dd>
16625 * </dl>
16626 *
16627 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16628 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16629 */
16630 public static class MeasureSpec {
16631 private static final int MODE_SHIFT = 30;
16632 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16633
16634 /**
16635 * Measure specification mode: The parent has not imposed any constraint
16636 * on the child. It can be whatever size it wants.
16637 */
16638 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16639
16640 /**
16641 * Measure specification mode: The parent has determined an exact size
16642 * for the child. The child is going to be given those bounds regardless
16643 * of how big it wants to be.
16644 */
16645 public static final int EXACTLY = 1 << MODE_SHIFT;
16646
16647 /**
16648 * Measure specification mode: The child can be as large as it wants up
16649 * to the specified size.
16650 */
16651 public static final int AT_MOST = 2 << MODE_SHIFT;
16652
16653 /**
16654 * Creates a measure specification based on the supplied size and mode.
16655 *
16656 * The mode must always be one of the following:
16657 * <ul>
16658 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16659 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16660 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16661 * </ul>
16662 *
16663 * @param size the size of the measure specification
16664 * @param mode the mode of the measure specification
16665 * @return the measure specification based on size and mode
16666 */
16667 public static int makeMeasureSpec(int size, int mode) {
16668 return size + mode;
16669 }
16670
16671 /**
16672 * Extracts the mode from the supplied measure specification.
16673 *
16674 * @param measureSpec the measure specification to extract the mode from
16675 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16676 * {@link android.view.View.MeasureSpec#AT_MOST} or
16677 * {@link android.view.View.MeasureSpec#EXACTLY}
16678 */
16679 public static int getMode(int measureSpec) {
16680 return (measureSpec & MODE_MASK);
16681 }
16682
16683 /**
16684 * Extracts the size from the supplied measure specification.
16685 *
16686 * @param measureSpec the measure specification to extract the size from
16687 * @return the size in pixels defined in the supplied measure specification
16688 */
16689 public static int getSize(int measureSpec) {
16690 return (measureSpec & ~MODE_MASK);
16691 }
16692
16693 /**
16694 * Returns a String representation of the specified measure
16695 * specification.
16696 *
16697 * @param measureSpec the measure specification to convert to a String
16698 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16699 */
16700 public static String toString(int measureSpec) {
16701 int mode = getMode(measureSpec);
16702 int size = getSize(measureSpec);
16703
16704 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16705
16706 if (mode == UNSPECIFIED)
16707 sb.append("UNSPECIFIED ");
16708 else if (mode == EXACTLY)
16709 sb.append("EXACTLY ");
16710 else if (mode == AT_MOST)
16711 sb.append("AT_MOST ");
16712 else
16713 sb.append(mode).append(" ");
16714
16715 sb.append(size);
16716 return sb.toString();
16717 }
16718 }
16719
16720 class CheckForLongPress implements Runnable {
16721
16722 private int mOriginalWindowAttachCount;
16723
16724 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016725 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016726 && mOriginalWindowAttachCount == mWindowAttachCount) {
16727 if (performLongClick()) {
16728 mHasPerformedLongPress = true;
16729 }
16730 }
16731 }
16732
16733 public void rememberWindowAttachCount() {
16734 mOriginalWindowAttachCount = mWindowAttachCount;
16735 }
16736 }
Joe Malin32736f02011-01-19 16:14:20 -080016737
Adam Powelle14579b2009-12-16 18:39:52 -080016738 private final class CheckForTap implements Runnable {
16739 public void run() {
16740 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016741 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016742 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016743 }
16744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016745
Adam Powella35d7682010-03-12 14:48:13 -080016746 private final class PerformClick implements Runnable {
16747 public void run() {
16748 performClick();
16749 }
16750 }
16751
Dianne Hackborn63042d62011-01-26 18:56:29 -080016752 /** @hide */
16753 public void hackTurnOffWindowResizeAnim(boolean off) {
16754 mAttachInfo.mTurnOffWindowResizeAnim = off;
16755 }
Joe Malin32736f02011-01-19 16:14:20 -080016756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016757 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016758 * This method returns a ViewPropertyAnimator object, which can be used to animate
16759 * specific properties on this View.
16760 *
16761 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16762 */
16763 public ViewPropertyAnimator animate() {
16764 if (mAnimator == null) {
16765 mAnimator = new ViewPropertyAnimator(this);
16766 }
16767 return mAnimator;
16768 }
16769
16770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016771 * Interface definition for a callback to be invoked when a key event is
16772 * dispatched to this view. The callback will be invoked before the key
16773 * event is given to the view.
16774 */
16775 public interface OnKeyListener {
16776 /**
16777 * Called when a key is dispatched to a view. This allows listeners to
16778 * get a chance to respond before the target view.
16779 *
16780 * @param v The view the key has been dispatched to.
16781 * @param keyCode The code for the physical key that was pressed
16782 * @param event The KeyEvent object containing full information about
16783 * the event.
16784 * @return True if the listener has consumed the event, false otherwise.
16785 */
16786 boolean onKey(View v, int keyCode, KeyEvent event);
16787 }
16788
16789 /**
16790 * Interface definition for a callback to be invoked when a touch event is
16791 * dispatched to this view. The callback will be invoked before the touch
16792 * event is given to the view.
16793 */
16794 public interface OnTouchListener {
16795 /**
16796 * Called when a touch event is dispatched to a view. This allows listeners to
16797 * get a chance to respond before the target view.
16798 *
16799 * @param v The view the touch event has been dispatched to.
16800 * @param event The MotionEvent object containing full information about
16801 * the event.
16802 * @return True if the listener has consumed the event, false otherwise.
16803 */
16804 boolean onTouch(View v, MotionEvent event);
16805 }
16806
16807 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016808 * Interface definition for a callback to be invoked when a hover event is
16809 * dispatched to this view. The callback will be invoked before the hover
16810 * event is given to the view.
16811 */
16812 public interface OnHoverListener {
16813 /**
16814 * Called when a hover event is dispatched to a view. This allows listeners to
16815 * get a chance to respond before the target view.
16816 *
16817 * @param v The view the hover event has been dispatched to.
16818 * @param event The MotionEvent object containing full information about
16819 * the event.
16820 * @return True if the listener has consumed the event, false otherwise.
16821 */
16822 boolean onHover(View v, MotionEvent event);
16823 }
16824
16825 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016826 * Interface definition for a callback to be invoked when a generic motion event is
16827 * dispatched to this view. The callback will be invoked before the generic motion
16828 * event is given to the view.
16829 */
16830 public interface OnGenericMotionListener {
16831 /**
16832 * Called when a generic motion event is dispatched to a view. This allows listeners to
16833 * get a chance to respond before the target view.
16834 *
16835 * @param v The view the generic motion event has been dispatched to.
16836 * @param event The MotionEvent object containing full information about
16837 * the event.
16838 * @return True if the listener has consumed the event, false otherwise.
16839 */
16840 boolean onGenericMotion(View v, MotionEvent event);
16841 }
16842
16843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016844 * Interface definition for a callback to be invoked when a view has been clicked and held.
16845 */
16846 public interface OnLongClickListener {
16847 /**
16848 * Called when a view has been clicked and held.
16849 *
16850 * @param v The view that was clicked and held.
16851 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016852 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016853 */
16854 boolean onLongClick(View v);
16855 }
16856
16857 /**
Chris Tate32affef2010-10-18 15:29:21 -070016858 * Interface definition for a callback to be invoked when a drag is being dispatched
16859 * to this view. The callback will be invoked before the hosting view's own
16860 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16861 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016862 *
16863 * <div class="special reference">
16864 * <h3>Developer Guides</h3>
16865 * <p>For a guide to implementing drag and drop features, read the
16866 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16867 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016868 */
16869 public interface OnDragListener {
16870 /**
16871 * Called when a drag event is dispatched to a view. This allows listeners
16872 * to get a chance to override base View behavior.
16873 *
Joe Malin32736f02011-01-19 16:14:20 -080016874 * @param v The View that received the drag event.
16875 * @param event The {@link android.view.DragEvent} object for the drag event.
16876 * @return {@code true} if the drag event was handled successfully, or {@code false}
16877 * if the drag event was not handled. Note that {@code false} will trigger the View
16878 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016879 */
16880 boolean onDrag(View v, DragEvent event);
16881 }
16882
16883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016884 * Interface definition for a callback to be invoked when the focus state of
16885 * a view changed.
16886 */
16887 public interface OnFocusChangeListener {
16888 /**
16889 * Called when the focus state of a view has changed.
16890 *
16891 * @param v The view whose state has changed.
16892 * @param hasFocus The new focus state of v.
16893 */
16894 void onFocusChange(View v, boolean hasFocus);
16895 }
16896
16897 /**
16898 * Interface definition for a callback to be invoked when a view is clicked.
16899 */
16900 public interface OnClickListener {
16901 /**
16902 * Called when a view has been clicked.
16903 *
16904 * @param v The view that was clicked.
16905 */
16906 void onClick(View v);
16907 }
16908
16909 /**
16910 * Interface definition for a callback to be invoked when the context menu
16911 * for this view is being built.
16912 */
16913 public interface OnCreateContextMenuListener {
16914 /**
16915 * Called when the context menu for this view is being built. It is not
16916 * safe to hold onto the menu after this method returns.
16917 *
16918 * @param menu The context menu that is being built
16919 * @param v The view for which the context menu is being built
16920 * @param menuInfo Extra information about the item for which the
16921 * context menu should be shown. This information will vary
16922 * depending on the class of v.
16923 */
16924 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16925 }
16926
Joe Onorato664644d2011-01-23 17:53:23 -080016927 /**
16928 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016929 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016930 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016931 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016932 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016933 */
16934 public interface OnSystemUiVisibilityChangeListener {
16935 /**
16936 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016937 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016938 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016939 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16940 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16941 * This tells you the <strong>global</strong> state of these UI visibility
16942 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016943 */
16944 public void onSystemUiVisibilityChange(int visibility);
16945 }
16946
Adam Powell4afd62b2011-02-18 15:02:18 -080016947 /**
16948 * Interface definition for a callback to be invoked when this view is attached
16949 * or detached from its window.
16950 */
16951 public interface OnAttachStateChangeListener {
16952 /**
16953 * Called when the view is attached to a window.
16954 * @param v The view that was attached
16955 */
16956 public void onViewAttachedToWindow(View v);
16957 /**
16958 * Called when the view is detached from a window.
16959 * @param v The view that was detached
16960 */
16961 public void onViewDetachedFromWindow(View v);
16962 }
16963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016964 private final class UnsetPressedState implements Runnable {
16965 public void run() {
16966 setPressed(false);
16967 }
16968 }
16969
16970 /**
16971 * Base class for derived classes that want to save and restore their own
16972 * state in {@link android.view.View#onSaveInstanceState()}.
16973 */
16974 public static class BaseSavedState extends AbsSavedState {
16975 /**
16976 * Constructor used when reading from a parcel. Reads the state of the superclass.
16977 *
16978 * @param source
16979 */
16980 public BaseSavedState(Parcel source) {
16981 super(source);
16982 }
16983
16984 /**
16985 * Constructor called by derived classes when creating their SavedState objects
16986 *
16987 * @param superState The state of the superclass of this view
16988 */
16989 public BaseSavedState(Parcelable superState) {
16990 super(superState);
16991 }
16992
16993 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16994 new Parcelable.Creator<BaseSavedState>() {
16995 public BaseSavedState createFromParcel(Parcel in) {
16996 return new BaseSavedState(in);
16997 }
16998
16999 public BaseSavedState[] newArray(int size) {
17000 return new BaseSavedState[size];
17001 }
17002 };
17003 }
17004
17005 /**
17006 * A set of information given to a view when it is attached to its parent
17007 * window.
17008 */
17009 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017010 interface Callbacks {
17011 void playSoundEffect(int effectId);
17012 boolean performHapticFeedback(int effectId, boolean always);
17013 }
17014
17015 /**
17016 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17017 * to a Handler. This class contains the target (View) to invalidate and
17018 * the coordinates of the dirty rectangle.
17019 *
17020 * For performance purposes, this class also implements a pool of up to
17021 * POOL_LIMIT objects that get reused. This reduces memory allocations
17022 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017023 */
Romain Guyd928d682009-03-31 17:52:16 -070017024 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017025 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017026 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17027 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017028 public InvalidateInfo newInstance() {
17029 return new InvalidateInfo();
17030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017031
Romain Guyd928d682009-03-31 17:52:16 -070017032 public void onAcquired(InvalidateInfo element) {
17033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034
Romain Guyd928d682009-03-31 17:52:16 -070017035 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017036 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017037 }
17038 }, POOL_LIMIT)
17039 );
17040
17041 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017042 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017043
17044 View target;
17045
17046 int left;
17047 int top;
17048 int right;
17049 int bottom;
17050
Romain Guyd928d682009-03-31 17:52:16 -070017051 public void setNextPoolable(InvalidateInfo element) {
17052 mNext = element;
17053 }
17054
17055 public InvalidateInfo getNextPoolable() {
17056 return mNext;
17057 }
17058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017059 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017060 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017061 }
17062
17063 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017064 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017065 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017066
17067 public boolean isPooled() {
17068 return mIsPooled;
17069 }
17070
17071 public void setPooled(boolean isPooled) {
17072 mIsPooled = isPooled;
17073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017074 }
17075
17076 final IWindowSession mSession;
17077
17078 final IWindow mWindow;
17079
17080 final IBinder mWindowToken;
17081
17082 final Callbacks mRootCallbacks;
17083
Romain Guy59a12ca2011-06-09 17:48:21 -070017084 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017086 /**
17087 * The top view of the hierarchy.
17088 */
17089 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017091 IBinder mPanelParentWindowToken;
17092 Surface mSurface;
17093
Romain Guyb051e892010-09-28 19:09:36 -070017094 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017095 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017096 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017097
Romain Guy7e4e5612012-03-05 14:37:29 -080017098 boolean mScreenOn;
17099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017100 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017101 * Scale factor used by the compatibility mode
17102 */
17103 float mApplicationScale;
17104
17105 /**
17106 * Indicates whether the application is in compatibility mode
17107 */
17108 boolean mScalingRequired;
17109
17110 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017111 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017112 */
17113 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017114
Dianne Hackborn63042d62011-01-26 18:56:29 -080017115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017116 * Left position of this view's window
17117 */
17118 int mWindowLeft;
17119
17120 /**
17121 * Top position of this view's window
17122 */
17123 int mWindowTop;
17124
17125 /**
Adam Powell26153a32010-11-08 15:22:27 -080017126 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017127 */
Adam Powell26153a32010-11-08 15:22:27 -080017128 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017129
17130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017131 * For windows that are full-screen but using insets to layout inside
17132 * of the screen decorations, these are the current insets for the
17133 * content of the window.
17134 */
17135 final Rect mContentInsets = new Rect();
17136
17137 /**
17138 * For windows that are full-screen but using insets to layout inside
17139 * of the screen decorations, these are the current insets for the
17140 * actual visible parts of the window.
17141 */
17142 final Rect mVisibleInsets = new Rect();
17143
17144 /**
17145 * The internal insets given by this window. This value is
17146 * supplied by the client (through
17147 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17148 * be given to the window manager when changed to be used in laying
17149 * out windows behind it.
17150 */
17151 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17152 = new ViewTreeObserver.InternalInsetsInfo();
17153
17154 /**
17155 * All views in the window's hierarchy that serve as scroll containers,
17156 * used to determine if the window can be resized or must be panned
17157 * to adjust for a soft input area.
17158 */
17159 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17160
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017161 final KeyEvent.DispatcherState mKeyDispatchState
17162 = new KeyEvent.DispatcherState();
17163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017164 /**
17165 * Indicates whether the view's window currently has the focus.
17166 */
17167 boolean mHasWindowFocus;
17168
17169 /**
17170 * The current visibility of the window.
17171 */
17172 int mWindowVisibility;
17173
17174 /**
17175 * Indicates the time at which drawing started to occur.
17176 */
17177 long mDrawingTime;
17178
17179 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017180 * Indicates whether or not ignoring the DIRTY_MASK flags.
17181 */
17182 boolean mIgnoreDirtyState;
17183
17184 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017185 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17186 * to avoid clearing that flag prematurely.
17187 */
17188 boolean mSetIgnoreDirtyState = false;
17189
17190 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017191 * Indicates whether the view's window is currently in touch mode.
17192 */
17193 boolean mInTouchMode;
17194
17195 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017196 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017197 * the next time it performs a traversal
17198 */
17199 boolean mRecomputeGlobalAttributes;
17200
17201 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017202 * Always report new attributes at next traversal.
17203 */
17204 boolean mForceReportNewAttributes;
17205
17206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017207 * Set during a traveral if any views want to keep the screen on.
17208 */
17209 boolean mKeepScreenOn;
17210
17211 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017212 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17213 */
17214 int mSystemUiVisibility;
17215
17216 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017217 * Hack to force certain system UI visibility flags to be cleared.
17218 */
17219 int mDisabledSystemUiVisibility;
17220
17221 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017222 * Last global system UI visibility reported by the window manager.
17223 */
17224 int mGlobalSystemUiVisibility;
17225
17226 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017227 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17228 * attached.
17229 */
17230 boolean mHasSystemUiListeners;
17231
17232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017233 * Set if the visibility of any views has changed.
17234 */
17235 boolean mViewVisibilityChanged;
17236
17237 /**
17238 * Set to true if a view has been scrolled.
17239 */
17240 boolean mViewScrollChanged;
17241
17242 /**
17243 * Global to the view hierarchy used as a temporary for dealing with
17244 * x/y points in the transparent region computations.
17245 */
17246 final int[] mTransparentLocation = new int[2];
17247
17248 /**
17249 * Global to the view hierarchy used as a temporary for dealing with
17250 * x/y points in the ViewGroup.invalidateChild implementation.
17251 */
17252 final int[] mInvalidateChildLocation = new int[2];
17253
Chet Haasec3aa3612010-06-17 08:50:37 -070017254
17255 /**
17256 * Global to the view hierarchy used as a temporary for dealing with
17257 * x/y location when view is transformed.
17258 */
17259 final float[] mTmpTransformLocation = new float[2];
17260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017261 /**
17262 * The view tree observer used to dispatch global events like
17263 * layout, pre-draw, touch mode change, etc.
17264 */
17265 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17266
17267 /**
17268 * A Canvas used by the view hierarchy to perform bitmap caching.
17269 */
17270 Canvas mCanvas;
17271
17272 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017273 * The view root impl.
17274 */
17275 final ViewRootImpl mViewRootImpl;
17276
17277 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017278 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017279 * handler can be used to pump events in the UI events queue.
17280 */
17281 final Handler mHandler;
17282
17283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284 * Temporary for use in computing invalidate rectangles while
17285 * calling up the hierarchy.
17286 */
17287 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017288
17289 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017290 * Temporary for use in computing hit areas with transformed views
17291 */
17292 final RectF mTmpTransformRect = new RectF();
17293
17294 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017295 * Temporary list for use in collecting focusable descendents of a view.
17296 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017297 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017299 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017300 * The id of the window for accessibility purposes.
17301 */
17302 int mAccessibilityWindowId = View.NO_ID;
17303
17304 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017305 * Whether to ingore not exposed for accessibility Views when
17306 * reporting the view tree to accessibility services.
17307 */
17308 boolean mIncludeNotImportantViews;
17309
17310 /**
17311 * The drawable for highlighting accessibility focus.
17312 */
17313 Drawable mAccessibilityFocusDrawable;
17314
17315 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017316 * Show where the margins, bounds and layout bounds are for each view.
17317 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017318 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017319
17320 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017321 * Point used to compute visible regions.
17322 */
17323 final Point mPoint = new Point();
17324
17325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017326 * Creates a new set of attachment information with the specified
17327 * events handler and thread.
17328 *
17329 * @param handler the events handler the view must use
17330 */
17331 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017332 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017333 mSession = session;
17334 mWindow = window;
17335 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017336 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017337 mHandler = handler;
17338 mRootCallbacks = effectPlayer;
17339 }
17340 }
17341
17342 /**
17343 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17344 * is supported. This avoids keeping too many unused fields in most
17345 * instances of View.</p>
17346 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017347 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017348
Mike Cleronf116bf82009-09-27 19:14:12 -070017349 /**
17350 * Scrollbars are not visible
17351 */
17352 public static final int OFF = 0;
17353
17354 /**
17355 * Scrollbars are visible
17356 */
17357 public static final int ON = 1;
17358
17359 /**
17360 * Scrollbars are fading away
17361 */
17362 public static final int FADING = 2;
17363
17364 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017366 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017367 public int scrollBarDefaultDelayBeforeFade;
17368 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017369
17370 public int scrollBarSize;
17371 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017372 public float[] interpolatorValues;
17373 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017374
17375 public final Paint paint;
17376 public final Matrix matrix;
17377 public Shader shader;
17378
Mike Cleronf116bf82009-09-27 19:14:12 -070017379 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17380
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017381 private static final float[] OPAQUE = { 255 };
17382 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017383
Mike Cleronf116bf82009-09-27 19:14:12 -070017384 /**
17385 * When fading should start. This time moves into the future every time
17386 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17387 */
17388 public long fadeStartTime;
17389
17390
17391 /**
17392 * The current state of the scrollbars: ON, OFF, or FADING
17393 */
17394 public int state = OFF;
17395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017396 private int mLastColor;
17397
Mike Cleronf116bf82009-09-27 19:14:12 -070017398 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017399 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17400 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017401 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17402 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017403
17404 paint = new Paint();
17405 matrix = new Matrix();
17406 // use use a height of 1, and then wack the matrix each time we
17407 // actually use it.
17408 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017410 paint.setShader(shader);
17411 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017412 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017413 }
Romain Guy8506ab42009-06-11 17:35:47 -070017414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017415 public void setFadeColor(int color) {
17416 if (color != 0 && color != mLastColor) {
17417 mLastColor = color;
17418 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017419
Romain Guye55e1a72009-08-27 10:42:26 -070017420 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17421 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423 paint.setShader(shader);
17424 // Restore the default transfer mode (src_over)
17425 paint.setXfermode(null);
17426 }
17427 }
Joe Malin32736f02011-01-19 16:14:20 -080017428
Mike Cleronf116bf82009-09-27 19:14:12 -070017429 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017430 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017431 if (now >= fadeStartTime) {
17432
17433 // the animation fades the scrollbars out by changing
17434 // the opacity (alpha) from fully opaque to fully
17435 // transparent
17436 int nextFrame = (int) now;
17437 int framesCount = 0;
17438
17439 Interpolator interpolator = scrollBarInterpolator;
17440
17441 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017442 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017443
17444 // End transparent
17445 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017446 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017447
17448 state = FADING;
17449
17450 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017451 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017452 }
17453 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017454 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017455
Svetoslav Ganova0156172011-06-26 17:55:44 -070017456 /**
17457 * Resuable callback for sending
17458 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17459 */
17460 private class SendViewScrolledAccessibilityEvent implements Runnable {
17461 public volatile boolean mIsPending;
17462
17463 public void run() {
17464 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17465 mIsPending = false;
17466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017467 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017468
17469 /**
17470 * <p>
17471 * This class represents a delegate that can be registered in a {@link View}
17472 * to enhance accessibility support via composition rather via inheritance.
17473 * It is specifically targeted to widget developers that extend basic View
17474 * classes i.e. classes in package android.view, that would like their
17475 * applications to be backwards compatible.
17476 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017477 * <div class="special reference">
17478 * <h3>Developer Guides</h3>
17479 * <p>For more information about making applications accessible, read the
17480 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17481 * developer guide.</p>
17482 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017483 * <p>
17484 * A scenario in which a developer would like to use an accessibility delegate
17485 * is overriding a method introduced in a later API version then the minimal API
17486 * version supported by the application. For example, the method
17487 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17488 * in API version 4 when the accessibility APIs were first introduced. If a
17489 * developer would like his application to run on API version 4 devices (assuming
17490 * all other APIs used by the application are version 4 or lower) and take advantage
17491 * of this method, instead of overriding the method which would break the application's
17492 * backwards compatibility, he can override the corresponding method in this
17493 * delegate and register the delegate in the target View if the API version of
17494 * the system is high enough i.e. the API version is same or higher to the API
17495 * version that introduced
17496 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17497 * </p>
17498 * <p>
17499 * Here is an example implementation:
17500 * </p>
17501 * <code><pre><p>
17502 * if (Build.VERSION.SDK_INT >= 14) {
17503 * // If the API version is equal of higher than the version in
17504 * // which onInitializeAccessibilityNodeInfo was introduced we
17505 * // register a delegate with a customized implementation.
17506 * View view = findViewById(R.id.view_id);
17507 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17508 * public void onInitializeAccessibilityNodeInfo(View host,
17509 * AccessibilityNodeInfo info) {
17510 * // Let the default implementation populate the info.
17511 * super.onInitializeAccessibilityNodeInfo(host, info);
17512 * // Set some other information.
17513 * info.setEnabled(host.isEnabled());
17514 * }
17515 * });
17516 * }
17517 * </code></pre></p>
17518 * <p>
17519 * This delegate contains methods that correspond to the accessibility methods
17520 * in View. If a delegate has been specified the implementation in View hands
17521 * off handling to the corresponding method in this delegate. The default
17522 * implementation the delegate methods behaves exactly as the corresponding
17523 * method in View for the case of no accessibility delegate been set. Hence,
17524 * to customize the behavior of a View method, clients can override only the
17525 * corresponding delegate method without altering the behavior of the rest
17526 * accessibility related methods of the host view.
17527 * </p>
17528 */
17529 public static class AccessibilityDelegate {
17530
17531 /**
17532 * Sends an accessibility event of the given type. If accessibility is not
17533 * enabled this method has no effect.
17534 * <p>
17535 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17536 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17537 * been set.
17538 * </p>
17539 *
17540 * @param host The View hosting the delegate.
17541 * @param eventType The type of the event to send.
17542 *
17543 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17544 */
17545 public void sendAccessibilityEvent(View host, int eventType) {
17546 host.sendAccessibilityEventInternal(eventType);
17547 }
17548
17549 /**
alanv8eeefef2012-05-07 16:57:53 -070017550 * Performs the specified accessibility action on the view. For
17551 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17552 * <p>
17553 * The default implementation behaves as
17554 * {@link View#performAccessibilityAction(int, Bundle)
17555 * View#performAccessibilityAction(int, Bundle)} for the case of
17556 * no accessibility delegate been set.
17557 * </p>
17558 *
17559 * @param action The action to perform.
17560 * @return Whether the action was performed.
17561 *
17562 * @see View#performAccessibilityAction(int, Bundle)
17563 * View#performAccessibilityAction(int, Bundle)
17564 */
17565 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17566 return host.performAccessibilityActionInternal(action, args);
17567 }
17568
17569 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017570 * Sends an accessibility event. This method behaves exactly as
17571 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17572 * empty {@link AccessibilityEvent} and does not perform a check whether
17573 * accessibility is enabled.
17574 * <p>
17575 * The default implementation behaves as
17576 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17577 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17578 * the case of no accessibility delegate been set.
17579 * </p>
17580 *
17581 * @param host The View hosting the delegate.
17582 * @param event The event to send.
17583 *
17584 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17585 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17586 */
17587 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17588 host.sendAccessibilityEventUncheckedInternal(event);
17589 }
17590
17591 /**
17592 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17593 * to its children for adding their text content to the event.
17594 * <p>
17595 * The default implementation behaves as
17596 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17597 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17598 * the case of no accessibility delegate been set.
17599 * </p>
17600 *
17601 * @param host The View hosting the delegate.
17602 * @param event The event.
17603 * @return True if the event population was completed.
17604 *
17605 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17606 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17607 */
17608 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17609 return host.dispatchPopulateAccessibilityEventInternal(event);
17610 }
17611
17612 /**
17613 * Gives a chance to the host View to populate the accessibility event with its
17614 * text content.
17615 * <p>
17616 * The default implementation behaves as
17617 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17618 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17619 * the case of no accessibility delegate been set.
17620 * </p>
17621 *
17622 * @param host The View hosting the delegate.
17623 * @param event The accessibility event which to populate.
17624 *
17625 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17626 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17627 */
17628 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17629 host.onPopulateAccessibilityEventInternal(event);
17630 }
17631
17632 /**
17633 * Initializes an {@link AccessibilityEvent} with information about the
17634 * the host View which is the event source.
17635 * <p>
17636 * The default implementation behaves as
17637 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17638 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17639 * the case of no accessibility delegate been set.
17640 * </p>
17641 *
17642 * @param host The View hosting the delegate.
17643 * @param event The event to initialize.
17644 *
17645 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17646 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17647 */
17648 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17649 host.onInitializeAccessibilityEventInternal(event);
17650 }
17651
17652 /**
17653 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17654 * <p>
17655 * The default implementation behaves as
17656 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17657 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17658 * the case of no accessibility delegate been set.
17659 * </p>
17660 *
17661 * @param host The View hosting the delegate.
17662 * @param info The instance to initialize.
17663 *
17664 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17665 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17666 */
17667 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17668 host.onInitializeAccessibilityNodeInfoInternal(info);
17669 }
17670
17671 /**
17672 * Called when a child of the host View has requested sending an
17673 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17674 * to augment the event.
17675 * <p>
17676 * The default implementation behaves as
17677 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17678 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17679 * the case of no accessibility delegate been set.
17680 * </p>
17681 *
17682 * @param host The View hosting the delegate.
17683 * @param child The child which requests sending the event.
17684 * @param event The event to be sent.
17685 * @return True if the event should be sent
17686 *
17687 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17688 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17689 */
17690 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17691 AccessibilityEvent event) {
17692 return host.onRequestSendAccessibilityEventInternal(child, event);
17693 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017694
17695 /**
17696 * Gets the provider for managing a virtual view hierarchy rooted at this View
17697 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17698 * that explore the window content.
17699 * <p>
17700 * The default implementation behaves as
17701 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17702 * the case of no accessibility delegate been set.
17703 * </p>
17704 *
17705 * @return The provider.
17706 *
17707 * @see AccessibilityNodeProvider
17708 */
17709 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17710 return null;
17711 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017713}