blob: d6441f6bf75f85ead6518fbd3fd458b8f5bda494 [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>
Jean Chalard405bc512012-05-29 19:12:34 +0900207 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </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 Meglio30a21e12012-03-12 13:12:19 -0700346 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
347 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
348 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
349 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * </p>
351 *
352 * <p>
353 * Even though a view can define a padding, it does not provide any support for
354 * margins. However, view groups provide such a support. Refer to
355 * {@link android.view.ViewGroup} and
356 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
357 * </p>
358 *
359 * <a name="Layout"></a>
360 * <h3>Layout</h3>
361 * <p>
362 * Layout is a two pass process: a measure pass and a layout pass. The measuring
363 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
364 * of the view tree. Each view pushes dimension specifications down the tree
365 * during the recursion. At the end of the measure pass, every view has stored
366 * its measurements. The second pass happens in
367 * {@link #layout(int,int,int,int)} and is also top-down. During
368 * this pass each parent is responsible for positioning all of its children
369 * using the sizes computed in the measure pass.
370 * </p>
371 *
372 * <p>
373 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
374 * {@link #getMeasuredHeight()} values must be set, along with those for all of
375 * that view's descendants. A view's measured width and measured height values
376 * must respect the constraints imposed by the view's parents. This guarantees
377 * that at the end of the measure pass, all parents accept all of their
378 * children's measurements. A parent view may call measure() more than once on
379 * its children. For example, the parent may measure each child once with
380 * unspecified dimensions to find out how big they want to be, then call
381 * measure() on them again with actual numbers if the sum of all the children's
382 * unconstrained sizes is too big or too small.
383 * </p>
384 *
385 * <p>
386 * The measure pass uses two classes to communicate dimensions. The
387 * {@link MeasureSpec} class is used by views to tell their parents how they
388 * want to be measured and positioned. The base LayoutParams class just
389 * describes how big the view wants to be for both width and height. For each
390 * dimension, it can specify one of:
391 * <ul>
392 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800393 * <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 -0800394 * (minus padding)
395 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
396 * enclose its content (plus padding).
397 * </ul>
398 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
399 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
400 * an X and Y value.
401 * </p>
402 *
403 * <p>
404 * MeasureSpecs are used to push requirements down the tree from parent to
405 * child. A MeasureSpec can be in one of three modes:
406 * <ul>
407 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
408 * of a child view. For example, a LinearLayout may call measure() on its child
409 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
410 * tall the child view wants to be given a width of 240 pixels.
411 * <li>EXACTLY: This is used by the parent to impose an exact size on the
412 * child. The child must use this size, and guarantee that all of its
413 * descendants will fit within this size.
414 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
415 * child. The child must gurantee that it and all of its descendants will fit
416 * within this size.
417 * </ul>
418 * </p>
419 *
420 * <p>
421 * To intiate a layout, call {@link #requestLayout}. This method is typically
422 * called by a view on itself when it believes that is can no longer fit within
423 * its current bounds.
424 * </p>
425 *
426 * <a name="Drawing"></a>
427 * <h3>Drawing</h3>
428 * <p>
429 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700430 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * this means that parents will draw before (i.e., behind) their children, with
432 * siblings drawn in the order they appear in the tree.
433 * If you set a background drawable for a View, then the View will draw it for you
434 * before calling back to its <code>onDraw()</code> method.
435 * </p>
436 *
437 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700438 * 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 -0800439 * </p>
440 *
441 * <p>
442 * To force a view to draw, call {@link #invalidate()}.
443 * </p>
444 *
445 * <a name="EventHandlingThreading"></a>
446 * <h3>Event Handling and Threading</h3>
447 * <p>
448 * The basic cycle of a view is as follows:
449 * <ol>
450 * <li>An event comes in and is dispatched to the appropriate view. The view
451 * handles the event and notifies any listeners.</li>
452 * <li>If in the course of processing the event, the view's bounds may need
453 * to be changed, the view will call {@link #requestLayout()}.</li>
454 * <li>Similarly, if in the course of processing the event the view's appearance
455 * may need to be changed, the view will call {@link #invalidate()}.</li>
456 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
457 * the framework will take care of measuring, laying out, and drawing the tree
458 * as appropriate.</li>
459 * </ol>
460 * </p>
461 *
462 * <p><em>Note: The entire view tree is single threaded. You must always be on
463 * the UI thread when calling any method on any view.</em>
464 * If you are doing work on other threads and want to update the state of a view
465 * from that thread, you should use a {@link Handler}.
466 * </p>
467 *
468 * <a name="FocusHandling"></a>
469 * <h3>Focus Handling</h3>
470 * <p>
471 * The framework will handle routine focus movement in response to user input.
472 * This includes changing the focus as views are removed or hidden, or as new
473 * views become available. Views indicate their willingness to take focus
474 * through the {@link #isFocusable} method. To change whether a view can take
475 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
476 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
477 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
478 * </p>
479 * <p>
480 * Focus movement is based on an algorithm which finds the nearest neighbor in a
481 * given direction. In rare cases, the default algorithm may not match the
482 * intended behavior of the developer. In these situations, you can provide
483 * explicit overrides by using these XML attributes in the layout file:
484 * <pre>
485 * nextFocusDown
486 * nextFocusLeft
487 * nextFocusRight
488 * nextFocusUp
489 * </pre>
490 * </p>
491 *
492 *
493 * <p>
494 * To get a particular view to take focus, call {@link #requestFocus()}.
495 * </p>
496 *
497 * <a name="TouchMode"></a>
498 * <h3>Touch Mode</h3>
499 * <p>
500 * When a user is navigating a user interface via directional keys such as a D-pad, it is
501 * necessary to give focus to actionable items such as buttons so the user can see
502 * what will take input. If the device has touch capabilities, however, and the user
503 * begins interacting with the interface by touching it, it is no longer necessary to
504 * always highlight, or give focus to, a particular view. This motivates a mode
505 * for interaction named 'touch mode'.
506 * </p>
507 * <p>
508 * For a touch capable device, once the user touches the screen, the device
509 * will enter touch mode. From this point onward, only views for which
510 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
511 * Other views that are touchable, like buttons, will not take focus when touched; they will
512 * only fire the on click listeners.
513 * </p>
514 * <p>
515 * Any time a user hits a directional key, such as a D-pad direction, the view device will
516 * exit touch mode, and find a view to take focus, so that the user may resume interacting
517 * with the user interface without touching the screen again.
518 * </p>
519 * <p>
520 * The touch mode state is maintained across {@link android.app.Activity}s. Call
521 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
522 * </p>
523 *
524 * <a name="Scrolling"></a>
525 * <h3>Scrolling</h3>
526 * <p>
527 * The framework provides basic support for views that wish to internally
528 * scroll their content. This includes keeping track of the X and Y scroll
529 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800530 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700531 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * </p>
533 *
534 * <a name="Tags"></a>
535 * <h3>Tags</h3>
536 * <p>
537 * Unlike IDs, tags are not used to identify views. Tags are essentially an
538 * extra piece of information that can be associated with a view. They are most
539 * often used as a convenience to store data related to views in the views
540 * themselves rather than by putting them in a separate structure.
541 * </p>
542 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700543 * <a name="Properties"></a>
544 * <h3>Properties</h3>
545 * <p>
546 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
547 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
548 * available both in the {@link Property} form as well as in similarly-named setter/getter
549 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
550 * be used to set persistent state associated with these rendering-related properties on the view.
551 * The properties and methods can also be used in conjunction with
552 * {@link android.animation.Animator Animator}-based animations, described more in the
553 * <a href="#Animation">Animation</a> section.
554 * </p>
555 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 * <a name="Animation"></a>
557 * <h3>Animation</h3>
558 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700559 * Starting with Android 3.0, the preferred way of animating views is to use the
560 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
561 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
562 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
563 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
564 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
565 * makes animating these View properties particularly easy and efficient.
566 * </p>
567 * <p>
568 * 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 -0800569 * You can attach an {@link Animation} object to a view using
570 * {@link #setAnimation(Animation)} or
571 * {@link #startAnimation(Animation)}. The animation can alter the scale,
572 * rotation, translation and alpha of a view over time. If the animation is
573 * attached to a view that has children, the animation will affect the entire
574 * subtree rooted by that node. When an animation is started, the framework will
575 * take care of redrawing the appropriate views until the animation completes.
576 * </p>
577 *
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * <a name="Security"></a>
579 * <h3>Security</h3>
580 * <p>
581 * Sometimes it is essential that an application be able to verify that an action
582 * is being performed with the full knowledge and consent of the user, such as
583 * granting a permission request, making a purchase or clicking on an advertisement.
584 * Unfortunately, a malicious application could try to spoof the user into
585 * performing these actions, unaware, by concealing the intended purpose of the view.
586 * As a remedy, the framework offers a touch filtering mechanism that can be used to
587 * improve the security of views that provide access to sensitive functionality.
588 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700589 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800590 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700591 * will discard touches that are received whenever the view's window is obscured by
592 * another visible window. As a result, the view will not receive touches whenever a
593 * toast, dialog or other window appears above the view's window.
594 * </p><p>
595 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700596 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
597 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700598 * </p>
599 *
Romain Guy171c5922011-01-06 10:04:23 -0800600 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700601 * @attr ref android.R.styleable#View_background
602 * @attr ref android.R.styleable#View_clickable
603 * @attr ref android.R.styleable#View_contentDescription
604 * @attr ref android.R.styleable#View_drawingCacheQuality
605 * @attr ref android.R.styleable#View_duplicateParentState
606 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700607 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700608 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700610 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_isScrollContainer
613 * @attr ref android.R.styleable#View_focusable
614 * @attr ref android.R.styleable#View_focusableInTouchMode
615 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
616 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800617 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_longClickable
619 * @attr ref android.R.styleable#View_minHeight
620 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_nextFocusDown
622 * @attr ref android.R.styleable#View_nextFocusLeft
623 * @attr ref android.R.styleable#View_nextFocusRight
624 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_onClick
626 * @attr ref android.R.styleable#View_padding
627 * @attr ref android.R.styleable#View_paddingBottom
628 * @attr ref android.R.styleable#View_paddingLeft
629 * @attr ref android.R.styleable#View_paddingRight
630 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800631 * @attr ref android.R.styleable#View_paddingStart
632 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800634 * @attr ref android.R.styleable#View_rotation
635 * @attr ref android.R.styleable#View_rotationX
636 * @attr ref android.R.styleable#View_rotationY
637 * @attr ref android.R.styleable#View_scaleX
638 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * @attr ref android.R.styleable#View_scrollX
640 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_scrollbarSize
642 * @attr ref android.R.styleable#View_scrollbarStyle
643 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700644 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
645 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
647 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * @attr ref android.R.styleable#View_scrollbarThumbVertical
649 * @attr ref android.R.styleable#View_scrollbarTrackVertical
650 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700652 * @attr ref android.R.styleable#View_soundEffectsEnabled
653 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700654 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800655 * @attr ref android.R.styleable#View_transformPivotX
656 * @attr ref android.R.styleable#View_transformPivotY
657 * @attr ref android.R.styleable#View_translationX
658 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700659 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 *
661 * @see android.view.ViewGroup
662 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700663public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700664 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 private static final boolean DBG = false;
666
667 /**
668 * The logging tag used by this class with android.util.Log.
669 */
670 protected static final String VIEW_LOG_TAG = "View";
671
672 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700673 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700674 *
675 * @hide
676 */
677 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
678
679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * Used to mark a View that has no ID.
681 */
682 public static final int NO_ID = -1;
683
684 /**
685 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
686 * calling setFlags.
687 */
688 private static final int NOT_FOCUSABLE = 0x00000000;
689
690 /**
691 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
692 * setFlags.
693 */
694 private static final int FOCUSABLE = 0x00000001;
695
696 /**
697 * Mask for use with setFlags indicating bits used for focus.
698 */
699 private static final int FOCUSABLE_MASK = 0x00000001;
700
701 /**
702 * This view will adjust its padding to fit sytem windows (e.g. status bar)
703 */
704 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
705
706 /**
Scott Main812634c22011-07-27 13:22:35 -0700707 * This view is visible.
708 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
709 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public static final int VISIBLE = 0x00000000;
712
713 /**
714 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700715 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
716 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public static final int INVISIBLE = 0x00000004;
719
720 /**
721 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700722 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
723 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
725 public static final int GONE = 0x00000008;
726
727 /**
728 * Mask for use with setFlags indicating bits used for visibility.
729 * {@hide}
730 */
731 static final int VISIBILITY_MASK = 0x0000000C;
732
733 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
734
735 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700736 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * Use with ENABLED_MASK when calling setFlags.
738 * {@hide}
739 */
740 static final int ENABLED = 0x00000000;
741
742 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700743 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 * Use with ENABLED_MASK when calling setFlags.
745 * {@hide}
746 */
747 static final int DISABLED = 0x00000020;
748
749 /**
750 * Mask for use with setFlags indicating bits used for indicating whether
751 * this view is enabled
752 * {@hide}
753 */
754 static final int ENABLED_MASK = 0x00000020;
755
756 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700757 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
758 * called and further optimizations will be performed. It is okay to have
759 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * {@hide}
761 */
762 static final int WILL_NOT_DRAW = 0x00000080;
763
764 /**
765 * Mask for use with setFlags indicating bits used for indicating whether
766 * this view is will draw
767 * {@hide}
768 */
769 static final int DRAW_MASK = 0x00000080;
770
771 /**
772 * <p>This view doesn't show scrollbars.</p>
773 * {@hide}
774 */
775 static final int SCROLLBARS_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal scrollbars.</p>
779 * {@hide}
780 */
781 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
782
783 /**
784 * <p>This view shows vertical scrollbars.</p>
785 * {@hide}
786 */
787 static final int SCROLLBARS_VERTICAL = 0x00000200;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * scrollbars are enabled.</p>
792 * {@hide}
793 */
794 static final int SCROLLBARS_MASK = 0x00000300;
795
Jeff Brown85a31762010-09-01 17:01:00 -0700796 /**
797 * Indicates that the view should filter touches when its window is obscured.
798 * Refer to the class comments for more information about this security feature.
799 * {@hide}
800 */
801 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
802
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700803 /**
804 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
805 * that they are optional and should be skipped if the window has
806 * requested system UI flags that ignore those insets for layout.
807 */
808 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
810 /**
811 * <p>This view doesn't show fading edges.</p>
812 * {@hide}
813 */
814 static final int FADING_EDGE_NONE = 0x00000000;
815
816 /**
817 * <p>This view shows horizontal fading edges.</p>
818 * {@hide}
819 */
820 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
821
822 /**
823 * <p>This view shows vertical fading edges.</p>
824 * {@hide}
825 */
826 static final int FADING_EDGE_VERTICAL = 0x00002000;
827
828 /**
829 * <p>Mask for use with setFlags indicating bits used for indicating which
830 * fading edges are enabled.</p>
831 * {@hide}
832 */
833 static final int FADING_EDGE_MASK = 0x00003000;
834
835 /**
836 * <p>Indicates this view can be clicked. When clickable, a View reacts
837 * to clicks by notifying the OnClickListener.<p>
838 * {@hide}
839 */
840 static final int CLICKABLE = 0x00004000;
841
842 /**
843 * <p>Indicates this view is caching its drawing into a bitmap.</p>
844 * {@hide}
845 */
846 static final int DRAWING_CACHE_ENABLED = 0x00008000;
847
848 /**
849 * <p>Indicates that no icicle should be saved for this view.<p>
850 * {@hide}
851 */
852 static final int SAVE_DISABLED = 0x000010000;
853
854 /**
855 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
856 * property.</p>
857 * {@hide}
858 */
859 static final int SAVE_DISABLED_MASK = 0x000010000;
860
861 /**
862 * <p>Indicates that no drawing cache should ever be created for this view.<p>
863 * {@hide}
864 */
865 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
866
867 /**
868 * <p>Indicates this view can take / keep focus when int touch mode.</p>
869 * {@hide}
870 */
871 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
872
873 /**
874 * <p>Enables low quality mode for the drawing cache.</p>
875 */
876 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
877
878 /**
879 * <p>Enables high quality mode for the drawing cache.</p>
880 */
881 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
882
883 /**
884 * <p>Enables automatic quality mode for the drawing cache.</p>
885 */
886 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
887
888 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
889 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
890 };
891
892 /**
893 * <p>Mask for use with setFlags indicating bits used for the cache
894 * quality property.</p>
895 * {@hide}
896 */
897 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
898
899 /**
900 * <p>
901 * Indicates this view can be long clicked. When long clickable, a View
902 * reacts to long clicks by notifying the OnLongClickListener or showing a
903 * context menu.
904 * </p>
905 * {@hide}
906 */
907 static final int LONG_CLICKABLE = 0x00200000;
908
909 /**
910 * <p>Indicates that this view gets its drawable states from its direct parent
911 * and ignores its original internal states.</p>
912 *
913 * @hide
914 */
915 static final int DUPLICATE_PARENT_STATE = 0x00400000;
916
917 /**
918 * The scrollbar style to display the scrollbars inside the content area,
919 * without increasing the padding. The scrollbars will be overlaid with
920 * translucency on the view's content.
921 */
922 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
923
924 /**
925 * The scrollbar style to display the scrollbars inside the padded area,
926 * increasing the padding of the view. The scrollbars will not overlap the
927 * content area of the view.
928 */
929 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
930
931 /**
932 * The scrollbar style to display the scrollbars at the edge of the view,
933 * without increasing the padding. The scrollbars will be overlaid with
934 * translucency.
935 */
936 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
937
938 /**
939 * The scrollbar style to display the scrollbars at the edge of the view,
940 * increasing the padding of the view. The scrollbars will only overlap the
941 * background, if any.
942 */
943 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
944
945 /**
946 * Mask to check if the scrollbar style is overlay or inset.
947 * {@hide}
948 */
949 static final int SCROLLBARS_INSET_MASK = 0x01000000;
950
951 /**
952 * Mask to check if the scrollbar style is inside or outside.
953 * {@hide}
954 */
955 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
956
957 /**
958 * Mask for scrollbar style.
959 * {@hide}
960 */
961 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
962
963 /**
964 * View flag indicating that the screen should remain on while the
965 * window containing this view is visible to the user. This effectively
966 * takes care of automatically setting the WindowManager's
967 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
968 */
969 public static final int KEEP_SCREEN_ON = 0x04000000;
970
971 /**
972 * View flag indicating whether this view should have sound effects enabled
973 * for events such as clicking and touching.
974 */
975 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
976
977 /**
978 * View flag indicating whether this view should have haptic feedback
979 * enabled for events such as long presses.
980 */
981 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
982
983 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700984 * <p>Indicates that the view hierarchy should stop saving state when
985 * it reaches this view. If state saving is initiated immediately at
986 * the view, it will be allowed.
987 * {@hide}
988 */
989 static final int PARENT_SAVE_DISABLED = 0x20000000;
990
991 /**
992 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
993 * {@hide}
994 */
995 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
996
997 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700998 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
999 * should add all focusable Views regardless if they are focusable in touch mode.
1000 */
1001 public static final int FOCUSABLES_ALL = 0x00000000;
1002
1003 /**
1004 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1005 * should add only Views focusable in touch mode.
1006 */
1007 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1008
1009 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001010 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 * item.
1012 */
1013 public static final int FOCUS_BACKWARD = 0x00000001;
1014
1015 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001016 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 * item.
1018 */
1019 public static final int FOCUS_FORWARD = 0x00000002;
1020
1021 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001022 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 */
1024 public static final int FOCUS_LEFT = 0x00000011;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 */
1029 public static final int FOCUS_UP = 0x00000021;
1030
1031 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001032 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 */
1034 public static final int FOCUS_RIGHT = 0x00000042;
1035
1036 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001037 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 */
1039 public static final int FOCUS_DOWN = 0x00000082;
1040
Svetoslav Ganov42138042012-03-20 11:51:39 -07001041 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001042 * Bits of {@link #getMeasuredWidthAndState()} and
1043 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1044 */
1045 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1046
1047 /**
1048 * Bits of {@link #getMeasuredWidthAndState()} and
1049 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1050 */
1051 public static final int MEASURED_STATE_MASK = 0xff000000;
1052
1053 /**
1054 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1055 * for functions that combine both width and height into a single int,
1056 * such as {@link #getMeasuredState()} and the childState argument of
1057 * {@link #resolveSizeAndState(int, int, int)}.
1058 */
1059 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1060
1061 /**
1062 * Bit of {@link #getMeasuredWidthAndState()} and
1063 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1064 * is smaller that the space the view would like to have.
1065 */
1066 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1067
1068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * Base View state sets
1070 */
1071 // Singles
1072 /**
1073 * Indicates the view has no states set. States are used with
1074 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1075 * view depending on its state.
1076 *
1077 * @see android.graphics.drawable.Drawable
1078 * @see #getDrawableState()
1079 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001080 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /**
1082 * Indicates the view is enabled. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is focused. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is selected. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001107 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Indicates the view is pressed. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 * @hide
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view's window has focus. States are used with
1120 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1121 * view depending on its state.
1122 *
1123 * @see android.graphics.drawable.Drawable
1124 * @see #getDrawableState()
1125 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001126 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // Doubles
1128 /**
1129 * Indicates the view is enabled and has the focus.
1130 *
1131 * @see #ENABLED_STATE_SET
1132 * @see #FOCUSED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled and selected.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #SELECTED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled and that its window has focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #WINDOW_FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is focused and selected.
1151 *
1152 * @see #FOCUSED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view has the focus and that its window has the focus.
1158 *
1159 * @see #FOCUSED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 /**
1164 * Indicates the view is selected and that its window has the focus.
1165 *
1166 * @see #SELECTED_STATE_SET
1167 * @see #WINDOW_FOCUSED_STATE_SET
1168 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001169 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 // Triples
1171 /**
1172 * Indicates the view is enabled, focused and selected.
1173 *
1174 * @see #ENABLED_STATE_SET
1175 * @see #FOCUSED_STATE_SET
1176 * @see #SELECTED_STATE_SET
1177 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001178 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 /**
1180 * Indicates the view is enabled, focused and its window has the focus.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #WINDOW_FOCUSED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, selected and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #SELECTED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is focused, selected and its window has the focus.
1197 *
1198 * @see #FOCUSED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is enabled, focused, selected and its window
1205 * has the focus.
1206 *
1207 * @see #ENABLED_STATE_SET
1208 * @see #FOCUSED_STATE_SET
1209 * @see #SELECTED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is pressed and its window has the focus.
1215 *
1216 * @see #PRESSED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed and selected.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #SELECTED_STATE_SET
1225 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001226 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 /**
1228 * Indicates the view is pressed, selected and its window has the focus.
1229 *
1230 * @see #PRESSED_STATE_SET
1231 * @see #SELECTED_STATE_SET
1232 * @see #WINDOW_FOCUSED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed and focused.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is pressed, focused and its window has the focus.
1244 *
1245 * @see #PRESSED_STATE_SET
1246 * @see #FOCUSED_STATE_SET
1247 * @see #WINDOW_FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and selected.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #SELECTED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused, selected and its window has the focus.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #FOCUSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #WINDOW_FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed and enabled.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #ENABLED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled and its window has the focus.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #WINDOW_FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and selected.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled, selected and its window has the
1292 * focus.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #WINDOW_FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, enabled and focused.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled, focused and its window has the
1310 * focus.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #FOCUSED_STATE_SET
1315 * @see #WINDOW_FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, enabled, focused and selected.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #SELECTED_STATE_SET
1324 * @see #FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed, enabled, focused, selected and its window
1329 * has the focus.
1330 *
1331 * @see #PRESSED_STATE_SET
1332 * @see #ENABLED_STATE_SET
1333 * @see #SELECTED_STATE_SET
1334 * @see #FOCUSED_STATE_SET
1335 * @see #WINDOW_FOCUSED_STATE_SET
1336 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338
1339 /**
1340 * The order here is very important to {@link #getDrawableState()}
1341 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 private static final int[][] VIEW_STATE_SETS;
1343
Romain Guyb051e892010-09-28 19:09:36 -07001344 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1345 static final int VIEW_STATE_SELECTED = 1 << 1;
1346 static final int VIEW_STATE_FOCUSED = 1 << 2;
1347 static final int VIEW_STATE_ENABLED = 1 << 3;
1348 static final int VIEW_STATE_PRESSED = 1 << 4;
1349 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001350 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001351 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001352 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1353 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001354
1355 static final int[] VIEW_STATE_IDS = new int[] {
1356 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1357 R.attr.state_selected, VIEW_STATE_SELECTED,
1358 R.attr.state_focused, VIEW_STATE_FOCUSED,
1359 R.attr.state_enabled, VIEW_STATE_ENABLED,
1360 R.attr.state_pressed, VIEW_STATE_PRESSED,
1361 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001362 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001363 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001364 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001365 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 };
1367
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001369 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1370 throw new IllegalStateException(
1371 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1372 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001374 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001376 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001378 orderedIds[i * 2] = viewState;
1379 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 }
1381 }
1382 }
Romain Guyb051e892010-09-28 19:09:36 -07001383 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1384 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1385 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 int numBits = Integer.bitCount(i);
1387 int[] set = new int[numBits];
1388 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001389 for (int j = 0; j < orderedIds.length; j += 2) {
1390 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 set[pos++] = orderedIds[j];
1392 }
1393 }
1394 VIEW_STATE_SETS[i] = set;
1395 }
1396
1397 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1398 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1399 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1400 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1402 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1403 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1405 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1407 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_FOCUSED];
1410 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1411 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1413 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1415 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_ENABLED];
1418 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1420 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1422 | VIEW_STATE_ENABLED];
1423 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1429
1430 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1431 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1433 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1435 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1437 | VIEW_STATE_PRESSED];
1438 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1440 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1442 | VIEW_STATE_PRESSED];
1443 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1449 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1465 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1471 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1472 | VIEW_STATE_PRESSED];
1473 }
1474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001476 * Accessibility event types that are dispatched for text population.
1477 */
1478 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1479 AccessibilityEvent.TYPE_VIEW_CLICKED
1480 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1481 | AccessibilityEvent.TYPE_VIEW_SELECTED
1482 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1483 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1484 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001485 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001486 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001487 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001488 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1489 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001490
1491 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 * Temporary Rect currently for use in setBackground(). This will probably
1493 * be extended in the future to hold our own class with more than just
1494 * a Rect. :)
1495 */
1496 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001497
1498 /**
1499 * Map used to store views' tags.
1500 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001501 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001504 * The next available accessiiblity id.
1505 */
1506 private static int sNextAccessibilityViewId;
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * The animation currently associated with this view.
1510 * @hide
1511 */
1512 protected Animation mCurrentAnimation = null;
1513
1514 /**
1515 * Width as measured during measure pass.
1516 * {@hide}
1517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001518 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001519 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
1521 /**
1522 * Height as measured during measure pass.
1523 * {@hide}
1524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001525 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001526 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001529 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1530 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1531 * its display list. This flag, used only when hw accelerated, allows us to clear the
1532 * flag while retaining this information until it's needed (at getDisplayList() time and
1533 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1534 *
1535 * {@hide}
1536 */
1537 boolean mRecreateDisplayList = false;
1538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The view's identifier.
1541 * {@hide}
1542 *
1543 * @see #setId(int)
1544 * @see #getId()
1545 */
1546 @ViewDebug.ExportedProperty(resolveId = true)
1547 int mID = NO_ID;
1548
1549 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001550 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001551 */
1552 int mAccessibilityViewId = NO_ID;
1553
1554 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001555 * @hide
1556 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001557 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001558
1559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * The view's tag.
1561 * {@hide}
1562 *
1563 * @see #setTag(Object)
1564 * @see #getTag()
1565 */
1566 protected Object mTag;
1567
1568 // for mPrivateFlags:
1569 /** {@hide} */
1570 static final int WANTS_FOCUS = 0x00000001;
1571 /** {@hide} */
1572 static final int FOCUSED = 0x00000002;
1573 /** {@hide} */
1574 static final int SELECTED = 0x00000004;
1575 /** {@hide} */
1576 static final int IS_ROOT_NAMESPACE = 0x00000008;
1577 /** {@hide} */
1578 static final int HAS_BOUNDS = 0x00000010;
1579 /** {@hide} */
1580 static final int DRAWN = 0x00000020;
1581 /**
1582 * When this flag is set, this view is running an animation on behalf of its
1583 * children and should therefore not cancel invalidate requests, even if they
1584 * lie outside of this view's bounds.
1585 *
1586 * {@hide}
1587 */
1588 static final int DRAW_ANIMATION = 0x00000040;
1589 /** {@hide} */
1590 static final int SKIP_DRAW = 0x00000080;
1591 /** {@hide} */
1592 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1593 /** {@hide} */
1594 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1595 /** {@hide} */
1596 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1597 /** {@hide} */
1598 static final int MEASURED_DIMENSION_SET = 0x00000800;
1599 /** {@hide} */
1600 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001601 /** {@hide} */
1602 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603
1604 private static final int PRESSED = 0x00004000;
1605
1606 /** {@hide} */
1607 static final int DRAWING_CACHE_VALID = 0x00008000;
1608 /**
1609 * Flag used to indicate that this view should be drawn once more (and only once
1610 * more) after its animation has completed.
1611 * {@hide}
1612 */
1613 static final int ANIMATION_STARTED = 0x00010000;
1614
1615 private static final int SAVE_STATE_CALLED = 0x00020000;
1616
1617 /**
1618 * Indicates that the View returned true when onSetAlpha() was called and that
1619 * the alpha must be restored.
1620 * {@hide}
1621 */
1622 static final int ALPHA_SET = 0x00040000;
1623
1624 /**
1625 * Set by {@link #setScrollContainer(boolean)}.
1626 */
1627 static final int SCROLL_CONTAINER = 0x00080000;
1628
1629 /**
1630 * Set by {@link #setScrollContainer(boolean)}.
1631 */
1632 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1633
1634 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001635 * View flag indicating whether this view was invalidated (fully or partially.)
1636 *
1637 * @hide
1638 */
1639 static final int DIRTY = 0x00200000;
1640
1641 /**
1642 * View flag indicating whether this view was invalidated by an opaque
1643 * invalidate request.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_OPAQUE = 0x00400000;
1648
1649 /**
1650 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1651 *
1652 * @hide
1653 */
1654 static final int DIRTY_MASK = 0x00600000;
1655
1656 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001657 * Indicates whether the background is opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_BACKGROUND = 0x00800000;
1662
1663 /**
1664 * Indicates whether the scrollbars are opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_SCROLLBARS = 0x01000000;
1669
1670 /**
1671 * Indicates whether the view is opaque.
1672 *
1673 * @hide
1674 */
1675 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001676
Adam Powelle14579b2009-12-16 18:39:52 -08001677 /**
1678 * Indicates a prepressed state;
1679 * the short time between ACTION_DOWN and recognizing
1680 * a 'real' press. Prepressed is used to recognize quick taps
1681 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001682 *
Adam Powelle14579b2009-12-16 18:39:52 -08001683 * @hide
1684 */
1685 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powellc9fbaab2010-02-16 17:16:19 -08001687 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001688 * Indicates whether the view is temporarily detached.
1689 *
1690 * @hide
1691 */
1692 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001693
Adam Powell8568c3a2010-04-19 14:26:11 -07001694 /**
1695 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001696 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001697 * @hide
1698 */
1699 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001700
1701 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001702 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1703 * @hide
1704 */
1705 private static final int HOVERED = 0x10000000;
1706
1707 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001708 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1709 * for transform operations
1710 *
1711 * @hide
1712 */
Adam Powellf37df072010-09-17 16:22:49 -07001713 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001714
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001715 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001716 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001717
Chet Haasefd2b0022010-08-06 13:08:56 -07001718 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001719 * Indicates that this view was specifically invalidated, not just dirtied because some
1720 * child view was invalidated. The flag is used to determine when we need to recreate
1721 * a view's display list (as opposed to just returning a reference to its existing
1722 * display list).
1723 *
1724 * @hide
1725 */
1726 static final int INVALIDATED = 0x80000000;
1727
Christopher Tate3d4bf172011-03-28 16:16:46 -07001728 /* Masks for mPrivateFlags2 */
1729
1730 /**
1731 * Indicates that this view has reported that it can accept the current drag's content.
1732 * Cleared when the drag operation concludes.
1733 * @hide
1734 */
1735 static final int DRAG_CAN_ACCEPT = 0x00000001;
1736
1737 /**
1738 * Indicates that this view is currently directly under the drag location in a
1739 * drag-and-drop operation involving content that it can accept. Cleared when
1740 * the drag exits the view, or when the drag operation concludes.
1741 * @hide
1742 */
1743 static final int DRAG_HOVERED = 0x00000002;
1744
Cibu Johny86666632010-02-22 13:01:02 -08001745 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001746 * Horizontal layout direction of this view is from Left to Right.
1747 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001748 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001749 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001750
1751 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001752 * Horizontal layout direction of this view is from Right to Left.
1753 * Use with {@link #setLayoutDirection}.
1754 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001755 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001756
1757 /**
1758 * Horizontal layout direction of this view is inherited from its parent.
1759 * Use with {@link #setLayoutDirection}.
1760 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001761 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001762
1763 /**
1764 * Horizontal layout direction of this view is from deduced from the default language
1765 * script for the locale. Use with {@link #setLayoutDirection}.
1766 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001767 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001768
1769 /**
1770 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001771 * @hide
1772 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001773 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1774
1775 /**
1776 * Mask for use with private flags indicating bits used for horizontal layout direction.
1777 * @hide
1778 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001779 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001780
1781 /**
1782 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1783 * right-to-left direction.
1784 * @hide
1785 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001786 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001787
1788 /**
1789 * Indicates whether the view horizontal layout direction has been resolved.
1790 * @hide
1791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001793
1794 /**
1795 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1796 * @hide
1797 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001798 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001799
1800 /*
1801 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1802 * flag value.
1803 * @hide
1804 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001805 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1806 LAYOUT_DIRECTION_LTR,
1807 LAYOUT_DIRECTION_RTL,
1808 LAYOUT_DIRECTION_INHERIT,
1809 LAYOUT_DIRECTION_LOCALE
1810 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001811
1812 /**
1813 * Default horizontal layout direction.
1814 * @hide
1815 */
1816 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817
Adam Powell539ee872012-02-03 19:00:49 -08001818 /**
1819 * Indicates that the view is tracking some sort of transient state
1820 * that the app should not need to be aware of, but that the framework
1821 * should take special care to preserve.
1822 *
1823 * @hide
1824 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001825 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001826
1827
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001828 /**
1829 * Text direction is inherited thru {@link ViewGroup}
1830 */
1831 public static final int TEXT_DIRECTION_INHERIT = 0;
1832
1833 /**
1834 * Text direction is using "first strong algorithm". The first strong directional character
1835 * determines the paragraph direction. If there is no strong directional character, the
1836 * paragraph direction is the view's resolved layout direction.
1837 */
1838 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1839
1840 /**
1841 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1842 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1843 * If there are neither, the paragraph direction is the view's resolved layout direction.
1844 */
1845 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1846
1847 /**
1848 * Text direction is forced to LTR.
1849 */
1850 public static final int TEXT_DIRECTION_LTR = 3;
1851
1852 /**
1853 * Text direction is forced to RTL.
1854 */
1855 public static final int TEXT_DIRECTION_RTL = 4;
1856
1857 /**
1858 * Text direction is coming from the system Locale.
1859 */
1860 public static final int TEXT_DIRECTION_LOCALE = 5;
1861
1862 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001863 * Default text direction is inherited
1864 */
1865 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1866
1867 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001868 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1869 * @hide
1870 */
1871 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1872
1873 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 * Mask for use with private flags indicating bits used for text direction.
1875 * @hide
1876 */
1877 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1878
1879 /**
1880 * Array of text direction flags for mapping attribute "textDirection" to correct
1881 * flag value.
1882 * @hide
1883 */
1884 private static final int[] TEXT_DIRECTION_FLAGS = {
1885 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1886 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1887 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1888 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1889 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1891 };
1892
1893 /**
1894 * Indicates whether the view text direction has been resolved.
1895 * @hide
1896 */
1897 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1898
1899 /**
1900 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1901 * @hide
1902 */
1903 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1904
1905 /**
1906 * Mask for use with private flags indicating bits used for resolved text direction.
1907 * @hide
1908 */
1909 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1910
1911 /**
1912 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1913 * @hide
1914 */
1915 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1916 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1917
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001918 /*
1919 * Default text alignment. The text alignment of this View is inherited from its parent.
1920 * Use with {@link #setTextAlignment(int)}
1921 */
1922 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1923
1924 /**
1925 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1926 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1927 *
1928 * Use with {@link #setTextAlignment(int)}
1929 */
1930 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1931
1932 /**
1933 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1934 *
1935 * Use with {@link #setTextAlignment(int)}
1936 */
1937 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1938
1939 /**
1940 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1941 *
1942 * Use with {@link #setTextAlignment(int)}
1943 */
1944 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1945
1946 /**
1947 * Center the paragraph, e.g. ALIGN_CENTER.
1948 *
1949 * Use with {@link #setTextAlignment(int)}
1950 */
1951 public static final int TEXT_ALIGNMENT_CENTER = 4;
1952
1953 /**
1954 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1955 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1956 *
1957 * Use with {@link #setTextAlignment(int)}
1958 */
1959 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1960
1961 /**
1962 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1963 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1964 *
1965 * Use with {@link #setTextAlignment(int)}
1966 */
1967 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1968
1969 /**
1970 * Default text alignment is inherited
1971 */
1972 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
1973
1974 /**
1975 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1976 * @hide
1977 */
1978 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
1979
1980 /**
1981 * Mask for use with private flags indicating bits used for text alignment.
1982 * @hide
1983 */
1984 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
1985
1986 /**
1987 * Array of text direction flags for mapping attribute "textAlignment" to correct
1988 * flag value.
1989 * @hide
1990 */
1991 private static final int[] TEXT_ALIGNMENT_FLAGS = {
1992 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
1993 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
1994 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
1995 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
1996 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
1997 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
1998 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
1999 };
2000
2001 /**
2002 * Indicates whether the view text alignment has been resolved.
2003 * @hide
2004 */
2005 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2006
2007 /**
2008 * Bit shift to get the resolved text alignment.
2009 * @hide
2010 */
2011 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2012
2013 /**
2014 * Mask for use with private flags indicating bits used for text alignment.
2015 * @hide
2016 */
2017 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2018
2019 /**
2020 * Indicates whether if the view text alignment has been resolved to gravity
2021 */
2022 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2023 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2024
Svetoslav Ganov42138042012-03-20 11:51:39 -07002025 // Accessiblity constants for mPrivateFlags2
2026
2027 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002028 * Shift for the bits in {@link #mPrivateFlags2} related to the
2029 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002030 */
2031 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2032
2033 /**
2034 * Automatically determine whether a view is important for accessibility.
2035 */
2036 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2037
2038 /**
2039 * The view is important for accessibility.
2040 */
2041 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2042
2043 /**
2044 * The view is not important for accessibility.
2045 */
2046 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2047
2048 /**
2049 * The default whether the view is important for accessiblity.
2050 */
2051 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2052
2053 /**
2054 * Mask for obtainig the bits which specify how to determine
2055 * whether a view is important for accessibility.
2056 */
2057 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2058 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2059 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2060
2061 /**
2062 * Flag indicating whether a view has accessibility focus.
2063 */
2064 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2065
2066 /**
2067 * Flag indicating whether a view state for accessibility has changed.
2068 */
2069 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002070
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002071 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002072 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2073 * is used to check whether later changes to the view's transform should invalidate the
2074 * view to force the quickReject test to run again.
2075 */
Chet Haase21433372012-06-05 07:54:09 -07002076 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002077
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002078 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002079
Christopher Tate3d4bf172011-03-28 16:16:46 -07002080 /* End of masks for mPrivateFlags2 */
2081
Chet Haase21433372012-06-05 07:54:09 -07002082 /* Masks for mPrivateFlags3 */
2083
2084 /**
2085 * Flag indicating that view has a transform animation set on it. This is used to track whether
2086 * an animation is cleared between successive frames, in order to tell the associated
2087 * DisplayList to clear its animation matrix.
2088 */
2089 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2090
2091 /**
2092 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2093 * animation is cleared between successive frames, in order to tell the associated
2094 * DisplayList to restore its alpha value.
2095 */
2096 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2097
2098
2099 /* End of masks for mPrivateFlags3 */
2100
Christopher Tate3d4bf172011-03-28 16:16:46 -07002101 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2102
Chet Haasedaf98e92011-01-10 14:10:36 -08002103 /**
Adam Powell637d3372010-08-25 14:37:03 -07002104 * Always allow a user to over-scroll this view, provided it is a
2105 * view that can scroll.
2106 *
2107 * @see #getOverScrollMode()
2108 * @see #setOverScrollMode(int)
2109 */
2110 public static final int OVER_SCROLL_ALWAYS = 0;
2111
2112 /**
2113 * Allow a user to over-scroll this view only if the content is large
2114 * enough to meaningfully scroll, provided it is a view that can scroll.
2115 *
2116 * @see #getOverScrollMode()
2117 * @see #setOverScrollMode(int)
2118 */
2119 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2120
2121 /**
2122 * Never allow a user to over-scroll this view.
2123 *
2124 * @see #getOverScrollMode()
2125 * @see #setOverScrollMode(int)
2126 */
2127 public static final int OVER_SCROLL_NEVER = 2;
2128
2129 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002130 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2131 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002132 *
Joe Malin32736f02011-01-19 16:14:20 -08002133 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002134 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002135 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002136
2137 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002138 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2139 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002140 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002141 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002142 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002143 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002144 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002145 *
Joe Malin32736f02011-01-19 16:14:20 -08002146 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002147 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002148 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2149
2150 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002151 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2152 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002153 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002154 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002155 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2156 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2157 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002158 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002159 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2160 * window flags) for displaying content using every last pixel on the display.
2161 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002162 * <p>There is a limitation: because navigation controls are so important, the least user
2163 * interaction will cause them to reappear immediately. When this happens, both
2164 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2165 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002166 *
2167 * @see #setSystemUiVisibility(int)
2168 */
2169 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2170
2171 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002172 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2173 * into the normal fullscreen mode so that its content can take over the screen
2174 * while still allowing the user to interact with the application.
2175 *
2176 * <p>This has the same visual effect as
2177 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2178 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2179 * meaning that non-critical screen decorations (such as the status bar) will be
2180 * hidden while the user is in the View's window, focusing the experience on
2181 * that content. Unlike the window flag, if you are using ActionBar in
2182 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2183 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2184 * hide the action bar.
2185 *
2186 * <p>This approach to going fullscreen is best used over the window flag when
2187 * it is a transient state -- that is, the application does this at certain
2188 * points in its user interaction where it wants to allow the user to focus
2189 * on content, but not as a continuous state. For situations where the application
2190 * would like to simply stay full screen the entire time (such as a game that
2191 * wants to take over the screen), the
2192 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2193 * is usually a better approach. The state set here will be removed by the system
2194 * in various situations (such as the user moving to another application) like
2195 * the other system UI states.
2196 *
2197 * <p>When using this flag, the application should provide some easy facility
2198 * for the user to go out of it. A common example would be in an e-book
2199 * reader, where tapping on the screen brings back whatever screen and UI
2200 * decorations that had been hidden while the user was immersed in reading
2201 * the book.
2202 *
2203 * @see #setSystemUiVisibility(int)
2204 */
2205 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2206
2207 /**
2208 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2209 * flags, we would like a stable view of the content insets given to
2210 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2211 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002212 * as a continuous state. In the stock Android UI this is the space for
2213 * the system bar, nav bar, and status bar, but not more transient elements
2214 * such as an input method.
2215 *
2216 * The stable layout your UI sees is based on the system UI modes you can
2217 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2218 * then you will get a stable layout for changes of the
2219 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2220 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2221 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2222 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2223 * with a stable layout. (Note that you should avoid using
2224 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2225 *
2226 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2227 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2228 * then a hidden status bar will be considered a "stable" state for purposes
2229 * here. This allows your UI to continually hide the status bar, while still
2230 * using the system UI flags to hide the action bar while still retaining
2231 * a stable layout. Note that changing the window fullscreen flag will never
2232 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002233 *
2234 * <p>If you are using ActionBar in
2235 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2236 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2237 * insets it adds to those given to the application.
2238 */
2239 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2240
2241 /**
2242 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2243 * to be layed out as if it has requested
2244 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2245 * allows it to avoid artifacts when switching in and out of that mode, at
2246 * the expense that some of its user interface may be covered by screen
2247 * decorations when they are shown. You can perform layout of your inner
2248 * UI elements to account for the navagation system UI through the
2249 * {@link #fitSystemWindows(Rect)} method.
2250 */
2251 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2252
2253 /**
2254 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2255 * to be layed out as if it has requested
2256 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2257 * allows it to avoid artifacts when switching in and out of that mode, at
2258 * the expense that some of its user interface may be covered by screen
2259 * decorations when they are shown. You can perform layout of your inner
2260 * UI elements to account for non-fullscreen system UI through the
2261 * {@link #fitSystemWindows(Rect)} method.
2262 */
2263 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2264
2265 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002266 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2267 */
2268 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2269
2270 /**
2271 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2272 */
2273 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002274
2275 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002276 * @hide
2277 *
2278 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2279 * out of the public fields to keep the undefined bits out of the developer's way.
2280 *
2281 * Flag to make the status bar not expandable. Unless you also
2282 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2283 */
2284 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2285
2286 /**
2287 * @hide
2288 *
2289 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2290 * out of the public fields to keep the undefined bits out of the developer's way.
2291 *
2292 * Flag to hide notification icons and scrolling ticker text.
2293 */
2294 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2295
2296 /**
2297 * @hide
2298 *
2299 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2300 * out of the public fields to keep the undefined bits out of the developer's way.
2301 *
2302 * Flag to disable incoming notification alerts. This will not block
2303 * icons, but it will block sound, vibrating and other visual or aural notifications.
2304 */
2305 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2306
2307 /**
2308 * @hide
2309 *
2310 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2311 * out of the public fields to keep the undefined bits out of the developer's way.
2312 *
2313 * Flag to hide only the scrolling ticker. Note that
2314 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2315 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2316 */
2317 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2318
2319 /**
2320 * @hide
2321 *
2322 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2323 * out of the public fields to keep the undefined bits out of the developer's way.
2324 *
2325 * Flag to hide the center system info area.
2326 */
2327 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2328
2329 /**
2330 * @hide
2331 *
2332 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2333 * out of the public fields to keep the undefined bits out of the developer's way.
2334 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002335 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002336 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2337 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002338 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002339
2340 /**
2341 * @hide
2342 *
2343 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2344 * out of the public fields to keep the undefined bits out of the developer's way.
2345 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002346 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002347 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2348 */
2349 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2350
2351 /**
2352 * @hide
2353 *
2354 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2355 * out of the public fields to keep the undefined bits out of the developer's way.
2356 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357 * Flag to hide only the clock. You might use this if your activity has
2358 * its own clock making the status bar's clock redundant.
2359 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002360 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2361
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002362 /**
2363 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002364 *
2365 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2366 * out of the public fields to keep the undefined bits out of the developer's way.
2367 *
2368 * Flag to hide only the recent apps button. Don't use this
2369 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2370 */
2371 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2372
2373 /**
2374 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002375 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002376 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002377
2378 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002379 * These are the system UI flags that can be cleared by events outside
2380 * of an application. Currently this is just the ability to tap on the
2381 * screen while hiding the navigation bar to have it return.
2382 * @hide
2383 */
2384 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002385 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2386 | SYSTEM_UI_FLAG_FULLSCREEN;
2387
2388 /**
2389 * Flags that can impact the layout in relation to system UI.
2390 */
2391 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2392 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2393 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002394
2395 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002396 * Find views that render the specified text.
2397 *
2398 * @see #findViewsWithText(ArrayList, CharSequence, int)
2399 */
2400 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2401
2402 /**
2403 * Find find views that contain the specified content description.
2404 *
2405 * @see #findViewsWithText(ArrayList, CharSequence, int)
2406 */
2407 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2408
2409 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002410 * Find views that contain {@link AccessibilityNodeProvider}. Such
2411 * a View is a root of virtual view hierarchy and may contain the searched
2412 * text. If this flag is set Views with providers are automatically
2413 * added and it is a responsibility of the client to call the APIs of
2414 * the provider to determine whether the virtual tree rooted at this View
2415 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2416 * represeting the virtual views with this text.
2417 *
2418 * @see #findViewsWithText(ArrayList, CharSequence, int)
2419 *
2420 * @hide
2421 */
2422 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2423
2424 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002425 * The undefined cursor position.
2426 */
2427 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2428
2429 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002430 * Indicates that the screen has changed state and is now off.
2431 *
2432 * @see #onScreenStateChanged(int)
2433 */
2434 public static final int SCREEN_STATE_OFF = 0x0;
2435
2436 /**
2437 * Indicates that the screen has changed state and is now on.
2438 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002439 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002440 */
2441 public static final int SCREEN_STATE_ON = 0x1;
2442
2443 /**
Adam Powell637d3372010-08-25 14:37:03 -07002444 * Controls the over-scroll mode for this view.
2445 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2446 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2447 * and {@link #OVER_SCROLL_NEVER}.
2448 */
2449 private int mOverScrollMode;
2450
2451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 * The parent this view is attached to.
2453 * {@hide}
2454 *
2455 * @see #getParent()
2456 */
2457 protected ViewParent mParent;
2458
2459 /**
2460 * {@hide}
2461 */
2462 AttachInfo mAttachInfo;
2463
2464 /**
2465 * {@hide}
2466 */
Romain Guy809a7f62009-05-14 15:44:42 -07002467 @ViewDebug.ExportedProperty(flagMapping = {
2468 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2469 name = "FORCE_LAYOUT"),
2470 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2471 name = "LAYOUT_REQUIRED"),
2472 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002473 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002474 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2475 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2476 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2477 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2478 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002480 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002481 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482
2483 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002484 * This view's request for the visibility of the status bar.
2485 * @hide
2486 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002487 @ViewDebug.ExportedProperty(flagMapping = {
2488 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2489 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2490 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2491 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2492 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2493 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2494 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2495 equals = SYSTEM_UI_FLAG_VISIBLE,
2496 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2497 })
Joe Onorato664644d2011-01-23 17:53:23 -08002498 int mSystemUiVisibility;
2499
2500 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002501 * Reference count for transient state.
2502 * @see #setHasTransientState(boolean)
2503 */
2504 int mTransientStateCount = 0;
2505
2506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 * Count of how many windows this view has been attached to.
2508 */
2509 int mWindowAttachCount;
2510
2511 /**
2512 * The layout parameters associated with this view and used by the parent
2513 * {@link android.view.ViewGroup} to determine how this view should be
2514 * laid out.
2515 * {@hide}
2516 */
2517 protected ViewGroup.LayoutParams mLayoutParams;
2518
2519 /**
2520 * The view flags hold various views states.
2521 * {@hide}
2522 */
2523 @ViewDebug.ExportedProperty
2524 int mViewFlags;
2525
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002526 static class TransformationInfo {
2527 /**
2528 * The transform matrix for the View. This transform is calculated internally
2529 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2530 * is used by default. Do *not* use this variable directly; instead call
2531 * getMatrix(), which will automatically recalculate the matrix if necessary
2532 * to get the correct matrix based on the latest rotation and scale properties.
2533 */
2534 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002535
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002536 /**
2537 * The transform matrix for the View. This transform is calculated internally
2538 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2539 * is used by default. Do *not* use this variable directly; instead call
2540 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2541 * to get the correct matrix based on the latest rotation and scale properties.
2542 */
2543 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002544
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002545 /**
2546 * An internal variable that tracks whether we need to recalculate the
2547 * transform matrix, based on whether the rotation or scaleX/Y properties
2548 * have changed since the matrix was last calculated.
2549 */
2550 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002551
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002552 /**
2553 * An internal variable that tracks whether we need to recalculate the
2554 * transform matrix, based on whether the rotation or scaleX/Y properties
2555 * have changed since the matrix was last calculated.
2556 */
2557 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002558
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002559 /**
2560 * A variable that tracks whether we need to recalculate the
2561 * transform matrix, based on whether the rotation or scaleX/Y properties
2562 * have changed since the matrix was last calculated. This variable
2563 * is only valid after a call to updateMatrix() or to a function that
2564 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2565 */
2566 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002567
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002568 /**
2569 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2570 */
2571 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002572
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002573 /**
2574 * This matrix is used when computing the matrix for 3D rotations.
2575 */
2576 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002577
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002578 /**
2579 * These prev values are used to recalculate a centered pivot point when necessary. The
2580 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2581 * set), so thes values are only used then as well.
2582 */
2583 private int mPrevWidth = -1;
2584 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002585
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002586 /**
2587 * The degrees rotation around the vertical axis through the pivot point.
2588 */
2589 @ViewDebug.ExportedProperty
2590 float mRotationY = 0f;
2591
2592 /**
2593 * The degrees rotation around the horizontal axis through the pivot point.
2594 */
2595 @ViewDebug.ExportedProperty
2596 float mRotationX = 0f;
2597
2598 /**
2599 * The degrees rotation around the pivot point.
2600 */
2601 @ViewDebug.ExportedProperty
2602 float mRotation = 0f;
2603
2604 /**
2605 * The amount of translation of the object away from its left property (post-layout).
2606 */
2607 @ViewDebug.ExportedProperty
2608 float mTranslationX = 0f;
2609
2610 /**
2611 * The amount of translation of the object away from its top property (post-layout).
2612 */
2613 @ViewDebug.ExportedProperty
2614 float mTranslationY = 0f;
2615
2616 /**
2617 * The amount of scale in the x direction around the pivot point. A
2618 * value of 1 means no scaling is applied.
2619 */
2620 @ViewDebug.ExportedProperty
2621 float mScaleX = 1f;
2622
2623 /**
2624 * The amount of scale in the y direction around the pivot point. A
2625 * value of 1 means no scaling is applied.
2626 */
2627 @ViewDebug.ExportedProperty
2628 float mScaleY = 1f;
2629
2630 /**
Chet Haasea33de552012-02-03 16:28:24 -08002631 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002632 */
2633 @ViewDebug.ExportedProperty
2634 float mPivotX = 0f;
2635
2636 /**
Chet Haasea33de552012-02-03 16:28:24 -08002637 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002638 */
2639 @ViewDebug.ExportedProperty
2640 float mPivotY = 0f;
2641
2642 /**
2643 * The opacity of the View. This is a value from 0 to 1, where 0 means
2644 * completely transparent and 1 means completely opaque.
2645 */
2646 @ViewDebug.ExportedProperty
2647 float mAlpha = 1f;
2648 }
2649
2650 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002651
Joe Malin32736f02011-01-19 16:14:20 -08002652 private boolean mLastIsOpaque;
2653
Chet Haasefd2b0022010-08-06 13:08:56 -07002654 /**
2655 * Convenience value to check for float values that are close enough to zero to be considered
2656 * zero.
2657 */
Romain Guy2542d192010-08-18 11:47:12 -07002658 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002659
2660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 * The distance in pixels from the left edge of this view's parent
2662 * to the left edge of this view.
2663 * {@hide}
2664 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002665 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 protected int mLeft;
2667 /**
2668 * The distance in pixels from the left edge of this view's parent
2669 * to the right edge of this view.
2670 * {@hide}
2671 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002672 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 protected int mRight;
2674 /**
2675 * The distance in pixels from the top edge of this view's parent
2676 * to the top edge of this view.
2677 * {@hide}
2678 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002679 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 protected int mTop;
2681 /**
2682 * The distance in pixels from the top edge of this view's parent
2683 * to the bottom edge of this view.
2684 * {@hide}
2685 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002686 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 protected int mBottom;
2688
2689 /**
2690 * The offset, in pixels, by which the content of this view is scrolled
2691 * horizontally.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mScrollX;
2696 /**
2697 * The offset, in pixels, by which the content of this view is scrolled
2698 * vertically.
2699 * {@hide}
2700 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002701 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 protected int mScrollY;
2703
2704 /**
2705 * The left padding in pixels, that is the distance in pixels between the
2706 * left edge of this view and the left edge of its content.
2707 * {@hide}
2708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002709 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 protected int mPaddingLeft;
2711 /**
2712 * The right padding in pixels, that is the distance in pixels between the
2713 * right edge of this view and the right edge of its content.
2714 * {@hide}
2715 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002716 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 protected int mPaddingRight;
2718 /**
2719 * The top padding in pixels, that is the distance in pixels between the
2720 * top edge of this view and the top edge of its content.
2721 * {@hide}
2722 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002723 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 protected int mPaddingTop;
2725 /**
2726 * The bottom padding in pixels, that is the distance in pixels between the
2727 * bottom edge of this view and the bottom edge of its content.
2728 * {@hide}
2729 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002730 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 protected int mPaddingBottom;
2732
2733 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002734 * The layout insets in pixels, that is the distance in pixels between the
2735 * visible edges of this view its bounds.
2736 */
2737 private Insets mLayoutInsets;
2738
2739 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002740 * Briefly describes the view and is primarily used for accessibility support.
2741 */
2742 private CharSequence mContentDescription;
2743
2744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002746 *
2747 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002749 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002750 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751
2752 /**
2753 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002754 *
2755 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002757 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002758 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002760 /**
Adam Powell20232d02010-12-08 21:08:53 -08002761 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002762 *
2763 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002764 */
2765 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002766 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002767
2768 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002769 * Cache if the user padding is relative.
2770 *
2771 */
2772 @ViewDebug.ExportedProperty(category = "padding")
2773 boolean mUserPaddingRelative;
2774
2775 /**
2776 * Cache the paddingStart set by the user to append to the scrollbar's size.
2777 *
2778 */
2779 @ViewDebug.ExportedProperty(category = "padding")
2780 int mUserPaddingStart;
2781
2782 /**
2783 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2784 *
2785 */
2786 @ViewDebug.ExportedProperty(category = "padding")
2787 int mUserPaddingEnd;
2788
2789 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002790 * @hide
2791 */
2792 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2793 /**
2794 * @hide
2795 */
2796 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797
Philip Milne6c8ea062012-04-03 17:38:43 -07002798 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
2800 private int mBackgroundResource;
2801 private boolean mBackgroundSizeChanged;
2802
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002803 static class ListenerInfo {
2804 /**
2805 * Listener used to dispatch focus change events.
2806 * This field should be made private, so it is hidden from the SDK.
2807 * {@hide}
2808 */
2809 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002811 /**
2812 * Listeners for layout change events.
2813 */
2814 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002815
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002816 /**
2817 * Listeners for attach events.
2818 */
2819 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002820
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002821 /**
2822 * Listener used to dispatch click events.
2823 * This field should be made private, so it is hidden from the SDK.
2824 * {@hide}
2825 */
2826 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002828 /**
2829 * Listener used to dispatch long click events.
2830 * This field should be made private, so it is hidden from the SDK.
2831 * {@hide}
2832 */
2833 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002835 /**
2836 * Listener used to build the context menu.
2837 * This field should be made private, so it is hidden from the SDK.
2838 * {@hide}
2839 */
2840 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002842 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002844 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002846 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002847
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002848 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002851
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002852 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2853 }
2854
2855 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 /**
2858 * The application environment this view lives in.
2859 * This field should be made private, so it is hidden from the SDK.
2860 * {@hide}
2861 */
2862 protected Context mContext;
2863
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002864 private final Resources mResources;
2865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 private ScrollabilityCache mScrollCache;
2867
2868 private int[] mDrawableState = null;
2869
Romain Guy0211a0a2011-02-14 16:34:59 -08002870 /**
2871 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002872 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002873 * @hide
2874 */
2875 public boolean mCachingFailed;
2876
Romain Guy02890fd2010-08-06 17:58:44 -07002877 private Bitmap mDrawingCache;
2878 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002879 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002880 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
2882 /**
2883 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2884 * the user may specify which view to go to next.
2885 */
2886 private int mNextFocusLeftId = View.NO_ID;
2887
2888 /**
2889 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2890 * the user may specify which view to go to next.
2891 */
2892 private int mNextFocusRightId = View.NO_ID;
2893
2894 /**
2895 * When this view has focus and the next focus is {@link #FOCUS_UP},
2896 * the user may specify which view to go to next.
2897 */
2898 private int mNextFocusUpId = View.NO_ID;
2899
2900 /**
2901 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2902 * the user may specify which view to go to next.
2903 */
2904 private int mNextFocusDownId = View.NO_ID;
2905
Jeff Brown4e6319b2010-12-13 10:36:51 -08002906 /**
2907 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2908 * the user may specify which view to go to next.
2909 */
2910 int mNextFocusForwardId = View.NO_ID;
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002913 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002914 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002915 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 private UnsetPressedState mUnsetPressedState;
2918
2919 /**
2920 * Whether the long press's action has been invoked. The tap's action is invoked on the
2921 * up event while a long press is invoked as soon as the long press duration is reached, so
2922 * a long press could be performed before the tap is checked, in which case the tap's action
2923 * should not be invoked.
2924 */
2925 private boolean mHasPerformedLongPress;
2926
2927 /**
2928 * The minimum height of the view. We'll try our best to have the height
2929 * of this view to at least this amount.
2930 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002931 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 private int mMinHeight;
2933
2934 /**
2935 * The minimum width of the view. We'll try our best to have the width
2936 * of this view to at least this amount.
2937 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002938 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 private int mMinWidth;
2940
2941 /**
2942 * The delegate to handle touch events that are physically in this view
2943 * but should be handled by another view.
2944 */
2945 private TouchDelegate mTouchDelegate = null;
2946
2947 /**
2948 * Solid color to use as a background when creating the drawing cache. Enables
2949 * the cache to use 16 bit bitmaps instead of 32 bit.
2950 */
2951 private int mDrawingCacheBackgroundColor = 0;
2952
2953 /**
2954 * Special tree observer used when mAttachInfo is null.
2955 */
2956 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002957
Adam Powelle14579b2009-12-16 18:39:52 -08002958 /**
2959 * Cache the touch slop from the context that created the view.
2960 */
2961 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002964 * Object that handles automatic animation of view properties.
2965 */
2966 private ViewPropertyAnimator mAnimator = null;
2967
2968 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002969 * Flag indicating that a drag can cross window boundaries. When
2970 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2971 * with this flag set, all visible applications will be able to participate
2972 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002973 *
2974 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002975 */
2976 public static final int DRAG_FLAG_GLOBAL = 1;
2977
2978 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002979 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2980 */
2981 private float mVerticalScrollFactor;
2982
2983 /**
Adam Powell20232d02010-12-08 21:08:53 -08002984 * Position of the vertical scroll bar.
2985 */
2986 private int mVerticalScrollbarPosition;
2987
2988 /**
2989 * Position the scroll bar at the default position as determined by the system.
2990 */
2991 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2992
2993 /**
2994 * Position the scroll bar along the left edge.
2995 */
2996 public static final int SCROLLBAR_POSITION_LEFT = 1;
2997
2998 /**
2999 * Position the scroll bar along the right edge.
3000 */
3001 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3002
3003 /**
Romain Guy171c5922011-01-06 10:04:23 -08003004 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003005 *
3006 * @see #getLayerType()
3007 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003008 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003009 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003010 */
3011 public static final int LAYER_TYPE_NONE = 0;
3012
3013 /**
3014 * <p>Indicates that the view has a software layer. A software layer is backed
3015 * by a bitmap and causes the view to be rendered using Android's software
3016 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003017 *
Romain Guy171c5922011-01-06 10:04:23 -08003018 * <p>Software layers have various usages:</p>
3019 * <p>When the application is not using hardware acceleration, a software layer
3020 * is useful to apply a specific color filter and/or blending mode and/or
3021 * translucency to a view and all its children.</p>
3022 * <p>When the application is using hardware acceleration, a software layer
3023 * is useful to render drawing primitives not supported by the hardware
3024 * accelerated pipeline. It can also be used to cache a complex view tree
3025 * into a texture and reduce the complexity of drawing operations. For instance,
3026 * when animating a complex view tree with a translation, a software layer can
3027 * be used to render the view tree only once.</p>
3028 * <p>Software layers should be avoided when the affected view tree updates
3029 * often. Every update will require to re-render the software layer, which can
3030 * potentially be slow (particularly when hardware acceleration is turned on
3031 * since the layer will have to be uploaded into a hardware texture after every
3032 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003033 *
3034 * @see #getLayerType()
3035 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003036 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003037 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003038 */
3039 public static final int LAYER_TYPE_SOFTWARE = 1;
3040
3041 /**
3042 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3043 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3044 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3045 * rendering pipeline, but only if hardware acceleration is turned on for the
3046 * view hierarchy. When hardware acceleration is turned off, hardware layers
3047 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003048 *
Romain Guy171c5922011-01-06 10:04:23 -08003049 * <p>A hardware layer is useful to apply a specific color filter and/or
3050 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003051 * <p>A hardware layer can be used to cache a complex view tree into a
3052 * texture and reduce the complexity of drawing operations. For instance,
3053 * when animating a complex view tree with a translation, a hardware layer can
3054 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003055 * <p>A hardware layer can also be used to increase the rendering quality when
3056 * rotation transformations are applied on a view. It can also be used to
3057 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003058 *
3059 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003060 * @see #setLayerType(int, android.graphics.Paint)
3061 * @see #LAYER_TYPE_NONE
3062 * @see #LAYER_TYPE_SOFTWARE
3063 */
3064 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003065
Romain Guy3aaff3a2011-01-12 14:18:47 -08003066 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3067 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3068 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3069 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3070 })
Romain Guy171c5922011-01-06 10:04:23 -08003071 int mLayerType = LAYER_TYPE_NONE;
3072 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003073 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003074
3075 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003076 * Set to true when the view is sending hover accessibility events because it
3077 * is the innermost hovered view.
3078 */
3079 private boolean mSendingHoverAccessibilityEvents;
3080
3081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 * Simple constructor to use when creating a view from code.
3083 *
3084 * @param context The Context the view is running in, through which it can
3085 * access the current theme, resources, etc.
3086 */
3087 public View(Context context) {
3088 mContext = context;
3089 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003090 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003091 // Set layout and text direction defaults
3092 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003093 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003094 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07003095 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003096 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003097 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003098 mUserPaddingStart = -1;
3099 mUserPaddingEnd = -1;
3100 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003104 * Delegate for injecting accessiblity functionality.
3105 */
3106 AccessibilityDelegate mAccessibilityDelegate;
3107
3108 /**
3109 * Consistency verifier for debugging purposes.
3110 * @hide
3111 */
3112 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3113 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3114 new InputEventConsistencyVerifier(this, 0) : null;
3115
3116 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 * Constructor that is called when inflating a view from XML. This is called
3118 * when a view is being constructed from an XML file, supplying attributes
3119 * that were specified in the XML file. This version uses a default style of
3120 * 0, so the only attribute values applied are those in the Context's Theme
3121 * and the given AttributeSet.
3122 *
3123 * <p>
3124 * The method onFinishInflate() will be called after all children have been
3125 * added.
3126 *
3127 * @param context The Context the view is running in, through which it can
3128 * access the current theme, resources, etc.
3129 * @param attrs The attributes of the XML tag that is inflating the view.
3130 * @see #View(Context, AttributeSet, int)
3131 */
3132 public View(Context context, AttributeSet attrs) {
3133 this(context, attrs, 0);
3134 }
3135
3136 /**
3137 * Perform inflation from XML and apply a class-specific base style. This
3138 * constructor of View allows subclasses to use their own base style when
3139 * they are inflating. For example, a Button class's constructor would call
3140 * this version of the super class constructor and supply
3141 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3142 * the theme's button style to modify all of the base view attributes (in
3143 * particular its background) as well as the Button class's attributes.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 * @param attrs The attributes of the XML tag that is inflating the view.
3148 * @param defStyle The default style to apply to this view. If 0, no style
3149 * will be applied (beyond what is included in the theme). This may
3150 * either be an attribute resource, whose value will be retrieved
3151 * from the current theme, or an explicit style resource.
3152 * @see #View(Context, AttributeSet)
3153 */
3154 public View(Context context, AttributeSet attrs, int defStyle) {
3155 this(context);
3156
3157 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3158 defStyle, 0);
3159
3160 Drawable background = null;
3161
3162 int leftPadding = -1;
3163 int topPadding = -1;
3164 int rightPadding = -1;
3165 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003166 int startPadding = -1;
3167 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
3169 int padding = -1;
3170
3171 int viewFlagValues = 0;
3172 int viewFlagMasks = 0;
3173
3174 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 int x = 0;
3177 int y = 0;
3178
Chet Haase73066682010-11-29 15:55:32 -08003179 float tx = 0;
3180 float ty = 0;
3181 float rotation = 0;
3182 float rotationX = 0;
3183 float rotationY = 0;
3184 float sx = 1f;
3185 float sy = 1f;
3186 boolean transformSet = false;
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3189
Adam Powell637d3372010-08-25 14:37:03 -07003190 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 final int N = a.getIndexCount();
3192 for (int i = 0; i < N; i++) {
3193 int attr = a.getIndex(i);
3194 switch (attr) {
3195 case com.android.internal.R.styleable.View_background:
3196 background = a.getDrawable(attr);
3197 break;
3198 case com.android.internal.R.styleable.View_padding:
3199 padding = a.getDimensionPixelSize(attr, -1);
3200 break;
3201 case com.android.internal.R.styleable.View_paddingLeft:
3202 leftPadding = a.getDimensionPixelSize(attr, -1);
3203 break;
3204 case com.android.internal.R.styleable.View_paddingTop:
3205 topPadding = a.getDimensionPixelSize(attr, -1);
3206 break;
3207 case com.android.internal.R.styleable.View_paddingRight:
3208 rightPadding = a.getDimensionPixelSize(attr, -1);
3209 break;
3210 case com.android.internal.R.styleable.View_paddingBottom:
3211 bottomPadding = a.getDimensionPixelSize(attr, -1);
3212 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003213 case com.android.internal.R.styleable.View_paddingStart:
3214 startPadding = a.getDimensionPixelSize(attr, -1);
3215 break;
3216 case com.android.internal.R.styleable.View_paddingEnd:
3217 endPadding = a.getDimensionPixelSize(attr, -1);
3218 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 case com.android.internal.R.styleable.View_scrollX:
3220 x = a.getDimensionPixelOffset(attr, 0);
3221 break;
3222 case com.android.internal.R.styleable.View_scrollY:
3223 y = a.getDimensionPixelOffset(attr, 0);
3224 break;
Chet Haase73066682010-11-29 15:55:32 -08003225 case com.android.internal.R.styleable.View_alpha:
3226 setAlpha(a.getFloat(attr, 1f));
3227 break;
3228 case com.android.internal.R.styleable.View_transformPivotX:
3229 setPivotX(a.getDimensionPixelOffset(attr, 0));
3230 break;
3231 case com.android.internal.R.styleable.View_transformPivotY:
3232 setPivotY(a.getDimensionPixelOffset(attr, 0));
3233 break;
3234 case com.android.internal.R.styleable.View_translationX:
3235 tx = a.getDimensionPixelOffset(attr, 0);
3236 transformSet = true;
3237 break;
3238 case com.android.internal.R.styleable.View_translationY:
3239 ty = a.getDimensionPixelOffset(attr, 0);
3240 transformSet = true;
3241 break;
3242 case com.android.internal.R.styleable.View_rotation:
3243 rotation = a.getFloat(attr, 0);
3244 transformSet = true;
3245 break;
3246 case com.android.internal.R.styleable.View_rotationX:
3247 rotationX = a.getFloat(attr, 0);
3248 transformSet = true;
3249 break;
3250 case com.android.internal.R.styleable.View_rotationY:
3251 rotationY = a.getFloat(attr, 0);
3252 transformSet = true;
3253 break;
3254 case com.android.internal.R.styleable.View_scaleX:
3255 sx = a.getFloat(attr, 1f);
3256 transformSet = true;
3257 break;
3258 case com.android.internal.R.styleable.View_scaleY:
3259 sy = a.getFloat(attr, 1f);
3260 transformSet = true;
3261 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003262 case com.android.internal.R.styleable.View_id:
3263 mID = a.getResourceId(attr, NO_ID);
3264 break;
3265 case com.android.internal.R.styleable.View_tag:
3266 mTag = a.getText(attr);
3267 break;
3268 case com.android.internal.R.styleable.View_fitsSystemWindows:
3269 if (a.getBoolean(attr, false)) {
3270 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3271 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3272 }
3273 break;
3274 case com.android.internal.R.styleable.View_focusable:
3275 if (a.getBoolean(attr, false)) {
3276 viewFlagValues |= FOCUSABLE;
3277 viewFlagMasks |= FOCUSABLE_MASK;
3278 }
3279 break;
3280 case com.android.internal.R.styleable.View_focusableInTouchMode:
3281 if (a.getBoolean(attr, false)) {
3282 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3283 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3284 }
3285 break;
3286 case com.android.internal.R.styleable.View_clickable:
3287 if (a.getBoolean(attr, false)) {
3288 viewFlagValues |= CLICKABLE;
3289 viewFlagMasks |= CLICKABLE;
3290 }
3291 break;
3292 case com.android.internal.R.styleable.View_longClickable:
3293 if (a.getBoolean(attr, false)) {
3294 viewFlagValues |= LONG_CLICKABLE;
3295 viewFlagMasks |= LONG_CLICKABLE;
3296 }
3297 break;
3298 case com.android.internal.R.styleable.View_saveEnabled:
3299 if (!a.getBoolean(attr, true)) {
3300 viewFlagValues |= SAVE_DISABLED;
3301 viewFlagMasks |= SAVE_DISABLED_MASK;
3302 }
3303 break;
3304 case com.android.internal.R.styleable.View_duplicateParentState:
3305 if (a.getBoolean(attr, false)) {
3306 viewFlagValues |= DUPLICATE_PARENT_STATE;
3307 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3308 }
3309 break;
3310 case com.android.internal.R.styleable.View_visibility:
3311 final int visibility = a.getInt(attr, 0);
3312 if (visibility != 0) {
3313 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3314 viewFlagMasks |= VISIBILITY_MASK;
3315 }
3316 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003317 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003318 // Clear any layout direction flags (included resolved bits) already set
3319 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3320 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003321 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003322 final int value = (layoutDirection != -1) ?
3323 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3324 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003325 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 case com.android.internal.R.styleable.View_drawingCacheQuality:
3327 final int cacheQuality = a.getInt(attr, 0);
3328 if (cacheQuality != 0) {
3329 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3330 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3331 }
3332 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003333 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003334 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003335 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3337 if (!a.getBoolean(attr, true)) {
3338 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3339 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3340 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003341 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3343 if (!a.getBoolean(attr, true)) {
3344 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3345 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3346 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003347 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 case R.styleable.View_scrollbars:
3349 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3350 if (scrollbars != SCROLLBARS_NONE) {
3351 viewFlagValues |= scrollbars;
3352 viewFlagMasks |= SCROLLBARS_MASK;
3353 initializeScrollbars(a);
3354 }
3355 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003356 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003358 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3359 // Ignore the attribute starting with ICS
3360 break;
3361 }
3362 // With builds < ICS, fall through and apply fading edges
3363 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3365 if (fadingEdge != FADING_EDGE_NONE) {
3366 viewFlagValues |= fadingEdge;
3367 viewFlagMasks |= FADING_EDGE_MASK;
3368 initializeFadingEdge(a);
3369 }
3370 break;
3371 case R.styleable.View_scrollbarStyle:
3372 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3373 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3374 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3375 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3376 }
3377 break;
3378 case R.styleable.View_isScrollContainer:
3379 setScrollContainer = true;
3380 if (a.getBoolean(attr, false)) {
3381 setScrollContainer(true);
3382 }
3383 break;
3384 case com.android.internal.R.styleable.View_keepScreenOn:
3385 if (a.getBoolean(attr, false)) {
3386 viewFlagValues |= KEEP_SCREEN_ON;
3387 viewFlagMasks |= KEEP_SCREEN_ON;
3388 }
3389 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003390 case R.styleable.View_filterTouchesWhenObscured:
3391 if (a.getBoolean(attr, false)) {
3392 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3393 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3394 }
3395 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 case R.styleable.View_nextFocusLeft:
3397 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3398 break;
3399 case R.styleable.View_nextFocusRight:
3400 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3401 break;
3402 case R.styleable.View_nextFocusUp:
3403 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3404 break;
3405 case R.styleable.View_nextFocusDown:
3406 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3407 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003408 case R.styleable.View_nextFocusForward:
3409 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3410 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 case R.styleable.View_minWidth:
3412 mMinWidth = a.getDimensionPixelSize(attr, 0);
3413 break;
3414 case R.styleable.View_minHeight:
3415 mMinHeight = a.getDimensionPixelSize(attr, 0);
3416 break;
Romain Guy9a817362009-05-01 10:57:14 -07003417 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003418 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003419 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003420 + "be used within a restricted context");
3421 }
3422
Romain Guy9a817362009-05-01 10:57:14 -07003423 final String handlerName = a.getString(attr);
3424 if (handlerName != null) {
3425 setOnClickListener(new OnClickListener() {
3426 private Method mHandler;
3427
3428 public void onClick(View v) {
3429 if (mHandler == null) {
3430 try {
3431 mHandler = getContext().getClass().getMethod(handlerName,
3432 View.class);
3433 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003434 int id = getId();
3435 String idText = id == NO_ID ? "" : " with id '"
3436 + getContext().getResources().getResourceEntryName(
3437 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003438 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003439 handlerName + "(View) in the activity "
3440 + getContext().getClass() + " for onClick handler"
3441 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003442 }
3443 }
3444
3445 try {
3446 mHandler.invoke(getContext(), View.this);
3447 } catch (IllegalAccessException e) {
3448 throw new IllegalStateException("Could not execute non "
3449 + "public method of the activity", e);
3450 } catch (InvocationTargetException e) {
3451 throw new IllegalStateException("Could not execute "
3452 + "method of the activity", e);
3453 }
3454 }
3455 });
3456 }
3457 break;
Adam Powell637d3372010-08-25 14:37:03 -07003458 case R.styleable.View_overScrollMode:
3459 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3460 break;
Adam Powell20232d02010-12-08 21:08:53 -08003461 case R.styleable.View_verticalScrollbarPosition:
3462 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3463 break;
Romain Guy171c5922011-01-06 10:04:23 -08003464 case R.styleable.View_layerType:
3465 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3466 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003467 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003468 // Clear any text direction flag already set
3469 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3470 // Set the text direction flags depending on the value of the attribute
3471 final int textDirection = a.getInt(attr, -1);
3472 if (textDirection != -1) {
3473 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3474 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003475 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003476 case R.styleable.View_textAlignment:
3477 // Clear any text alignment flag already set
3478 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3479 // Set the text alignment flag depending on the value of the attribute
3480 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3481 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3482 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003483 case R.styleable.View_importantForAccessibility:
3484 setImportantForAccessibility(a.getInt(attr,
3485 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003486 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
3488 }
3489
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003490 a.recycle();
3491
Adam Powell637d3372010-08-25 14:37:03 -07003492 setOverScrollMode(overScrollMode);
3493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003495 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 }
3497
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003498 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3499 // layout direction). Those cached values will be used later during padding resolution.
3500 mUserPaddingStart = startPadding;
3501 mUserPaddingEnd = endPadding;
3502
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003503 updateUserPaddingRelative();
3504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 if (padding >= 0) {
3506 leftPadding = padding;
3507 topPadding = padding;
3508 rightPadding = padding;
3509 bottomPadding = padding;
3510 }
3511
3512 // If the user specified the padding (either with android:padding or
3513 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3514 // use the default padding or the padding from the background drawable
3515 // (stored at this point in mPadding*)
3516 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3517 topPadding >= 0 ? topPadding : mPaddingTop,
3518 rightPadding >= 0 ? rightPadding : mPaddingRight,
3519 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3520
3521 if (viewFlagMasks != 0) {
3522 setFlags(viewFlagValues, viewFlagMasks);
3523 }
3524
3525 // Needs to be called after mViewFlags is set
3526 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3527 recomputePadding();
3528 }
3529
3530 if (x != 0 || y != 0) {
3531 scrollTo(x, y);
3532 }
3533
Chet Haase73066682010-11-29 15:55:32 -08003534 if (transformSet) {
3535 setTranslationX(tx);
3536 setTranslationY(ty);
3537 setRotation(rotation);
3538 setRotationX(rotationX);
3539 setRotationY(rotationY);
3540 setScaleX(sx);
3541 setScaleY(sy);
3542 }
3543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3545 setScrollContainer(true);
3546 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003547
3548 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 }
3550
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003551 private void updateUserPaddingRelative() {
3552 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3553 }
3554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 /**
3556 * Non-public constructor for use in testing
3557 */
3558 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003559 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 }
3561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 /**
3563 * <p>
3564 * Initializes the fading edges from a given set of styled attributes. This
3565 * method should be called by subclasses that need fading edges and when an
3566 * instance of these subclasses is created programmatically rather than
3567 * being inflated from XML. This method is automatically called when the XML
3568 * is inflated.
3569 * </p>
3570 *
3571 * @param a the styled attributes set to initialize the fading edges from
3572 */
3573 protected void initializeFadingEdge(TypedArray a) {
3574 initScrollCache();
3575
3576 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3577 R.styleable.View_fadingEdgeLength,
3578 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3579 }
3580
3581 /**
3582 * Returns the size of the vertical faded edges used to indicate that more
3583 * content in this view is visible.
3584 *
3585 * @return The size in pixels of the vertical faded edge or 0 if vertical
3586 * faded edges are not enabled for this view.
3587 * @attr ref android.R.styleable#View_fadingEdgeLength
3588 */
3589 public int getVerticalFadingEdgeLength() {
3590 if (isVerticalFadingEdgeEnabled()) {
3591 ScrollabilityCache cache = mScrollCache;
3592 if (cache != null) {
3593 return cache.fadingEdgeLength;
3594 }
3595 }
3596 return 0;
3597 }
3598
3599 /**
3600 * Set the size of the faded edge used to indicate that more content in this
3601 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003602 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3603 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3604 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003605 *
3606 * @param length The size in pixels of the faded edge used to indicate that more
3607 * content in this view is visible.
3608 */
3609 public void setFadingEdgeLength(int length) {
3610 initScrollCache();
3611 mScrollCache.fadingEdgeLength = length;
3612 }
3613
3614 /**
3615 * Returns the size of the horizontal faded edges used to indicate that more
3616 * content in this view is visible.
3617 *
3618 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3619 * faded edges are not enabled for this view.
3620 * @attr ref android.R.styleable#View_fadingEdgeLength
3621 */
3622 public int getHorizontalFadingEdgeLength() {
3623 if (isHorizontalFadingEdgeEnabled()) {
3624 ScrollabilityCache cache = mScrollCache;
3625 if (cache != null) {
3626 return cache.fadingEdgeLength;
3627 }
3628 }
3629 return 0;
3630 }
3631
3632 /**
3633 * Returns the width of the vertical scrollbar.
3634 *
3635 * @return The width in pixels of the vertical scrollbar or 0 if there
3636 * is no vertical scrollbar.
3637 */
3638 public int getVerticalScrollbarWidth() {
3639 ScrollabilityCache cache = mScrollCache;
3640 if (cache != null) {
3641 ScrollBarDrawable scrollBar = cache.scrollBar;
3642 if (scrollBar != null) {
3643 int size = scrollBar.getSize(true);
3644 if (size <= 0) {
3645 size = cache.scrollBarSize;
3646 }
3647 return size;
3648 }
3649 return 0;
3650 }
3651 return 0;
3652 }
3653
3654 /**
3655 * Returns the height of the horizontal scrollbar.
3656 *
3657 * @return The height in pixels of the horizontal scrollbar or 0 if
3658 * there is no horizontal scrollbar.
3659 */
3660 protected int getHorizontalScrollbarHeight() {
3661 ScrollabilityCache cache = mScrollCache;
3662 if (cache != null) {
3663 ScrollBarDrawable scrollBar = cache.scrollBar;
3664 if (scrollBar != null) {
3665 int size = scrollBar.getSize(false);
3666 if (size <= 0) {
3667 size = cache.scrollBarSize;
3668 }
3669 return size;
3670 }
3671 return 0;
3672 }
3673 return 0;
3674 }
3675
3676 /**
3677 * <p>
3678 * Initializes the scrollbars from a given set of styled attributes. This
3679 * method should be called by subclasses that need scrollbars and when an
3680 * instance of these subclasses is created programmatically rather than
3681 * being inflated from XML. This method is automatically called when the XML
3682 * is inflated.
3683 * </p>
3684 *
3685 * @param a the styled attributes set to initialize the scrollbars from
3686 */
3687 protected void initializeScrollbars(TypedArray a) {
3688 initScrollCache();
3689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003691
Mike Cleronf116bf82009-09-27 19:14:12 -07003692 if (scrollabilityCache.scrollBar == null) {
3693 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3694 }
Joe Malin32736f02011-01-19 16:14:20 -08003695
Romain Guy8bda2482010-03-02 11:42:11 -08003696 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697
Mike Cleronf116bf82009-09-27 19:14:12 -07003698 if (!fadeScrollbars) {
3699 scrollabilityCache.state = ScrollabilityCache.ON;
3700 }
3701 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003702
3703
Mike Cleronf116bf82009-09-27 19:14:12 -07003704 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3705 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3706 .getScrollBarFadeDuration());
3707 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3708 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3709 ViewConfiguration.getScrollDefaultDelay());
3710
Joe Malin32736f02011-01-19 16:14:20 -08003711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3713 com.android.internal.R.styleable.View_scrollbarSize,
3714 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3715
3716 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3717 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3718
3719 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3720 if (thumb != null) {
3721 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3722 }
3723
3724 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3725 false);
3726 if (alwaysDraw) {
3727 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3728 }
3729
3730 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3731 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3732
3733 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3734 if (thumb != null) {
3735 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3736 }
3737
3738 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3739 false);
3740 if (alwaysDraw) {
3741 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3742 }
3743
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003744 // Apply layout direction to the new Drawables if needed
3745 final int layoutDirection = getResolvedLayoutDirection();
3746 if (track != null) {
3747 track.setLayoutDirection(layoutDirection);
3748 }
3749 if (thumb != null) {
3750 thumb.setLayoutDirection(layoutDirection);
3751 }
3752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003753 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003754 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 }
3756
3757 /**
3758 * <p>
3759 * Initalizes the scrollability cache if necessary.
3760 * </p>
3761 */
3762 private void initScrollCache() {
3763 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003764 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 }
3766 }
3767
Philip Milne6c8ea062012-04-03 17:38:43 -07003768 private ScrollabilityCache getScrollCache() {
3769 initScrollCache();
3770 return mScrollCache;
3771 }
3772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 /**
Adam Powell20232d02010-12-08 21:08:53 -08003774 * Set the position of the vertical scroll bar. Should be one of
3775 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3776 * {@link #SCROLLBAR_POSITION_RIGHT}.
3777 *
3778 * @param position Where the vertical scroll bar should be positioned.
3779 */
3780 public void setVerticalScrollbarPosition(int position) {
3781 if (mVerticalScrollbarPosition != position) {
3782 mVerticalScrollbarPosition = position;
3783 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003784 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003785 }
3786 }
3787
3788 /**
3789 * @return The position where the vertical scroll bar will show, if applicable.
3790 * @see #setVerticalScrollbarPosition(int)
3791 */
3792 public int getVerticalScrollbarPosition() {
3793 return mVerticalScrollbarPosition;
3794 }
3795
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003796 ListenerInfo getListenerInfo() {
3797 if (mListenerInfo != null) {
3798 return mListenerInfo;
3799 }
3800 mListenerInfo = new ListenerInfo();
3801 return mListenerInfo;
3802 }
3803
Adam Powell20232d02010-12-08 21:08:53 -08003804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 * Register a callback to be invoked when focus of this view changed.
3806 *
3807 * @param l The callback that will run.
3808 */
3809 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003810 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 }
3812
3813 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003814 * Add a listener that will be called when the bounds of the view change due to
3815 * layout processing.
3816 *
3817 * @param listener The listener that will be called when layout bounds change.
3818 */
3819 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003820 ListenerInfo li = getListenerInfo();
3821 if (li.mOnLayoutChangeListeners == null) {
3822 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003823 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3825 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003826 }
Chet Haase21cd1382010-09-01 17:42:29 -07003827 }
3828
3829 /**
3830 * Remove a listener for layout changes.
3831 *
3832 * @param listener The listener for layout bounds change.
3833 */
3834 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003835 ListenerInfo li = mListenerInfo;
3836 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003837 return;
3838 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003839 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003840 }
3841
3842 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003843 * Add a listener for attach state changes.
3844 *
3845 * This listener will be called whenever this view is attached or detached
3846 * from a window. Remove the listener using
3847 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3848 *
3849 * @param listener Listener to attach
3850 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3851 */
3852 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003853 ListenerInfo li = getListenerInfo();
3854 if (li.mOnAttachStateChangeListeners == null) {
3855 li.mOnAttachStateChangeListeners
3856 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003857 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003858 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003859 }
3860
3861 /**
3862 * Remove a listener for attach state changes. The listener will receive no further
3863 * notification of window attach/detach events.
3864 *
3865 * @param listener Listener to remove
3866 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3867 */
3868 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003869 ListenerInfo li = mListenerInfo;
3870 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003871 return;
3872 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003873 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003874 }
3875
3876 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 * Returns the focus-change callback registered for this view.
3878 *
3879 * @return The callback, or null if one is not registered.
3880 */
3881 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003882 ListenerInfo li = mListenerInfo;
3883 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
3885
3886 /**
3887 * Register a callback to be invoked when this view is clicked. If this view is not
3888 * clickable, it becomes clickable.
3889 *
3890 * @param l The callback that will run
3891 *
3892 * @see #setClickable(boolean)
3893 */
3894 public void setOnClickListener(OnClickListener l) {
3895 if (!isClickable()) {
3896 setClickable(true);
3897 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003898 getListenerInfo().mOnClickListener = l;
3899 }
3900
3901 /**
3902 * Return whether this view has an attached OnClickListener. Returns
3903 * true if there is a listener, false if there is none.
3904 */
3905 public boolean hasOnClickListeners() {
3906 ListenerInfo li = mListenerInfo;
3907 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 }
3909
3910 /**
3911 * Register a callback to be invoked when this view is clicked and held. If this view is not
3912 * long clickable, it becomes long clickable.
3913 *
3914 * @param l The callback that will run
3915 *
3916 * @see #setLongClickable(boolean)
3917 */
3918 public void setOnLongClickListener(OnLongClickListener l) {
3919 if (!isLongClickable()) {
3920 setLongClickable(true);
3921 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003922 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 }
3924
3925 /**
3926 * Register a callback to be invoked when the context menu for this view is
3927 * being built. If this view is not long clickable, it becomes long clickable.
3928 *
3929 * @param l The callback that will run
3930 *
3931 */
3932 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3933 if (!isLongClickable()) {
3934 setLongClickable(true);
3935 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003936 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 }
3938
3939 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003940 * Call this view's OnClickListener, if it is defined. Performs all normal
3941 * actions associated with clicking: reporting accessibility event, playing
3942 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 *
3944 * @return True there was an assigned OnClickListener that was called, false
3945 * otherwise is returned.
3946 */
3947 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003948 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3949
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003950 ListenerInfo li = mListenerInfo;
3951 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003953 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 return true;
3955 }
3956
3957 return false;
3958 }
3959
3960 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003961 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3962 * this only calls the listener, and does not do any associated clicking
3963 * actions like reporting an accessibility event.
3964 *
3965 * @return True there was an assigned OnClickListener that was called, false
3966 * otherwise is returned.
3967 */
3968 public boolean callOnClick() {
3969 ListenerInfo li = mListenerInfo;
3970 if (li != null && li.mOnClickListener != null) {
3971 li.mOnClickListener.onClick(this);
3972 return true;
3973 }
3974 return false;
3975 }
3976
3977 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003978 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3979 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003981 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 */
3983 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003984 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003987 ListenerInfo li = mListenerInfo;
3988 if (li != null && li.mOnLongClickListener != null) {
3989 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991 if (!handled) {
3992 handled = showContextMenu();
3993 }
3994 if (handled) {
3995 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3996 }
3997 return handled;
3998 }
3999
4000 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004001 * Performs button-related actions during a touch down event.
4002 *
4003 * @param event The event.
4004 * @return True if the down was consumed.
4005 *
4006 * @hide
4007 */
4008 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4009 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4010 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4011 return true;
4012 }
4013 }
4014 return false;
4015 }
4016
4017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 * Bring up the context menu for this view.
4019 *
4020 * @return Whether a context menu was displayed.
4021 */
4022 public boolean showContextMenu() {
4023 return getParent().showContextMenuForChild(this);
4024 }
4025
4026 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004027 * Bring up the context menu for this view, referring to the item under the specified point.
4028 *
4029 * @param x The referenced x coordinate.
4030 * @param y The referenced y coordinate.
4031 * @param metaState The keyboard modifiers that were pressed.
4032 * @return Whether a context menu was displayed.
4033 *
4034 * @hide
4035 */
4036 public boolean showContextMenu(float x, float y, int metaState) {
4037 return showContextMenu();
4038 }
4039
4040 /**
Adam Powell6e346362010-07-23 10:18:23 -07004041 * Start an action mode.
4042 *
4043 * @param callback Callback that will control the lifecycle of the action mode
4044 * @return The new action mode if it is started, null otherwise
4045 *
4046 * @see ActionMode
4047 */
4048 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004049 ViewParent parent = getParent();
4050 if (parent == null) return null;
4051 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004052 }
4053
4054 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004055 * Register a callback to be invoked when a hardware key is pressed in this view.
4056 * Key presses in software input methods will generally not trigger the methods of
4057 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 * @param l the key listener to attach to this view
4059 */
4060 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063
4064 /**
4065 * Register a callback to be invoked when a touch event is sent to this view.
4066 * @param l the touch listener to attach to this view
4067 */
4068 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 }
4071
4072 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004073 * Register a callback to be invoked when a generic motion event is sent to this view.
4074 * @param l the generic motion listener to attach to this view
4075 */
4076 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004078 }
4079
4080 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004081 * Register a callback to be invoked when a hover event is sent to this view.
4082 * @param l the hover listener to attach to this view
4083 */
4084 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004085 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004086 }
4087
4088 /**
Joe Malin32736f02011-01-19 16:14:20 -08004089 * Register a drag event listener callback object for this View. The parameter is
4090 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4091 * View, the system calls the
4092 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4093 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004094 */
4095 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004096 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004097 }
4098
4099 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004100 * Give this view focus. This will cause
4101 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 *
4103 * Note: this does not check whether this {@link View} should get focus, it just
4104 * gives it focus no matter what. It should only be called internally by framework
4105 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4106 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004107 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4108 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 * focus moved when requestFocus() is called. It may not always
4110 * apply, in which case use the default View.FOCUS_DOWN.
4111 * @param previouslyFocusedRect The rectangle of the view that had focus
4112 * prior in this View's coordinate system.
4113 */
4114 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4115 if (DBG) {
4116 System.out.println(this + " requestFocus()");
4117 }
4118
4119 if ((mPrivateFlags & FOCUSED) == 0) {
4120 mPrivateFlags |= FOCUSED;
4121
4122 if (mParent != null) {
4123 mParent.requestChildFocus(this, this);
4124 }
4125
4126 onFocusChanged(true, direction, previouslyFocusedRect);
4127 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004128
4129 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4130 notifyAccessibilityStateChanged();
4131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
4133 }
4134
4135 /**
4136 * Request that a rectangle of this view be visible on the screen,
4137 * scrolling if necessary just enough.
4138 *
4139 * <p>A View should call this if it maintains some notion of which part
4140 * of its content is interesting. For example, a text editing view
4141 * should call this when its cursor moves.
4142 *
4143 * @param rectangle The rectangle.
4144 * @return Whether any parent scrolled.
4145 */
4146 public boolean requestRectangleOnScreen(Rect rectangle) {
4147 return requestRectangleOnScreen(rectangle, false);
4148 }
4149
4150 /**
4151 * Request that a rectangle of this view be visible on the screen,
4152 * scrolling if necessary just enough.
4153 *
4154 * <p>A View should call this if it maintains some notion of which part
4155 * of its content is interesting. For example, a text editing view
4156 * should call this when its cursor moves.
4157 *
4158 * <p>When <code>immediate</code> is set to true, scrolling will not be
4159 * animated.
4160 *
4161 * @param rectangle The rectangle.
4162 * @param immediate True to forbid animated scrolling, false otherwise
4163 * @return Whether any parent scrolled.
4164 */
4165 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4166 View child = this;
4167 ViewParent parent = mParent;
4168 boolean scrolled = false;
4169 while (parent != null) {
4170 scrolled |= parent.requestChildRectangleOnScreen(child,
4171 rectangle, immediate);
4172
4173 // offset rect so next call has the rectangle in the
4174 // coordinate system of its direct child.
4175 rectangle.offset(child.getLeft(), child.getTop());
4176 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4177
4178 if (!(parent instanceof View)) {
4179 break;
4180 }
Romain Guy8506ab42009-06-11 17:35:47 -07004181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 child = (View) parent;
4183 parent = child.getParent();
4184 }
4185 return scrolled;
4186 }
4187
4188 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004189 * Called when this view wants to give up focus. If focus is cleared
4190 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4191 * <p>
4192 * <strong>Note:</strong> When a View clears focus the framework is trying
4193 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004194 * View is the first from the top that can take focus, then all callbacks
4195 * related to clearing focus will be invoked after wich the framework will
4196 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004197 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 */
4199 public void clearFocus() {
4200 if (DBG) {
4201 System.out.println(this + " clearFocus()");
4202 }
4203
4204 if ((mPrivateFlags & FOCUSED) != 0) {
4205 mPrivateFlags &= ~FOCUSED;
4206
4207 if (mParent != null) {
4208 mParent.clearChildFocus(this);
4209 }
4210
4211 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004214
4215 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004216
4217 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4218 notifyAccessibilityStateChanged();
4219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 }
4221 }
4222
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004223 void ensureInputFocusOnFirstFocusable() {
4224 View root = getRootView();
4225 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004226 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228 }
4229
4230 /**
4231 * Called internally by the view system when a new view is getting focus.
4232 * This is what clears the old focus.
4233 */
4234 void unFocus() {
4235 if (DBG) {
4236 System.out.println(this + " unFocus()");
4237 }
4238
4239 if ((mPrivateFlags & FOCUSED) != 0) {
4240 mPrivateFlags &= ~FOCUSED;
4241
4242 onFocusChanged(false, 0, null);
4243 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
4251 /**
4252 * Returns true if this view has focus iteself, or is the ancestor of the
4253 * view that has focus.
4254 *
4255 * @return True if this view has or contains focus, false otherwise.
4256 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004257 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 public boolean hasFocus() {
4259 return (mPrivateFlags & FOCUSED) != 0;
4260 }
4261
4262 /**
4263 * Returns true if this view is focusable or if it contains a reachable View
4264 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4265 * is a View whose parents do not block descendants focus.
4266 *
4267 * Only {@link #VISIBLE} views are considered focusable.
4268 *
4269 * @return True if the view is focusable or if the view contains a focusable
4270 * View, false otherwise.
4271 *
4272 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4273 */
4274 public boolean hasFocusable() {
4275 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4276 }
4277
4278 /**
4279 * Called by the view system when the focus state of this view changes.
4280 * When the focus change event is caused by directional navigation, direction
4281 * and previouslyFocusedRect provide insight into where the focus is coming from.
4282 * When overriding, be sure to call up through to the super class so that
4283 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 * @param gainFocus True if the View has focus; false otherwise.
4286 * @param direction The direction focus has moved when requestFocus()
4287 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004288 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4289 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4290 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4292 * system, of the previously focused view. If applicable, this will be
4293 * passed in as finer grained information about where the focus is coming
4294 * from (in addition to direction). Will be <code>null</code> otherwise.
4295 */
4296 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004297 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004298 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4299 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004300 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004301 }
4302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 InputMethodManager imm = InputMethodManager.peekInstance();
4304 if (!gainFocus) {
4305 if (isPressed()) {
4306 setPressed(false);
4307 }
4308 if (imm != null && mAttachInfo != null
4309 && mAttachInfo.mHasWindowFocus) {
4310 imm.focusOut(this);
4311 }
Romain Guya2431d02009-04-30 16:30:00 -07004312 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004313 } else if (imm != null && mAttachInfo != null
4314 && mAttachInfo.mHasWindowFocus) {
4315 imm.focusIn(this);
4316 }
Romain Guy8506ab42009-06-11 17:35:47 -07004317
Romain Guy0fd89bf2011-01-26 15:41:30 -08004318 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004319 ListenerInfo li = mListenerInfo;
4320 if (li != null && li.mOnFocusChangeListener != null) {
4321 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 }
Joe Malin32736f02011-01-19 16:14:20 -08004323
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004324 if (mAttachInfo != null) {
4325 mAttachInfo.mKeyDispatchState.reset(this);
4326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328
4329 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004330 * Sends an accessibility event of the given type. If accessiiblity is
4331 * not enabled this method has no effect. The default implementation calls
4332 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4333 * to populate information about the event source (this View), then calls
4334 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4335 * populate the text content of the event source including its descendants,
4336 * and last calls
4337 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4338 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004339 * <p>
4340 * If an {@link AccessibilityDelegate} has been specified via calling
4341 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4342 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4343 * responsible for handling this call.
4344 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004345 *
Scott Mainb303d832011-10-12 16:45:18 -07004346 * @param eventType The type of the event to send, as defined by several types from
4347 * {@link android.view.accessibility.AccessibilityEvent}, such as
4348 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4349 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004350 *
4351 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4352 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4353 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004354 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004355 */
4356 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004357 if (mAccessibilityDelegate != null) {
4358 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4359 } else {
4360 sendAccessibilityEventInternal(eventType);
4361 }
4362 }
4363
4364 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004365 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4366 * {@link AccessibilityEvent} to make an announcement which is related to some
4367 * sort of a context change for which none of the events representing UI transitions
4368 * is a good fit. For example, announcing a new page in a book. If accessibility
4369 * is not enabled this method does nothing.
4370 *
4371 * @param text The announcement text.
4372 */
4373 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004374 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004375 AccessibilityEvent event = AccessibilityEvent.obtain(
4376 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004377 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004378 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004379 event.setContentDescription(null);
4380 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004381 }
4382 }
4383
4384 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004385 * @see #sendAccessibilityEvent(int)
4386 *
4387 * Note: Called from the default {@link AccessibilityDelegate}.
4388 */
4389 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004390 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4391 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4392 }
4393 }
4394
4395 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004396 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4397 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004398 * perform a check whether accessibility is enabled.
4399 * <p>
4400 * If an {@link AccessibilityDelegate} has been specified via calling
4401 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4402 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4403 * is responsible for handling this call.
4404 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004405 *
4406 * @param event The event to send.
4407 *
4408 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004409 */
4410 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004411 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004412 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004413 } else {
4414 sendAccessibilityEventUncheckedInternal(event);
4415 }
4416 }
4417
4418 /**
4419 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4420 *
4421 * Note: Called from the default {@link AccessibilityDelegate}.
4422 */
4423 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004424 if (!isShown()) {
4425 return;
4426 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004427 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004428 // Only a subset of accessibility events populates text content.
4429 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4430 dispatchPopulateAccessibilityEvent(event);
4431 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004432 // In the beginning we called #isShown(), so we know that getParent() is not null.
4433 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004434 }
4435
4436 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004437 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4438 * to its children for adding their text content to the event. Note that the
4439 * event text is populated in a separate dispatch path since we add to the
4440 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004441 * A typical implementation will call
4442 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4443 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4444 * on each child. Override this method if custom population of the event text
4445 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004446 * <p>
4447 * If an {@link AccessibilityDelegate} has been specified via calling
4448 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4449 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4450 * is responsible for handling this call.
4451 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004452 * <p>
4453 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4454 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4455 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004456 *
4457 * @param event The event.
4458 *
4459 * @return True if the event population was completed.
4460 */
4461 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004462 if (mAccessibilityDelegate != null) {
4463 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4464 } else {
4465 return dispatchPopulateAccessibilityEventInternal(event);
4466 }
4467 }
4468
4469 /**
4470 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4471 *
4472 * Note: Called from the default {@link AccessibilityDelegate}.
4473 */
4474 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004475 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004476 return false;
4477 }
4478
4479 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004480 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004481 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004482 * text content. While this method is free to modify event
4483 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004484 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4485 * <p>
4486 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004487 * to the text added by the super implementation:
4488 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004489 * super.onPopulateAccessibilityEvent(event);
4490 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4491 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4492 * mCurrentDate.getTimeInMillis(), flags);
4493 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004494 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004495 * <p>
4496 * If an {@link AccessibilityDelegate} has been specified via calling
4497 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4498 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4499 * is responsible for handling this call.
4500 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004501 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4502 * information to the event, in case the default implementation has basic information to add.
4503 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004504 *
4505 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004506 *
4507 * @see #sendAccessibilityEvent(int)
4508 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004509 */
4510 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004511 if (mAccessibilityDelegate != null) {
4512 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4513 } else {
4514 onPopulateAccessibilityEventInternal(event);
4515 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004516 }
4517
4518 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004519 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4520 *
4521 * Note: Called from the default {@link AccessibilityDelegate}.
4522 */
4523 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4524
4525 }
4526
4527 /**
4528 * Initializes an {@link AccessibilityEvent} with information about
4529 * this View which is the event source. In other words, the source of
4530 * an accessibility event is the view whose state change triggered firing
4531 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004532 * <p>
4533 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004534 * to properties set by the super implementation:
4535 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4536 * super.onInitializeAccessibilityEvent(event);
4537 * event.setPassword(true);
4538 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004539 * <p>
4540 * If an {@link AccessibilityDelegate} has been specified via calling
4541 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4542 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4543 * is responsible for handling this call.
4544 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004545 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4546 * information to the event, in case the default implementation has basic information to add.
4547 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004548 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004549 *
4550 * @see #sendAccessibilityEvent(int)
4551 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4552 */
4553 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004554 if (mAccessibilityDelegate != null) {
4555 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4556 } else {
4557 onInitializeAccessibilityEventInternal(event);
4558 }
4559 }
4560
4561 /**
4562 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4563 *
4564 * Note: Called from the default {@link AccessibilityDelegate}.
4565 */
4566 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004567 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004568 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004569 event.setPackageName(getContext().getPackageName());
4570 event.setEnabled(isEnabled());
4571 event.setContentDescription(mContentDescription);
4572
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004573 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004574 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004575 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4576 FOCUSABLES_ALL);
4577 event.setItemCount(focusablesTempList.size());
4578 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4579 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004580 }
4581 }
4582
4583 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004584 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4585 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4586 * This method is responsible for obtaining an accessibility node info from a
4587 * pool of reusable instances and calling
4588 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4589 * initialize the former.
4590 * <p>
4591 * Note: The client is responsible for recycling the obtained instance by calling
4592 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4593 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004594 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004595 * @return A populated {@link AccessibilityNodeInfo}.
4596 *
4597 * @see AccessibilityNodeInfo
4598 */
4599 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004600 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4601 if (provider != null) {
4602 return provider.createAccessibilityNodeInfo(View.NO_ID);
4603 } else {
4604 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4605 onInitializeAccessibilityNodeInfo(info);
4606 return info;
4607 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004608 }
4609
4610 /**
4611 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4612 * The base implementation sets:
4613 * <ul>
4614 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004615 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4616 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004617 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4618 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4619 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4620 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4621 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4622 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4623 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4624 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4625 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4626 * </ul>
4627 * <p>
4628 * Subclasses should override this method, call the super implementation,
4629 * and set additional attributes.
4630 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004631 * <p>
4632 * If an {@link AccessibilityDelegate} has been specified via calling
4633 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4634 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4635 * is responsible for handling this call.
4636 * </p>
4637 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004638 * @param info The instance to initialize.
4639 */
4640 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004641 if (mAccessibilityDelegate != null) {
4642 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4643 } else {
4644 onInitializeAccessibilityNodeInfoInternal(info);
4645 }
4646 }
4647
4648 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004649 * Gets the location of this view in screen coordintates.
4650 *
4651 * @param outRect The output location
4652 */
4653 private void getBoundsOnScreen(Rect outRect) {
4654 if (mAttachInfo == null) {
4655 return;
4656 }
4657
4658 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004659 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004660
4661 if (!hasIdentityMatrix()) {
4662 getMatrix().mapRect(position);
4663 }
4664
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004665 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004666
4667 ViewParent parent = mParent;
4668 while (parent instanceof View) {
4669 View parentView = (View) parent;
4670
4671 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4672
4673 if (!parentView.hasIdentityMatrix()) {
4674 parentView.getMatrix().mapRect(position);
4675 }
4676
4677 position.offset(parentView.mLeft, parentView.mTop);
4678
4679 parent = parentView.mParent;
4680 }
4681
4682 if (parent instanceof ViewRootImpl) {
4683 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4684 position.offset(0, -viewRootImpl.mCurScrollY);
4685 }
4686
4687 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4688
4689 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4690 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4691 }
4692
4693 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004694 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4695 *
4696 * Note: Called from the default {@link AccessibilityDelegate}.
4697 */
4698 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004699 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004700 final float applicationScale = mAttachInfo.mApplicationScale;
4701
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004702 getDrawingRect(bounds);
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004703 bounds.scale(applicationScale);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004704 info.setBoundsInParent(bounds);
4705
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004706 getBoundsOnScreen(bounds);
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004707 bounds.scale(applicationScale);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004708 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004709
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004710 ViewParent parent = getParentForAccessibility();
4711 if (parent instanceof View) {
4712 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004713 }
4714
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004715 info.setVisibleToUser(isVisibleToUser());
4716
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004717 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004718 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004719 info.setContentDescription(getContentDescription());
4720
4721 info.setEnabled(isEnabled());
4722 info.setClickable(isClickable());
4723 info.setFocusable(isFocusable());
4724 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004725 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004726 info.setSelected(isSelected());
4727 info.setLongClickable(isLongClickable());
4728
4729 // TODO: These make sense only if we are in an AdapterView but all
4730 // views can be selected. Maybe from accessiiblity perspective
4731 // we should report as selectable view in an AdapterView.
4732 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4733 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4734
4735 if (isFocusable()) {
4736 if (isFocused()) {
4737 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4738 } else {
4739 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4740 }
4741 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004742
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004743 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004744 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004745 } else {
4746 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4747 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004748
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004749 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004750 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4751 }
4752
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004753 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004754 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4755 }
4756
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004757 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004758 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4759 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4760 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004761 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4762 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004763 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004764 }
4765
4766 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -07004767 * Returns the delta between the actual and last reported window left.
4768 *
4769 * @hide
4770 */
4771 public int getActualAndReportedWindowLeftDelta() {
4772 if (mAttachInfo != null) {
4773 return mAttachInfo.mActualWindowLeft - mAttachInfo.mWindowLeft;
4774 }
4775 return 0;
4776 }
4777
4778 /**
4779 * Returns the delta between the actual and last reported window top.
4780 *
4781 * @hide
4782 */
4783 public int getActualAndReportedWindowTopDelta() {
4784 if (mAttachInfo != null) {
4785 return mAttachInfo.mActualWindowTop - mAttachInfo.mWindowTop;
4786 }
4787 return 0;
4788 }
4789
4790 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004791 * Computes whether this view is visible to the user. Such a view is
4792 * attached, visible, all its predecessors are visible, it is not clipped
4793 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004794 *
4795 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004796 *
4797 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004798 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004799 protected boolean isVisibleToUser() {
4800 return isVisibleToUser(null);
4801 }
4802
4803 /**
4804 * Computes whether the given portion of this view is visible to the user. Such a view is
4805 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4806 * the specified portion is not clipped entirely by its predecessors.
4807 *
4808 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4809 * <code>null</code>, and the entire view will be tested in this case.
4810 * When <code>true</code> is returned by the function, the actual visible
4811 * region will be stored in this parameter; that is, if boundInView is fully
4812 * contained within the view, no modification will be made, otherwise regions
4813 * outside of the visible area of the view will be clipped.
4814 *
4815 * @return Whether the specified portion of the view is visible on the screen.
4816 *
4817 * @hide
4818 */
4819 protected boolean isVisibleToUser(Rect boundInView) {
4820 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4821 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004822 // The first two checks are made also made by isShown() which
4823 // however traverses the tree up to the parent to catch that.
4824 // Therefore, we do some fail fast check to minimize the up
4825 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004826 boolean isVisible = mAttachInfo != null
4827 && mAttachInfo.mWindowVisibility == View.VISIBLE
4828 && getAlpha() > 0
4829 && isShown()
4830 && getGlobalVisibleRect(visibleRect, offset);
4831 if (isVisible && boundInView != null) {
4832 visibleRect.offset(-offset.x, -offset.y);
4833 isVisible &= boundInView.intersect(visibleRect);
4834 }
4835 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004836 }
4837
4838 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004839 * Sets a delegate for implementing accessibility support via compositon as
4840 * opposed to inheritance. The delegate's primary use is for implementing
4841 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4842 *
4843 * @param delegate The delegate instance.
4844 *
4845 * @see AccessibilityDelegate
4846 */
4847 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4848 mAccessibilityDelegate = delegate;
4849 }
4850
4851 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004852 * Gets the provider for managing a virtual view hierarchy rooted at this View
4853 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4854 * that explore the window content.
4855 * <p>
4856 * If this method returns an instance, this instance is responsible for managing
4857 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4858 * View including the one representing the View itself. Similarly the returned
4859 * instance is responsible for performing accessibility actions on any virtual
4860 * view or the root view itself.
4861 * </p>
4862 * <p>
4863 * If an {@link AccessibilityDelegate} has been specified via calling
4864 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4865 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4866 * is responsible for handling this call.
4867 * </p>
4868 *
4869 * @return The provider.
4870 *
4871 * @see AccessibilityNodeProvider
4872 */
4873 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4874 if (mAccessibilityDelegate != null) {
4875 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4876 } else {
4877 return null;
4878 }
4879 }
4880
4881 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004882 * Gets the unique identifier of this view on the screen for accessibility purposes.
4883 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4884 *
4885 * @return The view accessibility id.
4886 *
4887 * @hide
4888 */
4889 public int getAccessibilityViewId() {
4890 if (mAccessibilityViewId == NO_ID) {
4891 mAccessibilityViewId = sNextAccessibilityViewId++;
4892 }
4893 return mAccessibilityViewId;
4894 }
4895
4896 /**
4897 * Gets the unique identifier of the window in which this View reseides.
4898 *
4899 * @return The window accessibility id.
4900 *
4901 * @hide
4902 */
4903 public int getAccessibilityWindowId() {
4904 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4905 }
4906
4907 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004908 * Gets the {@link View} description. It briefly describes the view and is
4909 * primarily used for accessibility support. Set this property to enable
4910 * better accessibility support for your application. This is especially
4911 * true for views that do not have textual representation (For example,
4912 * ImageButton).
4913 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004914 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004915 *
4916 * @attr ref android.R.styleable#View_contentDescription
4917 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004918 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004919 public CharSequence getContentDescription() {
4920 return mContentDescription;
4921 }
4922
4923 /**
4924 * Sets the {@link View} description. It briefly describes the view and is
4925 * primarily used for accessibility support. Set this property to enable
4926 * better accessibility support for your application. This is especially
4927 * true for views that do not have textual representation (For example,
4928 * ImageButton).
4929 *
4930 * @param contentDescription The content description.
4931 *
4932 * @attr ref android.R.styleable#View_contentDescription
4933 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004934 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004935 public void setContentDescription(CharSequence contentDescription) {
4936 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07004937 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
4938 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
4939 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
4940 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004941 }
4942
4943 /**
Romain Guya2431d02009-04-30 16:30:00 -07004944 * Invoked whenever this view loses focus, either by losing window focus or by losing
4945 * focus within its window. This method can be used to clear any state tied to the
4946 * focus. For instance, if a button is held pressed with the trackball and the window
4947 * loses focus, this method can be used to cancel the press.
4948 *
4949 * Subclasses of View overriding this method should always call super.onFocusLost().
4950 *
4951 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004952 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004953 *
4954 * @hide pending API council approval
4955 */
4956 protected void onFocusLost() {
4957 resetPressedState();
4958 }
4959
4960 private void resetPressedState() {
4961 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4962 return;
4963 }
4964
4965 if (isPressed()) {
4966 setPressed(false);
4967
4968 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004969 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004970 }
4971 }
4972 }
4973
4974 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 * Returns true if this view has focus
4976 *
4977 * @return True if this view has focus, false otherwise.
4978 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004979 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 public boolean isFocused() {
4981 return (mPrivateFlags & FOCUSED) != 0;
4982 }
4983
4984 /**
4985 * Find the view in the hierarchy rooted at this view that currently has
4986 * focus.
4987 *
4988 * @return The view that currently has focus, or null if no focused view can
4989 * be found.
4990 */
4991 public View findFocus() {
4992 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4993 }
4994
4995 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004996 * Indicates whether this view is one of the set of scrollable containers in
4997 * its window.
4998 *
4999 * @return whether this view is one of the set of scrollable containers in
5000 * its window
5001 *
5002 * @attr ref android.R.styleable#View_isScrollContainer
5003 */
5004 public boolean isScrollContainer() {
5005 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5006 }
5007
5008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 * Change whether this view is one of the set of scrollable containers in
5010 * its window. This will be used to determine whether the window can
5011 * resize or must pan when a soft input area is open -- scrollable
5012 * containers allow the window to use resize mode since the container
5013 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005014 *
5015 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 */
5017 public void setScrollContainer(boolean isScrollContainer) {
5018 if (isScrollContainer) {
5019 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5020 mAttachInfo.mScrollContainers.add(this);
5021 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5022 }
5023 mPrivateFlags |= SCROLL_CONTAINER;
5024 } else {
5025 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5026 mAttachInfo.mScrollContainers.remove(this);
5027 }
5028 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5029 }
5030 }
5031
5032 /**
5033 * Returns the quality of the drawing cache.
5034 *
5035 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5036 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5037 *
5038 * @see #setDrawingCacheQuality(int)
5039 * @see #setDrawingCacheEnabled(boolean)
5040 * @see #isDrawingCacheEnabled()
5041 *
5042 * @attr ref android.R.styleable#View_drawingCacheQuality
5043 */
5044 public int getDrawingCacheQuality() {
5045 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5046 }
5047
5048 /**
5049 * Set the drawing cache quality of this view. This value is used only when the
5050 * drawing cache is enabled
5051 *
5052 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5053 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5054 *
5055 * @see #getDrawingCacheQuality()
5056 * @see #setDrawingCacheEnabled(boolean)
5057 * @see #isDrawingCacheEnabled()
5058 *
5059 * @attr ref android.R.styleable#View_drawingCacheQuality
5060 */
5061 public void setDrawingCacheQuality(int quality) {
5062 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5063 }
5064
5065 /**
5066 * Returns whether the screen should remain on, corresponding to the current
5067 * value of {@link #KEEP_SCREEN_ON}.
5068 *
5069 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5070 *
5071 * @see #setKeepScreenOn(boolean)
5072 *
5073 * @attr ref android.R.styleable#View_keepScreenOn
5074 */
5075 public boolean getKeepScreenOn() {
5076 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5077 }
5078
5079 /**
5080 * Controls whether the screen should remain on, modifying the
5081 * value of {@link #KEEP_SCREEN_ON}.
5082 *
5083 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5084 *
5085 * @see #getKeepScreenOn()
5086 *
5087 * @attr ref android.R.styleable#View_keepScreenOn
5088 */
5089 public void setKeepScreenOn(boolean keepScreenOn) {
5090 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5091 }
5092
5093 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005094 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5095 * @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 -08005096 *
5097 * @attr ref android.R.styleable#View_nextFocusLeft
5098 */
5099 public int getNextFocusLeftId() {
5100 return mNextFocusLeftId;
5101 }
5102
5103 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005104 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5105 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5106 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 *
5108 * @attr ref android.R.styleable#View_nextFocusLeft
5109 */
5110 public void setNextFocusLeftId(int nextFocusLeftId) {
5111 mNextFocusLeftId = nextFocusLeftId;
5112 }
5113
5114 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005115 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5116 * @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 -08005117 *
5118 * @attr ref android.R.styleable#View_nextFocusRight
5119 */
5120 public int getNextFocusRightId() {
5121 return mNextFocusRightId;
5122 }
5123
5124 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005125 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5126 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5127 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128 *
5129 * @attr ref android.R.styleable#View_nextFocusRight
5130 */
5131 public void setNextFocusRightId(int nextFocusRightId) {
5132 mNextFocusRightId = nextFocusRightId;
5133 }
5134
5135 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005136 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5137 * @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 -08005138 *
5139 * @attr ref android.R.styleable#View_nextFocusUp
5140 */
5141 public int getNextFocusUpId() {
5142 return mNextFocusUpId;
5143 }
5144
5145 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005146 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5147 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5148 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 *
5150 * @attr ref android.R.styleable#View_nextFocusUp
5151 */
5152 public void setNextFocusUpId(int nextFocusUpId) {
5153 mNextFocusUpId = nextFocusUpId;
5154 }
5155
5156 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005157 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5158 * @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 -08005159 *
5160 * @attr ref android.R.styleable#View_nextFocusDown
5161 */
5162 public int getNextFocusDownId() {
5163 return mNextFocusDownId;
5164 }
5165
5166 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005167 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5168 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5169 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 *
5171 * @attr ref android.R.styleable#View_nextFocusDown
5172 */
5173 public void setNextFocusDownId(int nextFocusDownId) {
5174 mNextFocusDownId = nextFocusDownId;
5175 }
5176
5177 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005178 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5179 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5180 *
5181 * @attr ref android.R.styleable#View_nextFocusForward
5182 */
5183 public int getNextFocusForwardId() {
5184 return mNextFocusForwardId;
5185 }
5186
5187 /**
5188 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5189 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5190 * decide automatically.
5191 *
5192 * @attr ref android.R.styleable#View_nextFocusForward
5193 */
5194 public void setNextFocusForwardId(int nextFocusForwardId) {
5195 mNextFocusForwardId = nextFocusForwardId;
5196 }
5197
5198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 * Returns the visibility of this view and all of its ancestors
5200 *
5201 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5202 */
5203 public boolean isShown() {
5204 View current = this;
5205 //noinspection ConstantConditions
5206 do {
5207 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5208 return false;
5209 }
5210 ViewParent parent = current.mParent;
5211 if (parent == null) {
5212 return false; // We are not attached to the view root
5213 }
5214 if (!(parent instanceof View)) {
5215 return true;
5216 }
5217 current = (View) parent;
5218 } while (current != null);
5219
5220 return false;
5221 }
5222
5223 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005224 * Called by the view hierarchy when the content insets for a window have
5225 * changed, to allow it to adjust its content to fit within those windows.
5226 * The content insets tell you the space that the status bar, input method,
5227 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005228 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005229 * <p>You do not normally need to deal with this function, since the default
5230 * window decoration given to applications takes care of applying it to the
5231 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5232 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5233 * and your content can be placed under those system elements. You can then
5234 * use this method within your view hierarchy if you have parts of your UI
5235 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005237 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005238 * inset's to the view's padding, consuming that content (modifying the
5239 * insets to be 0), and returning true. This behavior is off by default, but can
5240 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5241 *
5242 * <p>This function's traversal down the hierarchy is depth-first. The same content
5243 * insets object is propagated down the hierarchy, so any changes made to it will
5244 * be seen by all following views (including potentially ones above in
5245 * the hierarchy since this is a depth-first traversal). The first view
5246 * that returns true will abort the entire traversal.
5247 *
5248 * <p>The default implementation works well for a situation where it is
5249 * used with a container that covers the entire window, allowing it to
5250 * apply the appropriate insets to its content on all edges. If you need
5251 * a more complicated layout (such as two different views fitting system
5252 * windows, one on the top of the window, and one on the bottom),
5253 * you can override the method and handle the insets however you would like.
5254 * Note that the insets provided by the framework are always relative to the
5255 * far edges of the window, not accounting for the location of the called view
5256 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005257 * where the layout will place the view, as it is done before layout happens.)
5258 *
5259 * <p>Note: unlike many View methods, there is no dispatch phase to this
5260 * call. If you are overriding it in a ViewGroup and want to allow the
5261 * call to continue to your children, you must be sure to call the super
5262 * implementation.
5263 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005264 * <p>Here is a sample layout that makes use of fitting system windows
5265 * to have controls for a video view placed inside of the window decorations
5266 * that it hides and shows. This can be used with code like the second
5267 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5268 *
5269 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5270 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005271 * @param insets Current content insets of the window. Prior to
5272 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5273 * the insets or else you and Android will be unhappy.
5274 *
5275 * @return Return true if this view applied the insets and it should not
5276 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005277 * @see #getFitsSystemWindows()
5278 * @see #setFitsSystemWindows()
5279 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 */
5281 protected boolean fitSystemWindows(Rect insets) {
5282 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005283 mUserPaddingStart = -1;
5284 mUserPaddingEnd = -1;
5285 mUserPaddingRelative = false;
5286 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5287 || mAttachInfo == null
5288 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5289 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5290 return true;
5291 } else {
5292 internalSetPadding(0, 0, 0, 0);
5293 return false;
5294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 }
5296 return false;
5297 }
5298
5299 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005300 * Sets whether or not this view should account for system screen decorations
5301 * such as the status bar and inset its content; that is, controlling whether
5302 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5303 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005304 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005305 * <p>Note that if you are providing your own implementation of
5306 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5307 * flag to true -- your implementation will be overriding the default
5308 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005309 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005310 * @param fitSystemWindows If true, then the default implementation of
5311 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005312 *
5313 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005314 * @see #getFitsSystemWindows()
5315 * @see #fitSystemWindows(Rect)
5316 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005317 */
5318 public void setFitsSystemWindows(boolean fitSystemWindows) {
5319 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5320 }
5321
5322 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005323 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005324 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5325 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005326 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005327 * @return Returns true if the default implementation of
5328 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005329 *
5330 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005331 * @see #setFitsSystemWindows()
5332 * @see #fitSystemWindows(Rect)
5333 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005334 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005335 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005336 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5337 }
5338
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005339 /** @hide */
5340 public boolean fitsSystemWindows() {
5341 return getFitsSystemWindows();
5342 }
5343
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005344 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005345 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5346 */
5347 public void requestFitSystemWindows() {
5348 if (mParent != null) {
5349 mParent.requestFitSystemWindows();
5350 }
5351 }
5352
5353 /**
5354 * For use by PhoneWindow to make its own system window fitting optional.
5355 * @hide
5356 */
5357 public void makeOptionalFitsSystemWindows() {
5358 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5359 }
5360
5361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 * Returns the visibility status for this view.
5363 *
5364 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5365 * @attr ref android.R.styleable#View_visibility
5366 */
5367 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005368 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5369 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5370 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 })
5372 public int getVisibility() {
5373 return mViewFlags & VISIBILITY_MASK;
5374 }
5375
5376 /**
5377 * Set the enabled state of this view.
5378 *
5379 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5380 * @attr ref android.R.styleable#View_visibility
5381 */
5382 @RemotableViewMethod
5383 public void setVisibility(int visibility) {
5384 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005385 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 }
5387
5388 /**
5389 * Returns the enabled status for this view. The interpretation of the
5390 * enabled state varies by subclass.
5391 *
5392 * @return True if this view is enabled, false otherwise.
5393 */
5394 @ViewDebug.ExportedProperty
5395 public boolean isEnabled() {
5396 return (mViewFlags & ENABLED_MASK) == ENABLED;
5397 }
5398
5399 /**
5400 * Set the enabled state of this view. The interpretation of the enabled
5401 * state varies by subclass.
5402 *
5403 * @param enabled True if this view is enabled, false otherwise.
5404 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005405 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005407 if (enabled == isEnabled()) return;
5408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5410
5411 /*
5412 * The View most likely has to change its appearance, so refresh
5413 * the drawable state.
5414 */
5415 refreshDrawableState();
5416
5417 // Invalidate too, since the default behavior for views is to be
5418 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005419 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 }
5421
5422 /**
5423 * Set whether this view can receive the focus.
5424 *
5425 * Setting this to false will also ensure that this view is not focusable
5426 * in touch mode.
5427 *
5428 * @param focusable If true, this view can receive the focus.
5429 *
5430 * @see #setFocusableInTouchMode(boolean)
5431 * @attr ref android.R.styleable#View_focusable
5432 */
5433 public void setFocusable(boolean focusable) {
5434 if (!focusable) {
5435 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5436 }
5437 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5438 }
5439
5440 /**
5441 * Set whether this view can receive focus while in touch mode.
5442 *
5443 * Setting this to true will also ensure that this view is focusable.
5444 *
5445 * @param focusableInTouchMode If true, this view can receive the focus while
5446 * in touch mode.
5447 *
5448 * @see #setFocusable(boolean)
5449 * @attr ref android.R.styleable#View_focusableInTouchMode
5450 */
5451 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5452 // Focusable in touch mode should always be set before the focusable flag
5453 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5454 // which, in touch mode, will not successfully request focus on this view
5455 // because the focusable in touch mode flag is not set
5456 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5457 if (focusableInTouchMode) {
5458 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5459 }
5460 }
5461
5462 /**
5463 * Set whether this view should have sound effects enabled for events such as
5464 * clicking and touching.
5465 *
5466 * <p>You may wish to disable sound effects for a view if you already play sounds,
5467 * for instance, a dial key that plays dtmf tones.
5468 *
5469 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5470 * @see #isSoundEffectsEnabled()
5471 * @see #playSoundEffect(int)
5472 * @attr ref android.R.styleable#View_soundEffectsEnabled
5473 */
5474 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5475 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5476 }
5477
5478 /**
5479 * @return whether this view should have sound effects enabled for events such as
5480 * clicking and touching.
5481 *
5482 * @see #setSoundEffectsEnabled(boolean)
5483 * @see #playSoundEffect(int)
5484 * @attr ref android.R.styleable#View_soundEffectsEnabled
5485 */
5486 @ViewDebug.ExportedProperty
5487 public boolean isSoundEffectsEnabled() {
5488 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5489 }
5490
5491 /**
5492 * Set whether this view should have haptic feedback for events such as
5493 * long presses.
5494 *
5495 * <p>You may wish to disable haptic feedback if your view already controls
5496 * its own haptic feedback.
5497 *
5498 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5499 * @see #isHapticFeedbackEnabled()
5500 * @see #performHapticFeedback(int)
5501 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5502 */
5503 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5504 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5505 }
5506
5507 /**
5508 * @return whether this view should have haptic feedback enabled for events
5509 * long presses.
5510 *
5511 * @see #setHapticFeedbackEnabled(boolean)
5512 * @see #performHapticFeedback(int)
5513 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5514 */
5515 @ViewDebug.ExportedProperty
5516 public boolean isHapticFeedbackEnabled() {
5517 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5518 }
5519
5520 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005521 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005522 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005523 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5524 * {@link #LAYOUT_DIRECTION_RTL},
5525 * {@link #LAYOUT_DIRECTION_INHERIT} or
5526 * {@link #LAYOUT_DIRECTION_LOCALE}.
5527 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005528 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005529 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005530 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5531 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5532 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5533 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005534 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005535 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005536 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005537 }
5538
5539 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005540 * Set the layout direction for this view. This will propagate a reset of layout direction
5541 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005542 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005543 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5544 * {@link #LAYOUT_DIRECTION_RTL},
5545 * {@link #LAYOUT_DIRECTION_INHERIT} or
5546 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005547 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005548 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005549 */
5550 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005551 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005552 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005553 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005554 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005555 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005556 // Set the new layout direction (filtered) and ask for a layout pass
5557 mPrivateFlags2 |=
5558 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5559 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005560 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005561 }
5562
5563 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005564 * Returns the resolved layout direction for this view.
5565 *
5566 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005567 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005568 */
5569 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005570 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5571 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005572 })
5573 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005574 // The layout diretion will be resolved only if needed
5575 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5576 resolveLayoutDirection();
5577 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005578 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005579 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5580 }
5581
5582 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005583 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5584 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005585 *
5586 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005587 */
5588 @ViewDebug.ExportedProperty(category = "layout")
5589 public boolean isLayoutRtl() {
5590 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5591 }
5592
5593 /**
Adam Powell539ee872012-02-03 19:00:49 -08005594 * Indicates whether the view is currently tracking transient state that the
5595 * app should not need to concern itself with saving and restoring, but that
5596 * the framework should take special note to preserve when possible.
5597 *
Adam Powell785c4472012-05-02 21:25:39 -07005598 * <p>A view with transient state cannot be trivially rebound from an external
5599 * data source, such as an adapter binding item views in a list. This may be
5600 * because the view is performing an animation, tracking user selection
5601 * of content, or similar.</p>
5602 *
Adam Powell539ee872012-02-03 19:00:49 -08005603 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005604 */
5605 @ViewDebug.ExportedProperty(category = "layout")
5606 public boolean hasTransientState() {
5607 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5608 }
5609
5610 /**
5611 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005612 * framework should attempt to preserve when possible. This flag is reference counted,
5613 * so every call to setHasTransientState(true) should be paired with a later call
5614 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005615 *
Adam Powell785c4472012-05-02 21:25:39 -07005616 * <p>A view with transient state cannot be trivially rebound from an external
5617 * data source, such as an adapter binding item views in a list. This may be
5618 * because the view is performing an animation, tracking user selection
5619 * of content, or similar.</p>
5620 *
Adam Powell539ee872012-02-03 19:00:49 -08005621 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005622 */
5623 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005624 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5625 mTransientStateCount - 1;
5626 if (mTransientStateCount < 0) {
5627 mTransientStateCount = 0;
5628 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5629 "unmatched pair of setHasTransientState calls");
5630 }
5631 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005632 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005633 // update flag if we've just incremented up from 0 or decremented down to 0
5634 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5635 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5636 if (mParent != null) {
5637 try {
5638 mParent.childHasTransientStateChanged(this, hasTransientState);
5639 } catch (AbstractMethodError e) {
5640 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5641 " does not fully implement ViewParent", e);
5642 }
Adam Powell539ee872012-02-03 19:00:49 -08005643 }
5644 }
5645 }
5646
5647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 * If this view doesn't do any drawing on its own, set this flag to
5649 * allow further optimizations. By default, this flag is not set on
5650 * View, but could be set on some View subclasses such as ViewGroup.
5651 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005652 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5653 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 *
5655 * @param willNotDraw whether or not this View draw on its own
5656 */
5657 public void setWillNotDraw(boolean willNotDraw) {
5658 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5659 }
5660
5661 /**
5662 * Returns whether or not this View draws on its own.
5663 *
5664 * @return true if this view has nothing to draw, false otherwise
5665 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005666 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 public boolean willNotDraw() {
5668 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5669 }
5670
5671 /**
5672 * When a View's drawing cache is enabled, drawing is redirected to an
5673 * offscreen bitmap. Some views, like an ImageView, must be able to
5674 * bypass this mechanism if they already draw a single bitmap, to avoid
5675 * unnecessary usage of the memory.
5676 *
5677 * @param willNotCacheDrawing true if this view does not cache its
5678 * drawing, false otherwise
5679 */
5680 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5681 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5682 }
5683
5684 /**
5685 * Returns whether or not this View can cache its drawing or not.
5686 *
5687 * @return true if this view does not cache its drawing, false otherwise
5688 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005689 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 public boolean willNotCacheDrawing() {
5691 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5692 }
5693
5694 /**
5695 * Indicates whether this view reacts to click events or not.
5696 *
5697 * @return true if the view is clickable, false otherwise
5698 *
5699 * @see #setClickable(boolean)
5700 * @attr ref android.R.styleable#View_clickable
5701 */
5702 @ViewDebug.ExportedProperty
5703 public boolean isClickable() {
5704 return (mViewFlags & CLICKABLE) == CLICKABLE;
5705 }
5706
5707 /**
5708 * Enables or disables click events for this view. When a view
5709 * is clickable it will change its state to "pressed" on every click.
5710 * Subclasses should set the view clickable to visually react to
5711 * user's clicks.
5712 *
5713 * @param clickable true to make the view clickable, false otherwise
5714 *
5715 * @see #isClickable()
5716 * @attr ref android.R.styleable#View_clickable
5717 */
5718 public void setClickable(boolean clickable) {
5719 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5720 }
5721
5722 /**
5723 * Indicates whether this view reacts to long click events or not.
5724 *
5725 * @return true if the view is long clickable, false otherwise
5726 *
5727 * @see #setLongClickable(boolean)
5728 * @attr ref android.R.styleable#View_longClickable
5729 */
5730 public boolean isLongClickable() {
5731 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5732 }
5733
5734 /**
5735 * Enables or disables long click events for this view. When a view is long
5736 * clickable it reacts to the user holding down the button for a longer
5737 * duration than a tap. This event can either launch the listener or a
5738 * context menu.
5739 *
5740 * @param longClickable true to make the view long clickable, false otherwise
5741 * @see #isLongClickable()
5742 * @attr ref android.R.styleable#View_longClickable
5743 */
5744 public void setLongClickable(boolean longClickable) {
5745 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5746 }
5747
5748 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005749 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 *
5751 * @see #isClickable()
5752 * @see #setClickable(boolean)
5753 *
5754 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5755 * the View's internal state from a previously set "pressed" state.
5756 */
5757 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005758 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 if (pressed) {
5761 mPrivateFlags |= PRESSED;
5762 } else {
5763 mPrivateFlags &= ~PRESSED;
5764 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005765
5766 if (needsRefresh) {
5767 refreshDrawableState();
5768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 dispatchSetPressed(pressed);
5770 }
5771
5772 /**
5773 * Dispatch setPressed to all of this View's children.
5774 *
5775 * @see #setPressed(boolean)
5776 *
5777 * @param pressed The new pressed state
5778 */
5779 protected void dispatchSetPressed(boolean pressed) {
5780 }
5781
5782 /**
5783 * Indicates whether the view is currently in pressed state. Unless
5784 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5785 * the pressed state.
5786 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005787 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 * @see #isClickable()
5789 * @see #setClickable(boolean)
5790 *
5791 * @return true if the view is currently pressed, false otherwise
5792 */
5793 public boolean isPressed() {
5794 return (mPrivateFlags & PRESSED) == PRESSED;
5795 }
5796
5797 /**
5798 * Indicates whether this view will save its state (that is,
5799 * whether its {@link #onSaveInstanceState} method will be called).
5800 *
5801 * @return Returns true if the view state saving is enabled, else false.
5802 *
5803 * @see #setSaveEnabled(boolean)
5804 * @attr ref android.R.styleable#View_saveEnabled
5805 */
5806 public boolean isSaveEnabled() {
5807 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5808 }
5809
5810 /**
5811 * Controls whether the saving of this view's state is
5812 * enabled (that is, whether its {@link #onSaveInstanceState} method
5813 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005814 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 * for its state to be saved. This flag can only disable the
5816 * saving of this view; any child views may still have their state saved.
5817 *
5818 * @param enabled Set to false to <em>disable</em> state saving, or true
5819 * (the default) to allow it.
5820 *
5821 * @see #isSaveEnabled()
5822 * @see #setId(int)
5823 * @see #onSaveInstanceState()
5824 * @attr ref android.R.styleable#View_saveEnabled
5825 */
5826 public void setSaveEnabled(boolean enabled) {
5827 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5828 }
5829
Jeff Brown85a31762010-09-01 17:01:00 -07005830 /**
5831 * Gets whether the framework should discard touches when the view's
5832 * window is obscured by another visible window.
5833 * Refer to the {@link View} security documentation for more details.
5834 *
5835 * @return True if touch filtering is enabled.
5836 *
5837 * @see #setFilterTouchesWhenObscured(boolean)
5838 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5839 */
5840 @ViewDebug.ExportedProperty
5841 public boolean getFilterTouchesWhenObscured() {
5842 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5843 }
5844
5845 /**
5846 * Sets whether the framework should discard touches when the view's
5847 * window is obscured by another visible window.
5848 * Refer to the {@link View} security documentation for more details.
5849 *
5850 * @param enabled True if touch filtering should be enabled.
5851 *
5852 * @see #getFilterTouchesWhenObscured
5853 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5854 */
5855 public void setFilterTouchesWhenObscured(boolean enabled) {
5856 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5857 FILTER_TOUCHES_WHEN_OBSCURED);
5858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859
5860 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005861 * Indicates whether the entire hierarchy under this view will save its
5862 * state when a state saving traversal occurs from its parent. The default
5863 * is true; if false, these views will not be saved unless
5864 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5865 *
5866 * @return Returns true if the view state saving from parent is enabled, else false.
5867 *
5868 * @see #setSaveFromParentEnabled(boolean)
5869 */
5870 public boolean isSaveFromParentEnabled() {
5871 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5872 }
5873
5874 /**
5875 * Controls whether the entire hierarchy under this view will save its
5876 * state when a state saving traversal occurs from its parent. The default
5877 * is true; if false, these views will not be saved unless
5878 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5879 *
5880 * @param enabled Set to false to <em>disable</em> state saving, or true
5881 * (the default) to allow it.
5882 *
5883 * @see #isSaveFromParentEnabled()
5884 * @see #setId(int)
5885 * @see #onSaveInstanceState()
5886 */
5887 public void setSaveFromParentEnabled(boolean enabled) {
5888 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5889 }
5890
5891
5892 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 * Returns whether this View is able to take focus.
5894 *
5895 * @return True if this view can take focus, or false otherwise.
5896 * @attr ref android.R.styleable#View_focusable
5897 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005898 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 public final boolean isFocusable() {
5900 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5901 }
5902
5903 /**
5904 * When a view is focusable, it may not want to take focus when in touch mode.
5905 * For example, a button would like focus when the user is navigating via a D-pad
5906 * so that the user can click on it, but once the user starts touching the screen,
5907 * the button shouldn't take focus
5908 * @return Whether the view is focusable in touch mode.
5909 * @attr ref android.R.styleable#View_focusableInTouchMode
5910 */
5911 @ViewDebug.ExportedProperty
5912 public final boolean isFocusableInTouchMode() {
5913 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5914 }
5915
5916 /**
5917 * Find the nearest view in the specified direction that can take focus.
5918 * This does not actually give focus to that view.
5919 *
5920 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5921 *
5922 * @return The nearest focusable in the specified direction, or null if none
5923 * can be found.
5924 */
5925 public View focusSearch(int direction) {
5926 if (mParent != null) {
5927 return mParent.focusSearch(this, direction);
5928 } else {
5929 return null;
5930 }
5931 }
5932
5933 /**
5934 * This method is the last chance for the focused view and its ancestors to
5935 * respond to an arrow key. This is called when the focused view did not
5936 * consume the key internally, nor could the view system find a new view in
5937 * the requested direction to give focus to.
5938 *
5939 * @param focused The currently focused view.
5940 * @param direction The direction focus wants to move. One of FOCUS_UP,
5941 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5942 * @return True if the this view consumed this unhandled move.
5943 */
5944 public boolean dispatchUnhandledMove(View focused, int direction) {
5945 return false;
5946 }
5947
5948 /**
5949 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005950 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005952 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5953 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 * @return The user specified next view, or null if there is none.
5955 */
5956 View findUserSetNextFocus(View root, int direction) {
5957 switch (direction) {
5958 case FOCUS_LEFT:
5959 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005960 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 case FOCUS_RIGHT:
5962 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005963 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 case FOCUS_UP:
5965 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005966 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 case FOCUS_DOWN:
5968 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005969 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005970 case FOCUS_FORWARD:
5971 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005972 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005973 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005974 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005975 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005976 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005977 @Override
5978 public boolean apply(View t) {
5979 return t.mNextFocusForwardId == id;
5980 }
5981 });
5982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 }
5984 return null;
5985 }
5986
Jeff Brown4dfbec22011-08-15 14:55:37 -07005987 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5988 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5989 @Override
5990 public boolean apply(View t) {
5991 return t.mID == childViewId;
5992 }
5993 });
5994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 if (result == null) {
5996 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5997 + "by user for id " + childViewId);
5998 }
5999 return result;
6000 }
6001
6002 /**
6003 * Find and return all focusable views that are descendants of this view,
6004 * possibly including this view if it is focusable itself.
6005 *
6006 * @param direction The direction of the focus
6007 * @return A list of focusable views
6008 */
6009 public ArrayList<View> getFocusables(int direction) {
6010 ArrayList<View> result = new ArrayList<View>(24);
6011 addFocusables(result, direction);
6012 return result;
6013 }
6014
6015 /**
6016 * Add any focusable views that are descendants of this view (possibly
6017 * including this view if it is focusable itself) to views. If we are in touch mode,
6018 * only add views that are also focusable in touch mode.
6019 *
6020 * @param views Focusable views found so far
6021 * @param direction The direction of the focus
6022 */
6023 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006024 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026
svetoslavganov75986cf2009-05-14 22:28:01 -07006027 /**
6028 * Adds any focusable views that are descendants of this view (possibly
6029 * including this view if it is focusable itself) to views. This method
6030 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006031 * only views focusable in touch mode if we are in touch mode or
6032 * only views that can take accessibility focus if accessibility is enabeld
6033 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006034 *
6035 * @param views Focusable views found so far or null if all we are interested is
6036 * the number of focusables.
6037 * @param direction The direction of the focus.
6038 * @param focusableMode The type of focusables to be added.
6039 *
6040 * @see #FOCUSABLES_ALL
6041 * @see #FOCUSABLES_TOUCH_MODE
6042 */
6043 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006044 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006045 return;
6046 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006047 if (!isFocusable()) {
6048 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006049 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006050 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6051 && isInTouchMode() && !isFocusableInTouchMode()) {
6052 return;
6053 }
6054 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 }
6056
6057 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006058 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006059 * The search is performed by either the text that the View renders or the content
6060 * description that describes the view for accessibility purposes and the view does
6061 * not render or both. Clients can specify how the search is to be performed via
6062 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6063 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006064 *
6065 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006066 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006067 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006068 * @see #FIND_VIEWS_WITH_TEXT
6069 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6070 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006071 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006072 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006073 if (getAccessibilityNodeProvider() != null) {
6074 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6075 outViews.add(this);
6076 }
6077 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006078 && (searched != null && searched.length() > 0)
6079 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006080 String searchedLowerCase = searched.toString().toLowerCase();
6081 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6082 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6083 outViews.add(this);
6084 }
6085 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006086 }
6087
6088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 * Find and return all touchable views that are descendants of this view,
6090 * possibly including this view if it is touchable itself.
6091 *
6092 * @return A list of touchable views
6093 */
6094 public ArrayList<View> getTouchables() {
6095 ArrayList<View> result = new ArrayList<View>();
6096 addTouchables(result);
6097 return result;
6098 }
6099
6100 /**
6101 * Add any touchable views that are descendants of this view (possibly
6102 * including this view if it is touchable itself) to views.
6103 *
6104 * @param views Touchable views found so far
6105 */
6106 public void addTouchables(ArrayList<View> views) {
6107 final int viewFlags = mViewFlags;
6108
6109 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6110 && (viewFlags & ENABLED_MASK) == ENABLED) {
6111 views.add(this);
6112 }
6113 }
6114
6115 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006116 * Returns whether this View is accessibility focused.
6117 *
6118 * @return True if this View is accessibility focused.
6119 */
6120 boolean isAccessibilityFocused() {
6121 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6122 }
6123
6124 /**
6125 * Call this to try to give accessibility focus to this view.
6126 *
6127 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6128 * returns false or the view is no visible or the view already has accessibility
6129 * focus.
6130 *
6131 * See also {@link #focusSearch(int)}, which is what you call to say that you
6132 * have focus, and you want your parent to look for the next one.
6133 *
6134 * @return Whether this view actually took accessibility focus.
6135 *
6136 * @hide
6137 */
6138 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006139 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6140 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006141 return false;
6142 }
6143 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6144 return false;
6145 }
6146 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6147 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6148 ViewRootImpl viewRootImpl = getViewRootImpl();
6149 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006150 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006151 }
6152 invalidate();
6153 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6154 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006155 return true;
6156 }
6157 return false;
6158 }
6159
6160 /**
6161 * Call this to try to clear accessibility focus of this view.
6162 *
6163 * See also {@link #focusSearch(int)}, which is what you call to say that you
6164 * have focus, and you want your parent to look for the next one.
6165 *
6166 * @hide
6167 */
6168 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006169 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6170 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006171 invalidate();
6172 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6173 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006174 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006175 // Clear the global reference of accessibility focus if this
6176 // view or any of its descendants had accessibility focus.
6177 ViewRootImpl viewRootImpl = getViewRootImpl();
6178 if (viewRootImpl != null) {
6179 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6180 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006181 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006182 }
6183 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006184 }
6185
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006186 private void sendAccessibilityHoverEvent(int eventType) {
6187 // Since we are not delivering to a client accessibility events from not
6188 // important views (unless the clinet request that) we need to fire the
6189 // event from the deepest view exposed to the client. As a consequence if
6190 // the user crosses a not exposed view the client will see enter and exit
6191 // of the exposed predecessor followed by and enter and exit of that same
6192 // predecessor when entering and exiting the not exposed descendant. This
6193 // is fine since the client has a clear idea which view is hovered at the
6194 // price of a couple more events being sent. This is a simple and
6195 // working solution.
6196 View source = this;
6197 while (true) {
6198 if (source.includeForAccessibility()) {
6199 source.sendAccessibilityEvent(eventType);
6200 return;
6201 }
6202 ViewParent parent = source.getParent();
6203 if (parent instanceof View) {
6204 source = (View) parent;
6205 } else {
6206 return;
6207 }
6208 }
6209 }
6210
Svetoslav Ganov42138042012-03-20 11:51:39 -07006211 /**
6212 * Clears accessibility focus without calling any callback methods
6213 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6214 * is used for clearing accessibility focus when giving this focus to
6215 * another view.
6216 */
6217 void clearAccessibilityFocusNoCallbacks() {
6218 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6219 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6220 invalidate();
6221 }
6222 }
6223
6224 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 * Call this to try to give focus to a specific view or to one of its
6226 * descendants.
6227 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006228 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6229 * false), or if it is focusable and it is not focusable in touch mode
6230 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006232 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 * have focus, and you want your parent to look for the next one.
6234 *
6235 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6236 * {@link #FOCUS_DOWN} and <code>null</code>.
6237 *
6238 * @return Whether this view or one of its descendants actually took focus.
6239 */
6240 public final boolean requestFocus() {
6241 return requestFocus(View.FOCUS_DOWN);
6242 }
6243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006244 /**
6245 * Call this to try to give focus to a specific view or to one of its
6246 * descendants and give it a hint about what direction focus is heading.
6247 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006248 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6249 * false), or if it is focusable and it is not focusable in touch mode
6250 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006252 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006253 * have focus, and you want your parent to look for the next one.
6254 *
6255 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6256 * <code>null</code> set for the previously focused rectangle.
6257 *
6258 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6259 * @return Whether this view or one of its descendants actually took focus.
6260 */
6261 public final boolean requestFocus(int direction) {
6262 return requestFocus(direction, null);
6263 }
6264
6265 /**
6266 * Call this to try to give focus to a specific view or to one of its descendants
6267 * and give it hints about the direction and a specific rectangle that the focus
6268 * is coming from. The rectangle can help give larger views a finer grained hint
6269 * about where focus is coming from, and therefore, where to show selection, or
6270 * forward focus change internally.
6271 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006272 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6273 * false), or if it is focusable and it is not focusable in touch mode
6274 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 *
6276 * A View will not take focus if it is not visible.
6277 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006278 * A View will not take focus if one of its parents has
6279 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6280 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006281 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006282 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 * have focus, and you want your parent to look for the next one.
6284 *
6285 * You may wish to override this method if your custom {@link View} has an internal
6286 * {@link View} that it wishes to forward the request to.
6287 *
6288 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6289 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6290 * to give a finer grained hint about where focus is coming from. May be null
6291 * if there is no hint.
6292 * @return Whether this view or one of its descendants actually took focus.
6293 */
6294 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006295 return requestFocusNoSearch(direction, previouslyFocusedRect);
6296 }
6297
6298 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006299 // need to be focusable
6300 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6301 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6302 return false;
6303 }
6304
6305 // need to be focusable in touch mode if in touch mode
6306 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006307 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6308 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 }
6310
6311 // need to not have any parents blocking us
6312 if (hasAncestorThatBlocksDescendantFocus()) {
6313 return false;
6314 }
6315
6316 handleFocusGainInternal(direction, previouslyFocusedRect);
6317 return true;
6318 }
6319
6320 /**
6321 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6322 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6323 * touch mode to request focus when they are touched.
6324 *
6325 * @return Whether this view or one of its descendants actually took focus.
6326 *
6327 * @see #isInTouchMode()
6328 *
6329 */
6330 public final boolean requestFocusFromTouch() {
6331 // Leave touch mode if we need to
6332 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006333 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006334 if (viewRoot != null) {
6335 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006336 }
6337 }
6338 return requestFocus(View.FOCUS_DOWN);
6339 }
6340
6341 /**
6342 * @return Whether any ancestor of this view blocks descendant focus.
6343 */
6344 private boolean hasAncestorThatBlocksDescendantFocus() {
6345 ViewParent ancestor = mParent;
6346 while (ancestor instanceof ViewGroup) {
6347 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6348 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6349 return true;
6350 } else {
6351 ancestor = vgAncestor.getParent();
6352 }
6353 }
6354 return false;
6355 }
6356
6357 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006358 * Gets the mode for determining whether this View is important for accessibility
6359 * which is if it fires accessibility events and if it is reported to
6360 * accessibility services that query the screen.
6361 *
6362 * @return The mode for determining whether a View is important for accessibility.
6363 *
6364 * @attr ref android.R.styleable#View_importantForAccessibility
6365 *
6366 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6367 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6368 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6369 */
6370 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006371 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6372 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6373 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006374 })
6375 public int getImportantForAccessibility() {
6376 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6377 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6378 }
6379
6380 /**
6381 * Sets how to determine whether this view is important for accessibility
6382 * which is if it fires accessibility events and if it is reported to
6383 * accessibility services that query the screen.
6384 *
6385 * @param mode How to determine whether this view is important for accessibility.
6386 *
6387 * @attr ref android.R.styleable#View_importantForAccessibility
6388 *
6389 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6390 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6391 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6392 */
6393 public void setImportantForAccessibility(int mode) {
6394 if (mode != getImportantForAccessibility()) {
6395 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6396 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6397 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6398 notifyAccessibilityStateChanged();
6399 }
6400 }
6401
6402 /**
6403 * Gets whether this view should be exposed for accessibility.
6404 *
6405 * @return Whether the view is exposed for accessibility.
6406 *
6407 * @hide
6408 */
6409 public boolean isImportantForAccessibility() {
6410 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6411 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6412 switch (mode) {
6413 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6414 return true;
6415 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6416 return false;
6417 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6418 return isActionableForAccessibility() || hasListenersForAccessibility();
6419 default:
6420 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6421 + mode);
6422 }
6423 }
6424
6425 /**
6426 * Gets the parent for accessibility purposes. Note that the parent for
6427 * accessibility is not necessary the immediate parent. It is the first
6428 * predecessor that is important for accessibility.
6429 *
6430 * @return The parent for accessibility purposes.
6431 */
6432 public ViewParent getParentForAccessibility() {
6433 if (mParent instanceof View) {
6434 View parentView = (View) mParent;
6435 if (parentView.includeForAccessibility()) {
6436 return mParent;
6437 } else {
6438 return mParent.getParentForAccessibility();
6439 }
6440 }
6441 return null;
6442 }
6443
6444 /**
6445 * Adds the children of a given View for accessibility. Since some Views are
6446 * not important for accessibility the children for accessibility are not
6447 * necessarily direct children of the riew, rather they are the first level of
6448 * descendants important for accessibility.
6449 *
6450 * @param children The list of children for accessibility.
6451 */
6452 public void addChildrenForAccessibility(ArrayList<View> children) {
6453 if (includeForAccessibility()) {
6454 children.add(this);
6455 }
6456 }
6457
6458 /**
6459 * Whether to regard this view for accessibility. A view is regarded for
6460 * accessibility if it is important for accessibility or the querying
6461 * accessibility service has explicitly requested that view not
6462 * important for accessibility are regarded.
6463 *
6464 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006465 *
6466 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006467 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006468 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006469 if (mAttachInfo != null) {
6470 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006471 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006472 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006473 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006474 }
6475 return false;
6476 }
6477
6478 /**
6479 * Returns whether the View is considered actionable from
6480 * accessibility perspective. Such view are important for
6481 * accessiiblity.
6482 *
6483 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006484 *
6485 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006486 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006487 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006488 return (isClickable() || isLongClickable() || isFocusable());
6489 }
6490
6491 /**
6492 * Returns whether the View has registered callbacks wich makes it
6493 * important for accessiiblity.
6494 *
6495 * @return True if the view is actionable for accessibility.
6496 */
6497 private boolean hasListenersForAccessibility() {
6498 ListenerInfo info = getListenerInfo();
6499 return mTouchDelegate != null || info.mOnKeyListener != null
6500 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6501 || info.mOnHoverListener != null || info.mOnDragListener != null;
6502 }
6503
6504 /**
6505 * Notifies accessibility services that some view's important for
6506 * accessibility state has changed. Note that such notifications
6507 * are made at most once every
6508 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6509 * to avoid unnecessary load to the system. Also once a view has
6510 * made a notifucation this method is a NOP until the notification has
6511 * been sent to clients.
6512 *
6513 * @hide
6514 *
6515 * TODO: Makse sure this method is called for any view state change
6516 * that is interesting for accessilility purposes.
6517 */
6518 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006519 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6520 return;
6521 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006522 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6523 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6524 if (mParent != null) {
6525 mParent.childAccessibilityStateChanged(this);
6526 }
6527 }
6528 }
6529
6530 /**
6531 * Reset the state indicating the this view has requested clients
6532 * interested in its accessiblity state to be notified.
6533 *
6534 * @hide
6535 */
6536 public void resetAccessibilityStateChanged() {
6537 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6538 }
6539
6540 /**
6541 * Performs the specified accessibility action on the view. For
6542 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006543 * <p>
6544 * If an {@link AccessibilityDelegate} has been specified via calling
6545 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6546 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6547 * is responsible for handling this call.
6548 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006549 *
6550 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006551 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006552 * @return Whether the action was performed.
6553 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006554 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006555 if (mAccessibilityDelegate != null) {
6556 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6557 } else {
6558 return performAccessibilityActionInternal(action, arguments);
6559 }
6560 }
6561
6562 /**
6563 * @see #performAccessibilityAction(int, Bundle)
6564 *
6565 * Note: Called from the default {@link AccessibilityDelegate}.
6566 */
6567 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006568 switch (action) {
6569 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006570 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006571 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006572 }
6573 } break;
6574 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6575 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006576 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006577 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006578 } break;
6579 case AccessibilityNodeInfo.ACTION_FOCUS: {
6580 if (!hasFocus()) {
6581 // Get out of touch mode since accessibility
6582 // wants to move focus around.
6583 getViewRootImpl().ensureTouchMode(false);
6584 return requestFocus();
6585 }
6586 } break;
6587 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6588 if (hasFocus()) {
6589 clearFocus();
6590 return !isFocused();
6591 }
6592 } break;
6593 case AccessibilityNodeInfo.ACTION_SELECT: {
6594 if (!isSelected()) {
6595 setSelected(true);
6596 return isSelected();
6597 }
6598 } break;
6599 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6600 if (isSelected()) {
6601 setSelected(false);
6602 return !isSelected();
6603 }
6604 } break;
6605 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006606 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006607 return requestAccessibilityFocus();
6608 }
6609 } break;
6610 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6611 if (isAccessibilityFocused()) {
6612 clearAccessibilityFocus();
6613 return true;
6614 }
6615 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006616 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6617 if (arguments != null) {
6618 final int granularity = arguments.getInt(
6619 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6620 return nextAtGranularity(granularity);
6621 }
6622 } break;
6623 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6624 if (arguments != null) {
6625 final int granularity = arguments.getInt(
6626 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6627 return previousAtGranularity(granularity);
6628 }
6629 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006630 }
6631 return false;
6632 }
6633
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006634 private boolean nextAtGranularity(int granularity) {
6635 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006636 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006637 return false;
6638 }
6639 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6640 if (iterator == null) {
6641 return false;
6642 }
6643 final int current = getAccessibilityCursorPosition();
6644 final int[] range = iterator.following(current);
6645 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006646 return false;
6647 }
6648 final int start = range[0];
6649 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006650 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006651 sendViewTextTraversedAtGranularityEvent(
6652 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6653 granularity, start, end);
6654 return true;
6655 }
6656
6657 private boolean previousAtGranularity(int granularity) {
6658 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006659 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006660 return false;
6661 }
6662 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6663 if (iterator == null) {
6664 return false;
6665 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006666 int current = getAccessibilityCursorPosition();
6667 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6668 current = text.length();
6669 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6670 // When traversing by character we always put the cursor after the character
6671 // to ease edit and have to compensate before asking the for previous segment.
6672 current--;
6673 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006674 final int[] range = iterator.preceding(current);
6675 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006676 return false;
6677 }
6678 final int start = range[0];
6679 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006680 // Always put the cursor after the character to ease edit.
6681 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6682 setAccessibilityCursorPosition(end);
6683 } else {
6684 setAccessibilityCursorPosition(start);
6685 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006686 sendViewTextTraversedAtGranularityEvent(
6687 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6688 granularity, start, end);
6689 return true;
6690 }
6691
6692 /**
6693 * Gets the text reported for accessibility purposes.
6694 *
6695 * @return The accessibility text.
6696 *
6697 * @hide
6698 */
6699 public CharSequence getIterableTextForAccessibility() {
6700 return mContentDescription;
6701 }
6702
6703 /**
6704 * @hide
6705 */
6706 public int getAccessibilityCursorPosition() {
6707 return mAccessibilityCursorPosition;
6708 }
6709
6710 /**
6711 * @hide
6712 */
6713 public void setAccessibilityCursorPosition(int position) {
6714 mAccessibilityCursorPosition = position;
6715 }
6716
6717 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6718 int fromIndex, int toIndex) {
6719 if (mParent == null) {
6720 return;
6721 }
6722 AccessibilityEvent event = AccessibilityEvent.obtain(
6723 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6724 onInitializeAccessibilityEvent(event);
6725 onPopulateAccessibilityEvent(event);
6726 event.setFromIndex(fromIndex);
6727 event.setToIndex(toIndex);
6728 event.setAction(action);
6729 event.setMovementGranularity(granularity);
6730 mParent.requestSendAccessibilityEvent(this, event);
6731 }
6732
6733 /**
6734 * @hide
6735 */
6736 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6737 switch (granularity) {
6738 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6739 CharSequence text = getIterableTextForAccessibility();
6740 if (text != null && text.length() > 0) {
6741 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006742 CharacterTextSegmentIterator.getInstance(
6743 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006744 iterator.initialize(text.toString());
6745 return iterator;
6746 }
6747 } break;
6748 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6749 CharSequence text = getIterableTextForAccessibility();
6750 if (text != null && text.length() > 0) {
6751 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006752 WordTextSegmentIterator.getInstance(
6753 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006754 iterator.initialize(text.toString());
6755 return iterator;
6756 }
6757 } break;
6758 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6759 CharSequence text = getIterableTextForAccessibility();
6760 if (text != null && text.length() > 0) {
6761 ParagraphTextSegmentIterator iterator =
6762 ParagraphTextSegmentIterator.getInstance();
6763 iterator.initialize(text.toString());
6764 return iterator;
6765 }
6766 } break;
6767 }
6768 return null;
6769 }
6770
Svetoslav Ganov42138042012-03-20 11:51:39 -07006771 /**
Romain Guya440b002010-02-24 15:57:54 -08006772 * @hide
6773 */
6774 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006775 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006776 clearDisplayList();
6777
Romain Guya440b002010-02-24 15:57:54 -08006778 onStartTemporaryDetach();
6779 }
6780
6781 /**
6782 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6784 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006785 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 */
6787 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006788 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006789 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006790 }
6791
6792 /**
6793 * @hide
6794 */
6795 public void dispatchFinishTemporaryDetach() {
6796 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 }
Romain Guy8506ab42009-06-11 17:35:47 -07006798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006799 /**
6800 * Called after {@link #onStartTemporaryDetach} when the container is done
6801 * changing the view.
6802 */
6803 public void onFinishTemporaryDetach() {
6804 }
Romain Guy8506ab42009-06-11 17:35:47 -07006805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006807 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6808 * for this view's window. Returns null if the view is not currently attached
6809 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006810 * just use the standard high-level event callbacks like
6811 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006812 */
6813 public KeyEvent.DispatcherState getKeyDispatcherState() {
6814 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6815 }
Joe Malin32736f02011-01-19 16:14:20 -08006816
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 * Dispatch a key event before it is processed by any input method
6819 * associated with the view hierarchy. This can be used to intercept
6820 * key events in special situations before the IME consumes them; a
6821 * typical example would be handling the BACK key to update the application's
6822 * UI instead of allowing the IME to see it and close itself.
6823 *
6824 * @param event The key event to be dispatched.
6825 * @return True if the event was handled, false otherwise.
6826 */
6827 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6828 return onKeyPreIme(event.getKeyCode(), event);
6829 }
6830
6831 /**
6832 * Dispatch a key event to the next view on the focus path. This path runs
6833 * from the top of the view tree down to the currently focused view. If this
6834 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6835 * the next node down the focus path. This method also fires any key
6836 * listeners.
6837 *
6838 * @param event The key event to be dispatched.
6839 * @return True if the event was handled, false otherwise.
6840 */
6841 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006842 if (mInputEventConsistencyVerifier != null) {
6843 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006845
Jeff Brown21bc5c92011-02-28 18:27:14 -08006846 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006847 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006848 ListenerInfo li = mListenerInfo;
6849 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6850 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006851 return true;
6852 }
6853
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006854 if (event.dispatch(this, mAttachInfo != null
6855 ? mAttachInfo.mKeyDispatchState : null, this)) {
6856 return true;
6857 }
6858
6859 if (mInputEventConsistencyVerifier != null) {
6860 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6861 }
6862 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863 }
6864
6865 /**
6866 * Dispatches a key shortcut event.
6867 *
6868 * @param event The key event to be dispatched.
6869 * @return True if the event was handled by the view, false otherwise.
6870 */
6871 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6872 return onKeyShortcut(event.getKeyCode(), event);
6873 }
6874
6875 /**
6876 * Pass the touch screen motion event down to the target view, or this
6877 * view if it is the target.
6878 *
6879 * @param event The motion event to be dispatched.
6880 * @return True if the event was handled by the view, false otherwise.
6881 */
6882 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006883 if (mInputEventConsistencyVerifier != null) {
6884 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6885 }
6886
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006887 if (onFilterTouchEventForSecurity(event)) {
6888 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006889 ListenerInfo li = mListenerInfo;
6890 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6891 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006892 return true;
6893 }
6894
6895 if (onTouchEvent(event)) {
6896 return true;
6897 }
Jeff Brown85a31762010-09-01 17:01:00 -07006898 }
6899
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006900 if (mInputEventConsistencyVerifier != null) {
6901 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006903 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006904 }
6905
6906 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006907 * Filter the touch event to apply security policies.
6908 *
6909 * @param event The motion event to be filtered.
6910 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006911 *
Jeff Brown85a31762010-09-01 17:01:00 -07006912 * @see #getFilterTouchesWhenObscured
6913 */
6914 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006915 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006916 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6917 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6918 // Window is obscured, drop this touch.
6919 return false;
6920 }
6921 return true;
6922 }
6923
6924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006925 * Pass a trackball motion event down to the focused view.
6926 *
6927 * @param event The motion event to be dispatched.
6928 * @return True if the event was handled by the view, false otherwise.
6929 */
6930 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006931 if (mInputEventConsistencyVerifier != null) {
6932 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6933 }
6934
Romain Guy02ccac62011-06-24 13:20:23 -07006935 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 }
6937
6938 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006939 * Dispatch a generic motion event.
6940 * <p>
6941 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6942 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006943 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006944 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006945 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006946 *
6947 * @param event The motion event to be dispatched.
6948 * @return True if the event was handled by the view, false otherwise.
6949 */
6950 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006951 if (mInputEventConsistencyVerifier != null) {
6952 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6953 }
6954
Jeff Browna032cc02011-03-07 16:56:21 -08006955 final int source = event.getSource();
6956 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6957 final int action = event.getAction();
6958 if (action == MotionEvent.ACTION_HOVER_ENTER
6959 || action == MotionEvent.ACTION_HOVER_MOVE
6960 || action == MotionEvent.ACTION_HOVER_EXIT) {
6961 if (dispatchHoverEvent(event)) {
6962 return true;
6963 }
6964 } else if (dispatchGenericPointerEvent(event)) {
6965 return true;
6966 }
6967 } else if (dispatchGenericFocusedEvent(event)) {
6968 return true;
6969 }
6970
Jeff Brown10b62902011-06-20 16:40:37 -07006971 if (dispatchGenericMotionEventInternal(event)) {
6972 return true;
6973 }
6974
6975 if (mInputEventConsistencyVerifier != null) {
6976 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6977 }
6978 return false;
6979 }
6980
6981 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006982 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006983 ListenerInfo li = mListenerInfo;
6984 if (li != null && li.mOnGenericMotionListener != null
6985 && (mViewFlags & ENABLED_MASK) == ENABLED
6986 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006987 return true;
6988 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006989
6990 if (onGenericMotionEvent(event)) {
6991 return true;
6992 }
6993
6994 if (mInputEventConsistencyVerifier != null) {
6995 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6996 }
6997 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006998 }
6999
7000 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007001 * Dispatch a hover event.
7002 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007003 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007004 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007005 * </p>
7006 *
7007 * @param event The motion event to be dispatched.
7008 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007009 */
7010 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007011 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007012 ListenerInfo li = mListenerInfo;
7013 if (li != null && li.mOnHoverListener != null
7014 && (mViewFlags & ENABLED_MASK) == ENABLED
7015 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007016 return true;
7017 }
7018
Jeff Browna032cc02011-03-07 16:56:21 -08007019 return onHoverEvent(event);
7020 }
7021
7022 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007023 * Returns true if the view has a child to which it has recently sent
7024 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7025 * it does not have a hovered child, then it must be the innermost hovered view.
7026 * @hide
7027 */
7028 protected boolean hasHoveredChild() {
7029 return false;
7030 }
7031
7032 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007033 * Dispatch a generic motion event to the view under the first pointer.
7034 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007035 * Do not call this method directly.
7036 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007037 * </p>
7038 *
7039 * @param event The motion event to be dispatched.
7040 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007041 */
7042 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7043 return false;
7044 }
7045
7046 /**
7047 * Dispatch a generic motion event to the currently focused view.
7048 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007049 * Do not call this method directly.
7050 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007051 * </p>
7052 *
7053 * @param event The motion event to be dispatched.
7054 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007055 */
7056 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7057 return false;
7058 }
7059
7060 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007061 * Dispatch a pointer event.
7062 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007063 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7064 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7065 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007066 * and should not be expected to handle other pointing device features.
7067 * </p>
7068 *
7069 * @param event The motion event to be dispatched.
7070 * @return True if the event was handled by the view, false otherwise.
7071 * @hide
7072 */
7073 public final boolean dispatchPointerEvent(MotionEvent event) {
7074 if (event.isTouchEvent()) {
7075 return dispatchTouchEvent(event);
7076 } else {
7077 return dispatchGenericMotionEvent(event);
7078 }
7079 }
7080
7081 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 * Called when the window containing this view gains or loses window focus.
7083 * ViewGroups should override to route to their children.
7084 *
7085 * @param hasFocus True if the window containing this view now has focus,
7086 * false otherwise.
7087 */
7088 public void dispatchWindowFocusChanged(boolean hasFocus) {
7089 onWindowFocusChanged(hasFocus);
7090 }
7091
7092 /**
7093 * Called when the window containing this view gains or loses focus. Note
7094 * that this is separate from view focus: to receive key events, both
7095 * your view and its window must have focus. If a window is displayed
7096 * on top of yours that takes input focus, then your own window will lose
7097 * focus but the view focus will remain unchanged.
7098 *
7099 * @param hasWindowFocus True if the window containing this view now has
7100 * focus, false otherwise.
7101 */
7102 public void onWindowFocusChanged(boolean hasWindowFocus) {
7103 InputMethodManager imm = InputMethodManager.peekInstance();
7104 if (!hasWindowFocus) {
7105 if (isPressed()) {
7106 setPressed(false);
7107 }
7108 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7109 imm.focusOut(this);
7110 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007111 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007112 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007113 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7115 imm.focusIn(this);
7116 }
7117 refreshDrawableState();
7118 }
7119
7120 /**
7121 * Returns true if this view is in a window that currently has window focus.
7122 * Note that this is not the same as the view itself having focus.
7123 *
7124 * @return True if this view is in a window that currently has window focus.
7125 */
7126 public boolean hasWindowFocus() {
7127 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7128 }
7129
7130 /**
Adam Powell326d8082009-12-09 15:10:07 -08007131 * Dispatch a view visibility change down the view hierarchy.
7132 * ViewGroups should override to route to their children.
7133 * @param changedView The view whose visibility changed. Could be 'this' or
7134 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007135 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7136 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007137 */
7138 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7139 onVisibilityChanged(changedView, visibility);
7140 }
7141
7142 /**
7143 * Called when the visibility of the view or an ancestor of the view is changed.
7144 * @param changedView The view whose visibility changed. Could be 'this' or
7145 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007146 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7147 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007148 */
7149 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007150 if (visibility == VISIBLE) {
7151 if (mAttachInfo != null) {
7152 initialAwakenScrollBars();
7153 } else {
7154 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7155 }
7156 }
Adam Powell326d8082009-12-09 15:10:07 -08007157 }
7158
7159 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007160 * Dispatch a hint about whether this view is displayed. For instance, when
7161 * a View moves out of the screen, it might receives a display hint indicating
7162 * the view is not displayed. Applications should not <em>rely</em> on this hint
7163 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007164 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007165 * @param hint A hint about whether or not this view is displayed:
7166 * {@link #VISIBLE} or {@link #INVISIBLE}.
7167 */
7168 public void dispatchDisplayHint(int hint) {
7169 onDisplayHint(hint);
7170 }
7171
7172 /**
7173 * Gives this view a hint about whether is displayed or not. For instance, when
7174 * a View moves out of the screen, it might receives a display hint indicating
7175 * the view is not displayed. Applications should not <em>rely</em> on this hint
7176 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007177 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007178 * @param hint A hint about whether or not this view is displayed:
7179 * {@link #VISIBLE} or {@link #INVISIBLE}.
7180 */
7181 protected void onDisplayHint(int hint) {
7182 }
7183
7184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007185 * Dispatch a window visibility change down the view hierarchy.
7186 * ViewGroups should override to route to their children.
7187 *
7188 * @param visibility The new visibility of the window.
7189 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007190 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 */
7192 public void dispatchWindowVisibilityChanged(int visibility) {
7193 onWindowVisibilityChanged(visibility);
7194 }
7195
7196 /**
7197 * Called when the window containing has change its visibility
7198 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7199 * that this tells you whether or not your window is being made visible
7200 * to the window manager; this does <em>not</em> tell you whether or not
7201 * your window is obscured by other windows on the screen, even if it
7202 * is itself visible.
7203 *
7204 * @param visibility The new visibility of the window.
7205 */
7206 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007207 if (visibility == VISIBLE) {
7208 initialAwakenScrollBars();
7209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 }
7211
7212 /**
7213 * Returns the current visibility of the window this view is attached to
7214 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7215 *
7216 * @return Returns the current visibility of the view's window.
7217 */
7218 public int getWindowVisibility() {
7219 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7220 }
7221
7222 /**
7223 * Retrieve the overall visible display size in which the window this view is
7224 * attached to has been positioned in. This takes into account screen
7225 * decorations above the window, for both cases where the window itself
7226 * is being position inside of them or the window is being placed under
7227 * then and covered insets are used for the window to position its content
7228 * inside. In effect, this tells you the available area where content can
7229 * be placed and remain visible to users.
7230 *
7231 * <p>This function requires an IPC back to the window manager to retrieve
7232 * the requested information, so should not be used in performance critical
7233 * code like drawing.
7234 *
7235 * @param outRect Filled in with the visible display frame. If the view
7236 * is not attached to a window, this is simply the raw display size.
7237 */
7238 public void getWindowVisibleDisplayFrame(Rect outRect) {
7239 if (mAttachInfo != null) {
7240 try {
7241 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7242 } catch (RemoteException e) {
7243 return;
7244 }
7245 // XXX This is really broken, and probably all needs to be done
7246 // in the window manager, and we need to know more about whether
7247 // we want the area behind or in front of the IME.
7248 final Rect insets = mAttachInfo.mVisibleInsets;
7249 outRect.left += insets.left;
7250 outRect.top += insets.top;
7251 outRect.right -= insets.right;
7252 outRect.bottom -= insets.bottom;
7253 return;
7254 }
7255 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007256 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 }
7258
7259 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007260 * Dispatch a notification about a resource configuration change down
7261 * the view hierarchy.
7262 * ViewGroups should override to route to their children.
7263 *
7264 * @param newConfig The new resource configuration.
7265 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007266 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007267 */
7268 public void dispatchConfigurationChanged(Configuration newConfig) {
7269 onConfigurationChanged(newConfig);
7270 }
7271
7272 /**
7273 * Called when the current configuration of the resources being used
7274 * by the application have changed. You can use this to decide when
7275 * to reload resources that can changed based on orientation and other
7276 * configuration characterstics. You only need to use this if you are
7277 * not relying on the normal {@link android.app.Activity} mechanism of
7278 * recreating the activity instance upon a configuration change.
7279 *
7280 * @param newConfig The new resource configuration.
7281 */
7282 protected void onConfigurationChanged(Configuration newConfig) {
7283 }
7284
7285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 * Private function to aggregate all per-view attributes in to the view
7287 * root.
7288 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007289 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7290 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 }
7292
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007293 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7294 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007295 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007296 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007297 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007298 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007299 ListenerInfo li = mListenerInfo;
7300 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007301 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 }
7304 }
7305
7306 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007307 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007309 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7310 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 ai.mRecomputeGlobalAttributes = true;
7312 }
7313 }
7314 }
7315
7316 /**
7317 * Returns whether the device is currently in touch mode. Touch mode is entered
7318 * once the user begins interacting with the device by touch, and affects various
7319 * things like whether focus is always visible to the user.
7320 *
7321 * @return Whether the device is in touch mode.
7322 */
7323 @ViewDebug.ExportedProperty
7324 public boolean isInTouchMode() {
7325 if (mAttachInfo != null) {
7326 return mAttachInfo.mInTouchMode;
7327 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007328 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 }
7330 }
7331
7332 /**
7333 * Returns the context the view is running in, through which it can
7334 * access the current theme, resources, etc.
7335 *
7336 * @return The view's Context.
7337 */
7338 @ViewDebug.CapturedViewProperty
7339 public final Context getContext() {
7340 return mContext;
7341 }
7342
7343 /**
7344 * Handle a key event before it is processed by any input method
7345 * associated with the view hierarchy. This can be used to intercept
7346 * key events in special situations before the IME consumes them; a
7347 * typical example would be handling the BACK key to update the application's
7348 * UI instead of allowing the IME to see it and close itself.
7349 *
7350 * @param keyCode The value in event.getKeyCode().
7351 * @param event Description of the key event.
7352 * @return If you handled the event, return true. If you want to allow the
7353 * event to be handled by the next receiver, return false.
7354 */
7355 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7356 return false;
7357 }
7358
7359 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007360 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7361 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7363 * is released, if the view is enabled and clickable.
7364 *
Jean Chalard405bc512012-05-29 19:12:34 +09007365 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7366 * although some may elect to do so in some situations. Do not rely on this to
7367 * catch software key presses.
7368 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 * @param keyCode A key code that represents the button pressed, from
7370 * {@link android.view.KeyEvent}.
7371 * @param event The KeyEvent object that defines the button action.
7372 */
7373 public boolean onKeyDown(int keyCode, KeyEvent event) {
7374 boolean result = false;
7375
7376 switch (keyCode) {
7377 case KeyEvent.KEYCODE_DPAD_CENTER:
7378 case KeyEvent.KEYCODE_ENTER: {
7379 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7380 return true;
7381 }
7382 // Long clickable items don't necessarily have to be clickable
7383 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7384 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7385 (event.getRepeatCount() == 0)) {
7386 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007387 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 return true;
7389 }
7390 break;
7391 }
7392 }
7393 return result;
7394 }
7395
7396 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007397 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7398 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7399 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007400 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7401 * although some may elect to do so in some situations. Do not rely on this to
7402 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007403 */
7404 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7405 return false;
7406 }
7407
7408 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007409 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7410 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7412 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007413 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7414 * although some may elect to do so in some situations. Do not rely on this to
7415 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 *
7417 * @param keyCode A key code that represents the button pressed, from
7418 * {@link android.view.KeyEvent}.
7419 * @param event The KeyEvent object that defines the button action.
7420 */
7421 public boolean onKeyUp(int keyCode, KeyEvent event) {
7422 boolean result = false;
7423
7424 switch (keyCode) {
7425 case KeyEvent.KEYCODE_DPAD_CENTER:
7426 case KeyEvent.KEYCODE_ENTER: {
7427 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7428 return true;
7429 }
7430 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7431 setPressed(false);
7432
7433 if (!mHasPerformedLongPress) {
7434 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007435 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436
7437 result = performClick();
7438 }
7439 }
7440 break;
7441 }
7442 }
7443 return result;
7444 }
7445
7446 /**
7447 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7448 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7449 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007450 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7451 * although some may elect to do so in some situations. Do not rely on this to
7452 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 *
7454 * @param keyCode A key code that represents the button pressed, from
7455 * {@link android.view.KeyEvent}.
7456 * @param repeatCount The number of times the action was made.
7457 * @param event The KeyEvent object that defines the button action.
7458 */
7459 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7460 return false;
7461 }
7462
7463 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007464 * Called on the focused view when a key shortcut event is not handled.
7465 * Override this method to implement local key shortcuts for the View.
7466 * Key shortcuts can also be implemented by setting the
7467 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007468 *
7469 * @param keyCode The value in event.getKeyCode().
7470 * @param event Description of the key event.
7471 * @return If you handled the event, return true. If you want to allow the
7472 * event to be handled by the next receiver, return false.
7473 */
7474 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7475 return false;
7476 }
7477
7478 /**
7479 * Check whether the called view is a text editor, in which case it
7480 * would make sense to automatically display a soft input window for
7481 * it. Subclasses should override this if they implement
7482 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007483 * a call on that method would return a non-null InputConnection, and
7484 * they are really a first-class editor that the user would normally
7485 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007486 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007487 * <p>The default implementation always returns false. This does
7488 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7489 * will not be called or the user can not otherwise perform edits on your
7490 * view; it is just a hint to the system that this is not the primary
7491 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007492 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 * @return Returns true if this view is a text editor, else false.
7494 */
7495 public boolean onCheckIsTextEditor() {
7496 return false;
7497 }
Romain Guy8506ab42009-06-11 17:35:47 -07007498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 /**
7500 * Create a new InputConnection for an InputMethod to interact
7501 * with the view. The default implementation returns null, since it doesn't
7502 * support input methods. You can override this to implement such support.
7503 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007504 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 * <p>When implementing this, you probably also want to implement
7506 * {@link #onCheckIsTextEditor()} to indicate you will return a
7507 * non-null InputConnection.
7508 *
7509 * @param outAttrs Fill in with attribute information about the connection.
7510 */
7511 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7512 return null;
7513 }
7514
7515 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007516 * Called by the {@link android.view.inputmethod.InputMethodManager}
7517 * when a view who is not the current
7518 * input connection target is trying to make a call on the manager. The
7519 * default implementation returns false; you can override this to return
7520 * true for certain views if you are performing InputConnection proxying
7521 * to them.
7522 * @param view The View that is making the InputMethodManager call.
7523 * @return Return true to allow the call, false to reject.
7524 */
7525 public boolean checkInputConnectionProxy(View view) {
7526 return false;
7527 }
Romain Guy8506ab42009-06-11 17:35:47 -07007528
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530 * Show the context menu for this view. It is not safe to hold on to the
7531 * menu after returning from this method.
7532 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007533 * You should normally not overload this method. Overload
7534 * {@link #onCreateContextMenu(ContextMenu)} or define an
7535 * {@link OnCreateContextMenuListener} to add items to the context menu.
7536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 * @param menu The context menu to populate
7538 */
7539 public void createContextMenu(ContextMenu menu) {
7540 ContextMenuInfo menuInfo = getContextMenuInfo();
7541
7542 // Sets the current menu info so all items added to menu will have
7543 // my extra info set.
7544 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7545
7546 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007547 ListenerInfo li = mListenerInfo;
7548 if (li != null && li.mOnCreateContextMenuListener != null) {
7549 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007550 }
7551
7552 // Clear the extra information so subsequent items that aren't mine don't
7553 // have my extra info.
7554 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7555
7556 if (mParent != null) {
7557 mParent.createContextMenu(menu);
7558 }
7559 }
7560
7561 /**
7562 * Views should implement this if they have extra information to associate
7563 * with the context menu. The return result is supplied as a parameter to
7564 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7565 * callback.
7566 *
7567 * @return Extra information about the item for which the context menu
7568 * should be shown. This information will vary across different
7569 * subclasses of View.
7570 */
7571 protected ContextMenuInfo getContextMenuInfo() {
7572 return null;
7573 }
7574
7575 /**
7576 * Views should implement this if the view itself is going to add items to
7577 * the context menu.
7578 *
7579 * @param menu the context menu to populate
7580 */
7581 protected void onCreateContextMenu(ContextMenu menu) {
7582 }
7583
7584 /**
7585 * Implement this method to handle trackball motion events. The
7586 * <em>relative</em> movement of the trackball since the last event
7587 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7588 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7589 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7590 * they will often be fractional values, representing the more fine-grained
7591 * movement information available from a trackball).
7592 *
7593 * @param event The motion event.
7594 * @return True if the event was handled, false otherwise.
7595 */
7596 public boolean onTrackballEvent(MotionEvent event) {
7597 return false;
7598 }
7599
7600 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007601 * Implement this method to handle generic motion events.
7602 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007603 * Generic motion events describe joystick movements, mouse hovers, track pad
7604 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007605 * {@link MotionEvent#getSource() source} of the motion event specifies
7606 * the class of input that was received. Implementations of this method
7607 * must examine the bits in the source before processing the event.
7608 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007609 * </p><p>
7610 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7611 * are delivered to the view under the pointer. All other generic motion events are
7612 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007613 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007614 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007615 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007616 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7617 * // process the joystick movement...
7618 * return true;
7619 * }
7620 * }
7621 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7622 * switch (event.getAction()) {
7623 * case MotionEvent.ACTION_HOVER_MOVE:
7624 * // process the mouse hover movement...
7625 * return true;
7626 * case MotionEvent.ACTION_SCROLL:
7627 * // process the scroll wheel movement...
7628 * return true;
7629 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007630 * }
7631 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007632 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007633 *
7634 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007635 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007636 */
7637 public boolean onGenericMotionEvent(MotionEvent event) {
7638 return false;
7639 }
7640
7641 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007642 * Implement this method to handle hover events.
7643 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007644 * This method is called whenever a pointer is hovering into, over, or out of the
7645 * bounds of a view and the view is not currently being touched.
7646 * Hover events are represented as pointer events with action
7647 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7648 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7649 * </p>
7650 * <ul>
7651 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7652 * when the pointer enters the bounds of the view.</li>
7653 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7654 * when the pointer has already entered the bounds of the view and has moved.</li>
7655 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7656 * when the pointer has exited the bounds of the view or when the pointer is
7657 * about to go down due to a button click, tap, or similar user action that
7658 * causes the view to be touched.</li>
7659 * </ul>
7660 * <p>
7661 * The view should implement this method to return true to indicate that it is
7662 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007663 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007664 * The default implementation calls {@link #setHovered} to update the hovered state
7665 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007666 * is enabled and is clickable. The default implementation also sends hover
7667 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007668 * </p>
7669 *
7670 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007671 * @return True if the view handled the hover event.
7672 *
7673 * @see #isHovered
7674 * @see #setHovered
7675 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007676 */
7677 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007678 // The root view may receive hover (or touch) events that are outside the bounds of
7679 // the window. This code ensures that we only send accessibility events for
7680 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007681 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007682 if (!mSendingHoverAccessibilityEvents) {
7683 if ((action == MotionEvent.ACTION_HOVER_ENTER
7684 || action == MotionEvent.ACTION_HOVER_MOVE)
7685 && !hasHoveredChild()
7686 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007687 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007688 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007689 }
7690 } else {
7691 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007692 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007693 && !pointInView(event.getX(), event.getY()))) {
7694 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007695 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007696 // If the window does not have input focus we take away accessibility
7697 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007698 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007699 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007700 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007701 }
Jeff Browna1b24182011-07-28 13:38:24 -07007702 }
7703
Jeff Brown87b7f802011-06-21 18:35:45 -07007704 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007705 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007706 case MotionEvent.ACTION_HOVER_ENTER:
7707 setHovered(true);
7708 break;
7709 case MotionEvent.ACTION_HOVER_EXIT:
7710 setHovered(false);
7711 break;
7712 }
Jeff Browna1b24182011-07-28 13:38:24 -07007713
7714 // Dispatch the event to onGenericMotionEvent before returning true.
7715 // This is to provide compatibility with existing applications that
7716 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7717 // break because of the new default handling for hoverable views
7718 // in onHoverEvent.
7719 // Note that onGenericMotionEvent will be called by default when
7720 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7721 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007722 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007723 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007724
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007725 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007726 }
7727
7728 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007729 * Returns true if the view should handle {@link #onHoverEvent}
7730 * by calling {@link #setHovered} to change its hovered state.
7731 *
7732 * @return True if the view is hoverable.
7733 */
7734 private boolean isHoverable() {
7735 final int viewFlags = mViewFlags;
7736 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7737 return false;
7738 }
7739
7740 return (viewFlags & CLICKABLE) == CLICKABLE
7741 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7742 }
7743
7744 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007745 * Returns true if the view is currently hovered.
7746 *
7747 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007748 *
7749 * @see #setHovered
7750 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007751 */
Jeff Brown10b62902011-06-20 16:40:37 -07007752 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007753 public boolean isHovered() {
7754 return (mPrivateFlags & HOVERED) != 0;
7755 }
7756
7757 /**
7758 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007759 * <p>
7760 * Calling this method also changes the drawable state of the view. This
7761 * enables the view to react to hover by using different drawable resources
7762 * to change its appearance.
7763 * </p><p>
7764 * The {@link #onHoverChanged} method is called when the hovered state changes.
7765 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007766 *
7767 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007768 *
7769 * @see #isHovered
7770 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007771 */
7772 public void setHovered(boolean hovered) {
7773 if (hovered) {
7774 if ((mPrivateFlags & HOVERED) == 0) {
7775 mPrivateFlags |= HOVERED;
7776 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007777 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007778 }
7779 } else {
7780 if ((mPrivateFlags & HOVERED) != 0) {
7781 mPrivateFlags &= ~HOVERED;
7782 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007783 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007784 }
7785 }
7786 }
7787
7788 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007789 * Implement this method to handle hover state changes.
7790 * <p>
7791 * This method is called whenever the hover state changes as a result of a
7792 * call to {@link #setHovered}.
7793 * </p>
7794 *
7795 * @param hovered The current hover state, as returned by {@link #isHovered}.
7796 *
7797 * @see #isHovered
7798 * @see #setHovered
7799 */
7800 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007801 }
7802
7803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 * Implement this method to handle touch screen motion events.
7805 *
7806 * @param event The motion event.
7807 * @return True if the event was handled, false otherwise.
7808 */
7809 public boolean onTouchEvent(MotionEvent event) {
7810 final int viewFlags = mViewFlags;
7811
7812 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007813 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007814 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007816 // A disabled view that is clickable still consumes the touch
7817 // events, it just doesn't respond to them.
7818 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7819 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7820 }
7821
7822 if (mTouchDelegate != null) {
7823 if (mTouchDelegate.onTouchEvent(event)) {
7824 return true;
7825 }
7826 }
7827
7828 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7829 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7830 switch (event.getAction()) {
7831 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007832 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7833 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 // take focus if we don't have it already and we should in
7835 // touch mode.
7836 boolean focusTaken = false;
7837 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7838 focusTaken = requestFocus();
7839 }
7840
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007841 if (prepressed) {
7842 // The button is being released before we actually
7843 // showed it as pressed. Make it show the pressed
7844 // state now (before scheduling the click) to ensure
7845 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007846 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007847 }
Joe Malin32736f02011-01-19 16:14:20 -08007848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 if (!mHasPerformedLongPress) {
7850 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007851 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007852
7853 // Only perform take click actions if we were in the pressed state
7854 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007855 // Use a Runnable and post this rather than calling
7856 // performClick directly. This lets other visual state
7857 // of the view update before click actions start.
7858 if (mPerformClick == null) {
7859 mPerformClick = new PerformClick();
7860 }
7861 if (!post(mPerformClick)) {
7862 performClick();
7863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 }
7865 }
7866
7867 if (mUnsetPressedState == null) {
7868 mUnsetPressedState = new UnsetPressedState();
7869 }
7870
Adam Powelle14579b2009-12-16 18:39:52 -08007871 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007872 postDelayed(mUnsetPressedState,
7873 ViewConfiguration.getPressedStateDuration());
7874 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007875 // If the post failed, unpress right now
7876 mUnsetPressedState.run();
7877 }
Adam Powelle14579b2009-12-16 18:39:52 -08007878 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 }
7880 break;
7881
7882 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007883 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007884
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007885 if (performButtonActionOnTouchDown(event)) {
7886 break;
7887 }
7888
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007889 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007890 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007891
7892 // For views inside a scrolling container, delay the pressed feedback for
7893 // a short period in case this is a scroll.
7894 if (isInScrollingContainer) {
7895 mPrivateFlags |= PREPRESSED;
7896 if (mPendingCheckForTap == null) {
7897 mPendingCheckForTap = new CheckForTap();
7898 }
7899 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7900 } else {
7901 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007902 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007903 checkForLongClick(0);
7904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 break;
7906
7907 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007908 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007909 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007910 break;
7911
7912 case MotionEvent.ACTION_MOVE:
7913 final int x = (int) event.getX();
7914 final int y = (int) event.getY();
7915
7916 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007917 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007919 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007921 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007922 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923
Adam Powell4d6f0662012-02-21 15:11:11 -08007924 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007926 }
7927 break;
7928 }
7929 return true;
7930 }
7931
7932 return false;
7933 }
7934
7935 /**
Adam Powell10298662011-08-14 18:26:30 -07007936 * @hide
7937 */
7938 public boolean isInScrollingContainer() {
7939 ViewParent p = getParent();
7940 while (p != null && p instanceof ViewGroup) {
7941 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7942 return true;
7943 }
7944 p = p.getParent();
7945 }
7946 return false;
7947 }
7948
7949 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007950 * Remove the longpress detection timer.
7951 */
7952 private void removeLongPressCallback() {
7953 if (mPendingCheckForLongPress != null) {
7954 removeCallbacks(mPendingCheckForLongPress);
7955 }
7956 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007957
7958 /**
7959 * Remove the pending click action
7960 */
7961 private void removePerformClickCallback() {
7962 if (mPerformClick != null) {
7963 removeCallbacks(mPerformClick);
7964 }
7965 }
7966
Adam Powelle14579b2009-12-16 18:39:52 -08007967 /**
Romain Guya440b002010-02-24 15:57:54 -08007968 * Remove the prepress detection timer.
7969 */
7970 private void removeUnsetPressCallback() {
7971 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7972 setPressed(false);
7973 removeCallbacks(mUnsetPressedState);
7974 }
7975 }
7976
7977 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007978 * Remove the tap detection timer.
7979 */
7980 private void removeTapCallback() {
7981 if (mPendingCheckForTap != null) {
7982 mPrivateFlags &= ~PREPRESSED;
7983 removeCallbacks(mPendingCheckForTap);
7984 }
7985 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007986
7987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 * Cancels a pending long press. Your subclass can use this if you
7989 * want the context menu to come up if the user presses and holds
7990 * at the same place, but you don't want it to come up if they press
7991 * and then move around enough to cause scrolling.
7992 */
7993 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007994 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007995
7996 /*
7997 * The prepressed state handled by the tap callback is a display
7998 * construct, but the tap callback will post a long press callback
7999 * less its own timeout. Remove it here.
8000 */
8001 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008002 }
8003
8004 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008005 * Remove the pending callback for sending a
8006 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8007 */
8008 private void removeSendViewScrolledAccessibilityEventCallback() {
8009 if (mSendViewScrolledAccessibilityEvent != null) {
8010 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008011 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008012 }
8013 }
8014
8015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 * Sets the TouchDelegate for this View.
8017 */
8018 public void setTouchDelegate(TouchDelegate delegate) {
8019 mTouchDelegate = delegate;
8020 }
8021
8022 /**
8023 * Gets the TouchDelegate for this View.
8024 */
8025 public TouchDelegate getTouchDelegate() {
8026 return mTouchDelegate;
8027 }
8028
8029 /**
8030 * Set flags controlling behavior of this view.
8031 *
8032 * @param flags Constant indicating the value which should be set
8033 * @param mask Constant indicating the bit range that should be changed
8034 */
8035 void setFlags(int flags, int mask) {
8036 int old = mViewFlags;
8037 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8038
8039 int changed = mViewFlags ^ old;
8040 if (changed == 0) {
8041 return;
8042 }
8043 int privateFlags = mPrivateFlags;
8044
8045 /* Check if the FOCUSABLE bit has changed */
8046 if (((changed & FOCUSABLE_MASK) != 0) &&
8047 ((privateFlags & HAS_BOUNDS) !=0)) {
8048 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8049 && ((privateFlags & FOCUSED) != 0)) {
8050 /* Give up focus if we are no longer focusable */
8051 clearFocus();
8052 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8053 && ((privateFlags & FOCUSED) == 0)) {
8054 /*
8055 * Tell the view system that we are now available to take focus
8056 * if no one else already has it.
8057 */
8058 if (mParent != null) mParent.focusableViewAvailable(this);
8059 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008060 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8061 notifyAccessibilityStateChanged();
8062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 }
8064
8065 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8066 if ((changed & VISIBILITY_MASK) != 0) {
8067 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008068 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008069 * it was not visible. Marking it drawn ensures that the invalidation will
8070 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008072 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008073 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074
8075 needGlobalAttributesUpdate(true);
8076
8077 // a view becoming visible is worth notifying the parent
8078 // about in case nothing has focus. even if this specific view
8079 // isn't focusable, it may contain something that is, so let
8080 // the root view try to give this focus if nothing else does.
8081 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8082 mParent.focusableViewAvailable(this);
8083 }
8084 }
8085 }
8086
8087 /* Check if the GONE bit has changed */
8088 if ((changed & GONE) != 0) {
8089 needGlobalAttributesUpdate(false);
8090 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091
Romain Guyecd80ee2009-12-03 17:13:02 -08008092 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8093 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008094 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008095 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008096 if (mParent instanceof View) {
8097 // GONE views noop invalidation, so invalidate the parent
8098 ((View) mParent).invalidate(true);
8099 }
8100 // Mark the view drawn to ensure that it gets invalidated properly the next
8101 // time it is visible and gets invalidated
8102 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 }
8104 if (mAttachInfo != null) {
8105 mAttachInfo.mViewVisibilityChanged = true;
8106 }
8107 }
8108
8109 /* Check if the VISIBLE bit has changed */
8110 if ((changed & INVISIBLE) != 0) {
8111 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008112 /*
8113 * If this view is becoming invisible, set the DRAWN flag so that
8114 * the next invalidate() will not be skipped.
8115 */
8116 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117
8118 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008119 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 if (getRootView() != this) {
8121 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008122 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 }
8124 }
8125 if (mAttachInfo != null) {
8126 mAttachInfo.mViewVisibilityChanged = true;
8127 }
8128 }
8129
Adam Powell326d8082009-12-09 15:10:07 -08008130 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008131 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008132 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8133 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008134 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008135 } else if (mParent != null) {
8136 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008137 }
Adam Powell326d8082009-12-09 15:10:07 -08008138 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8139 }
8140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8142 destroyDrawingCache();
8143 }
8144
8145 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8146 destroyDrawingCache();
8147 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008148 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 }
8150
8151 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8152 destroyDrawingCache();
8153 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8154 }
8155
8156 if ((changed & DRAW_MASK) != 0) {
8157 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008158 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 mPrivateFlags &= ~SKIP_DRAW;
8160 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8161 } else {
8162 mPrivateFlags |= SKIP_DRAW;
8163 }
8164 } else {
8165 mPrivateFlags &= ~SKIP_DRAW;
8166 }
8167 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008168 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 }
8170
8171 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008172 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 mParent.recomputeViewAttributes(this);
8174 }
8175 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008176
8177 if (AccessibilityManager.getInstance(mContext).isEnabled()
8178 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8179 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8180 notifyAccessibilityStateChanged();
8181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 }
8183
8184 /**
8185 * Change the view's z order in the tree, so it's on top of other sibling
8186 * views
8187 */
8188 public void bringToFront() {
8189 if (mParent != null) {
8190 mParent.bringChildToFront(this);
8191 }
8192 }
8193
8194 /**
8195 * This is called in response to an internal scroll in this view (i.e., the
8196 * view scrolled its own contents). This is typically as a result of
8197 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8198 * called.
8199 *
8200 * @param l Current horizontal scroll origin.
8201 * @param t Current vertical scroll origin.
8202 * @param oldl Previous horizontal scroll origin.
8203 * @param oldt Previous vertical scroll origin.
8204 */
8205 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008206 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8207 postSendViewScrolledAccessibilityEventCallback();
8208 }
8209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 mBackgroundSizeChanged = true;
8211
8212 final AttachInfo ai = mAttachInfo;
8213 if (ai != null) {
8214 ai.mViewScrollChanged = true;
8215 }
8216 }
8217
8218 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008219 * Interface definition for a callback to be invoked when the layout bounds of a view
8220 * changes due to layout processing.
8221 */
8222 public interface OnLayoutChangeListener {
8223 /**
8224 * Called when the focus state of a view has changed.
8225 *
8226 * @param v The view whose state has changed.
8227 * @param left The new value of the view's left property.
8228 * @param top The new value of the view's top property.
8229 * @param right The new value of the view's right property.
8230 * @param bottom The new value of the view's bottom property.
8231 * @param oldLeft The previous value of the view's left property.
8232 * @param oldTop The previous value of the view's top property.
8233 * @param oldRight The previous value of the view's right property.
8234 * @param oldBottom The previous value of the view's bottom property.
8235 */
8236 void onLayoutChange(View v, int left, int top, int right, int bottom,
8237 int oldLeft, int oldTop, int oldRight, int oldBottom);
8238 }
8239
8240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241 * This is called during layout when the size of this view has changed. If
8242 * you were just added to the view hierarchy, you're called with the old
8243 * values of 0.
8244 *
8245 * @param w Current width of this view.
8246 * @param h Current height of this view.
8247 * @param oldw Old width of this view.
8248 * @param oldh Old height of this view.
8249 */
8250 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8251 }
8252
8253 /**
8254 * Called by draw to draw the child views. This may be overridden
8255 * by derived classes to gain control just before its children are drawn
8256 * (but after its own view has been drawn).
8257 * @param canvas the canvas on which to draw the view
8258 */
8259 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
8262
8263 /**
8264 * Gets the parent of this view. Note that the parent is a
8265 * ViewParent and not necessarily a View.
8266 *
8267 * @return Parent of this view.
8268 */
8269 public final ViewParent getParent() {
8270 return mParent;
8271 }
8272
8273 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008274 * Set the horizontal scrolled position of your view. This will cause a call to
8275 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8276 * invalidated.
8277 * @param value the x position to scroll to
8278 */
8279 public void setScrollX(int value) {
8280 scrollTo(value, mScrollY);
8281 }
8282
8283 /**
8284 * Set the vertical scrolled position of your view. This will cause a call to
8285 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8286 * invalidated.
8287 * @param value the y position to scroll to
8288 */
8289 public void setScrollY(int value) {
8290 scrollTo(mScrollX, value);
8291 }
8292
8293 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 * Return the scrolled left position of this view. This is the left edge of
8295 * the displayed part of your view. You do not need to draw any pixels
8296 * farther left, since those are outside of the frame of your view on
8297 * screen.
8298 *
8299 * @return The left edge of the displayed part of your view, in pixels.
8300 */
8301 public final int getScrollX() {
8302 return mScrollX;
8303 }
8304
8305 /**
8306 * Return the scrolled top position of this view. This is the top edge of
8307 * the displayed part of your view. You do not need to draw any pixels above
8308 * it, since those are outside of the frame of your view on screen.
8309 *
8310 * @return The top edge of the displayed part of your view, in pixels.
8311 */
8312 public final int getScrollY() {
8313 return mScrollY;
8314 }
8315
8316 /**
8317 * Return the width of the your view.
8318 *
8319 * @return The width of your view, in pixels.
8320 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008321 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 public final int getWidth() {
8323 return mRight - mLeft;
8324 }
8325
8326 /**
8327 * Return the height of your view.
8328 *
8329 * @return The height of your view, in pixels.
8330 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008331 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 public final int getHeight() {
8333 return mBottom - mTop;
8334 }
8335
8336 /**
8337 * Return the visible drawing bounds of your view. Fills in the output
8338 * rectangle with the values from getScrollX(), getScrollY(),
8339 * getWidth(), and getHeight().
8340 *
8341 * @param outRect The (scrolled) drawing bounds of the view.
8342 */
8343 public void getDrawingRect(Rect outRect) {
8344 outRect.left = mScrollX;
8345 outRect.top = mScrollY;
8346 outRect.right = mScrollX + (mRight - mLeft);
8347 outRect.bottom = mScrollY + (mBottom - mTop);
8348 }
8349
8350 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008351 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8352 * raw width component (that is the result is masked by
8353 * {@link #MEASURED_SIZE_MASK}).
8354 *
8355 * @return The raw measured width of this view.
8356 */
8357 public final int getMeasuredWidth() {
8358 return mMeasuredWidth & MEASURED_SIZE_MASK;
8359 }
8360
8361 /**
8362 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008363 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008364 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 * This should be used during measurement and layout calculations only. Use
8366 * {@link #getWidth()} to see how wide a view is after layout.
8367 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008368 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008370 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 return mMeasuredWidth;
8372 }
8373
8374 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008375 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8376 * raw width component (that is the result is masked by
8377 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008379 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 */
8381 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008382 return mMeasuredHeight & MEASURED_SIZE_MASK;
8383 }
8384
8385 /**
8386 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008387 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008388 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8389 * This should be used during measurement and layout calculations only. Use
8390 * {@link #getHeight()} to see how wide a view is after layout.
8391 *
8392 * @return The measured width of this view as a bit mask.
8393 */
8394 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008395 return mMeasuredHeight;
8396 }
8397
8398 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008399 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8400 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8401 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8402 * and the height component is at the shifted bits
8403 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8404 */
8405 public final int getMeasuredState() {
8406 return (mMeasuredWidth&MEASURED_STATE_MASK)
8407 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8408 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8409 }
8410
8411 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008412 * The transform matrix of this view, which is calculated based on the current
8413 * roation, scale, and pivot properties.
8414 *
8415 * @see #getRotation()
8416 * @see #getScaleX()
8417 * @see #getScaleY()
8418 * @see #getPivotX()
8419 * @see #getPivotY()
8420 * @return The current transform matrix for the view
8421 */
8422 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008423 if (mTransformationInfo != null) {
8424 updateMatrix();
8425 return mTransformationInfo.mMatrix;
8426 }
8427 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008428 }
8429
8430 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008431 * Utility function to determine if the value is far enough away from zero to be
8432 * considered non-zero.
8433 * @param value A floating point value to check for zero-ness
8434 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8435 */
8436 private static boolean nonzero(float value) {
8437 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8438 }
8439
8440 /**
Jeff Brown86671742010-09-30 20:00:15 -07008441 * Returns true if the transform matrix is the identity matrix.
8442 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008443 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008444 * @return True if the transform matrix is the identity matrix, false otherwise.
8445 */
Jeff Brown86671742010-09-30 20:00:15 -07008446 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008447 if (mTransformationInfo != null) {
8448 updateMatrix();
8449 return mTransformationInfo.mMatrixIsIdentity;
8450 }
8451 return true;
8452 }
8453
8454 void ensureTransformationInfo() {
8455 if (mTransformationInfo == null) {
8456 mTransformationInfo = new TransformationInfo();
8457 }
Jeff Brown86671742010-09-30 20:00:15 -07008458 }
8459
8460 /**
8461 * Recomputes the transform matrix if necessary.
8462 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008463 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008464 final TransformationInfo info = mTransformationInfo;
8465 if (info == null) {
8466 return;
8467 }
8468 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008469 // transform-related properties have changed since the last time someone
8470 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008471
8472 // Figure out if we need to update the pivot point
8473 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008474 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8475 info.mPrevWidth = mRight - mLeft;
8476 info.mPrevHeight = mBottom - mTop;
8477 info.mPivotX = info.mPrevWidth / 2f;
8478 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008479 }
8480 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008481 info.mMatrix.reset();
8482 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8483 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8484 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8485 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008486 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008487 if (info.mCamera == null) {
8488 info.mCamera = new Camera();
8489 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008490 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008491 info.mCamera.save();
8492 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8493 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8494 info.mCamera.getMatrix(info.matrix3D);
8495 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8496 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8497 info.mPivotY + info.mTranslationY);
8498 info.mMatrix.postConcat(info.matrix3D);
8499 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008500 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008501 info.mMatrixDirty = false;
8502 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8503 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008504 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008505 }
8506
8507 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008508 * When searching for a view to focus this rectangle is used when considering if this view is
8509 * a good candidate for receiving focus.
8510 *
8511 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8512 *
8513 * @param r The rectangle to fill in, in this view's coordinates.
8514 */
8515 public void getFocusRect(Rect r) {
8516 getDrawingRect(r);
8517 }
8518
8519 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008520 * Utility method to retrieve the inverse of the current mMatrix property.
8521 * We cache the matrix to avoid recalculating it when transform properties
8522 * have not changed.
8523 *
8524 * @return The inverse of the current matrix of this view.
8525 */
Jeff Brown86671742010-09-30 20:00:15 -07008526 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008527 final TransformationInfo info = mTransformationInfo;
8528 if (info != null) {
8529 updateMatrix();
8530 if (info.mInverseMatrixDirty) {
8531 if (info.mInverseMatrix == null) {
8532 info.mInverseMatrix = new Matrix();
8533 }
8534 info.mMatrix.invert(info.mInverseMatrix);
8535 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008536 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008537 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008538 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008539 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008540 }
8541
8542 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008543 * Gets the distance along the Z axis from the camera to this view.
8544 *
8545 * @see #setCameraDistance(float)
8546 *
8547 * @return The distance along the Z axis.
8548 */
8549 public float getCameraDistance() {
8550 ensureTransformationInfo();
8551 final float dpi = mResources.getDisplayMetrics().densityDpi;
8552 final TransformationInfo info = mTransformationInfo;
8553 if (info.mCamera == null) {
8554 info.mCamera = new Camera();
8555 info.matrix3D = new Matrix();
8556 }
8557 return -(info.mCamera.getLocationZ() * dpi);
8558 }
8559
8560 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008561 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8562 * views are drawn) from the camera to this view. The camera's distance
8563 * affects 3D transformations, for instance rotations around the X and Y
8564 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008565 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008566 * use a camera distance that's greater than the height (X axis rotation) or
8567 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008568 *
Romain Guya5364ee2011-02-24 14:46:04 -08008569 * <p>The distance of the camera from the view plane can have an affect on the
8570 * perspective distortion of the view when it is rotated around the x or y axis.
8571 * For example, a large distance will result in a large viewing angle, and there
8572 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008573 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008574 * also result in some drawing artifacts if the rotated view ends up partially
8575 * behind the camera (which is why the recommendation is to use a distance at
8576 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008577 *
Romain Guya5364ee2011-02-24 14:46:04 -08008578 * <p>The distance is expressed in "depth pixels." The default distance depends
8579 * on the screen density. For instance, on a medium density display, the
8580 * default distance is 1280. On a high density display, the default distance
8581 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008582 *
Romain Guya5364ee2011-02-24 14:46:04 -08008583 * <p>If you want to specify a distance that leads to visually consistent
8584 * results across various densities, use the following formula:</p>
8585 * <pre>
8586 * float scale = context.getResources().getDisplayMetrics().density;
8587 * view.setCameraDistance(distance * scale);
8588 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008589 *
Romain Guya5364ee2011-02-24 14:46:04 -08008590 * <p>The density scale factor of a high density display is 1.5,
8591 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008592 *
Romain Guya5364ee2011-02-24 14:46:04 -08008593 * @param distance The distance in "depth pixels", if negative the opposite
8594 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008595 *
8596 * @see #setRotationX(float)
8597 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008598 */
8599 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008600 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008601
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008602 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008603 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008604 final TransformationInfo info = mTransformationInfo;
8605 if (info.mCamera == null) {
8606 info.mCamera = new Camera();
8607 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008608 }
8609
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008610 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8611 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008612
Chet Haase9d1992d2012-03-13 11:03:25 -07008613 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008614 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008615 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008616 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008617 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8618 // View was rejected last time it was drawn by its parent; this may have changed
8619 invalidateParentIfNeeded();
8620 }
Romain Guya5364ee2011-02-24 14:46:04 -08008621 }
8622
8623 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008624 * The degrees that the view is rotated around the pivot point.
8625 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008626 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008627 * @see #getPivotX()
8628 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008629 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008630 * @return The degrees of rotation.
8631 */
Chet Haasea5531132012-02-02 13:41:44 -08008632 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008633 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008634 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008635 }
8636
8637 /**
Chet Haase897247b2010-09-09 14:54:47 -07008638 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8639 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008640 *
8641 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008642 *
8643 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008644 * @see #getPivotX()
8645 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008646 * @see #setRotationX(float)
8647 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008648 *
8649 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008650 */
8651 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008652 ensureTransformationInfo();
8653 final TransformationInfo info = mTransformationInfo;
8654 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008655 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008656 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008657 info.mRotation = rotation;
8658 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008659 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008660 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008661 mDisplayList.setRotation(rotation);
8662 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008663 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8664 // View was rejected last time it was drawn by its parent; this may have changed
8665 invalidateParentIfNeeded();
8666 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008667 }
8668 }
8669
8670 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008671 * The degrees that the view is rotated around the vertical axis through the pivot point.
8672 *
8673 * @see #getPivotX()
8674 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008675 * @see #setRotationY(float)
8676 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008677 * @return The degrees of Y rotation.
8678 */
Chet Haasea5531132012-02-02 13:41:44 -08008679 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008680 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008681 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008682 }
8683
8684 /**
Chet Haase897247b2010-09-09 14:54:47 -07008685 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8686 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8687 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008688 *
Romain Guya5364ee2011-02-24 14:46:04 -08008689 * When rotating large views, it is recommended to adjust the camera distance
8690 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008691 *
8692 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008693 *
8694 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008695 * @see #getPivotX()
8696 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008697 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008698 * @see #setRotationX(float)
8699 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008700 *
8701 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008702 */
8703 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 ensureTransformationInfo();
8705 final TransformationInfo info = mTransformationInfo;
8706 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008707 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008708 info.mRotationY = rotationY;
8709 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008710 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008711 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008712 mDisplayList.setRotationY(rotationY);
8713 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008714 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8715 // View was rejected last time it was drawn by its parent; this may have changed
8716 invalidateParentIfNeeded();
8717 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008718 }
8719 }
8720
8721 /**
8722 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8723 *
8724 * @see #getPivotX()
8725 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008726 * @see #setRotationX(float)
8727 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008728 * @return The degrees of X rotation.
8729 */
Chet Haasea5531132012-02-02 13:41:44 -08008730 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008731 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008732 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008733 }
8734
8735 /**
Chet Haase897247b2010-09-09 14:54:47 -07008736 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8737 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8738 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008739 *
Romain Guya5364ee2011-02-24 14:46:04 -08008740 * When rotating large views, it is recommended to adjust the camera distance
8741 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008742 *
8743 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008744 *
8745 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008746 * @see #getPivotX()
8747 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008748 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008749 * @see #setRotationY(float)
8750 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008751 *
8752 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008753 */
8754 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008755 ensureTransformationInfo();
8756 final TransformationInfo info = mTransformationInfo;
8757 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008758 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008759 info.mRotationX = rotationX;
8760 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008761 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008762 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008763 mDisplayList.setRotationX(rotationX);
8764 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008765 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8766 // View was rejected last time it was drawn by its parent; this may have changed
8767 invalidateParentIfNeeded();
8768 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008769 }
8770 }
8771
8772 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008773 * The amount that the view is scaled in x around the pivot point, as a proportion of
8774 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8775 *
Joe Onorato93162322010-09-16 15:42:01 -04008776 * <p>By default, this is 1.0f.
8777 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008778 * @see #getPivotX()
8779 * @see #getPivotY()
8780 * @return The scaling factor.
8781 */
Chet Haasea5531132012-02-02 13:41:44 -08008782 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008783 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008784 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008785 }
8786
8787 /**
8788 * Sets 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 means that no scaling is applied.
8790 *
8791 * @param scaleX The scaling factor.
8792 * @see #getPivotX()
8793 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008794 *
8795 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008796 */
8797 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008798 ensureTransformationInfo();
8799 final TransformationInfo info = mTransformationInfo;
8800 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008801 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008802 info.mScaleX = scaleX;
8803 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008804 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008805 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008806 mDisplayList.setScaleX(scaleX);
8807 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008808 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8809 // View was rejected last time it was drawn by its parent; this may have changed
8810 invalidateParentIfNeeded();
8811 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008812 }
8813 }
8814
8815 /**
8816 * The amount that the view is scaled in y around the pivot point, as a proportion of
8817 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8818 *
Joe Onorato93162322010-09-16 15:42:01 -04008819 * <p>By default, this is 1.0f.
8820 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008821 * @see #getPivotX()
8822 * @see #getPivotY()
8823 * @return The scaling factor.
8824 */
Chet Haasea5531132012-02-02 13:41:44 -08008825 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008826 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008827 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008828 }
8829
8830 /**
8831 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8832 * the view's unscaled width. A value of 1 means that no scaling is applied.
8833 *
8834 * @param scaleY The scaling factor.
8835 * @see #getPivotX()
8836 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008837 *
8838 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008839 */
8840 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008841 ensureTransformationInfo();
8842 final TransformationInfo info = mTransformationInfo;
8843 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008844 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008845 info.mScaleY = scaleY;
8846 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008847 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008848 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008849 mDisplayList.setScaleY(scaleY);
8850 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008851 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8852 // View was rejected last time it was drawn by its parent; this may have changed
8853 invalidateParentIfNeeded();
8854 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008855 }
8856 }
8857
8858 /**
8859 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8860 * and {@link #setScaleX(float) scaled}.
8861 *
8862 * @see #getRotation()
8863 * @see #getScaleX()
8864 * @see #getScaleY()
8865 * @see #getPivotY()
8866 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008867 *
8868 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008869 */
Chet Haasea5531132012-02-02 13:41:44 -08008870 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008871 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008872 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008873 }
8874
8875 /**
8876 * Sets the x location of the point around which the view is
8877 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008878 * By default, the pivot point is centered on the object.
8879 * Setting this property disables this behavior and causes the view to use only the
8880 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008881 *
8882 * @param pivotX The x location of the pivot point.
8883 * @see #getRotation()
8884 * @see #getScaleX()
8885 * @see #getScaleY()
8886 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008887 *
8888 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008889 */
8890 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008891 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008892 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 final TransformationInfo info = mTransformationInfo;
8894 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008895 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008896 info.mPivotX = pivotX;
8897 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008898 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008899 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008900 mDisplayList.setPivotX(pivotX);
8901 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008902 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8903 // View was rejected last time it was drawn by its parent; this may have changed
8904 invalidateParentIfNeeded();
8905 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008906 }
8907 }
8908
8909 /**
8910 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8911 * and {@link #setScaleY(float) scaled}.
8912 *
8913 * @see #getRotation()
8914 * @see #getScaleX()
8915 * @see #getScaleY()
8916 * @see #getPivotY()
8917 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008918 *
8919 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008920 */
Chet Haasea5531132012-02-02 13:41:44 -08008921 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008922 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008923 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008924 }
8925
8926 /**
8927 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008928 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8929 * Setting this property disables this behavior and causes the view to use only the
8930 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008931 *
8932 * @param pivotY The y location of the pivot point.
8933 * @see #getRotation()
8934 * @see #getScaleX()
8935 * @see #getScaleY()
8936 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008937 *
8938 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 */
8940 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008941 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008942 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008943 final TransformationInfo info = mTransformationInfo;
8944 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008945 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008946 info.mPivotY = pivotY;
8947 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008948 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008949 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008950 mDisplayList.setPivotY(pivotY);
8951 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008952 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8953 // View was rejected last time it was drawn by its parent; this may have changed
8954 invalidateParentIfNeeded();
8955 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 }
8957 }
8958
8959 /**
8960 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8961 * completely transparent and 1 means the view is completely opaque.
8962 *
Joe Onorato93162322010-09-16 15:42:01 -04008963 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008964 * @return The opacity of the view.
8965 */
Chet Haasea5531132012-02-02 13:41:44 -08008966 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008967 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008968 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008969 }
8970
8971 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008972 * Returns whether this View has content which overlaps. This function, intended to be
8973 * overridden by specific View types, is an optimization when alpha is set on a view. If
8974 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8975 * and then composited it into place, which can be expensive. If the view has no overlapping
8976 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8977 * An example of overlapping rendering is a TextView with a background image, such as a
8978 * Button. An example of non-overlapping rendering is a TextView with no background, or
8979 * an ImageView with only the foreground image. The default implementation returns true;
8980 * subclasses should override if they have cases which can be optimized.
8981 *
8982 * @return true if the content in this view might overlap, false otherwise.
8983 */
8984 public boolean hasOverlappingRendering() {
8985 return true;
8986 }
8987
8988 /**
Romain Guy171c5922011-01-06 10:04:23 -08008989 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8990 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008991 *
Romain Guy171c5922011-01-06 10:04:23 -08008992 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8993 * responsible for applying the opacity itself. Otherwise, calling this method is
8994 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008995 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008996 *
Chet Haasea5531132012-02-02 13:41:44 -08008997 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8998 * performance implications. It is generally best to use the alpha property sparingly and
8999 * transiently, as in the case of fading animations.</p>
9000 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009001 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009002 *
Joe Malin32736f02011-01-19 16:14:20 -08009003 * @see #setLayerType(int, android.graphics.Paint)
9004 *
Chet Haase73066682010-11-29 15:55:32 -08009005 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 */
9007 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009008 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009009 if (mTransformationInfo.mAlpha != alpha) {
9010 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009011 if (onSetAlpha((int) (alpha * 255))) {
9012 mPrivateFlags |= ALPHA_SET;
9013 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009014 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009015 invalidate(true);
9016 } else {
9017 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009018 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009019 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009020 mDisplayList.setAlpha(alpha);
9021 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009022 }
Chet Haaseed032702010-10-01 14:05:54 -07009023 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009024 }
9025
9026 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009027 * Faster version of setAlpha() which performs the same steps except there are
9028 * no calls to invalidate(). The caller of this function should perform proper invalidation
9029 * on the parent and this object. The return value indicates whether the subclass handles
9030 * alpha (the return value for onSetAlpha()).
9031 *
9032 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009033 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9034 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009035 */
9036 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009037 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009038 if (mTransformationInfo.mAlpha != alpha) {
9039 mTransformationInfo.mAlpha = alpha;
9040 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9041 if (subclassHandlesAlpha) {
9042 mPrivateFlags |= ALPHA_SET;
9043 return true;
9044 } else {
9045 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009046 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009047 mDisplayList.setAlpha(alpha);
9048 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009049 }
Chet Haasea00f3862011-02-22 06:34:40 -08009050 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009051 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009052 }
9053
9054 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009055 * Top position of this view relative to its parent.
9056 *
9057 * @return The top of this view, in pixels.
9058 */
9059 @ViewDebug.CapturedViewProperty
9060 public final int getTop() {
9061 return mTop;
9062 }
9063
9064 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009065 * Sets the top position of this view relative to its parent. This method is meant to be called
9066 * by the layout system and should not generally be called otherwise, because the property
9067 * may be changed at any time by the layout.
9068 *
9069 * @param top The top of this view, in pixels.
9070 */
9071 public final void setTop(int top) {
9072 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009073 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009074 final boolean matrixIsIdentity = mTransformationInfo == null
9075 || mTransformationInfo.mMatrixIsIdentity;
9076 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009077 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009078 int minTop;
9079 int yLoc;
9080 if (top < mTop) {
9081 minTop = top;
9082 yLoc = top - mTop;
9083 } else {
9084 minTop = mTop;
9085 yLoc = 0;
9086 }
Chet Haasee9140a72011-02-16 16:23:29 -08009087 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009088 }
9089 } else {
9090 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009091 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009092 }
9093
Chet Haaseed032702010-10-01 14:05:54 -07009094 int width = mRight - mLeft;
9095 int oldHeight = mBottom - mTop;
9096
Chet Haase21cd1382010-09-01 17:42:29 -07009097 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009098 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009099 mDisplayList.setTop(mTop);
9100 }
Chet Haase21cd1382010-09-01 17:42:29 -07009101
Chet Haaseed032702010-10-01 14:05:54 -07009102 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9103
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009104 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009105 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9106 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009107 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009108 }
Chet Haase21cd1382010-09-01 17:42:29 -07009109 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009110 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009111 }
Chet Haase55dbb652010-12-21 20:15:08 -08009112 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009113 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009114 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9115 // View was rejected last time it was drawn by its parent; this may have changed
9116 invalidateParentIfNeeded();
9117 }
Chet Haase21cd1382010-09-01 17:42:29 -07009118 }
9119 }
9120
9121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 * Bottom position of this view relative to its parent.
9123 *
9124 * @return The bottom of this view, in pixels.
9125 */
9126 @ViewDebug.CapturedViewProperty
9127 public final int getBottom() {
9128 return mBottom;
9129 }
9130
9131 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009132 * True if this view has changed since the last time being drawn.
9133 *
9134 * @return The dirty state of this view.
9135 */
9136 public boolean isDirty() {
9137 return (mPrivateFlags & DIRTY_MASK) != 0;
9138 }
9139
9140 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009141 * Sets the bottom position of this view relative to its parent. This method is meant to be
9142 * called by the layout system and should not generally be called otherwise, because the
9143 * property may be changed at any time by the layout.
9144 *
9145 * @param bottom The bottom of this view, in pixels.
9146 */
9147 public final void setBottom(int bottom) {
9148 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009149 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009150 final boolean matrixIsIdentity = mTransformationInfo == null
9151 || mTransformationInfo.mMatrixIsIdentity;
9152 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009153 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009154 int maxBottom;
9155 if (bottom < mBottom) {
9156 maxBottom = mBottom;
9157 } else {
9158 maxBottom = bottom;
9159 }
Chet Haasee9140a72011-02-16 16:23:29 -08009160 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009161 }
9162 } else {
9163 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009164 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009165 }
9166
Chet Haaseed032702010-10-01 14:05:54 -07009167 int width = mRight - mLeft;
9168 int oldHeight = mBottom - mTop;
9169
Chet Haase21cd1382010-09-01 17:42:29 -07009170 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009171 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009172 mDisplayList.setBottom(mBottom);
9173 }
Chet Haase21cd1382010-09-01 17:42:29 -07009174
Chet Haaseed032702010-10-01 14:05:54 -07009175 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9176
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009177 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009178 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9179 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009180 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009181 }
Chet Haase21cd1382010-09-01 17:42:29 -07009182 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009183 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009184 }
Chet Haase55dbb652010-12-21 20:15:08 -08009185 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009186 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009187 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9188 // View was rejected last time it was drawn by its parent; this may have changed
9189 invalidateParentIfNeeded();
9190 }
Chet Haase21cd1382010-09-01 17:42:29 -07009191 }
9192 }
9193
9194 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009195 * Left position of this view relative to its parent.
9196 *
9197 * @return The left edge of this view, in pixels.
9198 */
9199 @ViewDebug.CapturedViewProperty
9200 public final int getLeft() {
9201 return mLeft;
9202 }
9203
9204 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009205 * Sets the left position of this view relative to its parent. This method is meant to be called
9206 * by the layout system and should not generally be called otherwise, because the property
9207 * may be changed at any time by the layout.
9208 *
9209 * @param left The bottom of this view, in pixels.
9210 */
9211 public final void setLeft(int left) {
9212 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009213 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009214 final boolean matrixIsIdentity = mTransformationInfo == null
9215 || mTransformationInfo.mMatrixIsIdentity;
9216 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009217 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009218 int minLeft;
9219 int xLoc;
9220 if (left < mLeft) {
9221 minLeft = left;
9222 xLoc = left - mLeft;
9223 } else {
9224 minLeft = mLeft;
9225 xLoc = 0;
9226 }
Chet Haasee9140a72011-02-16 16:23:29 -08009227 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009228 }
9229 } else {
9230 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009231 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009232 }
9233
Chet Haaseed032702010-10-01 14:05:54 -07009234 int oldWidth = mRight - mLeft;
9235 int height = mBottom - mTop;
9236
Chet Haase21cd1382010-09-01 17:42:29 -07009237 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009238 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009239 mDisplayList.setLeft(left);
9240 }
Chet Haase21cd1382010-09-01 17:42:29 -07009241
Chet Haaseed032702010-10-01 14:05:54 -07009242 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9243
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009244 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009245 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9246 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009247 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009248 }
Chet Haase21cd1382010-09-01 17:42:29 -07009249 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009250 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009251 }
Chet Haase55dbb652010-12-21 20:15:08 -08009252 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009253 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009254 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9255 // View was rejected last time it was drawn by its parent; this may have changed
9256 invalidateParentIfNeeded();
9257 }
Chet Haase21cd1382010-09-01 17:42:29 -07009258 }
9259 }
9260
9261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 * Right position of this view relative to its parent.
9263 *
9264 * @return The right edge of this view, in pixels.
9265 */
9266 @ViewDebug.CapturedViewProperty
9267 public final int getRight() {
9268 return mRight;
9269 }
9270
9271 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009272 * Sets the right position of this view relative to its parent. This method is meant to be called
9273 * by the layout system and should not generally be called otherwise, because the property
9274 * may be changed at any time by the layout.
9275 *
9276 * @param right The bottom of this view, in pixels.
9277 */
9278 public final void setRight(int right) {
9279 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009280 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009281 final boolean matrixIsIdentity = mTransformationInfo == null
9282 || mTransformationInfo.mMatrixIsIdentity;
9283 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009284 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009285 int maxRight;
9286 if (right < mRight) {
9287 maxRight = mRight;
9288 } else {
9289 maxRight = right;
9290 }
Chet Haasee9140a72011-02-16 16:23:29 -08009291 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009292 }
9293 } else {
9294 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009295 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009296 }
9297
Chet Haaseed032702010-10-01 14:05:54 -07009298 int oldWidth = mRight - mLeft;
9299 int height = mBottom - mTop;
9300
Chet Haase21cd1382010-09-01 17:42:29 -07009301 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009302 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009303 mDisplayList.setRight(mRight);
9304 }
Chet Haase21cd1382010-09-01 17:42:29 -07009305
Chet Haaseed032702010-10-01 14:05:54 -07009306 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9307
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009308 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009309 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9310 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009312 }
Chet Haase21cd1382010-09-01 17:42:29 -07009313 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009314 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009315 }
Chet Haase55dbb652010-12-21 20:15:08 -08009316 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009317 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009318 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9319 // View was rejected last time it was drawn by its parent; this may have changed
9320 invalidateParentIfNeeded();
9321 }
Chet Haase21cd1382010-09-01 17:42:29 -07009322 }
9323 }
9324
9325 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009326 * The visual x position of this view, in pixels. This is equivalent to the
9327 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009328 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009329 *
Chet Haasedf030d22010-07-30 17:22:38 -07009330 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009331 */
Chet Haasea5531132012-02-02 13:41:44 -08009332 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009333 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009334 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009335 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009336
Chet Haasedf030d22010-07-30 17:22:38 -07009337 /**
9338 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9339 * {@link #setTranslationX(float) translationX} property to be the difference between
9340 * the x value passed in and the current {@link #getLeft() left} property.
9341 *
9342 * @param x The visual x position of this view, in pixels.
9343 */
9344 public void setX(float x) {
9345 setTranslationX(x - mLeft);
9346 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009347
Chet Haasedf030d22010-07-30 17:22:38 -07009348 /**
9349 * The visual y position of this view, in pixels. This is equivalent to the
9350 * {@link #setTranslationY(float) translationY} property plus the current
9351 * {@link #getTop() top} property.
9352 *
9353 * @return The visual y position of this view, in pixels.
9354 */
Chet Haasea5531132012-02-02 13:41:44 -08009355 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009356 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009357 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009358 }
9359
9360 /**
9361 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9362 * {@link #setTranslationY(float) translationY} property to be the difference between
9363 * the y value passed in and the current {@link #getTop() top} property.
9364 *
9365 * @param y The visual y position of this view, in pixels.
9366 */
9367 public void setY(float y) {
9368 setTranslationY(y - mTop);
9369 }
9370
9371
9372 /**
9373 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9374 * This position is post-layout, in addition to wherever the object's
9375 * layout placed it.
9376 *
9377 * @return The horizontal position of this view relative to its left position, in pixels.
9378 */
Chet Haasea5531132012-02-02 13:41:44 -08009379 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009380 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009381 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009382 }
9383
9384 /**
9385 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9386 * This effectively positions the object post-layout, in addition to wherever the object's
9387 * layout placed it.
9388 *
9389 * @param translationX The horizontal position of this view relative to its left position,
9390 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009391 *
9392 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009393 */
9394 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009395 ensureTransformationInfo();
9396 final TransformationInfo info = mTransformationInfo;
9397 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009398 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009399 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009400 info.mTranslationX = translationX;
9401 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009402 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009403 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009404 mDisplayList.setTranslationX(translationX);
9405 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009406 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9407 // View was rejected last time it was drawn by its parent; this may have changed
9408 invalidateParentIfNeeded();
9409 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009410 }
9411 }
9412
9413 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009414 * The horizontal location of this view relative to its {@link #getTop() top} position.
9415 * This position is post-layout, in addition to wherever the object's
9416 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009417 *
Chet Haasedf030d22010-07-30 17:22:38 -07009418 * @return The vertical position of this view relative to its top position,
9419 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009420 */
Chet Haasea5531132012-02-02 13:41:44 -08009421 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009422 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009423 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009424 }
9425
9426 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009427 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9428 * This effectively positions the object post-layout, in addition to wherever the object's
9429 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009430 *
Chet Haasedf030d22010-07-30 17:22:38 -07009431 * @param translationY The vertical position of this view relative to its top position,
9432 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009433 *
9434 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009435 */
Chet Haasedf030d22010-07-30 17:22:38 -07009436 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009437 ensureTransformationInfo();
9438 final TransformationInfo info = mTransformationInfo;
9439 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009440 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009441 info.mTranslationY = translationY;
9442 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009443 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009444 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009445 mDisplayList.setTranslationY(translationY);
9446 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009447 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9448 // View was rejected last time it was drawn by its parent; this may have changed
9449 invalidateParentIfNeeded();
9450 }
Chet Haasedf030d22010-07-30 17:22:38 -07009451 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009452 }
9453
9454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009455 * Hit rectangle in parent's coordinates
9456 *
9457 * @param outRect The hit rectangle of the view.
9458 */
9459 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009460 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009461 final TransformationInfo info = mTransformationInfo;
9462 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009463 outRect.set(mLeft, mTop, mRight, mBottom);
9464 } else {
9465 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009466 tmpRect.set(-info.mPivotX, -info.mPivotY,
9467 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9468 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009469 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9470 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009471 }
9472 }
9473
9474 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009475 * Determines whether the given point, in local coordinates is inside the view.
9476 */
9477 /*package*/ final boolean pointInView(float localX, float localY) {
9478 return localX >= 0 && localX < (mRight - mLeft)
9479 && localY >= 0 && localY < (mBottom - mTop);
9480 }
9481
9482 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009483 * Utility method to determine whether the given point, in local coordinates,
9484 * is inside the view, where the area of the view is expanded by the slop factor.
9485 * This method is called while processing touch-move events to determine if the event
9486 * is still within the view.
9487 */
9488 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009489 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009490 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 }
9492
9493 /**
9494 * When a view has focus and the user navigates away from it, the next view is searched for
9495 * starting from the rectangle filled in by this method.
9496 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009497 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9498 * of the view. However, if your view maintains some idea of internal selection,
9499 * such as a cursor, or a selected row or column, you should override this method and
9500 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501 *
9502 * @param r The rectangle to fill in, in this view's coordinates.
9503 */
9504 public void getFocusedRect(Rect r) {
9505 getDrawingRect(r);
9506 }
9507
9508 /**
9509 * If some part of this view is not clipped by any of its parents, then
9510 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009511 * coordinates (without taking possible View rotations into account), offset
9512 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9513 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 *
9515 * @param r If true is returned, r holds the global coordinates of the
9516 * visible portion of this view.
9517 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9518 * between this view and its root. globalOffet may be null.
9519 * @return true if r is non-empty (i.e. part of the view is visible at the
9520 * root level.
9521 */
9522 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9523 int width = mRight - mLeft;
9524 int height = mBottom - mTop;
9525 if (width > 0 && height > 0) {
9526 r.set(0, 0, width, height);
9527 if (globalOffset != null) {
9528 globalOffset.set(-mScrollX, -mScrollY);
9529 }
9530 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9531 }
9532 return false;
9533 }
9534
9535 public final boolean getGlobalVisibleRect(Rect r) {
9536 return getGlobalVisibleRect(r, null);
9537 }
9538
9539 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009540 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009541 if (getGlobalVisibleRect(r, offset)) {
9542 r.offset(-offset.x, -offset.y); // make r local
9543 return true;
9544 }
9545 return false;
9546 }
9547
9548 /**
9549 * Offset this view's vertical location by the specified number of pixels.
9550 *
9551 * @param offset the number of pixels to offset the view by
9552 */
9553 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009554 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009555 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009556 final boolean matrixIsIdentity = mTransformationInfo == null
9557 || mTransformationInfo.mMatrixIsIdentity;
9558 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009559 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009560 invalidateViewProperty(false, false);
9561 } else {
9562 final ViewParent p = mParent;
9563 if (p != null && mAttachInfo != null) {
9564 final Rect r = mAttachInfo.mTmpInvalRect;
9565 int minTop;
9566 int maxBottom;
9567 int yLoc;
9568 if (offset < 0) {
9569 minTop = mTop + offset;
9570 maxBottom = mBottom;
9571 yLoc = offset;
9572 } else {
9573 minTop = mTop;
9574 maxBottom = mBottom + offset;
9575 yLoc = 0;
9576 }
9577 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9578 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009579 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009580 }
9581 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009582 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009583 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009584
Chet Haasec3aa3612010-06-17 08:50:37 -07009585 mTop += offset;
9586 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009587 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009588 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009589 invalidateViewProperty(false, false);
9590 } else {
9591 if (!matrixIsIdentity) {
9592 invalidateViewProperty(false, true);
9593 }
9594 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009595 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 }
9598
9599 /**
9600 * Offset this view's horizontal location by the specified amount of pixels.
9601 *
9602 * @param offset the numer of pixels to offset the view by
9603 */
9604 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009605 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009606 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009607 final boolean matrixIsIdentity = mTransformationInfo == null
9608 || mTransformationInfo.mMatrixIsIdentity;
9609 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009610 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009611 invalidateViewProperty(false, false);
9612 } else {
9613 final ViewParent p = mParent;
9614 if (p != null && mAttachInfo != null) {
9615 final Rect r = mAttachInfo.mTmpInvalRect;
9616 int minLeft;
9617 int maxRight;
9618 if (offset < 0) {
9619 minLeft = mLeft + offset;
9620 maxRight = mRight;
9621 } else {
9622 minLeft = mLeft;
9623 maxRight = mRight + offset;
9624 }
9625 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9626 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009627 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009628 }
9629 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009630 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009631 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009632
Chet Haasec3aa3612010-06-17 08:50:37 -07009633 mLeft += offset;
9634 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009635 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009636 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009637 invalidateViewProperty(false, false);
9638 } else {
9639 if (!matrixIsIdentity) {
9640 invalidateViewProperty(false, true);
9641 }
9642 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009643 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 }
9646
9647 /**
9648 * Get the LayoutParams associated with this view. All views should have
9649 * layout parameters. These supply parameters to the <i>parent</i> of this
9650 * view specifying how it should be arranged. There are many subclasses of
9651 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9652 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009653 *
9654 * This method may return null if this View is not attached to a parent
9655 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9656 * was not invoked successfully. When a View is attached to a parent
9657 * ViewGroup, this method must not return null.
9658 *
9659 * @return The LayoutParams associated with this view, or null if no
9660 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009662 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009663 public ViewGroup.LayoutParams getLayoutParams() {
9664 return mLayoutParams;
9665 }
9666
9667 /**
9668 * Set the layout parameters associated with this view. These supply
9669 * parameters to the <i>parent</i> of this view specifying how it should be
9670 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9671 * correspond to the different subclasses of ViewGroup that are responsible
9672 * for arranging their children.
9673 *
Romain Guy01c174b2011-02-22 11:51:06 -08009674 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 */
9676 public void setLayoutParams(ViewGroup.LayoutParams params) {
9677 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009678 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 }
9680 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009681 if (mParent instanceof ViewGroup) {
9682 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009684 requestLayout();
9685 }
9686
9687 /**
9688 * Set the scrolled position of your view. This will cause a call to
9689 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9690 * invalidated.
9691 * @param x the x position to scroll to
9692 * @param y the y position to scroll to
9693 */
9694 public void scrollTo(int x, int y) {
9695 if (mScrollX != x || mScrollY != y) {
9696 int oldX = mScrollX;
9697 int oldY = mScrollY;
9698 mScrollX = x;
9699 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009700 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009702 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009703 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 }
9706 }
9707
9708 /**
9709 * Move the scrolled position of your view. This will cause a call to
9710 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9711 * invalidated.
9712 * @param x the amount of pixels to scroll by horizontally
9713 * @param y the amount of pixels to scroll by vertically
9714 */
9715 public void scrollBy(int x, int y) {
9716 scrollTo(mScrollX + x, mScrollY + y);
9717 }
9718
9719 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009720 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9721 * animation to fade the scrollbars out after a default delay. If a subclass
9722 * provides animated scrolling, the start delay should equal the duration
9723 * of the scrolling animation.</p>
9724 *
9725 * <p>The animation starts only if at least one of the scrollbars is
9726 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9727 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9728 * this method returns true, and false otherwise. If the animation is
9729 * started, this method calls {@link #invalidate()}; in that case the
9730 * caller should not call {@link #invalidate()}.</p>
9731 *
9732 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009733 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009734 *
9735 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9736 * and {@link #scrollTo(int, int)}.</p>
9737 *
9738 * @return true if the animation is played, false otherwise
9739 *
9740 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009741 * @see #scrollBy(int, int)
9742 * @see #scrollTo(int, int)
9743 * @see #isHorizontalScrollBarEnabled()
9744 * @see #isVerticalScrollBarEnabled()
9745 * @see #setHorizontalScrollBarEnabled(boolean)
9746 * @see #setVerticalScrollBarEnabled(boolean)
9747 */
9748 protected boolean awakenScrollBars() {
9749 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009750 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009751 }
9752
9753 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009754 * Trigger the scrollbars to draw.
9755 * This method differs from awakenScrollBars() only in its default duration.
9756 * initialAwakenScrollBars() will show the scroll bars for longer than
9757 * usual to give the user more of a chance to notice them.
9758 *
9759 * @return true if the animation is played, false otherwise.
9760 */
9761 private boolean initialAwakenScrollBars() {
9762 return mScrollCache != null &&
9763 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9764 }
9765
9766 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009767 * <p>
9768 * Trigger the scrollbars to draw. When invoked this method starts an
9769 * animation to fade the scrollbars out after a fixed delay. If a subclass
9770 * provides animated scrolling, the start delay should equal the duration of
9771 * the scrolling animation.
9772 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009773 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009774 * <p>
9775 * The animation starts only if at least one of the scrollbars is enabled,
9776 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9777 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9778 * this method returns true, and false otherwise. If the animation is
9779 * started, this method calls {@link #invalidate()}; in that case the caller
9780 * should not call {@link #invalidate()}.
9781 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009782 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009783 * <p>
9784 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009785 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009786 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009787 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009788 * @param startDelay the delay, in milliseconds, after which the animation
9789 * should start; when the delay is 0, the animation starts
9790 * immediately
9791 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009792 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009793 * @see #scrollBy(int, int)
9794 * @see #scrollTo(int, int)
9795 * @see #isHorizontalScrollBarEnabled()
9796 * @see #isVerticalScrollBarEnabled()
9797 * @see #setHorizontalScrollBarEnabled(boolean)
9798 * @see #setVerticalScrollBarEnabled(boolean)
9799 */
9800 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009801 return awakenScrollBars(startDelay, true);
9802 }
Joe Malin32736f02011-01-19 16:14:20 -08009803
Mike Cleron290947b2009-09-29 18:34:32 -07009804 /**
9805 * <p>
9806 * Trigger the scrollbars to draw. When invoked this method starts an
9807 * animation to fade the scrollbars out after a fixed delay. If a subclass
9808 * provides animated scrolling, the start delay should equal the duration of
9809 * the scrolling animation.
9810 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009811 *
Mike Cleron290947b2009-09-29 18:34:32 -07009812 * <p>
9813 * The animation starts only if at least one of the scrollbars is enabled,
9814 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9815 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9816 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009817 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009818 * is set to true; in that case the caller
9819 * should not call {@link #invalidate()}.
9820 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009821 *
Mike Cleron290947b2009-09-29 18:34:32 -07009822 * <p>
9823 * This method should be invoked everytime a subclass directly updates the
9824 * scroll parameters.
9825 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009826 *
Mike Cleron290947b2009-09-29 18:34:32 -07009827 * @param startDelay the delay, in milliseconds, after which the animation
9828 * should start; when the delay is 0, the animation starts
9829 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009830 *
Mike Cleron290947b2009-09-29 18:34:32 -07009831 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009832 *
Mike Cleron290947b2009-09-29 18:34:32 -07009833 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009834 *
Mike Cleron290947b2009-09-29 18:34:32 -07009835 * @see #scrollBy(int, int)
9836 * @see #scrollTo(int, int)
9837 * @see #isHorizontalScrollBarEnabled()
9838 * @see #isVerticalScrollBarEnabled()
9839 * @see #setHorizontalScrollBarEnabled(boolean)
9840 * @see #setVerticalScrollBarEnabled(boolean)
9841 */
9842 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009843 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009844
Mike Cleronf116bf82009-09-27 19:14:12 -07009845 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9846 return false;
9847 }
9848
9849 if (scrollCache.scrollBar == null) {
9850 scrollCache.scrollBar = new ScrollBarDrawable();
9851 }
9852
9853 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9854
Mike Cleron290947b2009-09-29 18:34:32 -07009855 if (invalidate) {
9856 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009857 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009858 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009859
9860 if (scrollCache.state == ScrollabilityCache.OFF) {
9861 // FIXME: this is copied from WindowManagerService.
9862 // We should get this value from the system when it
9863 // is possible to do so.
9864 final int KEY_REPEAT_FIRST_DELAY = 750;
9865 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9866 }
9867
9868 // Tell mScrollCache when we should start fading. This may
9869 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009870 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009871 scrollCache.fadeStartTime = fadeStartTime;
9872 scrollCache.state = ScrollabilityCache.ON;
9873
9874 // Schedule our fader to run, unscheduling any old ones first
9875 if (mAttachInfo != null) {
9876 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9877 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9878 }
9879
9880 return true;
9881 }
9882
9883 return false;
9884 }
9885
9886 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009887 * Do not invalidate views which are not visible and which are not running an animation. They
9888 * will not get drawn and they should not set dirty flags as if they will be drawn
9889 */
9890 private boolean skipInvalidate() {
9891 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9892 (!(mParent instanceof ViewGroup) ||
9893 !((ViewGroup) mParent).isViewTransitioning(this));
9894 }
9895 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009896 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009897 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9898 * in the future. This must be called from a UI thread. To call from a non-UI
9899 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 *
9901 * WARNING: This method is destructive to dirty.
9902 * @param dirty the rectangle representing the bounds of the dirty region
9903 */
9904 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009905 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009906 return;
9907 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009908 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009909 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9910 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009912 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009913 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 final ViewParent p = mParent;
9915 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009916 //noinspection PointlessBooleanExpression,ConstantConditions
9917 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9918 if (p != null && ai != null && ai.mHardwareAccelerated) {
9919 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009920 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009921 p.invalidateChild(this, null);
9922 return;
9923 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 if (p != null && ai != null) {
9926 final int scrollX = mScrollX;
9927 final int scrollY = mScrollY;
9928 final Rect r = ai.mTmpInvalRect;
9929 r.set(dirty.left - scrollX, dirty.top - scrollY,
9930 dirty.right - scrollX, dirty.bottom - scrollY);
9931 mParent.invalidateChild(this, r);
9932 }
9933 }
9934 }
9935
9936 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009937 * 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 -08009938 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009939 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9940 * will be called at some point in the future. This must be called from
9941 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009942 * @param l the left position of the dirty region
9943 * @param t the top position of the dirty region
9944 * @param r the right position of the dirty region
9945 * @param b the bottom position of the dirty region
9946 */
9947 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -07009948 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009949 return;
9950 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009951 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009952 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9953 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009955 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009956 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 final ViewParent p = mParent;
9958 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009959 //noinspection PointlessBooleanExpression,ConstantConditions
9960 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9961 if (p != null && ai != null && ai.mHardwareAccelerated) {
9962 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009963 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009964 p.invalidateChild(this, null);
9965 return;
9966 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 if (p != null && ai != null && l < r && t < b) {
9969 final int scrollX = mScrollX;
9970 final int scrollY = mScrollY;
9971 final Rect tmpr = ai.mTmpInvalRect;
9972 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9973 p.invalidateChild(this, tmpr);
9974 }
9975 }
9976 }
9977
9978 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009979 * Invalidate the whole view. If the view is visible,
9980 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9981 * the future. This must be called from a UI thread. To call from a non-UI thread,
9982 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009983 */
9984 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009985 invalidate(true);
9986 }
Joe Malin32736f02011-01-19 16:14:20 -08009987
Chet Haaseed032702010-10-01 14:05:54 -07009988 /**
9989 * This is where the invalidate() work actually happens. A full invalidate()
9990 * causes the drawing cache to be invalidated, but this function can be called with
9991 * invalidateCache set to false to skip that invalidation step for cases that do not
9992 * need it (for example, a component that remains at the same dimensions with the same
9993 * content).
9994 *
9995 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9996 * well. This is usually true for a full invalidate, but may be set to false if the
9997 * View's contents or dimensions have not changed.
9998 */
Romain Guy849d0a32011-02-01 17:20:48 -08009999 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010000 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010001 return;
10002 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010003 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010004 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010005 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10006 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010007 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010008 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010009 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010010 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010011 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010014 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010015 //noinspection PointlessBooleanExpression,ConstantConditions
10016 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10017 if (p != null && ai != null && ai.mHardwareAccelerated) {
10018 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010019 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010020 p.invalidateChild(this, null);
10021 return;
10022 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010023 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 if (p != null && ai != null) {
10026 final Rect r = ai.mTmpInvalRect;
10027 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10028 // Don't call invalidate -- we don't want to internally scroll
10029 // our own bounds
10030 p.invalidateChild(this, r);
10031 }
10032 }
10033 }
10034
10035 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010036 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10037 * set any flags or handle all of the cases handled by the default invalidation methods.
10038 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10039 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10040 * walk up the hierarchy, transforming the dirty rect as necessary.
10041 *
10042 * The method also handles normal invalidation logic if display list properties are not
10043 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10044 * backup approach, to handle these cases used in the various property-setting methods.
10045 *
10046 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10047 * are not being used in this view
10048 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10049 * list properties are not being used in this view
10050 */
10051 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010052 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010053 if (invalidateParent) {
10054 invalidateParentCaches();
10055 }
10056 if (forceRedraw) {
10057 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10058 }
10059 invalidate(false);
10060 } else {
10061 final AttachInfo ai = mAttachInfo;
10062 final ViewParent p = mParent;
10063 if (p != null && ai != null) {
10064 final Rect r = ai.mTmpInvalRect;
10065 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10066 if (mParent instanceof ViewGroup) {
10067 ((ViewGroup) mParent).invalidateChildFast(this, r);
10068 } else {
10069 mParent.invalidateChild(this, r);
10070 }
10071 }
10072 }
10073 }
10074
10075 /**
10076 * Utility method to transform a given Rect by the current matrix of this view.
10077 */
10078 void transformRect(final Rect rect) {
10079 if (!getMatrix().isIdentity()) {
10080 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10081 boundingRect.set(rect);
10082 getMatrix().mapRect(boundingRect);
10083 rect.set((int) (boundingRect.left - 0.5f),
10084 (int) (boundingRect.top - 0.5f),
10085 (int) (boundingRect.right + 0.5f),
10086 (int) (boundingRect.bottom + 0.5f));
10087 }
10088 }
10089
10090 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010091 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010092 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10093 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010094 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10095 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010096 *
10097 * @hide
10098 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010099 protected void invalidateParentCaches() {
10100 if (mParent instanceof View) {
10101 ((View) mParent).mPrivateFlags |= INVALIDATED;
10102 }
10103 }
Joe Malin32736f02011-01-19 16:14:20 -080010104
Romain Guy0fd89bf2011-01-26 15:41:30 -080010105 /**
10106 * Used to indicate that the parent of this view should be invalidated. This functionality
10107 * 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
10109 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10110 * an invalidation event to the parent.
10111 *
10112 * @hide
10113 */
10114 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010115 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010116 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010117 }
10118 }
10119
10120 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010121 * Indicates whether this View is opaque. An opaque View guarantees that it will
10122 * draw all the pixels overlapping its bounds using a fully opaque color.
10123 *
10124 * Subclasses of View should override this method whenever possible to indicate
10125 * whether an instance is opaque. Opaque Views are treated in a special way by
10126 * the View hierarchy, possibly allowing it to perform optimizations during
10127 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010128 *
Romain Guy24443ea2009-05-11 11:56:30 -070010129 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010130 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010131 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010132 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010133 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010134 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10135 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010136 }
10137
Adam Powell20232d02010-12-08 21:08:53 -080010138 /**
10139 * @hide
10140 */
10141 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010142 // Opaque if:
10143 // - Has a background
10144 // - Background is opaque
10145 // - Doesn't have scrollbars or scrollbars are inside overlay
10146
Philip Milne6c8ea062012-04-03 17:38:43 -070010147 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010148 mPrivateFlags |= OPAQUE_BACKGROUND;
10149 } else {
10150 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10151 }
10152
10153 final int flags = mViewFlags;
10154 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10155 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10156 mPrivateFlags |= OPAQUE_SCROLLBARS;
10157 } else {
10158 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10159 }
10160 }
10161
10162 /**
10163 * @hide
10164 */
10165 protected boolean hasOpaqueScrollbars() {
10166 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010167 }
10168
10169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 * @return A handler associated with the thread running the View. This
10171 * handler can be used to pump events in the UI events queue.
10172 */
10173 public Handler getHandler() {
10174 if (mAttachInfo != null) {
10175 return mAttachInfo.mHandler;
10176 }
10177 return null;
10178 }
10179
10180 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010181 * Gets the view root associated with the View.
10182 * @return The view root, or null if none.
10183 * @hide
10184 */
10185 public ViewRootImpl getViewRootImpl() {
10186 if (mAttachInfo != null) {
10187 return mAttachInfo.mViewRootImpl;
10188 }
10189 return null;
10190 }
10191
10192 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010193 * <p>Causes the Runnable to be added to the message queue.
10194 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010195 *
Romain Guye63a4f32011-08-11 11:33:31 -070010196 * <p>This method can be invoked from outside of the UI thread
10197 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 *
10199 * @param action The Runnable that will be executed.
10200 *
10201 * @return Returns true if the Runnable was successfully placed in to the
10202 * message queue. Returns false on failure, usually because the
10203 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010204 *
10205 * @see #postDelayed
10206 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 */
10208 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010209 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010210 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010211 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010213 // Assume that post will succeed later
10214 ViewRootImpl.getRunQueue().post(action);
10215 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 }
10217
10218 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010219 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010221 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010222 *
Romain Guye63a4f32011-08-11 11:33:31 -070010223 * <p>This method can be invoked from outside of the UI thread
10224 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 *
10226 * @param action The Runnable that will be executed.
10227 * @param delayMillis The delay (in milliseconds) until the Runnable
10228 * will be executed.
10229 *
10230 * @return true if the Runnable was successfully placed in to the
10231 * message queue. Returns false on failure, usually because the
10232 * looper processing the message queue is exiting. Note that a
10233 * result of true does not mean the Runnable will be processed --
10234 * if the looper is quit before the delivery time of the message
10235 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010236 *
10237 * @see #post
10238 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 */
10240 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010241 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010242 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010243 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010244 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010245 // Assume that post will succeed later
10246 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10247 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 }
10249
10250 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010251 * <p>Causes the Runnable to execute on the next animation time step.
10252 * The runnable will be run on the user interface thread.</p>
10253 *
10254 * <p>This method can be invoked from outside of the UI thread
10255 * only when this View is attached to a window.</p>
10256 *
10257 * @param action The Runnable that will be executed.
10258 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010259 * @see #postOnAnimationDelayed
10260 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010261 */
10262 public void postOnAnimation(Runnable action) {
10263 final AttachInfo attachInfo = mAttachInfo;
10264 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010265 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10266 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010267 } else {
10268 // Assume that post will succeed later
10269 ViewRootImpl.getRunQueue().post(action);
10270 }
10271 }
10272
10273 /**
10274 * <p>Causes the Runnable to execute on the next animation time step,
10275 * after the specified amount of time elapses.
10276 * The runnable will be run on the user interface thread.</p>
10277 *
10278 * <p>This method can be invoked from outside of the UI thread
10279 * only when this View is attached to a window.</p>
10280 *
10281 * @param action The Runnable that will be executed.
10282 * @param delayMillis The delay (in milliseconds) until the Runnable
10283 * will be executed.
10284 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010285 * @see #postOnAnimation
10286 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010287 */
10288 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10289 final AttachInfo attachInfo = mAttachInfo;
10290 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010291 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10292 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010293 } else {
10294 // Assume that post will succeed later
10295 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10296 }
10297 }
10298
10299 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010300 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010301 *
Romain Guye63a4f32011-08-11 11:33:31 -070010302 * <p>This method can be invoked from outside of the UI thread
10303 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010304 *
10305 * @param action The Runnable to remove from the message handling queue
10306 *
10307 * @return true if this view could ask the Handler to remove the Runnable,
10308 * false otherwise. When the returned value is true, the Runnable
10309 * may or may not have been actually removed from the message queue
10310 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010311 *
10312 * @see #post
10313 * @see #postDelayed
10314 * @see #postOnAnimation
10315 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 */
10317 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010318 if (action != null) {
10319 final AttachInfo attachInfo = mAttachInfo;
10320 if (attachInfo != null) {
10321 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010322 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10323 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010324 } else {
10325 // Assume that post will succeed later
10326 ViewRootImpl.getRunQueue().removeCallbacks(action);
10327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 return true;
10330 }
10331
10332 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010333 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10334 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 *
Romain Guye63a4f32011-08-11 11:33:31 -070010336 * <p>This method can be invoked from outside of the UI thread
10337 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010338 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010340 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 */
10342 public void postInvalidate() {
10343 postInvalidateDelayed(0);
10344 }
10345
10346 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010347 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10348 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010349 *
Romain Guye63a4f32011-08-11 11:33:31 -070010350 * <p>This method can be invoked from outside of the UI thread
10351 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 *
10353 * @param left The left coordinate of the rectangle to invalidate.
10354 * @param top The top coordinate of the rectangle to invalidate.
10355 * @param right The right coordinate of the rectangle to invalidate.
10356 * @param bottom The bottom coordinate of the rectangle to invalidate.
10357 *
10358 * @see #invalidate(int, int, int, int)
10359 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010360 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 */
10362 public void postInvalidate(int left, int top, int right, int bottom) {
10363 postInvalidateDelayed(0, left, top, right, bottom);
10364 }
10365
10366 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010367 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10368 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010369 *
Romain Guye63a4f32011-08-11 11:33:31 -070010370 * <p>This method can be invoked from outside of the UI thread
10371 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 *
10373 * @param delayMilliseconds the duration in milliseconds to delay the
10374 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010375 *
10376 * @see #invalidate()
10377 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 */
10379 public void postInvalidateDelayed(long delayMilliseconds) {
10380 // We try only with the AttachInfo because there's no point in invalidating
10381 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010382 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010383 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010384 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 }
10386 }
10387
10388 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010389 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10390 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010391 *
Romain Guye63a4f32011-08-11 11:33:31 -070010392 * <p>This method can be invoked from outside of the UI thread
10393 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 *
10395 * @param delayMilliseconds the duration in milliseconds to delay the
10396 * invalidation by
10397 * @param left The left coordinate of the rectangle to invalidate.
10398 * @param top The top coordinate of the rectangle to invalidate.
10399 * @param right The right coordinate of the rectangle to invalidate.
10400 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010401 *
10402 * @see #invalidate(int, int, int, int)
10403 * @see #invalidate(Rect)
10404 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 */
10406 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10407 int right, int bottom) {
10408
10409 // We try only with the AttachInfo because there's no point in invalidating
10410 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010411 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010412 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10414 info.target = this;
10415 info.left = left;
10416 info.top = top;
10417 info.right = right;
10418 info.bottom = bottom;
10419
Jeff Browna175a5b2012-02-15 19:18:31 -080010420 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 }
10422 }
10423
10424 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010425 * <p>Cause an invalidate to happen on the next animation time step, typically the
10426 * next display frame.</p>
10427 *
10428 * <p>This method can be invoked from outside of the UI thread
10429 * only when this View is attached to a window.</p>
10430 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010431 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010432 */
10433 public void postInvalidateOnAnimation() {
10434 // We try only with the AttachInfo because there's no point in invalidating
10435 // if we are not attached to our window
10436 final AttachInfo attachInfo = mAttachInfo;
10437 if (attachInfo != null) {
10438 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10439 }
10440 }
10441
10442 /**
10443 * <p>Cause an invalidate of the specified area to happen on the next animation
10444 * time step, typically the next display frame.</p>
10445 *
10446 * <p>This method can be invoked from outside of the UI thread
10447 * only when this View is attached to a window.</p>
10448 *
10449 * @param left The left coordinate of the rectangle to invalidate.
10450 * @param top The top coordinate of the rectangle to invalidate.
10451 * @param right The right coordinate of the rectangle to invalidate.
10452 * @param bottom The bottom coordinate of the rectangle to invalidate.
10453 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010454 * @see #invalidate(int, int, int, int)
10455 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010456 */
10457 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10458 // We try only with the AttachInfo because there's no point in invalidating
10459 // if we are not attached to our window
10460 final AttachInfo attachInfo = mAttachInfo;
10461 if (attachInfo != null) {
10462 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10463 info.target = this;
10464 info.left = left;
10465 info.top = top;
10466 info.right = right;
10467 info.bottom = bottom;
10468
10469 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10470 }
10471 }
10472
10473 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010474 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10475 * This event is sent at most once every
10476 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10477 */
10478 private void postSendViewScrolledAccessibilityEventCallback() {
10479 if (mSendViewScrolledAccessibilityEvent == null) {
10480 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10481 }
10482 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10483 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10484 postDelayed(mSendViewScrolledAccessibilityEvent,
10485 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10486 }
10487 }
10488
10489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010490 * Called by a parent to request that a child update its values for mScrollX
10491 * and mScrollY if necessary. This will typically be done if the child is
10492 * animating a scroll using a {@link android.widget.Scroller Scroller}
10493 * object.
10494 */
10495 public void computeScroll() {
10496 }
10497
10498 /**
10499 * <p>Indicate whether the horizontal edges are faded when the view is
10500 * scrolled horizontally.</p>
10501 *
10502 * @return true if the horizontal edges should are faded on scroll, false
10503 * otherwise
10504 *
10505 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010506 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010507 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 */
10509 public boolean isHorizontalFadingEdgeEnabled() {
10510 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10511 }
10512
10513 /**
10514 * <p>Define whether the horizontal edges should be faded when this view
10515 * is scrolled horizontally.</p>
10516 *
10517 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10518 * be faded when the view is scrolled
10519 * horizontally
10520 *
10521 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010522 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010523 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 */
10525 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10526 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10527 if (horizontalFadingEdgeEnabled) {
10528 initScrollCache();
10529 }
10530
10531 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10532 }
10533 }
10534
10535 /**
10536 * <p>Indicate whether the vertical edges are faded when the view is
10537 * scrolled horizontally.</p>
10538 *
10539 * @return true if the vertical edges should are faded on scroll, false
10540 * otherwise
10541 *
10542 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010543 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010544 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 */
10546 public boolean isVerticalFadingEdgeEnabled() {
10547 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10548 }
10549
10550 /**
10551 * <p>Define whether the vertical edges should be faded when this view
10552 * is scrolled vertically.</p>
10553 *
10554 * @param verticalFadingEdgeEnabled true if the vertical edges should
10555 * be faded when the view is scrolled
10556 * vertically
10557 *
10558 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010559 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010560 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010561 */
10562 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10563 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10564 if (verticalFadingEdgeEnabled) {
10565 initScrollCache();
10566 }
10567
10568 mViewFlags ^= FADING_EDGE_VERTICAL;
10569 }
10570 }
10571
10572 /**
10573 * Returns the strength, or intensity, of the top faded edge. The strength is
10574 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10575 * returns 0.0 or 1.0 but no value in between.
10576 *
10577 * Subclasses should override this method to provide a smoother fade transition
10578 * when scrolling occurs.
10579 *
10580 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10581 */
10582 protected float getTopFadingEdgeStrength() {
10583 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10584 }
10585
10586 /**
10587 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10588 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10589 * returns 0.0 or 1.0 but no value in between.
10590 *
10591 * Subclasses should override this method to provide a smoother fade transition
10592 * when scrolling occurs.
10593 *
10594 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10595 */
10596 protected float getBottomFadingEdgeStrength() {
10597 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10598 computeVerticalScrollRange() ? 1.0f : 0.0f;
10599 }
10600
10601 /**
10602 * Returns the strength, or intensity, of the left 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 left fade as a float between 0.0f and 1.0f
10610 */
10611 protected float getLeftFadingEdgeStrength() {
10612 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10613 }
10614
10615 /**
10616 * Returns the strength, or intensity, of the right faded edge. The strength is
10617 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10618 * returns 0.0 or 1.0 but no value in between.
10619 *
10620 * Subclasses should override this method to provide a smoother fade transition
10621 * when scrolling occurs.
10622 *
10623 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10624 */
10625 protected float getRightFadingEdgeStrength() {
10626 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10627 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10628 }
10629
10630 /**
10631 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10632 * scrollbar is not drawn by default.</p>
10633 *
10634 * @return true if the horizontal scrollbar should be painted, false
10635 * otherwise
10636 *
10637 * @see #setHorizontalScrollBarEnabled(boolean)
10638 */
10639 public boolean isHorizontalScrollBarEnabled() {
10640 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10641 }
10642
10643 /**
10644 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10645 * scrollbar is not drawn by default.</p>
10646 *
10647 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10648 * be painted
10649 *
10650 * @see #isHorizontalScrollBarEnabled()
10651 */
10652 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10653 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10654 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010655 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010656 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 }
10658 }
10659
10660 /**
10661 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10662 * scrollbar is not drawn by default.</p>
10663 *
10664 * @return true if the vertical scrollbar should be painted, false
10665 * otherwise
10666 *
10667 * @see #setVerticalScrollBarEnabled(boolean)
10668 */
10669 public boolean isVerticalScrollBarEnabled() {
10670 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10671 }
10672
10673 /**
10674 * <p>Define whether the vertical scrollbar should be drawn or not. The
10675 * scrollbar is not drawn by default.</p>
10676 *
10677 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10678 * be painted
10679 *
10680 * @see #isVerticalScrollBarEnabled()
10681 */
10682 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10683 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10684 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010685 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010686 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 }
10688 }
10689
Adam Powell20232d02010-12-08 21:08:53 -080010690 /**
10691 * @hide
10692 */
10693 protected void recomputePadding() {
10694 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 }
Joe Malin32736f02011-01-19 16:14:20 -080010696
Mike Cleronfe81d382009-09-28 14:22:16 -070010697 /**
10698 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010699 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010700 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010701 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010702 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010703 */
10704 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10705 initScrollCache();
10706 final ScrollabilityCache scrollabilityCache = mScrollCache;
10707 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010708 if (fadeScrollbars) {
10709 scrollabilityCache.state = ScrollabilityCache.OFF;
10710 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010711 scrollabilityCache.state = ScrollabilityCache.ON;
10712 }
10713 }
Joe Malin32736f02011-01-19 16:14:20 -080010714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 /**
Joe Malin32736f02011-01-19 16:14:20 -080010716 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010717 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010718 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010719 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010720 *
10721 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010722 */
10723 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010724 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010725 }
Joe Malin32736f02011-01-19 16:14:20 -080010726
Mike Cleron52f0a642009-09-28 18:21:37 -070010727 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010728 *
10729 * Returns the delay before scrollbars fade.
10730 *
10731 * @return the delay before scrollbars fade
10732 *
10733 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10734 */
10735 public int getScrollBarDefaultDelayBeforeFade() {
10736 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10737 mScrollCache.scrollBarDefaultDelayBeforeFade;
10738 }
10739
10740 /**
10741 * Define the delay before scrollbars fade.
10742 *
10743 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10744 *
10745 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10746 */
10747 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10748 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10749 }
10750
10751 /**
10752 *
10753 * Returns the scrollbar fade duration.
10754 *
10755 * @return the scrollbar fade duration
10756 *
10757 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10758 */
10759 public int getScrollBarFadeDuration() {
10760 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10761 mScrollCache.scrollBarFadeDuration;
10762 }
10763
10764 /**
10765 * Define the scrollbar fade duration.
10766 *
10767 * @param scrollBarFadeDuration - the scrollbar fade duration
10768 *
10769 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10770 */
10771 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10772 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10773 }
10774
10775 /**
10776 *
10777 * Returns the scrollbar size.
10778 *
10779 * @return the scrollbar size
10780 *
10781 * @attr ref android.R.styleable#View_scrollbarSize
10782 */
10783 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010784 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010785 mScrollCache.scrollBarSize;
10786 }
10787
10788 /**
10789 * Define the scrollbar size.
10790 *
10791 * @param scrollBarSize - the scrollbar size
10792 *
10793 * @attr ref android.R.styleable#View_scrollbarSize
10794 */
10795 public void setScrollBarSize(int scrollBarSize) {
10796 getScrollCache().scrollBarSize = scrollBarSize;
10797 }
10798
10799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10801 * inset. When inset, they add to the padding of the view. And the scrollbars
10802 * can be drawn inside the padding area or on the edge of the view. For example,
10803 * if a view has a background drawable and you want to draw the scrollbars
10804 * inside the padding specified by the drawable, you can use
10805 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10806 * appear at the edge of the view, ignoring the padding, then you can use
10807 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10808 * @param style the style of the scrollbars. Should be one of
10809 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10810 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10811 * @see #SCROLLBARS_INSIDE_OVERLAY
10812 * @see #SCROLLBARS_INSIDE_INSET
10813 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10814 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010815 *
10816 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 */
10818 public void setScrollBarStyle(int style) {
10819 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10820 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010821 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010822 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 }
10824 }
10825
10826 /**
10827 * <p>Returns the current scrollbar style.</p>
10828 * @return the current scrollbar style
10829 * @see #SCROLLBARS_INSIDE_OVERLAY
10830 * @see #SCROLLBARS_INSIDE_INSET
10831 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10832 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010833 *
10834 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010836 @ViewDebug.ExportedProperty(mapping = {
10837 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10838 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10839 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10840 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10841 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010842 public int getScrollBarStyle() {
10843 return mViewFlags & SCROLLBARS_STYLE_MASK;
10844 }
10845
10846 /**
10847 * <p>Compute the horizontal range that the horizontal scrollbar
10848 * represents.</p>
10849 *
10850 * <p>The range is expressed in arbitrary units that must be the same as the
10851 * units used by {@link #computeHorizontalScrollExtent()} and
10852 * {@link #computeHorizontalScrollOffset()}.</p>
10853 *
10854 * <p>The default range is the drawing width of this view.</p>
10855 *
10856 * @return the total horizontal range represented by the horizontal
10857 * scrollbar
10858 *
10859 * @see #computeHorizontalScrollExtent()
10860 * @see #computeHorizontalScrollOffset()
10861 * @see android.widget.ScrollBarDrawable
10862 */
10863 protected int computeHorizontalScrollRange() {
10864 return getWidth();
10865 }
10866
10867 /**
10868 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10869 * within the horizontal range. This value is used to compute the position
10870 * of the thumb within the scrollbar's track.</p>
10871 *
10872 * <p>The range is expressed in arbitrary units that must be the same as the
10873 * units used by {@link #computeHorizontalScrollRange()} and
10874 * {@link #computeHorizontalScrollExtent()}.</p>
10875 *
10876 * <p>The default offset is the scroll offset of this view.</p>
10877 *
10878 * @return the horizontal offset of the scrollbar's thumb
10879 *
10880 * @see #computeHorizontalScrollRange()
10881 * @see #computeHorizontalScrollExtent()
10882 * @see android.widget.ScrollBarDrawable
10883 */
10884 protected int computeHorizontalScrollOffset() {
10885 return mScrollX;
10886 }
10887
10888 /**
10889 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10890 * within the horizontal range. This value is used to compute the length
10891 * of the thumb within the scrollbar's track.</p>
10892 *
10893 * <p>The range is expressed in arbitrary units that must be the same as the
10894 * units used by {@link #computeHorizontalScrollRange()} and
10895 * {@link #computeHorizontalScrollOffset()}.</p>
10896 *
10897 * <p>The default extent is the drawing width of this view.</p>
10898 *
10899 * @return the horizontal extent of the scrollbar's thumb
10900 *
10901 * @see #computeHorizontalScrollRange()
10902 * @see #computeHorizontalScrollOffset()
10903 * @see android.widget.ScrollBarDrawable
10904 */
10905 protected int computeHorizontalScrollExtent() {
10906 return getWidth();
10907 }
10908
10909 /**
10910 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10911 *
10912 * <p>The range is expressed in arbitrary units that must be the same as the
10913 * units used by {@link #computeVerticalScrollExtent()} and
10914 * {@link #computeVerticalScrollOffset()}.</p>
10915 *
10916 * @return the total vertical range represented by the vertical scrollbar
10917 *
10918 * <p>The default range is the drawing height of this view.</p>
10919 *
10920 * @see #computeVerticalScrollExtent()
10921 * @see #computeVerticalScrollOffset()
10922 * @see android.widget.ScrollBarDrawable
10923 */
10924 protected int computeVerticalScrollRange() {
10925 return getHeight();
10926 }
10927
10928 /**
10929 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10930 * within the horizontal range. This value is used to compute the position
10931 * of the thumb within the scrollbar's track.</p>
10932 *
10933 * <p>The range is expressed in arbitrary units that must be the same as the
10934 * units used by {@link #computeVerticalScrollRange()} and
10935 * {@link #computeVerticalScrollExtent()}.</p>
10936 *
10937 * <p>The default offset is the scroll offset of this view.</p>
10938 *
10939 * @return the vertical offset of the scrollbar's thumb
10940 *
10941 * @see #computeVerticalScrollRange()
10942 * @see #computeVerticalScrollExtent()
10943 * @see android.widget.ScrollBarDrawable
10944 */
10945 protected int computeVerticalScrollOffset() {
10946 return mScrollY;
10947 }
10948
10949 /**
10950 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10951 * within the vertical range. This value is used to compute the length
10952 * of the thumb within the scrollbar's track.</p>
10953 *
10954 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010955 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 * {@link #computeVerticalScrollOffset()}.</p>
10957 *
10958 * <p>The default extent is the drawing height of this view.</p>
10959 *
10960 * @return the vertical extent of the scrollbar's thumb
10961 *
10962 * @see #computeVerticalScrollRange()
10963 * @see #computeVerticalScrollOffset()
10964 * @see android.widget.ScrollBarDrawable
10965 */
10966 protected int computeVerticalScrollExtent() {
10967 return getHeight();
10968 }
10969
10970 /**
Adam Powell69159442011-06-13 17:53:06 -070010971 * Check if this view can be scrolled horizontally in a certain direction.
10972 *
10973 * @param direction Negative to check scrolling left, positive to check scrolling right.
10974 * @return true if this view can be scrolled in the specified direction, false otherwise.
10975 */
10976 public boolean canScrollHorizontally(int direction) {
10977 final int offset = computeHorizontalScrollOffset();
10978 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10979 if (range == 0) return false;
10980 if (direction < 0) {
10981 return offset > 0;
10982 } else {
10983 return offset < range - 1;
10984 }
10985 }
10986
10987 /**
10988 * Check if this view can be scrolled vertically in a certain direction.
10989 *
10990 * @param direction Negative to check scrolling up, positive to check scrolling down.
10991 * @return true if this view can be scrolled in the specified direction, false otherwise.
10992 */
10993 public boolean canScrollVertically(int direction) {
10994 final int offset = computeVerticalScrollOffset();
10995 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10996 if (range == 0) return false;
10997 if (direction < 0) {
10998 return offset > 0;
10999 } else {
11000 return offset < range - 1;
11001 }
11002 }
11003
11004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11006 * scrollbars are painted only if they have been awakened first.</p>
11007 *
11008 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011009 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011010 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011012 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 // scrollbars are drawn only when the animation is running
11014 final ScrollabilityCache cache = mScrollCache;
11015 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011016
Mike Cleronf116bf82009-09-27 19:14:12 -070011017 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011018
Mike Cleronf116bf82009-09-27 19:14:12 -070011019 if (state == ScrollabilityCache.OFF) {
11020 return;
11021 }
Joe Malin32736f02011-01-19 16:14:20 -080011022
Mike Cleronf116bf82009-09-27 19:14:12 -070011023 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011024
Mike Cleronf116bf82009-09-27 19:14:12 -070011025 if (state == ScrollabilityCache.FADING) {
11026 // We're fading -- get our fade interpolation
11027 if (cache.interpolatorValues == null) {
11028 cache.interpolatorValues = new float[1];
11029 }
Joe Malin32736f02011-01-19 16:14:20 -080011030
Mike Cleronf116bf82009-09-27 19:14:12 -070011031 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011032
Mike Cleronf116bf82009-09-27 19:14:12 -070011033 // Stops the animation if we're done
11034 if (cache.scrollBarInterpolator.timeToValues(values) ==
11035 Interpolator.Result.FREEZE_END) {
11036 cache.state = ScrollabilityCache.OFF;
11037 } else {
11038 cache.scrollBar.setAlpha(Math.round(values[0]));
11039 }
Joe Malin32736f02011-01-19 16:14:20 -080011040
11041 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011042 // drawing. We only want this when we're fading so that
11043 // we prevent excessive redraws
11044 invalidate = true;
11045 } else {
11046 // We're just on -- but we may have been fading before so
11047 // reset alpha
11048 cache.scrollBar.setAlpha(255);
11049 }
11050
Joe Malin32736f02011-01-19 16:14:20 -080011051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 final int viewFlags = mViewFlags;
11053
11054 final boolean drawHorizontalScrollBar =
11055 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11056 final boolean drawVerticalScrollBar =
11057 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11058 && !isVerticalScrollBarHidden();
11059
11060 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11061 final int width = mRight - mLeft;
11062 final int height = mBottom - mTop;
11063
11064 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011065
Mike Reede8853fc2009-09-04 14:01:48 -040011066 final int scrollX = mScrollX;
11067 final int scrollY = mScrollY;
11068 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11069
Mike Cleronf116bf82009-09-27 19:14:12 -070011070 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011072 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011073 int size = scrollBar.getSize(false);
11074 if (size <= 0) {
11075 size = cache.scrollBarSize;
11076 }
11077
Mike Cleronf116bf82009-09-27 19:14:12 -070011078 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011079 computeHorizontalScrollOffset(),
11080 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011081 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011082 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011083 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011084 left = scrollX + (mPaddingLeft & inside);
11085 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11086 bottom = top + size;
11087 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11088 if (invalidate) {
11089 invalidate(left, top, right, bottom);
11090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011091 }
11092
11093 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011094 int size = scrollBar.getSize(true);
11095 if (size <= 0) {
11096 size = cache.scrollBarSize;
11097 }
11098
Mike Reede8853fc2009-09-04 14:01:48 -040011099 scrollBar.setParameters(computeVerticalScrollRange(),
11100 computeVerticalScrollOffset(),
11101 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011102 switch (mVerticalScrollbarPosition) {
11103 default:
11104 case SCROLLBAR_POSITION_DEFAULT:
11105 case SCROLLBAR_POSITION_RIGHT:
11106 left = scrollX + width - size - (mUserPaddingRight & inside);
11107 break;
11108 case SCROLLBAR_POSITION_LEFT:
11109 left = scrollX + (mUserPaddingLeft & inside);
11110 break;
11111 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011112 top = scrollY + (mPaddingTop & inside);
11113 right = left + size;
11114 bottom = scrollY + height - (mUserPaddingBottom & inside);
11115 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11116 if (invalidate) {
11117 invalidate(left, top, right, bottom);
11118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 }
11120 }
11121 }
11122 }
Romain Guy8506ab42009-06-11 17:35:47 -070011123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011125 * 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 -080011126 * FastScroller is visible.
11127 * @return whether to temporarily hide the vertical scrollbar
11128 * @hide
11129 */
11130 protected boolean isVerticalScrollBarHidden() {
11131 return false;
11132 }
11133
11134 /**
11135 * <p>Draw the horizontal scrollbar if
11136 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 * @param canvas the canvas on which to draw the scrollbar
11139 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 *
11141 * @see #isHorizontalScrollBarEnabled()
11142 * @see #computeHorizontalScrollRange()
11143 * @see #computeHorizontalScrollExtent()
11144 * @see #computeHorizontalScrollOffset()
11145 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011146 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011147 */
Romain Guy8fb95422010-08-17 18:38:51 -070011148 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11149 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011150 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011151 scrollBar.draw(canvas);
11152 }
Mike Reede8853fc2009-09-04 14:01:48 -040011153
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11156 * returns true.</p>
11157 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 * @param canvas the canvas on which to draw the scrollbar
11159 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 *
11161 * @see #isVerticalScrollBarEnabled()
11162 * @see #computeVerticalScrollRange()
11163 * @see #computeVerticalScrollExtent()
11164 * @see #computeVerticalScrollOffset()
11165 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011166 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011167 */
Romain Guy8fb95422010-08-17 18:38:51 -070011168 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11169 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011170 scrollBar.setBounds(l, t, r, b);
11171 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 }
11173
11174 /**
11175 * Implement this to do your drawing.
11176 *
11177 * @param canvas the canvas on which the background will be drawn
11178 */
11179 protected void onDraw(Canvas canvas) {
11180 }
11181
11182 /*
11183 * Caller is responsible for calling requestLayout if necessary.
11184 * (This allows addViewInLayout to not request a new layout.)
11185 */
11186 void assignParent(ViewParent parent) {
11187 if (mParent == null) {
11188 mParent = parent;
11189 } else if (parent == null) {
11190 mParent = null;
11191 } else {
11192 throw new RuntimeException("view " + this + " being added, but"
11193 + " it already has a parent");
11194 }
11195 }
11196
11197 /**
11198 * This is called when the view is attached to a window. At this point it
11199 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011200 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11201 * however it may be called any time before the first onDraw -- including
11202 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 *
11204 * @see #onDetachedFromWindow()
11205 */
11206 protected void onAttachedToWindow() {
11207 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11208 mParent.requestTransparentRegion(this);
11209 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011210
Adam Powell8568c3a2010-04-19 14:26:11 -070011211 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11212 initialAwakenScrollBars();
11213 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11214 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011215
Chet Haasea9b61ac2010-12-20 07:40:25 -080011216 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011217
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011218 // Order is important here: LayoutDirection MUST be resolved before Padding
11219 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011220 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011221 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011222 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011223 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011224
Svetoslav Ganov42138042012-03-20 11:51:39 -070011225 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011226 if (isFocused()) {
11227 InputMethodManager imm = InputMethodManager.peekInstance();
11228 imm.focusIn(this);
11229 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011230
11231 if (mAttachInfo != null && mDisplayList != null) {
11232 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11233 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011234 }
Cibu Johny86666632010-02-22 13:01:02 -080011235
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011236 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011237 * @see #onScreenStateChanged(int)
11238 */
11239 void dispatchScreenStateChanged(int screenState) {
11240 onScreenStateChanged(screenState);
11241 }
11242
11243 /**
11244 * This method is called whenever the state of the screen this view is
11245 * attached to changes. A state change will usually occurs when the screen
11246 * turns on or off (whether it happens automatically or the user does it
11247 * manually.)
11248 *
11249 * @param screenState The new state of the screen. Can be either
11250 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11251 */
11252 public void onScreenStateChanged(int screenState) {
11253 }
11254
11255 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011256 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11257 */
11258 private boolean hasRtlSupport() {
11259 return mContext.getApplicationInfo().hasRtlSupport();
11260 }
11261
11262 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011263 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11264 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011265 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011266 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011267 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011268 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011269 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011270
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011271 if (hasRtlSupport()) {
11272 // Set resolved depending on layout direction
11273 switch (getLayoutDirection()) {
11274 case LAYOUT_DIRECTION_INHERIT:
11275 // If this is root view, no need to look at parent's layout dir.
11276 if (canResolveLayoutDirection()) {
11277 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011278
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011279 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11280 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11281 }
11282 } else {
11283 // Nothing to do, LTR by default
11284 }
11285 break;
11286 case LAYOUT_DIRECTION_RTL:
11287 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11288 break;
11289 case LAYOUT_DIRECTION_LOCALE:
11290 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011291 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11292 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011293 break;
11294 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011295 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011296 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011297 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011298
11299 // Set to resolved
11300 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011301 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011302 // Resolve padding
11303 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011304 }
11305
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011306 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011307 * Called when layout direction has been resolved.
11308 *
11309 * The default implementation does nothing.
11310 */
11311 public void onResolvedLayoutDirectionChanged() {
11312 }
11313
11314 /**
11315 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011316 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011317 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011318 // If the user specified the absolute padding (either with android:padding or
11319 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11320 // use the default padding or the padding from the background drawable
11321 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011322 int resolvedLayoutDirection = getResolvedLayoutDirection();
11323 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011324 case LAYOUT_DIRECTION_RTL:
11325 // Start user padding override Right user padding. Otherwise, if Right user
11326 // padding is not defined, use the default Right padding. If Right user padding
11327 // is defined, just use it.
11328 if (mUserPaddingStart >= 0) {
11329 mUserPaddingRight = mUserPaddingStart;
11330 } else if (mUserPaddingRight < 0) {
11331 mUserPaddingRight = mPaddingRight;
11332 }
11333 if (mUserPaddingEnd >= 0) {
11334 mUserPaddingLeft = mUserPaddingEnd;
11335 } else if (mUserPaddingLeft < 0) {
11336 mUserPaddingLeft = mPaddingLeft;
11337 }
11338 break;
11339 case LAYOUT_DIRECTION_LTR:
11340 default:
11341 // Start user padding override Left user padding. Otherwise, if Left user
11342 // padding is not defined, use the default left padding. If Left user padding
11343 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011344 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011345 mUserPaddingLeft = mUserPaddingStart;
11346 } else if (mUserPaddingLeft < 0) {
11347 mUserPaddingLeft = mPaddingLeft;
11348 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011349 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011350 mUserPaddingRight = mUserPaddingEnd;
11351 } else if (mUserPaddingRight < 0) {
11352 mUserPaddingRight = mPaddingRight;
11353 }
11354 }
11355
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011356 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11357
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011358 if(isPaddingRelative()) {
11359 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11360 } else {
11361 recomputePadding();
11362 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011363 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011364 }
11365
11366 /**
11367 * Resolve padding depending on the layout direction. Subclasses that care about
11368 * padding resolution should override this method. The default implementation does
11369 * nothing.
11370 *
11371 * @param layoutDirection the direction of the layout
11372 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011373 * @see {@link #LAYOUT_DIRECTION_LTR}
11374 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011375 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011376 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011377 }
11378
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011379 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011380 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011381 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011382 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011383 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011384 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011385 switch (getLayoutDirection()) {
11386 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011387 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011388 default:
11389 return true;
11390 }
11391 }
11392
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011393 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011394 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11395 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011396 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011397 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011398 // Reset the current resolved bits
11399 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011400 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011401 // Reset also the text direction
11402 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011403 }
11404
11405 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011406 * Called during reset of resolved layout direction.
11407 *
11408 * Subclasses need to override this method to clear cached information that depends on the
11409 * resolved layout direction, or to inform child views that inherit their layout direction.
11410 *
11411 * The default implementation does nothing.
11412 */
11413 public void onResolvedLayoutDirectionReset() {
11414 }
11415
11416 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011417 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011418 *
11419 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011420 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011421 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011422 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011423 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424 }
11425
11426 /**
11427 * This is called when the view is detached from a window. At this point it
11428 * no longer has a surface for drawing.
11429 *
11430 * @see #onAttachedToWindow()
11431 */
11432 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011433 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011434
Romain Guya440b002010-02-24 15:57:54 -080011435 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011436 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011437 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011438 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011440 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011441
Romain Guya998dff2012-03-23 18:58:36 -070011442 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011443
11444 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011445 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011446 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011447 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011448 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011449 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011450 // Should never happen
11451 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011452 }
11453
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011454 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011455
11456 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011457 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011458 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 }
11460
11461 /**
11462 * @return The number of times this view has been attached to a window
11463 */
11464 protected int getWindowAttachCount() {
11465 return mWindowAttachCount;
11466 }
11467
11468 /**
11469 * Retrieve a unique token identifying the window this view is attached to.
11470 * @return Return the window's token for use in
11471 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11472 */
11473 public IBinder getWindowToken() {
11474 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11475 }
11476
11477 /**
11478 * Retrieve a unique token identifying the top-level "real" window of
11479 * the window that this view is attached to. That is, this is like
11480 * {@link #getWindowToken}, except if the window this view in is a panel
11481 * window (attached to another containing window), then the token of
11482 * the containing window is returned instead.
11483 *
11484 * @return Returns the associated window token, either
11485 * {@link #getWindowToken()} or the containing window's token.
11486 */
11487 public IBinder getApplicationWindowToken() {
11488 AttachInfo ai = mAttachInfo;
11489 if (ai != null) {
11490 IBinder appWindowToken = ai.mPanelParentWindowToken;
11491 if (appWindowToken == null) {
11492 appWindowToken = ai.mWindowToken;
11493 }
11494 return appWindowToken;
11495 }
11496 return null;
11497 }
11498
11499 /**
11500 * Retrieve private session object this view hierarchy is using to
11501 * communicate with the window manager.
11502 * @return the session object to communicate with the window manager
11503 */
11504 /*package*/ IWindowSession getWindowSession() {
11505 return mAttachInfo != null ? mAttachInfo.mSession : null;
11506 }
11507
11508 /**
11509 * @param info the {@link android.view.View.AttachInfo} to associated with
11510 * this view
11511 */
11512 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11513 //System.out.println("Attached! " + this);
11514 mAttachInfo = info;
11515 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011516 // We will need to evaluate the drawable state at least once.
11517 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 if (mFloatingTreeObserver != null) {
11519 info.mTreeObserver.merge(mFloatingTreeObserver);
11520 mFloatingTreeObserver = null;
11521 }
11522 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11523 mAttachInfo.mScrollContainers.add(this);
11524 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11525 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011526 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011527 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011528
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011529 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011530 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011531 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011532 if (listeners != null && listeners.size() > 0) {
11533 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11534 // perform the dispatching. The iterator is a safe guard against listeners that
11535 // could mutate the list by calling the various add/remove methods. This prevents
11536 // the array from being modified while we iterate it.
11537 for (OnAttachStateChangeListener listener : listeners) {
11538 listener.onViewAttachedToWindow(this);
11539 }
11540 }
11541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011542 int vis = info.mWindowVisibility;
11543 if (vis != GONE) {
11544 onWindowVisibilityChanged(vis);
11545 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011546 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11547 // If nobody has evaluated the drawable state yet, then do it now.
11548 refreshDrawableState();
11549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 }
11551
11552 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011553 AttachInfo info = mAttachInfo;
11554 if (info != null) {
11555 int vis = info.mWindowVisibility;
11556 if (vis != GONE) {
11557 onWindowVisibilityChanged(GONE);
11558 }
11559 }
11560
11561 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011562
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011563 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011564 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011565 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011566 if (listeners != null && listeners.size() > 0) {
11567 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11568 // perform the dispatching. The iterator is a safe guard against listeners that
11569 // could mutate the list by calling the various add/remove methods. This prevents
11570 // the array from being modified while we iterate it.
11571 for (OnAttachStateChangeListener listener : listeners) {
11572 listener.onViewDetachedFromWindow(this);
11573 }
11574 }
11575
Romain Guy01d5edc2011-01-28 11:28:53 -080011576 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011577 mAttachInfo.mScrollContainers.remove(this);
11578 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11579 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 mAttachInfo = null;
11582 }
11583
11584 /**
11585 * Store this view hierarchy's frozen state into the given container.
11586 *
11587 * @param container The SparseArray in which to save the view's state.
11588 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011589 * @see #restoreHierarchyState(android.util.SparseArray)
11590 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11591 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 */
11593 public void saveHierarchyState(SparseArray<Parcelable> container) {
11594 dispatchSaveInstanceState(container);
11595 }
11596
11597 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011598 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11599 * this view and its children. May be overridden to modify how freezing happens to a
11600 * 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 -080011601 *
11602 * @param container The SparseArray in which to save the view's state.
11603 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011604 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11605 * @see #saveHierarchyState(android.util.SparseArray)
11606 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011607 */
11608 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11609 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11610 mPrivateFlags &= ~SAVE_STATE_CALLED;
11611 Parcelable state = onSaveInstanceState();
11612 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11613 throw new IllegalStateException(
11614 "Derived class did not call super.onSaveInstanceState()");
11615 }
11616 if (state != null) {
11617 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11618 // + ": " + state);
11619 container.put(mID, state);
11620 }
11621 }
11622 }
11623
11624 /**
11625 * Hook allowing a view to generate a representation of its internal state
11626 * that can later be used to create a new instance with that same state.
11627 * This state should only contain information that is not persistent or can
11628 * not be reconstructed later. For example, you will never store your
11629 * current position on screen because that will be computed again when a
11630 * new instance of the view is placed in its view hierarchy.
11631 * <p>
11632 * Some examples of things you may store here: the current cursor position
11633 * in a text view (but usually not the text itself since that is stored in a
11634 * content provider or other persistent storage), the currently selected
11635 * item in a list view.
11636 *
11637 * @return Returns a Parcelable object containing the view's current dynamic
11638 * state, or null if there is nothing interesting to save. The
11639 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011640 * @see #onRestoreInstanceState(android.os.Parcelable)
11641 * @see #saveHierarchyState(android.util.SparseArray)
11642 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011643 * @see #setSaveEnabled(boolean)
11644 */
11645 protected Parcelable onSaveInstanceState() {
11646 mPrivateFlags |= SAVE_STATE_CALLED;
11647 return BaseSavedState.EMPTY_STATE;
11648 }
11649
11650 /**
11651 * Restore this view hierarchy's frozen state from the given container.
11652 *
11653 * @param container The SparseArray which holds previously frozen states.
11654 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011655 * @see #saveHierarchyState(android.util.SparseArray)
11656 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11657 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 */
11659 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11660 dispatchRestoreInstanceState(container);
11661 }
11662
11663 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011664 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11665 * state for this view and its children. May be overridden to modify how restoring
11666 * happens to a view's children; for example, some views may want to not store state
11667 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 *
11669 * @param container The SparseArray which holds previously saved state.
11670 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011671 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11672 * @see #restoreHierarchyState(android.util.SparseArray)
11673 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 */
11675 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11676 if (mID != NO_ID) {
11677 Parcelable state = container.get(mID);
11678 if (state != null) {
11679 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11680 // + ": " + state);
11681 mPrivateFlags &= ~SAVE_STATE_CALLED;
11682 onRestoreInstanceState(state);
11683 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11684 throw new IllegalStateException(
11685 "Derived class did not call super.onRestoreInstanceState()");
11686 }
11687 }
11688 }
11689 }
11690
11691 /**
11692 * Hook allowing a view to re-apply a representation of its internal state that had previously
11693 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11694 * null state.
11695 *
11696 * @param state The frozen state that had previously been returned by
11697 * {@link #onSaveInstanceState}.
11698 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011699 * @see #onSaveInstanceState()
11700 * @see #restoreHierarchyState(android.util.SparseArray)
11701 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011702 */
11703 protected void onRestoreInstanceState(Parcelable state) {
11704 mPrivateFlags |= SAVE_STATE_CALLED;
11705 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011706 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11707 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011708 + "when two views of different type have the same id in the same hierarchy. "
11709 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011710 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011711 }
11712 }
11713
11714 /**
11715 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11716 *
11717 * @return the drawing start time in milliseconds
11718 */
11719 public long getDrawingTime() {
11720 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11721 }
11722
11723 /**
11724 * <p>Enables or disables the duplication of the parent's state into this view. When
11725 * duplication is enabled, this view gets its drawable state from its parent rather
11726 * than from its own internal properties.</p>
11727 *
11728 * <p>Note: in the current implementation, setting this property to true after the
11729 * view was added to a ViewGroup might have no effect at all. This property should
11730 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11731 *
11732 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11733 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011734 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011735 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11736 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 *
11738 * @param enabled True to enable duplication of the parent's drawable state, false
11739 * to disable it.
11740 *
11741 * @see #getDrawableState()
11742 * @see #isDuplicateParentStateEnabled()
11743 */
11744 public void setDuplicateParentStateEnabled(boolean enabled) {
11745 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11746 }
11747
11748 /**
11749 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11750 *
11751 * @return True if this view's drawable state is duplicated from the parent,
11752 * false otherwise
11753 *
11754 * @see #getDrawableState()
11755 * @see #setDuplicateParentStateEnabled(boolean)
11756 */
11757 public boolean isDuplicateParentStateEnabled() {
11758 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11759 }
11760
11761 /**
Romain Guy171c5922011-01-06 10:04:23 -080011762 * <p>Specifies the type of layer backing this view. The layer can be
11763 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11764 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011765 *
Romain Guy171c5922011-01-06 10:04:23 -080011766 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11767 * instance that controls how the layer is composed on screen. The following
11768 * properties of the paint are taken into account when composing the layer:</p>
11769 * <ul>
11770 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11771 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11772 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11773 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011774 *
Romain Guy171c5922011-01-06 10:04:23 -080011775 * <p>If this view has an alpha value set to < 1.0 by calling
11776 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11777 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11778 * equivalent to setting a hardware layer on this view and providing a paint with
11779 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011780 *
Romain Guy171c5922011-01-06 10:04:23 -080011781 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11782 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11783 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011784 *
Romain Guy171c5922011-01-06 10:04:23 -080011785 * @param layerType The ype of layer to use with this view, must be one of
11786 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11787 * {@link #LAYER_TYPE_HARDWARE}
11788 * @param paint The paint used to compose the layer. This argument is optional
11789 * and can be null. It is ignored when the layer type is
11790 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011791 *
11792 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011793 * @see #LAYER_TYPE_NONE
11794 * @see #LAYER_TYPE_SOFTWARE
11795 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011796 * @see #setAlpha(float)
11797 *
Romain Guy171c5922011-01-06 10:04:23 -080011798 * @attr ref android.R.styleable#View_layerType
11799 */
11800 public void setLayerType(int layerType, Paint paint) {
11801 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011802 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011803 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11804 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011805
Romain Guyd6cd5722011-01-17 14:42:41 -080011806 if (layerType == mLayerType) {
11807 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11808 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011809 invalidateParentCaches();
11810 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011811 }
11812 return;
11813 }
Romain Guy171c5922011-01-06 10:04:23 -080011814
11815 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011816 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011817 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011818 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011819 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011820 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011821 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011822 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011823 default:
11824 break;
Romain Guy171c5922011-01-06 10:04:23 -080011825 }
11826
11827 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011828 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11829 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11830 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011831
Romain Guy0fd89bf2011-01-26 15:41:30 -080011832 invalidateParentCaches();
11833 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011834 }
11835
11836 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011837 * Indicates whether this view has a static layer. A view with layer type
11838 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11839 * dynamic.
11840 */
11841 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011842 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011843 }
11844
11845 /**
Romain Guy171c5922011-01-06 10:04:23 -080011846 * Indicates what type of layer is currently associated with this view. By default
11847 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11848 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11849 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011850 *
Romain Guy171c5922011-01-06 10:04:23 -080011851 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11852 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011853 *
11854 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011855 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011856 * @see #LAYER_TYPE_NONE
11857 * @see #LAYER_TYPE_SOFTWARE
11858 * @see #LAYER_TYPE_HARDWARE
11859 */
11860 public int getLayerType() {
11861 return mLayerType;
11862 }
Joe Malin32736f02011-01-19 16:14:20 -080011863
Romain Guy6c319ca2011-01-11 14:29:25 -080011864 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011865 * Forces this view's layer to be created and this view to be rendered
11866 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11867 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011868 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011869 * This method can for instance be used to render a view into its layer before
11870 * starting an animation. If this view is complex, rendering into the layer
11871 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011872 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011873 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011874 *
11875 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011876 */
11877 public void buildLayer() {
11878 if (mLayerType == LAYER_TYPE_NONE) return;
11879
11880 if (mAttachInfo == null) {
11881 throw new IllegalStateException("This view must be attached to a window first");
11882 }
11883
11884 switch (mLayerType) {
11885 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011886 if (mAttachInfo.mHardwareRenderer != null &&
11887 mAttachInfo.mHardwareRenderer.isEnabled() &&
11888 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011889 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011890 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011891 break;
11892 case LAYER_TYPE_SOFTWARE:
11893 buildDrawingCache(true);
11894 break;
11895 }
11896 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011897
Romain Guy9c4b79a2011-11-10 19:23:58 -080011898 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11899 void flushLayer() {
11900 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11901 mHardwareLayer.flush();
11902 }
11903 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011904
11905 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011906 * <p>Returns a hardware layer that can be used to draw this view again
11907 * without executing its draw method.</p>
11908 *
11909 * @return A HardwareLayer ready to render, or null if an error occurred.
11910 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011911 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011912 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11913 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011914 return null;
11915 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011916
Romain Guy9c4b79a2011-11-10 19:23:58 -080011917 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011918
11919 final int width = mRight - mLeft;
11920 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011921
Romain Guy6c319ca2011-01-11 14:29:25 -080011922 if (width == 0 || height == 0) {
11923 return null;
11924 }
11925
11926 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11927 if (mHardwareLayer == null) {
11928 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11929 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011930 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011931 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11932 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011933 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011934 }
11935
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011936 // The layer is not valid if the underlying GPU resources cannot be allocated
11937 if (!mHardwareLayer.isValid()) {
11938 return null;
11939 }
11940
Chet Haasea1cff502012-02-21 13:43:44 -080011941 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011942 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011943 }
11944
11945 return mHardwareLayer;
11946 }
Romain Guy171c5922011-01-06 10:04:23 -080011947
Romain Guy589b0bb2011-10-10 13:57:47 -070011948 /**
11949 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011950 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011951 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011952 *
11953 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011954 * @see #LAYER_TYPE_HARDWARE
11955 */
Romain Guya998dff2012-03-23 18:58:36 -070011956 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011957 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011958 AttachInfo info = mAttachInfo;
11959 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011960 info.mHardwareRenderer.isEnabled() &&
11961 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011962 mHardwareLayer.destroy();
11963 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011964
Romain Guy9c4b79a2011-11-10 19:23:58 -080011965 invalidate(true);
11966 invalidateParentCaches();
11967 }
Romain Guy65b345f2011-07-27 18:51:50 -070011968 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011969 }
Romain Guy65b345f2011-07-27 18:51:50 -070011970 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011971 }
11972
Romain Guy171c5922011-01-06 10:04:23 -080011973 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011974 * Destroys all hardware rendering resources. This method is invoked
11975 * when the system needs to reclaim resources. Upon execution of this
11976 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011977 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011978 * Note: you <strong>must</strong> call
11979 * <code>super.destroyHardwareResources()</code> when overriding
11980 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011981 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011982 * @hide
11983 */
11984 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011985 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011986 }
11987
11988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11990 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11991 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11992 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11993 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11994 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011995 *
Romain Guy171c5922011-01-06 10:04:23 -080011996 * <p>Enabling the drawing cache is similar to
11997 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011998 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11999 * drawing cache has no effect on rendering because the system uses a different mechanism
12000 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12001 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12002 * for information on how to enable software and hardware layers.</p>
12003 *
12004 * <p>This API can be used to manually generate
12005 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12006 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012007 *
12008 * @param enabled true to enable the drawing cache, false otherwise
12009 *
12010 * @see #isDrawingCacheEnabled()
12011 * @see #getDrawingCache()
12012 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012013 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012014 */
12015 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012016 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12018 }
12019
12020 /**
12021 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12022 *
12023 * @return true if the drawing cache is enabled
12024 *
12025 * @see #setDrawingCacheEnabled(boolean)
12026 * @see #getDrawingCache()
12027 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012028 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012029 public boolean isDrawingCacheEnabled() {
12030 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12031 }
12032
12033 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012034 * Debugging utility which recursively outputs the dirty state of a view and its
12035 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012036 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012037 * @hide
12038 */
Romain Guy676b1732011-02-14 14:45:33 -080012039 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012040 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12041 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12042 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12043 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12044 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12045 if (clear) {
12046 mPrivateFlags &= clearMask;
12047 }
12048 if (this instanceof ViewGroup) {
12049 ViewGroup parent = (ViewGroup) this;
12050 final int count = parent.getChildCount();
12051 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012052 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012053 child.outputDirtyFlags(indent + " ", clear, clearMask);
12054 }
12055 }
12056 }
12057
12058 /**
12059 * This method is used by ViewGroup to cause its children to restore or recreate their
12060 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12061 * to recreate its own display list, which would happen if it went through the normal
12062 * draw/dispatchDraw mechanisms.
12063 *
12064 * @hide
12065 */
12066 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012067
12068 /**
12069 * A view that is not attached or hardware accelerated cannot create a display list.
12070 * This method checks these conditions and returns the appropriate result.
12071 *
12072 * @return true if view has the ability to create a display list, false otherwise.
12073 *
12074 * @hide
12075 */
12076 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012077 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012078 }
Joe Malin32736f02011-01-19 16:14:20 -080012079
Chet Haasedaf98e92011-01-10 14:10:36 -080012080 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012081 * @return The HardwareRenderer associated with that view or null if hardware rendering
12082 * is not supported or this this has not been attached to a window.
12083 *
12084 * @hide
12085 */
12086 public HardwareRenderer getHardwareRenderer() {
12087 if (mAttachInfo != null) {
12088 return mAttachInfo.mHardwareRenderer;
12089 }
12090 return null;
12091 }
12092
12093 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012094 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12095 * Otherwise, the same display list will be returned (after having been rendered into
12096 * along the way, depending on the invalidation state of the view).
12097 *
12098 * @param displayList The previous version of this displayList, could be null.
12099 * @param isLayer Whether the requester of the display list is a layer. If so,
12100 * the view will avoid creating a layer inside the resulting display list.
12101 * @return A new or reused DisplayList object.
12102 */
12103 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12104 if (!canHaveDisplayList()) {
12105 return null;
12106 }
12107
12108 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12109 displayList == null || !displayList.isValid() ||
12110 (!isLayer && mRecreateDisplayList))) {
12111 // Don't need to recreate the display list, just need to tell our
12112 // children to restore/recreate theirs
12113 if (displayList != null && displayList.isValid() &&
12114 !isLayer && !mRecreateDisplayList) {
12115 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12116 mPrivateFlags &= ~DIRTY_MASK;
12117 dispatchGetDisplayList();
12118
12119 return displayList;
12120 }
12121
12122 if (!isLayer) {
12123 // If we got here, we're recreating it. Mark it as such to ensure that
12124 // we copy in child display lists into ours in drawChild()
12125 mRecreateDisplayList = true;
12126 }
12127 if (displayList == null) {
12128 final String name = getClass().getSimpleName();
12129 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12130 // If we're creating a new display list, make sure our parent gets invalidated
12131 // since they will need to recreate their display list to account for this
12132 // new child display list.
12133 invalidateParentCaches();
12134 }
12135
12136 boolean caching = false;
12137 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012138 int width = mRight - mLeft;
12139 int height = mBottom - mTop;
12140
12141 try {
12142 canvas.setViewport(width, height);
12143 // The dirty rect should always be null for a display list
12144 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012145 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012146 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012147 if (layerType == LAYER_TYPE_HARDWARE) {
12148 final HardwareLayer layer = getHardwareLayer();
12149 if (layer != null && layer.isValid()) {
12150 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12151 } else {
12152 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12153 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12154 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12155 }
12156 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012157 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012158 buildDrawingCache(true);
12159 Bitmap cache = getDrawingCache(true);
12160 if (cache != null) {
12161 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12162 caching = true;
12163 }
Chet Haasea1cff502012-02-21 13:43:44 -080012164 }
Chet Haasea1cff502012-02-21 13:43:44 -080012165 } else {
12166
12167 computeScroll();
12168
Chet Haasea1cff502012-02-21 13:43:44 -080012169 canvas.translate(-mScrollX, -mScrollY);
12170 if (!isLayer) {
12171 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12172 mPrivateFlags &= ~DIRTY_MASK;
12173 }
12174
12175 // Fast path for layouts with no backgrounds
12176 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12177 dispatchDraw(canvas);
12178 } else {
12179 draw(canvas);
12180 }
12181 }
12182 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012183 canvas.onPostDraw();
12184
12185 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012186 displayList.setCaching(caching);
12187 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012188 displayList.setLeftTopRightBottom(0, 0, width, height);
12189 } else {
12190 setDisplayListProperties(displayList);
12191 }
12192 }
12193 } else if (!isLayer) {
12194 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12195 mPrivateFlags &= ~DIRTY_MASK;
12196 }
12197
12198 return displayList;
12199 }
12200
12201 /**
12202 * Get the DisplayList for the HardwareLayer
12203 *
12204 * @param layer The HardwareLayer whose DisplayList we want
12205 * @return A DisplayList fopr the specified HardwareLayer
12206 */
12207 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12208 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12209 layer.setDisplayList(displayList);
12210 return displayList;
12211 }
12212
12213
12214 /**
Romain Guyb051e892010-09-28 19:09:36 -070012215 * <p>Returns a display list that can be used to draw this view again
12216 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012217 *
Romain Guyb051e892010-09-28 19:09:36 -070012218 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012219 *
12220 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012221 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012222 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012223 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012224 return mDisplayList;
12225 }
12226
Romain Guy38c2ece2012-05-24 14:20:56 -070012227 private void clearDisplayList() {
12228 if (mDisplayList != null) {
12229 mDisplayList.invalidate();
12230 mDisplayList.clear();
12231 }
12232 }
12233
Romain Guyb051e892010-09-28 19:09:36 -070012234 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012235 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012236 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012237 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012238 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012239 * @see #getDrawingCache(boolean)
12240 */
12241 public Bitmap getDrawingCache() {
12242 return getDrawingCache(false);
12243 }
12244
12245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012246 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12247 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12248 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12249 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12250 * request the drawing cache by calling this method and draw it on screen if the
12251 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012252 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012253 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12254 * this method will create a bitmap of the same size as this view. Because this bitmap
12255 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12256 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12257 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12258 * size than the view. This implies that your application must be able to handle this
12259 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012260 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012261 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12262 * the current density of the screen when the application is in compatibility
12263 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012264 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012265 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012266 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012267 * @see #setDrawingCacheEnabled(boolean)
12268 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012269 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012270 * @see #destroyDrawingCache()
12271 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012272 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012273 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12274 return null;
12275 }
12276 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012277 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012278 }
Romain Guy02890fd2010-08-06 17:58:44 -070012279 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012280 }
12281
12282 /**
12283 * <p>Frees the resources used by the drawing cache. If you call
12284 * {@link #buildDrawingCache()} manually without calling
12285 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12286 * should cleanup the cache with this method afterwards.</p>
12287 *
12288 * @see #setDrawingCacheEnabled(boolean)
12289 * @see #buildDrawingCache()
12290 * @see #getDrawingCache()
12291 */
12292 public void destroyDrawingCache() {
12293 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012294 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012295 mDrawingCache = null;
12296 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012297 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012298 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012299 mUnscaledDrawingCache = null;
12300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012301 }
12302
12303 /**
12304 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012305 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306 * view will always be drawn on top of a solid color.
12307 *
12308 * @param color The background color to use for the drawing cache's bitmap
12309 *
12310 * @see #setDrawingCacheEnabled(boolean)
12311 * @see #buildDrawingCache()
12312 * @see #getDrawingCache()
12313 */
12314 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012315 if (color != mDrawingCacheBackgroundColor) {
12316 mDrawingCacheBackgroundColor = color;
12317 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012319 }
12320
12321 /**
12322 * @see #setDrawingCacheBackgroundColor(int)
12323 *
12324 * @return The background color to used for the drawing cache's bitmap
12325 */
12326 public int getDrawingCacheBackgroundColor() {
12327 return mDrawingCacheBackgroundColor;
12328 }
12329
12330 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012331 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012332 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012333 * @see #buildDrawingCache(boolean)
12334 */
12335 public void buildDrawingCache() {
12336 buildDrawingCache(false);
12337 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012338
Romain Guyfbd8f692009-06-26 14:51:58 -070012339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012340 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12341 *
12342 * <p>If you call {@link #buildDrawingCache()} manually without calling
12343 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12344 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012345 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012346 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12347 * this method will create a bitmap of the same size as this view. Because this bitmap
12348 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12349 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12350 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12351 * size than the view. This implies that your application must be able to handle this
12352 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012353 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012354 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12355 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012356 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012357 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012358 *
12359 * @see #getDrawingCache()
12360 * @see #destroyDrawingCache()
12361 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012362 public void buildDrawingCache(boolean autoScale) {
12363 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012364 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012365 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366
Romain Guy8506ab42009-06-11 17:35:47 -070012367 int width = mRight - mLeft;
12368 int height = mBottom - mTop;
12369
12370 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012371 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012372
Romain Guye1123222009-06-29 14:24:56 -070012373 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012374 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12375 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377
12378 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012379 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012380 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381
12382 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012383 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012384 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012385 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12386 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012387 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388 return;
12389 }
12390
12391 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012392 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012393
12394 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395 Bitmap.Config quality;
12396 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012397 // Never pick ARGB_4444 because it looks awful
12398 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012399 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12400 case DRAWING_CACHE_QUALITY_AUTO:
12401 quality = Bitmap.Config.ARGB_8888;
12402 break;
12403 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012404 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 break;
12406 case DRAWING_CACHE_QUALITY_HIGH:
12407 quality = Bitmap.Config.ARGB_8888;
12408 break;
12409 default:
12410 quality = Bitmap.Config.ARGB_8888;
12411 break;
12412 }
12413 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012414 // Optimization for translucent windows
12415 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012416 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012417 }
12418
12419 // Try to cleanup memory
12420 if (bitmap != null) bitmap.recycle();
12421
12422 try {
12423 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012424 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012425 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012426 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012427 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012428 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012429 }
Adam Powell26153a32010-11-08 15:22:27 -080012430 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012431 } catch (OutOfMemoryError e) {
12432 // If there is not enough memory to create the bitmap cache, just
12433 // ignore the issue as bitmap caches are not required to draw the
12434 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012435 if (autoScale) {
12436 mDrawingCache = null;
12437 } else {
12438 mUnscaledDrawingCache = null;
12439 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012440 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012441 return;
12442 }
12443
12444 clear = drawingCacheBackgroundColor != 0;
12445 }
12446
12447 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012448 if (attachInfo != null) {
12449 canvas = attachInfo.mCanvas;
12450 if (canvas == null) {
12451 canvas = new Canvas();
12452 }
12453 canvas.setBitmap(bitmap);
12454 // Temporarily clobber the cached Canvas in case one of our children
12455 // is also using a drawing cache. Without this, the children would
12456 // steal the canvas by attaching their own bitmap to it and bad, bad
12457 // thing would happen (invisible views, corrupted drawings, etc.)
12458 attachInfo.mCanvas = null;
12459 } else {
12460 // This case should hopefully never or seldom happen
12461 canvas = new Canvas(bitmap);
12462 }
12463
12464 if (clear) {
12465 bitmap.eraseColor(drawingCacheBackgroundColor);
12466 }
12467
12468 computeScroll();
12469 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012470
Romain Guye1123222009-06-29 14:24:56 -070012471 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012472 final float scale = attachInfo.mApplicationScale;
12473 canvas.scale(scale, scale);
12474 }
Joe Malin32736f02011-01-19 16:14:20 -080012475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 canvas.translate(-mScrollX, -mScrollY);
12477
Romain Guy5bcdff42009-05-14 21:27:18 -070012478 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012479 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12480 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012481 mPrivateFlags |= DRAWING_CACHE_VALID;
12482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483
12484 // Fast path for layouts with no backgrounds
12485 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012486 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012487 dispatchDraw(canvas);
12488 } else {
12489 draw(canvas);
12490 }
12491
12492 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012493 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494
12495 if (attachInfo != null) {
12496 // Restore the cached Canvas for our siblings
12497 attachInfo.mCanvas = canvas;
12498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012499 }
12500 }
12501
12502 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012503 * Create a snapshot of the view into a bitmap. We should probably make
12504 * some form of this public, but should think about the API.
12505 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012506 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012507 int width = mRight - mLeft;
12508 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012509
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012510 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012511 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012512 width = (int) ((width * scale) + 0.5f);
12513 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012514
Romain Guy8c11e312009-09-14 15:15:30 -070012515 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012516 if (bitmap == null) {
12517 throw new OutOfMemoryError();
12518 }
12519
Romain Guyc529d8d2011-09-06 15:01:39 -070012520 Resources resources = getResources();
12521 if (resources != null) {
12522 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12523 }
Joe Malin32736f02011-01-19 16:14:20 -080012524
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012525 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012526 if (attachInfo != null) {
12527 canvas = attachInfo.mCanvas;
12528 if (canvas == null) {
12529 canvas = new Canvas();
12530 }
12531 canvas.setBitmap(bitmap);
12532 // Temporarily clobber the cached Canvas in case one of our children
12533 // is also using a drawing cache. Without this, the children would
12534 // steal the canvas by attaching their own bitmap to it and bad, bad
12535 // things would happen (invisible views, corrupted drawings, etc.)
12536 attachInfo.mCanvas = null;
12537 } else {
12538 // This case should hopefully never or seldom happen
12539 canvas = new Canvas(bitmap);
12540 }
12541
Romain Guy5bcdff42009-05-14 21:27:18 -070012542 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012543 bitmap.eraseColor(backgroundColor);
12544 }
12545
12546 computeScroll();
12547 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012548 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012549 canvas.translate(-mScrollX, -mScrollY);
12550
Romain Guy5bcdff42009-05-14 21:27:18 -070012551 // Temporarily remove the dirty mask
12552 int flags = mPrivateFlags;
12553 mPrivateFlags &= ~DIRTY_MASK;
12554
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012555 // Fast path for layouts with no backgrounds
12556 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12557 dispatchDraw(canvas);
12558 } else {
12559 draw(canvas);
12560 }
12561
Romain Guy5bcdff42009-05-14 21:27:18 -070012562 mPrivateFlags = flags;
12563
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012564 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012565 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012566
12567 if (attachInfo != null) {
12568 // Restore the cached Canvas for our siblings
12569 attachInfo.mCanvas = canvas;
12570 }
Romain Guy8506ab42009-06-11 17:35:47 -070012571
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012572 return bitmap;
12573 }
12574
12575 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012576 * Indicates whether this View is currently in edit mode. A View is usually
12577 * in edit mode when displayed within a developer tool. For instance, if
12578 * this View is being drawn by a visual user interface builder, this method
12579 * should return true.
12580 *
12581 * Subclasses should check the return value of this method to provide
12582 * different behaviors if their normal behavior might interfere with the
12583 * host environment. For instance: the class spawns a thread in its
12584 * constructor, the drawing code relies on device-specific features, etc.
12585 *
12586 * This method is usually checked in the drawing code of custom widgets.
12587 *
12588 * @return True if this View is in edit mode, false otherwise.
12589 */
12590 public boolean isInEditMode() {
12591 return false;
12592 }
12593
12594 /**
12595 * If the View draws content inside its padding and enables fading edges,
12596 * it needs to support padding offsets. Padding offsets are added to the
12597 * fading edges to extend the length of the fade so that it covers pixels
12598 * drawn inside the padding.
12599 *
12600 * Subclasses of this class should override this method if they need
12601 * to draw content inside the padding.
12602 *
12603 * @return True if padding offset must be applied, false otherwise.
12604 *
12605 * @see #getLeftPaddingOffset()
12606 * @see #getRightPaddingOffset()
12607 * @see #getTopPaddingOffset()
12608 * @see #getBottomPaddingOffset()
12609 *
12610 * @since CURRENT
12611 */
12612 protected boolean isPaddingOffsetRequired() {
12613 return false;
12614 }
12615
12616 /**
12617 * Amount by which to extend the left fading region. Called only when
12618 * {@link #isPaddingOffsetRequired()} returns true.
12619 *
12620 * @return The left padding offset in pixels.
12621 *
12622 * @see #isPaddingOffsetRequired()
12623 *
12624 * @since CURRENT
12625 */
12626 protected int getLeftPaddingOffset() {
12627 return 0;
12628 }
12629
12630 /**
12631 * Amount by which to extend the right fading region. Called only when
12632 * {@link #isPaddingOffsetRequired()} returns true.
12633 *
12634 * @return The right padding offset in pixels.
12635 *
12636 * @see #isPaddingOffsetRequired()
12637 *
12638 * @since CURRENT
12639 */
12640 protected int getRightPaddingOffset() {
12641 return 0;
12642 }
12643
12644 /**
12645 * Amount by which to extend the top fading region. Called only when
12646 * {@link #isPaddingOffsetRequired()} returns true.
12647 *
12648 * @return The top padding offset in pixels.
12649 *
12650 * @see #isPaddingOffsetRequired()
12651 *
12652 * @since CURRENT
12653 */
12654 protected int getTopPaddingOffset() {
12655 return 0;
12656 }
12657
12658 /**
12659 * Amount by which to extend the bottom fading region. Called only when
12660 * {@link #isPaddingOffsetRequired()} returns true.
12661 *
12662 * @return The bottom padding offset in pixels.
12663 *
12664 * @see #isPaddingOffsetRequired()
12665 *
12666 * @since CURRENT
12667 */
12668 protected int getBottomPaddingOffset() {
12669 return 0;
12670 }
12671
12672 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012673 * @hide
12674 * @param offsetRequired
12675 */
12676 protected int getFadeTop(boolean offsetRequired) {
12677 int top = mPaddingTop;
12678 if (offsetRequired) top += getTopPaddingOffset();
12679 return top;
12680 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012681
Romain Guyf2fc4602011-07-19 15:20:03 -070012682 /**
12683 * @hide
12684 * @param offsetRequired
12685 */
12686 protected int getFadeHeight(boolean offsetRequired) {
12687 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012688 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012689 return mBottom - mTop - mPaddingBottom - padding;
12690 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012691
Romain Guyf2fc4602011-07-19 15:20:03 -070012692 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012693 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012694 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012695 *
Romain Guy2bffd262010-09-12 17:40:02 -070012696 * <p>Even if this method returns true, it does not mean that every call
12697 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12698 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012699 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012700 * window is hardware accelerated,
12701 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12702 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012703 *
Romain Guy2bffd262010-09-12 17:40:02 -070012704 * @return True if the view is attached to a window and the window is
12705 * hardware accelerated; false in any other case.
12706 */
12707 public boolean isHardwareAccelerated() {
12708 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12709 }
Joe Malin32736f02011-01-19 16:14:20 -080012710
Romain Guy2bffd262010-09-12 17:40:02 -070012711 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012712 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12713 * case of an active Animation being run on the view.
12714 */
12715 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12716 Animation a, boolean scalingRequired) {
12717 Transformation invalidationTransform;
12718 final int flags = parent.mGroupFlags;
12719 final boolean initialized = a.isInitialized();
12720 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012721 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012722 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012723 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012724 onAnimationStart();
12725 }
12726
12727 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12728 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12729 if (parent.mInvalidationTransformation == null) {
12730 parent.mInvalidationTransformation = new Transformation();
12731 }
12732 invalidationTransform = parent.mInvalidationTransformation;
12733 a.getTransformation(drawingTime, invalidationTransform, 1f);
12734 } else {
12735 invalidationTransform = parent.mChildTransformation;
12736 }
Romain Guy393a52c2012-05-22 20:21:08 -070012737
Chet Haasebcca79a2012-02-14 08:45:14 -080012738 if (more) {
12739 if (!a.willChangeBounds()) {
12740 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12741 parent.FLAG_OPTIMIZE_INVALIDATE) {
12742 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12743 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12744 // The child need to draw an animation, potentially offscreen, so
12745 // make sure we do not cancel invalidate requests
12746 parent.mPrivateFlags |= DRAW_ANIMATION;
12747 parent.invalidate(mLeft, mTop, mRight, mBottom);
12748 }
12749 } else {
12750 if (parent.mInvalidateRegion == null) {
12751 parent.mInvalidateRegion = new RectF();
12752 }
12753 final RectF region = parent.mInvalidateRegion;
12754 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12755 invalidationTransform);
12756
12757 // The child need to draw an animation, potentially offscreen, so
12758 // make sure we do not cancel invalidate requests
12759 parent.mPrivateFlags |= DRAW_ANIMATION;
12760
12761 final int left = mLeft + (int) region.left;
12762 final int top = mTop + (int) region.top;
12763 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12764 top + (int) (region.height() + .5f));
12765 }
12766 }
12767 return more;
12768 }
12769
Chet Haasea1cff502012-02-21 13:43:44 -080012770 /**
12771 * This method is called by getDisplayList() when a display list is created or re-rendered.
12772 * It sets or resets the current value of all properties on that display list (resetting is
12773 * necessary when a display list is being re-created, because we need to make sure that
12774 * previously-set transform values
12775 */
12776 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012777 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012778 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012779 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012780 if (mParent instanceof ViewGroup) {
12781 displayList.setClipChildren(
12782 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12783 }
Chet Haase9420abd2012-03-29 16:28:32 -070012784 float alpha = 1;
12785 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12786 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12787 ViewGroup parentVG = (ViewGroup) mParent;
12788 final boolean hasTransform =
12789 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12790 if (hasTransform) {
12791 Transformation transform = parentVG.mChildTransformation;
12792 final int transformType = parentVG.mChildTransformation.getTransformationType();
12793 if (transformType != Transformation.TYPE_IDENTITY) {
12794 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12795 alpha = transform.getAlpha();
12796 }
12797 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12798 displayList.setStaticMatrix(transform.getMatrix());
12799 }
12800 }
12801 }
Chet Haasea1cff502012-02-21 13:43:44 -080012802 }
12803 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012804 alpha *= mTransformationInfo.mAlpha;
12805 if (alpha < 1) {
12806 final int multipliedAlpha = (int) (255 * alpha);
12807 if (onSetAlpha(multipliedAlpha)) {
12808 alpha = 1;
12809 }
12810 }
12811 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012812 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12813 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12814 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12815 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012816 if (mTransformationInfo.mCamera == null) {
12817 mTransformationInfo.mCamera = new Camera();
12818 mTransformationInfo.matrix3D = new Matrix();
12819 }
12820 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012821 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12822 displayList.setPivotX(getPivotX());
12823 displayList.setPivotY(getPivotY());
12824 }
Chet Haase9420abd2012-03-29 16:28:32 -070012825 } else if (alpha < 1) {
12826 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012827 }
12828 }
12829 }
12830
Chet Haasebcca79a2012-02-14 08:45:14 -080012831 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012832 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12833 * This draw() method is an implementation detail and is not intended to be overridden or
12834 * to be called from anywhere else other than ViewGroup.drawChild().
12835 */
12836 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012837 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012838 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012839 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012840 final int flags = parent.mGroupFlags;
12841
Chet Haasea1cff502012-02-21 13:43:44 -080012842 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012843 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012844 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012845 }
12846
12847 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012848 boolean concatMatrix = false;
12849
12850 boolean scalingRequired = false;
12851 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012852 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012853
12854 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012855 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12856 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012857 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012858 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012859 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12860 } else {
12861 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12862 }
12863
Chet Haasebcca79a2012-02-14 08:45:14 -080012864 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012865 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012866 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012867 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012868 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070012869 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012870 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012871 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012872 } else {
Chet Haase21433372012-06-05 07:54:09 -070012873 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012874 mDisplayList != null) {
12875 // No longer animating: clear out old animation matrix
12876 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070012877 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012878 }
12879 if (!useDisplayListProperties &&
12880 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12881 final boolean hasTransform =
12882 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12883 if (hasTransform) {
12884 final int transformType = parent.mChildTransformation.getTransformationType();
12885 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12886 parent.mChildTransformation : null;
12887 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12888 }
Chet Haase64a48c12012-02-13 16:33:29 -080012889 }
12890 }
12891
12892 concatMatrix |= !childHasIdentityMatrix;
12893
12894 // Sets the flag as early as possible to allow draw() implementations
12895 // to call invalidate() successfully when doing animations
12896 mPrivateFlags |= DRAWN;
12897
Chet Haasebcca79a2012-02-14 08:45:14 -080012898 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012899 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070012900 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012901 return more;
12902 }
Chet Haase1a3ab172012-05-11 08:41:20 -070012903 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080012904
12905 if (hardwareAccelerated) {
12906 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12907 // retain the flag's value temporarily in the mRecreateDisplayList flag
12908 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12909 mPrivateFlags &= ~INVALIDATED;
12910 }
12911
Chet Haase64a48c12012-02-13 16:33:29 -080012912 DisplayList displayList = null;
12913 Bitmap cache = null;
12914 boolean hasDisplayList = false;
12915 if (caching) {
12916 if (!hardwareAccelerated) {
12917 if (layerType != LAYER_TYPE_NONE) {
12918 layerType = LAYER_TYPE_SOFTWARE;
12919 buildDrawingCache(true);
12920 }
12921 cache = getDrawingCache(true);
12922 } else {
12923 switch (layerType) {
12924 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012925 if (useDisplayListProperties) {
12926 hasDisplayList = canHaveDisplayList();
12927 } else {
12928 buildDrawingCache(true);
12929 cache = getDrawingCache(true);
12930 }
Chet Haase64a48c12012-02-13 16:33:29 -080012931 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012932 case LAYER_TYPE_HARDWARE:
12933 if (useDisplayListProperties) {
12934 hasDisplayList = canHaveDisplayList();
12935 }
12936 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012937 case LAYER_TYPE_NONE:
12938 // Delay getting the display list until animation-driven alpha values are
12939 // set up and possibly passed on to the view
12940 hasDisplayList = canHaveDisplayList();
12941 break;
12942 }
12943 }
12944 }
Chet Haasea1cff502012-02-21 13:43:44 -080012945 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012946 if (useDisplayListProperties) {
12947 displayList = getDisplayList();
12948 if (!displayList.isValid()) {
12949 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12950 // to getDisplayList(), the display list will be marked invalid and we should not
12951 // try to use it again.
12952 displayList = null;
12953 hasDisplayList = false;
12954 useDisplayListProperties = false;
12955 }
12956 }
Chet Haase64a48c12012-02-13 16:33:29 -080012957
Chet Haase526057b2012-07-12 17:50:41 -070012958 int sx = 0;
12959 int sy = 0;
12960 if (!hasDisplayList) {
12961 computeScroll();
12962 sx = mScrollX;
12963 sy = mScrollY;
12964 }
12965
Chet Haase64a48c12012-02-13 16:33:29 -080012966 final boolean hasNoCache = cache == null || hasDisplayList;
12967 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12968 layerType != LAYER_TYPE_HARDWARE;
12969
Chet Haasea1cff502012-02-21 13:43:44 -080012970 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012971 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012972 restoreTo = canvas.save();
12973 }
Chet Haase64a48c12012-02-13 16:33:29 -080012974 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012975 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012976 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012977 if (!useDisplayListProperties) {
12978 canvas.translate(mLeft, mTop);
12979 }
Chet Haase64a48c12012-02-13 16:33:29 -080012980 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012981 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012982 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012983 restoreTo = canvas.save();
12984 }
Chet Haase64a48c12012-02-13 16:33:29 -080012985 // mAttachInfo cannot be null, otherwise scalingRequired == false
12986 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12987 canvas.scale(scale, scale);
12988 }
12989 }
12990
Chet Haasea1cff502012-02-21 13:43:44 -080012991 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070012992 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
12993 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080012994 if (transformToApply != null || !childHasIdentityMatrix) {
12995 int transX = 0;
12996 int transY = 0;
12997
12998 if (offsetForScroll) {
12999 transX = -sx;
13000 transY = -sy;
13001 }
13002
13003 if (transformToApply != null) {
13004 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013005 if (useDisplayListProperties) {
13006 displayList.setAnimationMatrix(transformToApply.getMatrix());
13007 } else {
13008 // Undo the scroll translation, apply the transformation matrix,
13009 // then redo the scroll translate to get the correct result.
13010 canvas.translate(-transX, -transY);
13011 canvas.concat(transformToApply.getMatrix());
13012 canvas.translate(transX, transY);
13013 }
Chet Haasea1cff502012-02-21 13:43:44 -080013014 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013015 }
13016
13017 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013018 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013019 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013020 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013021 }
13022 }
13023
Chet Haasea1cff502012-02-21 13:43:44 -080013024 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013025 canvas.translate(-transX, -transY);
13026 canvas.concat(getMatrix());
13027 canvas.translate(transX, transY);
13028 }
13029 }
13030
Chet Haase21433372012-06-05 07:54:09 -070013031 // Deal with alpha if it is or used to be <1
13032 if (alpha < 1 ||
13033 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13034 if (alpha < 1) {
13035 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13036 } else {
13037 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13038 }
Chet Haasea1cff502012-02-21 13:43:44 -080013039 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013040 if (hasNoCache) {
13041 final int multipliedAlpha = (int) (255 * alpha);
13042 if (!onSetAlpha(multipliedAlpha)) {
13043 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013044 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013045 layerType != LAYER_TYPE_NONE) {
13046 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13047 }
Chet Haase9420abd2012-03-29 16:28:32 -070013048 if (useDisplayListProperties) {
13049 displayList.setAlpha(alpha * getAlpha());
13050 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013051 final int scrollX = hasDisplayList ? 0 : sx;
13052 final int scrollY = hasDisplayList ? 0 : sy;
13053 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13054 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013055 }
13056 } else {
13057 // Alpha is handled by the child directly, clobber the layer's alpha
13058 mPrivateFlags |= ALPHA_SET;
13059 }
13060 }
13061 }
13062 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13063 onSetAlpha(255);
13064 mPrivateFlags &= ~ALPHA_SET;
13065 }
13066
Chet Haasea1cff502012-02-21 13:43:44 -080013067 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13068 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013069 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013070 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013071 } else {
13072 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013073 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013074 } else {
13075 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13076 }
13077 }
13078 }
13079
Chet Haase9420abd2012-03-29 16:28:32 -070013080 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013081 displayList = getDisplayList();
13082 if (!displayList.isValid()) {
13083 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13084 // to getDisplayList(), the display list will be marked invalid and we should not
13085 // try to use it again.
13086 displayList = null;
13087 hasDisplayList = false;
13088 }
13089 }
13090
13091 if (hasNoCache) {
13092 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013093 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013094 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013095 if (layer != null && layer.isValid()) {
13096 mLayerPaint.setAlpha((int) (alpha * 255));
13097 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13098 layerRendered = true;
13099 } else {
13100 final int scrollX = hasDisplayList ? 0 : sx;
13101 final int scrollY = hasDisplayList ? 0 : sy;
13102 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013103 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013104 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13105 }
13106 }
13107
13108 if (!layerRendered) {
13109 if (!hasDisplayList) {
13110 // Fast path for layouts with no backgrounds
13111 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013112 mPrivateFlags &= ~DIRTY_MASK;
13113 dispatchDraw(canvas);
13114 } else {
13115 draw(canvas);
13116 }
13117 } else {
13118 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013119 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013120 }
13121 }
13122 } else if (cache != null) {
13123 mPrivateFlags &= ~DIRTY_MASK;
13124 Paint cachePaint;
13125
13126 if (layerType == LAYER_TYPE_NONE) {
13127 cachePaint = parent.mCachePaint;
13128 if (cachePaint == null) {
13129 cachePaint = new Paint();
13130 cachePaint.setDither(false);
13131 parent.mCachePaint = cachePaint;
13132 }
Chet Haase9420abd2012-03-29 16:28:32 -070013133 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013134 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013135 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13136 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013137 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013138 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013139 }
13140 } else {
13141 cachePaint = mLayerPaint;
13142 cachePaint.setAlpha((int) (alpha * 255));
13143 }
13144 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13145 }
13146
Chet Haasea1cff502012-02-21 13:43:44 -080013147 if (restoreTo >= 0) {
13148 canvas.restoreToCount(restoreTo);
13149 }
Chet Haase64a48c12012-02-13 16:33:29 -080013150
13151 if (a != null && !more) {
13152 if (!hardwareAccelerated && !a.getFillAfter()) {
13153 onSetAlpha(255);
13154 }
13155 parent.finishAnimatingView(this, a);
13156 }
13157
13158 if (more && hardwareAccelerated) {
13159 // invalidation is the trigger to recreate display lists, so if we're using
13160 // display lists to render, force an invalidate to allow the animation to
13161 // continue drawing another frame
13162 parent.invalidate(true);
13163 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13164 // alpha animations should cause the child to recreate its display list
13165 invalidate(true);
13166 }
13167 }
13168
13169 mRecreateDisplayList = false;
13170
13171 return more;
13172 }
13173
13174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013175 * Manually render this view (and all of its children) to the given Canvas.
13176 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013177 * called. When implementing a view, implement
13178 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13179 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013180 *
13181 * @param canvas The Canvas to which the View is rendered.
13182 */
13183 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013184 final int privateFlags = mPrivateFlags;
13185 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13186 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13187 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013189 /*
13190 * Draw traversal performs several drawing steps which must be executed
13191 * in the appropriate order:
13192 *
13193 * 1. Draw the background
13194 * 2. If necessary, save the canvas' layers to prepare for fading
13195 * 3. Draw view's content
13196 * 4. Draw children
13197 * 5. If necessary, draw the fading edges and restore layers
13198 * 6. Draw decorations (scrollbars for instance)
13199 */
13200
13201 // Step 1, draw the background, if needed
13202 int saveCount;
13203
Romain Guy24443ea2009-05-11 11:56:30 -070013204 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013205 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013206 if (background != null) {
13207 final int scrollX = mScrollX;
13208 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209
Romain Guy24443ea2009-05-11 11:56:30 -070013210 if (mBackgroundSizeChanged) {
13211 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13212 mBackgroundSizeChanged = false;
13213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013214
Romain Guy24443ea2009-05-11 11:56:30 -070013215 if ((scrollX | scrollY) == 0) {
13216 background.draw(canvas);
13217 } else {
13218 canvas.translate(scrollX, scrollY);
13219 background.draw(canvas);
13220 canvas.translate(-scrollX, -scrollY);
13221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013222 }
13223 }
13224
13225 // skip step 2 & 5 if possible (common case)
13226 final int viewFlags = mViewFlags;
13227 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13228 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13229 if (!verticalEdges && !horizontalEdges) {
13230 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013231 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013232
13233 // Step 4, draw the children
13234 dispatchDraw(canvas);
13235
13236 // Step 6, draw decorations (scrollbars)
13237 onDrawScrollBars(canvas);
13238
13239 // we're done...
13240 return;
13241 }
13242
13243 /*
13244 * Here we do the full fledged routine...
13245 * (this is an uncommon case where speed matters less,
13246 * this is why we repeat some of the tests that have been
13247 * done above)
13248 */
13249
13250 boolean drawTop = false;
13251 boolean drawBottom = false;
13252 boolean drawLeft = false;
13253 boolean drawRight = false;
13254
13255 float topFadeStrength = 0.0f;
13256 float bottomFadeStrength = 0.0f;
13257 float leftFadeStrength = 0.0f;
13258 float rightFadeStrength = 0.0f;
13259
13260 // Step 2, save the canvas' layers
13261 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013262
13263 final boolean offsetRequired = isPaddingOffsetRequired();
13264 if (offsetRequired) {
13265 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013266 }
13267
13268 int left = mScrollX + paddingLeft;
13269 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013270 int top = mScrollY + getFadeTop(offsetRequired);
13271 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013272
13273 if (offsetRequired) {
13274 right += getRightPaddingOffset();
13275 bottom += getBottomPaddingOffset();
13276 }
13277
13278 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013279 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013280 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013281
13282 // clip the fade length if top and bottom fades overlap
13283 // overlapping fades produce odd-looking artifacts
13284 if (verticalEdges && (top + length > bottom - length)) {
13285 length = (bottom - top) / 2;
13286 }
13287
13288 // also clip horizontal fades if necessary
13289 if (horizontalEdges && (left + length > right - length)) {
13290 length = (right - left) / 2;
13291 }
13292
13293 if (verticalEdges) {
13294 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013295 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013296 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013297 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013298 }
13299
13300 if (horizontalEdges) {
13301 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013302 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013303 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013304 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013305 }
13306
13307 saveCount = canvas.getSaveCount();
13308
13309 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013310 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013311 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13312
13313 if (drawTop) {
13314 canvas.saveLayer(left, top, right, top + length, null, flags);
13315 }
13316
13317 if (drawBottom) {
13318 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13319 }
13320
13321 if (drawLeft) {
13322 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13323 }
13324
13325 if (drawRight) {
13326 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13327 }
13328 } else {
13329 scrollabilityCache.setFadeColor(solidColor);
13330 }
13331
13332 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013333 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013334
13335 // Step 4, draw the children
13336 dispatchDraw(canvas);
13337
13338 // Step 5, draw the fade effect and restore layers
13339 final Paint p = scrollabilityCache.paint;
13340 final Matrix matrix = scrollabilityCache.matrix;
13341 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013342
13343 if (drawTop) {
13344 matrix.setScale(1, fadeHeight * topFadeStrength);
13345 matrix.postTranslate(left, top);
13346 fade.setLocalMatrix(matrix);
13347 canvas.drawRect(left, top, right, top + length, p);
13348 }
13349
13350 if (drawBottom) {
13351 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13352 matrix.postRotate(180);
13353 matrix.postTranslate(left, bottom);
13354 fade.setLocalMatrix(matrix);
13355 canvas.drawRect(left, bottom - length, right, bottom, p);
13356 }
13357
13358 if (drawLeft) {
13359 matrix.setScale(1, fadeHeight * leftFadeStrength);
13360 matrix.postRotate(-90);
13361 matrix.postTranslate(left, top);
13362 fade.setLocalMatrix(matrix);
13363 canvas.drawRect(left, top, left + length, bottom, p);
13364 }
13365
13366 if (drawRight) {
13367 matrix.setScale(1, fadeHeight * rightFadeStrength);
13368 matrix.postRotate(90);
13369 matrix.postTranslate(right, top);
13370 fade.setLocalMatrix(matrix);
13371 canvas.drawRect(right - length, top, right, bottom, p);
13372 }
13373
13374 canvas.restoreToCount(saveCount);
13375
13376 // Step 6, draw decorations (scrollbars)
13377 onDrawScrollBars(canvas);
13378 }
13379
13380 /**
13381 * Override this if your view is known to always be drawn on top of a solid color background,
13382 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13383 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13384 * should be set to 0xFF.
13385 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013386 * @see #setVerticalFadingEdgeEnabled(boolean)
13387 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013388 *
13389 * @return The known solid color background for this view, or 0 if the color may vary
13390 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013391 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013392 public int getSolidColor() {
13393 return 0;
13394 }
13395
13396 /**
13397 * Build a human readable string representation of the specified view flags.
13398 *
13399 * @param flags the view flags to convert to a string
13400 * @return a String representing the supplied flags
13401 */
13402 private static String printFlags(int flags) {
13403 String output = "";
13404 int numFlags = 0;
13405 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13406 output += "TAKES_FOCUS";
13407 numFlags++;
13408 }
13409
13410 switch (flags & VISIBILITY_MASK) {
13411 case INVISIBLE:
13412 if (numFlags > 0) {
13413 output += " ";
13414 }
13415 output += "INVISIBLE";
13416 // USELESS HERE numFlags++;
13417 break;
13418 case GONE:
13419 if (numFlags > 0) {
13420 output += " ";
13421 }
13422 output += "GONE";
13423 // USELESS HERE numFlags++;
13424 break;
13425 default:
13426 break;
13427 }
13428 return output;
13429 }
13430
13431 /**
13432 * Build a human readable string representation of the specified private
13433 * view flags.
13434 *
13435 * @param privateFlags the private view flags to convert to a string
13436 * @return a String representing the supplied flags
13437 */
13438 private static String printPrivateFlags(int privateFlags) {
13439 String output = "";
13440 int numFlags = 0;
13441
13442 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13443 output += "WANTS_FOCUS";
13444 numFlags++;
13445 }
13446
13447 if ((privateFlags & FOCUSED) == FOCUSED) {
13448 if (numFlags > 0) {
13449 output += " ";
13450 }
13451 output += "FOCUSED";
13452 numFlags++;
13453 }
13454
13455 if ((privateFlags & SELECTED) == SELECTED) {
13456 if (numFlags > 0) {
13457 output += " ";
13458 }
13459 output += "SELECTED";
13460 numFlags++;
13461 }
13462
13463 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13464 if (numFlags > 0) {
13465 output += " ";
13466 }
13467 output += "IS_ROOT_NAMESPACE";
13468 numFlags++;
13469 }
13470
13471 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13472 if (numFlags > 0) {
13473 output += " ";
13474 }
13475 output += "HAS_BOUNDS";
13476 numFlags++;
13477 }
13478
13479 if ((privateFlags & DRAWN) == DRAWN) {
13480 if (numFlags > 0) {
13481 output += " ";
13482 }
13483 output += "DRAWN";
13484 // USELESS HERE numFlags++;
13485 }
13486 return output;
13487 }
13488
13489 /**
13490 * <p>Indicates whether or not this view's layout will be requested during
13491 * the next hierarchy layout pass.</p>
13492 *
13493 * @return true if the layout will be forced during next layout pass
13494 */
13495 public boolean isLayoutRequested() {
13496 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13497 }
13498
13499 /**
13500 * Assign a size and position to a view and all of its
13501 * descendants
13502 *
13503 * <p>This is the second phase of the layout mechanism.
13504 * (The first is measuring). In this phase, each parent calls
13505 * layout on all of its children to position them.
13506 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013507 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013508 *
Chet Haase9c087442011-01-12 16:20:16 -080013509 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 * Derived classes with children should override
13511 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013512 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013513 *
13514 * @param l Left position, relative to parent
13515 * @param t Top position, relative to parent
13516 * @param r Right position, relative to parent
13517 * @param b Bottom position, relative to parent
13518 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013519 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013520 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013521 int oldL = mLeft;
13522 int oldT = mTop;
13523 int oldB = mBottom;
13524 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 boolean changed = setFrame(l, t, r, b);
13526 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 onLayout(changed, l, t, r, b);
13528 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013529
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013530 ListenerInfo li = mListenerInfo;
13531 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013532 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013533 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013534 int numListeners = listenersCopy.size();
13535 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013536 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013537 }
13538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013539 }
13540 mPrivateFlags &= ~FORCE_LAYOUT;
13541 }
13542
13543 /**
13544 * Called from layout when this view should
13545 * assign a size and position to each of its children.
13546 *
13547 * Derived classes with children should override
13548 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013549 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013550 * @param changed This is a new size or position for this view
13551 * @param left Left position, relative to parent
13552 * @param top Top position, relative to parent
13553 * @param right Right position, relative to parent
13554 * @param bottom Bottom position, relative to parent
13555 */
13556 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13557 }
13558
13559 /**
13560 * Assign a size and position to this view.
13561 *
13562 * This is called from layout.
13563 *
13564 * @param left Left position, relative to parent
13565 * @param top Top position, relative to parent
13566 * @param right Right position, relative to parent
13567 * @param bottom Bottom position, relative to parent
13568 * @return true if the new size and position are different than the
13569 * previous ones
13570 * {@hide}
13571 */
13572 protected boolean setFrame(int left, int top, int right, int bottom) {
13573 boolean changed = false;
13574
13575 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013576 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577 + right + "," + bottom + ")");
13578 }
13579
13580 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13581 changed = true;
13582
13583 // Remember our drawn bit
13584 int drawn = mPrivateFlags & DRAWN;
13585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 int oldWidth = mRight - mLeft;
13587 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013588 int newWidth = right - left;
13589 int newHeight = bottom - top;
13590 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13591
13592 // Invalidate our old position
13593 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013594
13595 mLeft = left;
13596 mTop = top;
13597 mRight = right;
13598 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013599 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013600 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602
13603 mPrivateFlags |= HAS_BOUNDS;
13604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013605
Chet Haase75755e22011-07-18 17:48:25 -070013606 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013607 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13608 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013609 if (mTransformationInfo != null) {
13610 mTransformationInfo.mMatrixDirty = true;
13611 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013613 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13614 }
13615
13616 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13617 // If we are visible, force the DRAWN bit to on so that
13618 // this invalidate will go through (at least to our parent).
13619 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013620 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621 // the DRAWN bit.
13622 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013623 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013624 // parent display list may need to be recreated based on a change in the bounds
13625 // of any child
13626 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013627 }
13628
13629 // Reset drawn bit to original value (invalidate turns it off)
13630 mPrivateFlags |= drawn;
13631
13632 mBackgroundSizeChanged = true;
13633 }
13634 return changed;
13635 }
13636
13637 /**
13638 * Finalize inflating a view from XML. This is called as the last phase
13639 * of inflation, after all child views have been added.
13640 *
13641 * <p>Even if the subclass overrides onFinishInflate, they should always be
13642 * sure to call the super method, so that we get called.
13643 */
13644 protected void onFinishInflate() {
13645 }
13646
13647 /**
13648 * Returns the resources associated with this view.
13649 *
13650 * @return Resources object.
13651 */
13652 public Resources getResources() {
13653 return mResources;
13654 }
13655
13656 /**
13657 * Invalidates the specified Drawable.
13658 *
13659 * @param drawable the drawable to invalidate
13660 */
13661 public void invalidateDrawable(Drawable drawable) {
13662 if (verifyDrawable(drawable)) {
13663 final Rect dirty = drawable.getBounds();
13664 final int scrollX = mScrollX;
13665 final int scrollY = mScrollY;
13666
13667 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13668 dirty.right + scrollX, dirty.bottom + scrollY);
13669 }
13670 }
13671
13672 /**
13673 * Schedules an action on a drawable to occur at a specified time.
13674 *
13675 * @param who the recipient of the action
13676 * @param what the action to run on the drawable
13677 * @param when the time at which the action must occur. Uses the
13678 * {@link SystemClock#uptimeMillis} timebase.
13679 */
13680 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013681 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013682 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013683 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013684 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13685 Choreographer.CALLBACK_ANIMATION, what, who,
13686 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013687 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013688 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013690 }
13691 }
13692
13693 /**
13694 * Cancels a scheduled action on a drawable.
13695 *
13696 * @param who the recipient of the action
13697 * @param what the action to cancel
13698 */
13699 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013700 if (verifyDrawable(who) && what != null) {
13701 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013702 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13703 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013704 } else {
13705 ViewRootImpl.getRunQueue().removeCallbacks(what);
13706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013707 }
13708 }
13709
13710 /**
13711 * Unschedule any events associated with the given Drawable. This can be
13712 * used when selecting a new Drawable into a view, so that the previous
13713 * one is completely unscheduled.
13714 *
13715 * @param who The Drawable to unschedule.
13716 *
13717 * @see #drawableStateChanged
13718 */
13719 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013720 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013721 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13722 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723 }
13724 }
13725
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013726 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013727 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13728 * that the View directionality can and will be resolved before its Drawables.
13729 *
13730 * Will call {@link View#onResolveDrawables} when resolution is done.
13731 */
13732 public void resolveDrawables() {
13733 if (mBackground != null) {
13734 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13735 }
13736 onResolveDrawables(getResolvedLayoutDirection());
13737 }
13738
13739 /**
13740 * Called when layout direction has been resolved.
13741 *
13742 * The default implementation does nothing.
13743 *
13744 * @param layoutDirection The resolved layout direction.
13745 *
13746 * @see {@link #LAYOUT_DIRECTION_LTR}
13747 * @see {@link #LAYOUT_DIRECTION_RTL}
13748 */
13749 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013750 }
13751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 /**
13753 * If your view subclass is displaying its own Drawable objects, it should
13754 * override this function and return true for any Drawable it is
13755 * displaying. This allows animations for those drawables to be
13756 * scheduled.
13757 *
13758 * <p>Be sure to call through to the super class when overriding this
13759 * function.
13760 *
13761 * @param who The Drawable to verify. Return true if it is one you are
13762 * displaying, else return the result of calling through to the
13763 * super class.
13764 *
13765 * @return boolean If true than the Drawable is being displayed in the
13766 * view; else false and it is not allowed to animate.
13767 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013768 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13769 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 */
13771 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013772 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013773 }
13774
13775 /**
13776 * This function is called whenever the state of the view changes in such
13777 * a way that it impacts the state of drawables being shown.
13778 *
13779 * <p>Be sure to call through to the superclass when overriding this
13780 * function.
13781 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013782 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013783 */
13784 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013785 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013786 if (d != null && d.isStateful()) {
13787 d.setState(getDrawableState());
13788 }
13789 }
13790
13791 /**
13792 * Call this to force a view to update its drawable state. This will cause
13793 * drawableStateChanged to be called on this view. Views that are interested
13794 * in the new state should call getDrawableState.
13795 *
13796 * @see #drawableStateChanged
13797 * @see #getDrawableState
13798 */
13799 public void refreshDrawableState() {
13800 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13801 drawableStateChanged();
13802
13803 ViewParent parent = mParent;
13804 if (parent != null) {
13805 parent.childDrawableStateChanged(this);
13806 }
13807 }
13808
13809 /**
13810 * Return an array of resource IDs of the drawable states representing the
13811 * current state of the view.
13812 *
13813 * @return The current drawable state
13814 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013815 * @see Drawable#setState(int[])
13816 * @see #drawableStateChanged()
13817 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013818 */
13819 public final int[] getDrawableState() {
13820 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13821 return mDrawableState;
13822 } else {
13823 mDrawableState = onCreateDrawableState(0);
13824 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13825 return mDrawableState;
13826 }
13827 }
13828
13829 /**
13830 * Generate the new {@link android.graphics.drawable.Drawable} state for
13831 * this view. This is called by the view
13832 * system when the cached Drawable state is determined to be invalid. To
13833 * retrieve the current state, you should use {@link #getDrawableState}.
13834 *
13835 * @param extraSpace if non-zero, this is the number of extra entries you
13836 * would like in the returned array in which you can place your own
13837 * states.
13838 *
13839 * @return Returns an array holding the current {@link Drawable} state of
13840 * the view.
13841 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013842 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013843 */
13844 protected int[] onCreateDrawableState(int extraSpace) {
13845 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13846 mParent instanceof View) {
13847 return ((View) mParent).onCreateDrawableState(extraSpace);
13848 }
13849
13850 int[] drawableState;
13851
13852 int privateFlags = mPrivateFlags;
13853
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013854 int viewStateIndex = 0;
13855 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13856 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13857 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013858 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013859 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13860 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013861 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13862 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013863 // This is set if HW acceleration is requested, even if the current
13864 // process doesn't allow it. This is just to allow app preview
13865 // windows to better match their app.
13866 viewStateIndex |= VIEW_STATE_ACCELERATED;
13867 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013868 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869
Christopher Tate3d4bf172011-03-28 16:16:46 -070013870 final int privateFlags2 = mPrivateFlags2;
13871 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13872 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013874 drawableState = VIEW_STATE_SETS[viewStateIndex];
13875
13876 //noinspection ConstantIfStatement
13877 if (false) {
13878 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13879 Log.i("View", toString()
13880 + " pressed=" + ((privateFlags & PRESSED) != 0)
13881 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13882 + " fo=" + hasFocus()
13883 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013884 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013885 + ": " + Arrays.toString(drawableState));
13886 }
13887
13888 if (extraSpace == 0) {
13889 return drawableState;
13890 }
13891
13892 final int[] fullState;
13893 if (drawableState != null) {
13894 fullState = new int[drawableState.length + extraSpace];
13895 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13896 } else {
13897 fullState = new int[extraSpace];
13898 }
13899
13900 return fullState;
13901 }
13902
13903 /**
13904 * Merge your own state values in <var>additionalState</var> into the base
13905 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013906 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 *
13908 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013909 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 * own additional state values.
13911 *
13912 * @param additionalState The additional state values you would like
13913 * added to <var>baseState</var>; this array is not modified.
13914 *
13915 * @return As a convenience, the <var>baseState</var> array you originally
13916 * passed into the function is returned.
13917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 */
13920 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13921 final int N = baseState.length;
13922 int i = N - 1;
13923 while (i >= 0 && baseState[i] == 0) {
13924 i--;
13925 }
13926 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13927 return baseState;
13928 }
13929
13930 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013931 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13932 * on all Drawable objects associated with this view.
13933 */
13934 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013935 if (mBackground != null) {
13936 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013937 }
13938 }
13939
13940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 * Sets the background color for this view.
13942 * @param color the color of the background
13943 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013944 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013946 if (mBackground instanceof ColorDrawable) {
13947 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013948 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013949 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 }
13952
13953 /**
13954 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013955 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013958 * @attr ref android.R.styleable#View_background
13959 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013960 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 public void setBackgroundResource(int resid) {
13962 if (resid != 0 && resid == mBackgroundResource) {
13963 return;
13964 }
13965
13966 Drawable d= null;
13967 if (resid != 0) {
13968 d = mResources.getDrawable(resid);
13969 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013970 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971
13972 mBackgroundResource = resid;
13973 }
13974
13975 /**
13976 * Set the background to a given Drawable, or remove the background. If the
13977 * background has padding, this View's padding is set to the background's
13978 * padding. However, when a background is removed, this View's padding isn't
13979 * touched. If setting the padding is desired, please use
13980 * {@link #setPadding(int, int, int, int)}.
13981 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013982 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 * background
13984 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013985 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013986 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013987 setBackgroundDrawable(background);
13988 }
13989
13990 /**
13991 * @deprecated use {@link #setBackground(Drawable)} instead
13992 */
13993 @Deprecated
13994 public void setBackgroundDrawable(Drawable background) {
13995 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013996 return;
13997 }
13998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013999 boolean requestLayout = false;
14000
14001 mBackgroundResource = 0;
14002
14003 /*
14004 * Regardless of whether we're setting a new background or not, we want
14005 * to clear the previous drawable.
14006 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014007 if (mBackground != null) {
14008 mBackground.setCallback(null);
14009 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 }
14011
Philip Milne6c8ea062012-04-03 17:38:43 -070014012 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 Rect padding = sThreadLocal.get();
14014 if (padding == null) {
14015 padding = new Rect();
14016 sThreadLocal.set(padding);
14017 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014018 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014019 if (background.getPadding(padding)) {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014020 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014021 case LAYOUT_DIRECTION_RTL:
14022 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14023 break;
14024 case LAYOUT_DIRECTION_LTR:
14025 default:
14026 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014028 }
14029
14030 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14031 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014032 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14033 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014034 requestLayout = true;
14035 }
14036
Philip Milne6c8ea062012-04-03 17:38:43 -070014037 background.setCallback(this);
14038 if (background.isStateful()) {
14039 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014040 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014041 background.setVisible(getVisibility() == VISIBLE, false);
14042 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043
14044 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14045 mPrivateFlags &= ~SKIP_DRAW;
14046 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14047 requestLayout = true;
14048 }
14049 } else {
14050 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014051 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052
14053 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14054 /*
14055 * This view ONLY drew the background before and we're removing
14056 * the background, so now it won't draw anything
14057 * (hence we SKIP_DRAW)
14058 */
14059 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14060 mPrivateFlags |= SKIP_DRAW;
14061 }
14062
14063 /*
14064 * When the background is set, we try to apply its padding to this
14065 * View. When the background is removed, we don't touch this View's
14066 * padding. This is noted in the Javadocs. Hence, we don't need to
14067 * requestLayout(), the invalidate() below is sufficient.
14068 */
14069
14070 // The old background's minimum size could have affected this
14071 // View's layout, so let's requestLayout
14072 requestLayout = true;
14073 }
14074
Romain Guy8f1344f52009-05-15 16:03:59 -070014075 computeOpaqueFlags();
14076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 if (requestLayout) {
14078 requestLayout();
14079 }
14080
14081 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014082 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 }
14084
14085 /**
14086 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014087 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014088 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014089 *
14090 * @see #setBackground(Drawable)
14091 *
14092 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 */
14094 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014095 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 }
14097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014098 /**
14099 * Sets the padding. The view may add on the space required to display
14100 * the scrollbars, depending on the style and visibility of the scrollbars.
14101 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14102 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14103 * from the values set in this call.
14104 *
14105 * @attr ref android.R.styleable#View_padding
14106 * @attr ref android.R.styleable#View_paddingBottom
14107 * @attr ref android.R.styleable#View_paddingLeft
14108 * @attr ref android.R.styleable#View_paddingRight
14109 * @attr ref android.R.styleable#View_paddingTop
14110 * @param left the left padding in pixels
14111 * @param top the top padding in pixels
14112 * @param right the right padding in pixels
14113 * @param bottom the bottom padding in pixels
14114 */
14115 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014116 mUserPaddingStart = -1;
14117 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014118 mUserPaddingRelative = false;
14119
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014120 internalSetPadding(left, top, right, bottom);
14121 }
14122
14123 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014124 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014125 mUserPaddingRight = right;
14126 mUserPaddingBottom = bottom;
14127
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014128 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014129 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014130
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014131 // Common case is there are no scroll bars.
14132 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014133 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014134 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014135 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014136 switch (mVerticalScrollbarPosition) {
14137 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014138 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14139 left += offset;
14140 } else {
14141 right += offset;
14142 }
14143 break;
Adam Powell20232d02010-12-08 21:08:53 -080014144 case SCROLLBAR_POSITION_RIGHT:
14145 right += offset;
14146 break;
14147 case SCROLLBAR_POSITION_LEFT:
14148 left += offset;
14149 break;
14150 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014151 }
Adam Powell20232d02010-12-08 21:08:53 -080014152 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014153 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14154 ? 0 : getHorizontalScrollbarHeight();
14155 }
14156 }
Romain Guy8506ab42009-06-11 17:35:47 -070014157
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014158 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014159 changed = true;
14160 mPaddingLeft = left;
14161 }
14162 if (mPaddingTop != top) {
14163 changed = true;
14164 mPaddingTop = top;
14165 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014166 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014168 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014169 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014170 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014171 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014172 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014173 }
14174
14175 if (changed) {
14176 requestLayout();
14177 }
14178 }
14179
14180 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014181 * Sets the relative padding. The view may add on the space required to display
14182 * the scrollbars, depending on the style and visibility of the scrollbars.
14183 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14184 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14185 * from the values set in this call.
14186 *
14187 * @attr ref android.R.styleable#View_padding
14188 * @attr ref android.R.styleable#View_paddingBottom
14189 * @attr ref android.R.styleable#View_paddingStart
14190 * @attr ref android.R.styleable#View_paddingEnd
14191 * @attr ref android.R.styleable#View_paddingTop
14192 * @param start the start padding in pixels
14193 * @param top the top padding in pixels
14194 * @param end the end padding in pixels
14195 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014196 */
14197 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014198 mUserPaddingStart = start;
14199 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014200 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014201
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014202 switch(getResolvedLayoutDirection()) {
14203 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014204 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014205 break;
14206 case LAYOUT_DIRECTION_LTR:
14207 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014208 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014209 }
14210 }
14211
14212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 * Returns the top padding of this view.
14214 *
14215 * @return the top padding in pixels
14216 */
14217 public int getPaddingTop() {
14218 return mPaddingTop;
14219 }
14220
14221 /**
14222 * Returns the bottom padding of this view. If there are inset and enabled
14223 * scrollbars, this value may include the space required to display the
14224 * scrollbars as well.
14225 *
14226 * @return the bottom padding in pixels
14227 */
14228 public int getPaddingBottom() {
14229 return mPaddingBottom;
14230 }
14231
14232 /**
14233 * Returns the left padding of this view. If there are inset and enabled
14234 * scrollbars, this value may include the space required to display the
14235 * scrollbars as well.
14236 *
14237 * @return the left padding in pixels
14238 */
14239 public int getPaddingLeft() {
14240 return mPaddingLeft;
14241 }
14242
14243 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014244 * Returns the start padding of this view depending on its resolved layout direction.
14245 * If there are inset and enabled scrollbars, this value may include the space
14246 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014247 *
14248 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014249 */
14250 public int getPaddingStart() {
14251 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14252 mPaddingRight : mPaddingLeft;
14253 }
14254
14255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 * Returns the right padding of this view. If there are inset and enabled
14257 * scrollbars, this value may include the space required to display the
14258 * scrollbars as well.
14259 *
14260 * @return the right padding in pixels
14261 */
14262 public int getPaddingRight() {
14263 return mPaddingRight;
14264 }
14265
14266 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014267 * Returns the end padding of this view depending on its resolved layout direction.
14268 * If there are inset and enabled scrollbars, this value may include the space
14269 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014270 *
14271 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014272 */
14273 public int getPaddingEnd() {
14274 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14275 mPaddingLeft : mPaddingRight;
14276 }
14277
14278 /**
14279 * Return if the padding as been set thru relative values
14280 * {@link #setPaddingRelative(int, int, int, int)} or thru
14281 * @attr ref android.R.styleable#View_paddingStart or
14282 * @attr ref android.R.styleable#View_paddingEnd
14283 *
14284 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014285 */
14286 public boolean isPaddingRelative() {
14287 return mUserPaddingRelative;
14288 }
14289
14290 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014291 * @hide
14292 */
Philip Milne7a23b492012-04-24 22:12:36 -070014293 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014294 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014295 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014296 }
14297 return mLayoutInsets;
14298 }
14299
14300 /**
14301 * @hide
14302 */
14303 public void setLayoutInsets(Insets layoutInsets) {
14304 mLayoutInsets = layoutInsets;
14305 }
14306
14307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014308 * Changes the selection state of this view. A view can be selected or not.
14309 * Note that selection is not the same as focus. Views are typically
14310 * selected in the context of an AdapterView like ListView or GridView;
14311 * the selected view is the view that is highlighted.
14312 *
14313 * @param selected true if the view must be selected, false otherwise
14314 */
14315 public void setSelected(boolean selected) {
14316 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14317 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014318 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014319 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 refreshDrawableState();
14321 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014322 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14323 notifyAccessibilityStateChanged();
14324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014325 }
14326 }
14327
14328 /**
14329 * Dispatch setSelected to all of this View's children.
14330 *
14331 * @see #setSelected(boolean)
14332 *
14333 * @param selected The new selected state
14334 */
14335 protected void dispatchSetSelected(boolean selected) {
14336 }
14337
14338 /**
14339 * Indicates the selection state of this view.
14340 *
14341 * @return true if the view is selected, false otherwise
14342 */
14343 @ViewDebug.ExportedProperty
14344 public boolean isSelected() {
14345 return (mPrivateFlags & SELECTED) != 0;
14346 }
14347
14348 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014349 * Changes the activated state of this view. A view can be activated or not.
14350 * Note that activation is not the same as selection. Selection is
14351 * a transient property, representing the view (hierarchy) the user is
14352 * currently interacting with. Activation is a longer-term state that the
14353 * user can move views in and out of. For example, in a list view with
14354 * single or multiple selection enabled, the views in the current selection
14355 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14356 * here.) The activated state is propagated down to children of the view it
14357 * is set on.
14358 *
14359 * @param activated true if the view must be activated, false otherwise
14360 */
14361 public void setActivated(boolean activated) {
14362 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14363 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014364 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014365 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014366 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014367 }
14368 }
14369
14370 /**
14371 * Dispatch setActivated to all of this View's children.
14372 *
14373 * @see #setActivated(boolean)
14374 *
14375 * @param activated The new activated state
14376 */
14377 protected void dispatchSetActivated(boolean activated) {
14378 }
14379
14380 /**
14381 * Indicates the activation state of this view.
14382 *
14383 * @return true if the view is activated, false otherwise
14384 */
14385 @ViewDebug.ExportedProperty
14386 public boolean isActivated() {
14387 return (mPrivateFlags & ACTIVATED) != 0;
14388 }
14389
14390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014391 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14392 * observer can be used to get notifications when global events, like
14393 * layout, happen.
14394 *
14395 * The returned ViewTreeObserver observer is not guaranteed to remain
14396 * valid for the lifetime of this View. If the caller of this method keeps
14397 * a long-lived reference to ViewTreeObserver, it should always check for
14398 * the return value of {@link ViewTreeObserver#isAlive()}.
14399 *
14400 * @return The ViewTreeObserver for this view's hierarchy.
14401 */
14402 public ViewTreeObserver getViewTreeObserver() {
14403 if (mAttachInfo != null) {
14404 return mAttachInfo.mTreeObserver;
14405 }
14406 if (mFloatingTreeObserver == null) {
14407 mFloatingTreeObserver = new ViewTreeObserver();
14408 }
14409 return mFloatingTreeObserver;
14410 }
14411
14412 /**
14413 * <p>Finds the topmost view in the current view hierarchy.</p>
14414 *
14415 * @return the topmost view containing this view
14416 */
14417 public View getRootView() {
14418 if (mAttachInfo != null) {
14419 final View v = mAttachInfo.mRootView;
14420 if (v != null) {
14421 return v;
14422 }
14423 }
Romain Guy8506ab42009-06-11 17:35:47 -070014424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014425 View parent = this;
14426
14427 while (parent.mParent != null && parent.mParent instanceof View) {
14428 parent = (View) parent.mParent;
14429 }
14430
14431 return parent;
14432 }
14433
14434 /**
14435 * <p>Computes the coordinates of this view on the screen. The argument
14436 * must be an array of two integers. After the method returns, the array
14437 * contains the x and y location in that order.</p>
14438 *
14439 * @param location an array of two integers in which to hold the coordinates
14440 */
14441 public void getLocationOnScreen(int[] location) {
14442 getLocationInWindow(location);
14443
14444 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014445 if (info != null) {
14446 location[0] += info.mWindowLeft;
14447 location[1] += info.mWindowTop;
14448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014449 }
14450
14451 /**
14452 * <p>Computes the coordinates of this view in its window. The argument
14453 * must be an array of two integers. After the method returns, the array
14454 * contains the x and y location in that order.</p>
14455 *
14456 * @param location an array of two integers in which to hold the coordinates
14457 */
14458 public void getLocationInWindow(int[] location) {
14459 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014460 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 }
14462
Gilles Debunne6583ce52011-12-06 18:09:02 -080014463 if (mAttachInfo == null) {
14464 // When the view is not attached to a window, this method does not make sense
14465 location[0] = location[1] = 0;
14466 return;
14467 }
14468
Gilles Debunnecea45132011-11-24 02:19:27 +010014469 float[] position = mAttachInfo.mTmpTransformLocation;
14470 position[0] = position[1] = 0.0f;
14471
14472 if (!hasIdentityMatrix()) {
14473 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014475
Gilles Debunnecea45132011-11-24 02:19:27 +010014476 position[0] += mLeft;
14477 position[1] += mTop;
14478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 ViewParent viewParent = mParent;
14480 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014481 final View view = (View) viewParent;
14482
14483 position[0] -= view.mScrollX;
14484 position[1] -= view.mScrollY;
14485
14486 if (!view.hasIdentityMatrix()) {
14487 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014488 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014489
14490 position[0] += view.mLeft;
14491 position[1] += view.mTop;
14492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014494 }
Romain Guy8506ab42009-06-11 17:35:47 -070014495
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014496 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014498 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14499 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014500 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014501
14502 location[0] = (int) (position[0] + 0.5f);
14503 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 }
14505
14506 /**
14507 * {@hide}
14508 * @param id the id of the view to be found
14509 * @return the view of the specified id, null if cannot be found
14510 */
14511 protected View findViewTraversal(int id) {
14512 if (id == mID) {
14513 return this;
14514 }
14515 return null;
14516 }
14517
14518 /**
14519 * {@hide}
14520 * @param tag the tag of the view to be found
14521 * @return the view of specified tag, null if cannot be found
14522 */
14523 protected View findViewWithTagTraversal(Object tag) {
14524 if (tag != null && tag.equals(mTag)) {
14525 return this;
14526 }
14527 return null;
14528 }
14529
14530 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014531 * {@hide}
14532 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014533 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014534 * @return The first view that matches the predicate or null.
14535 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014536 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014537 if (predicate.apply(this)) {
14538 return this;
14539 }
14540 return null;
14541 }
14542
14543 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014544 * Look for a child view with the given id. If this view has the given
14545 * id, return this view.
14546 *
14547 * @param id The id to search for.
14548 * @return The view that has the given id in the hierarchy or null
14549 */
14550 public final View findViewById(int id) {
14551 if (id < 0) {
14552 return null;
14553 }
14554 return findViewTraversal(id);
14555 }
14556
14557 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014558 * Finds a view by its unuque and stable accessibility id.
14559 *
14560 * @param accessibilityId The searched accessibility id.
14561 * @return The found view.
14562 */
14563 final View findViewByAccessibilityId(int accessibilityId) {
14564 if (accessibilityId < 0) {
14565 return null;
14566 }
14567 return findViewByAccessibilityIdTraversal(accessibilityId);
14568 }
14569
14570 /**
14571 * Performs the traversal to find a view by its unuque and stable accessibility id.
14572 *
14573 * <strong>Note:</strong>This method does not stop at the root namespace
14574 * boundary since the user can touch the screen at an arbitrary location
14575 * potentially crossing the root namespace bounday which will send an
14576 * accessibility event to accessibility services and they should be able
14577 * to obtain the event source. Also accessibility ids are guaranteed to be
14578 * unique in the window.
14579 *
14580 * @param accessibilityId The accessibility id.
14581 * @return The found view.
14582 */
14583 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14584 if (getAccessibilityViewId() == accessibilityId) {
14585 return this;
14586 }
14587 return null;
14588 }
14589
14590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014591 * Look for a child view with the given tag. If this view has the given
14592 * tag, return this view.
14593 *
14594 * @param tag The tag to search for, using "tag.equals(getTag())".
14595 * @return The View that has the given tag in the hierarchy or null
14596 */
14597 public final View findViewWithTag(Object tag) {
14598 if (tag == null) {
14599 return null;
14600 }
14601 return findViewWithTagTraversal(tag);
14602 }
14603
14604 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014605 * {@hide}
14606 * Look for a child view that matches the specified predicate.
14607 * If this view matches the predicate, return this view.
14608 *
14609 * @param predicate The predicate to evaluate.
14610 * @return The first view that matches the predicate or null.
14611 */
14612 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014613 return findViewByPredicateTraversal(predicate, null);
14614 }
14615
14616 /**
14617 * {@hide}
14618 * Look for a child view that matches the specified predicate,
14619 * starting with the specified view and its descendents and then
14620 * recusively searching the ancestors and siblings of that view
14621 * until this view is reached.
14622 *
14623 * This method is useful in cases where the predicate does not match
14624 * a single unique view (perhaps multiple views use the same id)
14625 * and we are trying to find the view that is "closest" in scope to the
14626 * starting view.
14627 *
14628 * @param start The view to start from.
14629 * @param predicate The predicate to evaluate.
14630 * @return The first view that matches the predicate or null.
14631 */
14632 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14633 View childToSkip = null;
14634 for (;;) {
14635 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14636 if (view != null || start == this) {
14637 return view;
14638 }
14639
14640 ViewParent parent = start.getParent();
14641 if (parent == null || !(parent instanceof View)) {
14642 return null;
14643 }
14644
14645 childToSkip = start;
14646 start = (View) parent;
14647 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014648 }
14649
14650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014651 * Sets the identifier for this view. The identifier does not have to be
14652 * unique in this view's hierarchy. The identifier should be a positive
14653 * number.
14654 *
14655 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014656 * @see #getId()
14657 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658 *
14659 * @param id a number used to identify the view
14660 *
14661 * @attr ref android.R.styleable#View_id
14662 */
14663 public void setId(int id) {
14664 mID = id;
14665 }
14666
14667 /**
14668 * {@hide}
14669 *
14670 * @param isRoot true if the view belongs to the root namespace, false
14671 * otherwise
14672 */
14673 public void setIsRootNamespace(boolean isRoot) {
14674 if (isRoot) {
14675 mPrivateFlags |= IS_ROOT_NAMESPACE;
14676 } else {
14677 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14678 }
14679 }
14680
14681 /**
14682 * {@hide}
14683 *
14684 * @return true if the view belongs to the root namespace, false otherwise
14685 */
14686 public boolean isRootNamespace() {
14687 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14688 }
14689
14690 /**
14691 * Returns this view's identifier.
14692 *
14693 * @return a positive integer used to identify the view or {@link #NO_ID}
14694 * if the view has no ID
14695 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014696 * @see #setId(int)
14697 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014698 * @attr ref android.R.styleable#View_id
14699 */
14700 @ViewDebug.CapturedViewProperty
14701 public int getId() {
14702 return mID;
14703 }
14704
14705 /**
14706 * Returns this view's tag.
14707 *
14708 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014709 *
14710 * @see #setTag(Object)
14711 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014712 */
14713 @ViewDebug.ExportedProperty
14714 public Object getTag() {
14715 return mTag;
14716 }
14717
14718 /**
14719 * Sets the tag associated with this view. A tag can be used to mark
14720 * a view in its hierarchy and does not have to be unique within the
14721 * hierarchy. Tags can also be used to store data within a view without
14722 * resorting to another data structure.
14723 *
14724 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014725 *
14726 * @see #getTag()
14727 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014728 */
14729 public void setTag(final Object tag) {
14730 mTag = tag;
14731 }
14732
14733 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014734 * Returns the tag associated with this view and the specified key.
14735 *
14736 * @param key The key identifying the tag
14737 *
14738 * @return the Object stored in this view as a tag
14739 *
14740 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014741 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014742 */
14743 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014744 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014745 return null;
14746 }
14747
14748 /**
14749 * Sets a tag associated with this view and a key. A tag can be used
14750 * to mark a view in its hierarchy and does not have to be unique within
14751 * the hierarchy. Tags can also be used to store data within a view
14752 * without resorting to another data structure.
14753 *
14754 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014755 * application to ensure it is unique (see the <a
14756 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14757 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014758 * the Android framework or not associated with any package will cause
14759 * an {@link IllegalArgumentException} to be thrown.
14760 *
14761 * @param key The key identifying the tag
14762 * @param tag An Object to tag the view with
14763 *
14764 * @throws IllegalArgumentException If they specified key is not valid
14765 *
14766 * @see #setTag(Object)
14767 * @see #getTag(int)
14768 */
14769 public void setTag(int key, final Object tag) {
14770 // If the package id is 0x00 or 0x01, it's either an undefined package
14771 // or a framework id
14772 if ((key >>> 24) < 2) {
14773 throw new IllegalArgumentException("The key must be an application-specific "
14774 + "resource id.");
14775 }
14776
Adam Powell2b2f6d62011-09-23 11:15:39 -070014777 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014778 }
14779
14780 /**
14781 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14782 * framework id.
14783 *
14784 * @hide
14785 */
14786 public void setTagInternal(int key, Object tag) {
14787 if ((key >>> 24) != 0x1) {
14788 throw new IllegalArgumentException("The key must be a framework-specific "
14789 + "resource id.");
14790 }
14791
Adam Powell2b2f6d62011-09-23 11:15:39 -070014792 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014793 }
14794
Adam Powell2b2f6d62011-09-23 11:15:39 -070014795 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014796 if (mKeyedTags == null) {
14797 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014798 }
14799
Adam Powell7db82ac2011-09-22 19:44:04 -070014800 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014801 }
14802
14803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014804 * Prints information about this view in the log output, with the tag
14805 * {@link #VIEW_LOG_TAG}.
14806 *
14807 * @hide
14808 */
14809 public void debug() {
14810 debug(0);
14811 }
14812
14813 /**
14814 * Prints information about this view in the log output, with the tag
14815 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14816 * indentation defined by the <code>depth</code>.
14817 *
14818 * @param depth the indentation level
14819 *
14820 * @hide
14821 */
14822 protected void debug(int depth) {
14823 String output = debugIndent(depth - 1);
14824
14825 output += "+ " + this;
14826 int id = getId();
14827 if (id != -1) {
14828 output += " (id=" + id + ")";
14829 }
14830 Object tag = getTag();
14831 if (tag != null) {
14832 output += " (tag=" + tag + ")";
14833 }
14834 Log.d(VIEW_LOG_TAG, output);
14835
14836 if ((mPrivateFlags & FOCUSED) != 0) {
14837 output = debugIndent(depth) + " FOCUSED";
14838 Log.d(VIEW_LOG_TAG, output);
14839 }
14840
14841 output = debugIndent(depth);
14842 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14843 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14844 + "} ";
14845 Log.d(VIEW_LOG_TAG, output);
14846
14847 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14848 || mPaddingBottom != 0) {
14849 output = debugIndent(depth);
14850 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14851 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14852 Log.d(VIEW_LOG_TAG, output);
14853 }
14854
14855 output = debugIndent(depth);
14856 output += "mMeasureWidth=" + mMeasuredWidth +
14857 " mMeasureHeight=" + mMeasuredHeight;
14858 Log.d(VIEW_LOG_TAG, output);
14859
14860 output = debugIndent(depth);
14861 if (mLayoutParams == null) {
14862 output += "BAD! no layout params";
14863 } else {
14864 output = mLayoutParams.debug(output);
14865 }
14866 Log.d(VIEW_LOG_TAG, output);
14867
14868 output = debugIndent(depth);
14869 output += "flags={";
14870 output += View.printFlags(mViewFlags);
14871 output += "}";
14872 Log.d(VIEW_LOG_TAG, output);
14873
14874 output = debugIndent(depth);
14875 output += "privateFlags={";
14876 output += View.printPrivateFlags(mPrivateFlags);
14877 output += "}";
14878 Log.d(VIEW_LOG_TAG, output);
14879 }
14880
14881 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014882 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014883 *
14884 * @param depth the indentation level
14885 * @return a String containing (depth * 2 + 3) * 2 white spaces
14886 *
14887 * @hide
14888 */
14889 protected static String debugIndent(int depth) {
14890 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14891 for (int i = 0; i < (depth * 2) + 3; i++) {
14892 spaces.append(' ').append(' ');
14893 }
14894 return spaces.toString();
14895 }
14896
14897 /**
14898 * <p>Return the offset of the widget's text baseline from the widget's top
14899 * boundary. If this widget does not support baseline alignment, this
14900 * method returns -1. </p>
14901 *
14902 * @return the offset of the baseline within the widget's bounds or -1
14903 * if baseline alignment is not supported
14904 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014905 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014906 public int getBaseline() {
14907 return -1;
14908 }
14909
14910 /**
14911 * Call this when something has changed which has invalidated the
14912 * layout of this view. This will schedule a layout pass of the view
14913 * tree.
14914 */
14915 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014916 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014917 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014918
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014919 if (mLayoutParams != null) {
14920 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14921 }
14922
14923 if (mParent != null && !mParent.isLayoutRequested()) {
14924 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 }
14926 }
14927
14928 /**
14929 * Forces this view to be laid out during the next layout pass.
14930 * This method does not call requestLayout() or forceLayout()
14931 * on the parent.
14932 */
14933 public void forceLayout() {
14934 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014935 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936 }
14937
14938 /**
14939 * <p>
14940 * This is called to find out how big a view should be. The parent
14941 * supplies constraint information in the width and height parameters.
14942 * </p>
14943 *
14944 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014945 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014946 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014947 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014948 * </p>
14949 *
14950 *
14951 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14952 * parent
14953 * @param heightMeasureSpec Vertical space requirements as imposed by the
14954 * parent
14955 *
14956 * @see #onMeasure(int, int)
14957 */
14958 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14959 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14960 widthMeasureSpec != mOldWidthMeasureSpec ||
14961 heightMeasureSpec != mOldHeightMeasureSpec) {
14962
14963 // first clears the measured dimension flag
14964 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014966 // measure ourselves, this should set the measured dimension flag back
14967 onMeasure(widthMeasureSpec, heightMeasureSpec);
14968
14969 // flag not set, setMeasuredDimension() was not invoked, we raise
14970 // an exception to warn the developer
14971 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14972 throw new IllegalStateException("onMeasure() did not set the"
14973 + " measured dimension by calling"
14974 + " setMeasuredDimension()");
14975 }
14976
14977 mPrivateFlags |= LAYOUT_REQUIRED;
14978 }
14979
14980 mOldWidthMeasureSpec = widthMeasureSpec;
14981 mOldHeightMeasureSpec = heightMeasureSpec;
14982 }
14983
14984 /**
14985 * <p>
14986 * Measure the view and its content to determine the measured width and the
14987 * measured height. This method is invoked by {@link #measure(int, int)} and
14988 * should be overriden by subclasses to provide accurate and efficient
14989 * measurement of their contents.
14990 * </p>
14991 *
14992 * <p>
14993 * <strong>CONTRACT:</strong> When overriding this method, you
14994 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14995 * measured width and height of this view. Failure to do so will trigger an
14996 * <code>IllegalStateException</code>, thrown by
14997 * {@link #measure(int, int)}. Calling the superclass'
14998 * {@link #onMeasure(int, int)} is a valid use.
14999 * </p>
15000 *
15001 * <p>
15002 * The base class implementation of measure defaults to the background size,
15003 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15004 * override {@link #onMeasure(int, int)} to provide better measurements of
15005 * their content.
15006 * </p>
15007 *
15008 * <p>
15009 * If this method is overridden, it is the subclass's responsibility to make
15010 * sure the measured height and width are at least the view's minimum height
15011 * and width ({@link #getSuggestedMinimumHeight()} and
15012 * {@link #getSuggestedMinimumWidth()}).
15013 * </p>
15014 *
15015 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15016 * The requirements are encoded with
15017 * {@link android.view.View.MeasureSpec}.
15018 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15019 * The requirements are encoded with
15020 * {@link android.view.View.MeasureSpec}.
15021 *
15022 * @see #getMeasuredWidth()
15023 * @see #getMeasuredHeight()
15024 * @see #setMeasuredDimension(int, int)
15025 * @see #getSuggestedMinimumHeight()
15026 * @see #getSuggestedMinimumWidth()
15027 * @see android.view.View.MeasureSpec#getMode(int)
15028 * @see android.view.View.MeasureSpec#getSize(int)
15029 */
15030 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15031 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15032 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15033 }
15034
15035 /**
15036 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15037 * measured width and measured height. Failing to do so will trigger an
15038 * exception at measurement time.</p>
15039 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015040 * @param measuredWidth The measured width of this view. May be a complex
15041 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15042 * {@link #MEASURED_STATE_TOO_SMALL}.
15043 * @param measuredHeight The measured height of this view. May be a complex
15044 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15045 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015046 */
15047 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15048 mMeasuredWidth = measuredWidth;
15049 mMeasuredHeight = measuredHeight;
15050
15051 mPrivateFlags |= MEASURED_DIMENSION_SET;
15052 }
15053
15054 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015055 * Merge two states as returned by {@link #getMeasuredState()}.
15056 * @param curState The current state as returned from a view or the result
15057 * of combining multiple views.
15058 * @param newState The new view state to combine.
15059 * @return Returns a new integer reflecting the combination of the two
15060 * states.
15061 */
15062 public static int combineMeasuredStates(int curState, int newState) {
15063 return curState | newState;
15064 }
15065
15066 /**
15067 * Version of {@link #resolveSizeAndState(int, int, int)}
15068 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15069 */
15070 public static int resolveSize(int size, int measureSpec) {
15071 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15072 }
15073
15074 /**
15075 * Utility to reconcile a desired size and state, with constraints imposed
15076 * by a MeasureSpec. Will take the desired size, unless a different size
15077 * is imposed by the constraints. The returned value is a compound integer,
15078 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15079 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15080 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015081 *
15082 * @param size How big the view wants to be
15083 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015084 * @return Size information bit mask as defined by
15085 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015086 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015087 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 int result = size;
15089 int specMode = MeasureSpec.getMode(measureSpec);
15090 int specSize = MeasureSpec.getSize(measureSpec);
15091 switch (specMode) {
15092 case MeasureSpec.UNSPECIFIED:
15093 result = size;
15094 break;
15095 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015096 if (specSize < size) {
15097 result = specSize | MEASURED_STATE_TOO_SMALL;
15098 } else {
15099 result = size;
15100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015101 break;
15102 case MeasureSpec.EXACTLY:
15103 result = specSize;
15104 break;
15105 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015106 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015107 }
15108
15109 /**
15110 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015111 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 * by the MeasureSpec.
15113 *
15114 * @param size Default size for this view
15115 * @param measureSpec Constraints imposed by the parent
15116 * @return The size this view should be.
15117 */
15118 public static int getDefaultSize(int size, int measureSpec) {
15119 int result = size;
15120 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015121 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015122
15123 switch (specMode) {
15124 case MeasureSpec.UNSPECIFIED:
15125 result = size;
15126 break;
15127 case MeasureSpec.AT_MOST:
15128 case MeasureSpec.EXACTLY:
15129 result = specSize;
15130 break;
15131 }
15132 return result;
15133 }
15134
15135 /**
15136 * Returns the suggested minimum height that the view should use. This
15137 * returns the maximum of the view's minimum height
15138 * and the background's minimum height
15139 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15140 * <p>
15141 * When being used in {@link #onMeasure(int, int)}, the caller should still
15142 * ensure the returned height is within the requirements of the parent.
15143 *
15144 * @return The suggested minimum height of the view.
15145 */
15146 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015147 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015149 }
15150
15151 /**
15152 * Returns the suggested minimum width that the view should use. This
15153 * returns the maximum of the view's minimum width)
15154 * and the background's minimum width
15155 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15156 * <p>
15157 * When being used in {@link #onMeasure(int, int)}, the caller should still
15158 * ensure the returned width is within the requirements of the parent.
15159 *
15160 * @return The suggested minimum width of the view.
15161 */
15162 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015163 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015165
Philip Milne6c8ea062012-04-03 17:38:43 -070015166 /**
15167 * Returns the minimum height of the view.
15168 *
15169 * @return the minimum height the view will try to be.
15170 *
15171 * @see #setMinimumHeight(int)
15172 *
15173 * @attr ref android.R.styleable#View_minHeight
15174 */
15175 public int getMinimumHeight() {
15176 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015177 }
15178
15179 /**
15180 * Sets the minimum height of the view. It is not guaranteed the view will
15181 * be able to achieve this minimum height (for example, if its parent layout
15182 * constrains it with less available height).
15183 *
15184 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015185 *
15186 * @see #getMinimumHeight()
15187 *
15188 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015189 */
15190 public void setMinimumHeight(int minHeight) {
15191 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015192 requestLayout();
15193 }
15194
15195 /**
15196 * Returns the minimum width of the view.
15197 *
15198 * @return the minimum width the view will try to be.
15199 *
15200 * @see #setMinimumWidth(int)
15201 *
15202 * @attr ref android.R.styleable#View_minWidth
15203 */
15204 public int getMinimumWidth() {
15205 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015206 }
15207
15208 /**
15209 * Sets the minimum width of the view. It is not guaranteed the view will
15210 * be able to achieve this minimum width (for example, if its parent layout
15211 * constrains it with less available width).
15212 *
15213 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015214 *
15215 * @see #getMinimumWidth()
15216 *
15217 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015218 */
15219 public void setMinimumWidth(int minWidth) {
15220 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015221 requestLayout();
15222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015223 }
15224
15225 /**
15226 * Get the animation currently associated with this view.
15227 *
15228 * @return The animation that is currently playing or
15229 * scheduled to play for this view.
15230 */
15231 public Animation getAnimation() {
15232 return mCurrentAnimation;
15233 }
15234
15235 /**
15236 * Start the specified animation now.
15237 *
15238 * @param animation the animation to start now
15239 */
15240 public void startAnimation(Animation animation) {
15241 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15242 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015243 invalidateParentCaches();
15244 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015245 }
15246
15247 /**
15248 * Cancels any animations for this view.
15249 */
15250 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015251 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015252 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015255 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 }
15257
15258 /**
15259 * Sets the next animation to play for this view.
15260 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015261 * {@link #startAnimation(android.view.animation.Animation)} instead.
15262 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015263 * control over the start time and invalidation, but you
15264 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015265 * 2) the view's parent (which controls animations on its children)
15266 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015267 * start.
15268 *
15269 * @param animation The next animation, or null.
15270 */
15271 public void setAnimation(Animation animation) {
15272 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015274 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015275 // If the screen is off assume the animation start time is now instead of
15276 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15277 // would cause the animation to start when the screen turns back on
15278 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15279 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15280 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015282 animation.reset();
15283 }
15284 }
15285
15286 /**
15287 * Invoked by a parent ViewGroup to notify the start of the animation
15288 * currently associated with this view. If you override this method,
15289 * always call super.onAnimationStart();
15290 *
15291 * @see #setAnimation(android.view.animation.Animation)
15292 * @see #getAnimation()
15293 */
15294 protected void onAnimationStart() {
15295 mPrivateFlags |= ANIMATION_STARTED;
15296 }
15297
15298 /**
15299 * Invoked by a parent ViewGroup to notify the end of the animation
15300 * currently associated with this view. If you override this method,
15301 * always call super.onAnimationEnd();
15302 *
15303 * @see #setAnimation(android.view.animation.Animation)
15304 * @see #getAnimation()
15305 */
15306 protected void onAnimationEnd() {
15307 mPrivateFlags &= ~ANIMATION_STARTED;
15308 }
15309
15310 /**
15311 * Invoked if there is a Transform that involves alpha. Subclass that can
15312 * draw themselves with the specified alpha should return true, and then
15313 * respect that alpha when their onDraw() is called. If this returns false
15314 * then the view may be redirected to draw into an offscreen buffer to
15315 * fulfill the request, which will look fine, but may be slower than if the
15316 * subclass handles it internally. The default implementation returns false.
15317 *
15318 * @param alpha The alpha (0..255) to apply to the view's drawing
15319 * @return true if the view can draw with the specified alpha.
15320 */
15321 protected boolean onSetAlpha(int alpha) {
15322 return false;
15323 }
15324
15325 /**
15326 * This is used by the RootView to perform an optimization when
15327 * the view hierarchy contains one or several SurfaceView.
15328 * SurfaceView is always considered transparent, but its children are not,
15329 * therefore all View objects remove themselves from the global transparent
15330 * region (passed as a parameter to this function).
15331 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015332 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015333 *
15334 * @return Returns true if the effective visibility of the view at this
15335 * point is opaque, regardless of the transparent region; returns false
15336 * if it is possible for underlying windows to be seen behind the view.
15337 *
15338 * {@hide}
15339 */
15340 public boolean gatherTransparentRegion(Region region) {
15341 final AttachInfo attachInfo = mAttachInfo;
15342 if (region != null && attachInfo != null) {
15343 final int pflags = mPrivateFlags;
15344 if ((pflags & SKIP_DRAW) == 0) {
15345 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15346 // remove it from the transparent region.
15347 final int[] location = attachInfo.mTransparentLocation;
15348 getLocationInWindow(location);
15349 region.op(location[0], location[1], location[0] + mRight - mLeft,
15350 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015351 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015352 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15353 // exists, so we remove the background drawable's non-transparent
15354 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015355 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015356 }
15357 }
15358 return true;
15359 }
15360
15361 /**
15362 * Play a sound effect for this view.
15363 *
15364 * <p>The framework will play sound effects for some built in actions, such as
15365 * clicking, but you may wish to play these effects in your widget,
15366 * for instance, for internal navigation.
15367 *
15368 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15369 * {@link #isSoundEffectsEnabled()} is true.
15370 *
15371 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15372 */
15373 public void playSoundEffect(int soundConstant) {
15374 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15375 return;
15376 }
15377 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15378 }
15379
15380 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015381 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015382 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015383 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015384 *
15385 * <p>The framework will provide haptic feedback for some built in actions,
15386 * such as long presses, but you may wish to provide feedback for your
15387 * own widget.
15388 *
15389 * <p>The feedback will only be performed if
15390 * {@link #isHapticFeedbackEnabled()} is true.
15391 *
15392 * @param feedbackConstant One of the constants defined in
15393 * {@link HapticFeedbackConstants}
15394 */
15395 public boolean performHapticFeedback(int feedbackConstant) {
15396 return performHapticFeedback(feedbackConstant, 0);
15397 }
15398
15399 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015400 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015401 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015402 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015403 *
15404 * @param feedbackConstant One of the constants defined in
15405 * {@link HapticFeedbackConstants}
15406 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15407 */
15408 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15409 if (mAttachInfo == null) {
15410 return false;
15411 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015412 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015413 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015414 && !isHapticFeedbackEnabled()) {
15415 return false;
15416 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015417 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15418 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015419 }
15420
15421 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015422 * Request that the visibility of the status bar or other screen/window
15423 * decorations be changed.
15424 *
15425 * <p>This method is used to put the over device UI into temporary modes
15426 * where the user's attention is focused more on the application content,
15427 * by dimming or hiding surrounding system affordances. This is typically
15428 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15429 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15430 * to be placed behind the action bar (and with these flags other system
15431 * affordances) so that smooth transitions between hiding and showing them
15432 * can be done.
15433 *
15434 * <p>Two representative examples of the use of system UI visibility is
15435 * implementing a content browsing application (like a magazine reader)
15436 * and a video playing application.
15437 *
15438 * <p>The first code shows a typical implementation of a View in a content
15439 * browsing application. In this implementation, the application goes
15440 * into a content-oriented mode by hiding the status bar and action bar,
15441 * and putting the navigation elements into lights out mode. The user can
15442 * then interact with content while in this mode. Such an application should
15443 * provide an easy way for the user to toggle out of the mode (such as to
15444 * check information in the status bar or access notifications). In the
15445 * implementation here, this is done simply by tapping on the content.
15446 *
15447 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15448 * content}
15449 *
15450 * <p>This second code sample shows a typical implementation of a View
15451 * in a video playing application. In this situation, while the video is
15452 * playing the application would like to go into a complete full-screen mode,
15453 * to use as much of the display as possible for the video. When in this state
15454 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015455 * touching on the screen to pop the UI out of full screen mode. See
15456 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015457 *
15458 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15459 * content}
15460 *
15461 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15462 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15463 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15464 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015465 */
15466 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015467 if (visibility != mSystemUiVisibility) {
15468 mSystemUiVisibility = visibility;
15469 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15470 mParent.recomputeViewAttributes(this);
15471 }
Joe Onorato664644d2011-01-23 17:53:23 -080015472 }
15473 }
15474
15475 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015476 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15477 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15478 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15479 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15480 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015481 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015482 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015483 return mSystemUiVisibility;
15484 }
15485
Scott Mainec6331b2011-05-24 16:55:56 -070015486 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015487 * Returns the current system UI visibility that is currently set for
15488 * the entire window. This is the combination of the
15489 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15490 * views in the window.
15491 */
15492 public int getWindowSystemUiVisibility() {
15493 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15494 }
15495
15496 /**
15497 * Override to find out when the window's requested system UI visibility
15498 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15499 * This is different from the callbacks recieved through
15500 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15501 * in that this is only telling you about the local request of the window,
15502 * not the actual values applied by the system.
15503 */
15504 public void onWindowSystemUiVisibilityChanged(int visible) {
15505 }
15506
15507 /**
15508 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15509 * the view hierarchy.
15510 */
15511 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15512 onWindowSystemUiVisibilityChanged(visible);
15513 }
15514
15515 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015516 * Set a listener to receive callbacks when the visibility of the system bar changes.
15517 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15518 */
Joe Onorato664644d2011-01-23 17:53:23 -080015519 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015520 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015521 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15522 mParent.recomputeViewAttributes(this);
15523 }
15524 }
15525
15526 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015527 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15528 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015529 */
15530 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015531 ListenerInfo li = mListenerInfo;
15532 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15533 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015534 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015535 }
15536 }
15537
Dianne Hackborncf675782012-05-10 15:07:24 -070015538 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015539 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15540 if (val != mSystemUiVisibility) {
15541 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015542 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015543 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015544 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015545 }
15546
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015547 /** @hide */
15548 public void setDisabledSystemUiVisibility(int flags) {
15549 if (mAttachInfo != null) {
15550 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15551 mAttachInfo.mDisabledSystemUiVisibility = flags;
15552 if (mParent != null) {
15553 mParent.recomputeViewAttributes(this);
15554 }
15555 }
15556 }
15557 }
15558
Joe Onorato664644d2011-01-23 17:53:23 -080015559 /**
Joe Malin32736f02011-01-19 16:14:20 -080015560 * Creates an image that the system displays during the drag and drop
15561 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15562 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15563 * appearance as the given View. The default also positions the center of the drag shadow
15564 * directly under the touch point. If no View is provided (the constructor with no parameters
15565 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15566 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15567 * default is an invisible drag shadow.
15568 * <p>
15569 * You are not required to use the View you provide to the constructor as the basis of the
15570 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15571 * anything you want as the drag shadow.
15572 * </p>
15573 * <p>
15574 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15575 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15576 * size and position of the drag shadow. It uses this data to construct a
15577 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15578 * so that your application can draw the shadow image in the Canvas.
15579 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015580 *
15581 * <div class="special reference">
15582 * <h3>Developer Guides</h3>
15583 * <p>For a guide to implementing drag and drop features, read the
15584 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15585 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015586 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015587 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015588 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015589
15590 /**
Joe Malin32736f02011-01-19 16:14:20 -080015591 * Constructs a shadow image builder based on a View. By default, the resulting drag
15592 * shadow will have the same appearance and dimensions as the View, with the touch point
15593 * over the center of the View.
15594 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015595 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015596 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015597 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015598 }
15599
Christopher Tate17ed60c2011-01-18 12:50:26 -080015600 /**
15601 * Construct a shadow builder object with no associated View. This
15602 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15603 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15604 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015605 * reference to any View object. If they are not overridden, then the result is an
15606 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015607 */
15608 public DragShadowBuilder() {
15609 mView = new WeakReference<View>(null);
15610 }
15611
15612 /**
15613 * Returns the View object that had been passed to the
15614 * {@link #View.DragShadowBuilder(View)}
15615 * constructor. If that View parameter was {@code null} or if the
15616 * {@link #View.DragShadowBuilder()}
15617 * constructor was used to instantiate the builder object, this method will return
15618 * null.
15619 *
15620 * @return The View object associate with this builder object.
15621 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015622 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015623 final public View getView() {
15624 return mView.get();
15625 }
15626
Christopher Tate2c095f32010-10-04 14:13:40 -070015627 /**
Joe Malin32736f02011-01-19 16:14:20 -080015628 * Provides the metrics for the shadow image. These include the dimensions of
15629 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015630 * be centered under the touch location while dragging.
15631 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015632 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015633 * same as the dimensions of the View itself and centers the shadow under
15634 * the touch point.
15635 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015636 *
Joe Malin32736f02011-01-19 16:14:20 -080015637 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15638 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15639 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15640 * image.
15641 *
15642 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15643 * shadow image that should be underneath the touch point during the drag and drop
15644 * operation. Your application must set {@link android.graphics.Point#x} to the
15645 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015646 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015647 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015648 final View view = mView.get();
15649 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015650 shadowSize.set(view.getWidth(), view.getHeight());
15651 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015652 } else {
15653 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15654 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015655 }
15656
15657 /**
Joe Malin32736f02011-01-19 16:14:20 -080015658 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15659 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015660 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015661 *
Joe Malin32736f02011-01-19 16:14:20 -080015662 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015663 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015664 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015665 final View view = mView.get();
15666 if (view != null) {
15667 view.draw(canvas);
15668 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015669 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015670 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015671 }
15672 }
15673
15674 /**
Joe Malin32736f02011-01-19 16:14:20 -080015675 * Starts a drag and drop operation. When your application calls this method, it passes a
15676 * {@link android.view.View.DragShadowBuilder} object to the system. The
15677 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15678 * to get metrics for the drag shadow, and then calls the object's
15679 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15680 * <p>
15681 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15682 * drag events to all the View objects in your application that are currently visible. It does
15683 * this either by calling the View object's drag listener (an implementation of
15684 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15685 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15686 * Both are passed a {@link android.view.DragEvent} object that has a
15687 * {@link android.view.DragEvent#getAction()} value of
15688 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15689 * </p>
15690 * <p>
15691 * Your application can invoke startDrag() on any attached View object. The View object does not
15692 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15693 * be related to the View the user selected for dragging.
15694 * </p>
15695 * @param data A {@link android.content.ClipData} object pointing to the data to be
15696 * transferred by the drag and drop operation.
15697 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15698 * drag shadow.
15699 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15700 * drop operation. This Object is put into every DragEvent object sent by the system during the
15701 * current drag.
15702 * <p>
15703 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15704 * to the target Views. For example, it can contain flags that differentiate between a
15705 * a copy operation and a move operation.
15706 * </p>
15707 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15708 * so the parameter should be set to 0.
15709 * @return {@code true} if the method completes successfully, or
15710 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15711 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015712 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015713 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015714 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015715 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015716 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015717 }
15718 boolean okay = false;
15719
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015720 Point shadowSize = new Point();
15721 Point shadowTouchPoint = new Point();
15722 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015723
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015724 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15725 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15726 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015727 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015728
Chris Tatea32dcf72010-10-14 12:13:50 -070015729 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015730 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15731 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015732 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015733 Surface surface = new Surface();
15734 try {
15735 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015736 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015737 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015738 + " surface=" + surface);
15739 if (token != null) {
15740 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015741 try {
Chris Tate6b391282010-10-14 15:48:59 -070015742 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015743 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015744 } finally {
15745 surface.unlockCanvasAndPost(canvas);
15746 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015747
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015748 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015749
15750 // Cache the local state object for delivery with DragEvents
15751 root.setLocalDragState(myLocalState);
15752
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015753 // repurpose 'shadowSize' for the last touch point
15754 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015755
Christopher Tatea53146c2010-09-07 11:57:52 -070015756 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015757 shadowSize.x, shadowSize.y,
15758 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015759 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015760
15761 // Off and running! Release our local surface instance; the drag
15762 // shadow surface is now managed by the system process.
15763 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015764 }
15765 } catch (Exception e) {
15766 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15767 surface.destroy();
15768 }
15769
15770 return okay;
15771 }
15772
Christopher Tatea53146c2010-09-07 11:57:52 -070015773 /**
Joe Malin32736f02011-01-19 16:14:20 -080015774 * Handles drag events sent by the system following a call to
15775 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15776 *<p>
15777 * When the system calls this method, it passes a
15778 * {@link android.view.DragEvent} object. A call to
15779 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15780 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15781 * operation.
15782 * @param event The {@link android.view.DragEvent} sent by the system.
15783 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15784 * in DragEvent, indicating the type of drag event represented by this object.
15785 * @return {@code true} if the method was successful, otherwise {@code false}.
15786 * <p>
15787 * The method should return {@code true} in response to an action type of
15788 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15789 * operation.
15790 * </p>
15791 * <p>
15792 * The method should also return {@code true} in response to an action type of
15793 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15794 * {@code false} if it didn't.
15795 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015796 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015797 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015798 return false;
15799 }
15800
15801 /**
Joe Malin32736f02011-01-19 16:14:20 -080015802 * Detects if this View is enabled and has a drag event listener.
15803 * If both are true, then it calls the drag event listener with the
15804 * {@link android.view.DragEvent} it received. If the drag event listener returns
15805 * {@code true}, then dispatchDragEvent() returns {@code true}.
15806 * <p>
15807 * For all other cases, the method calls the
15808 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15809 * method and returns its result.
15810 * </p>
15811 * <p>
15812 * This ensures that a drag event is always consumed, even if the View does not have a drag
15813 * event listener. However, if the View has a listener and the listener returns true, then
15814 * onDragEvent() is not called.
15815 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015816 */
15817 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015818 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015819 ListenerInfo li = mListenerInfo;
15820 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15821 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015822 return true;
15823 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015824 return onDragEvent(event);
15825 }
15826
Christopher Tate3d4bf172011-03-28 16:16:46 -070015827 boolean canAcceptDrag() {
15828 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15829 }
15830
Christopher Tatea53146c2010-09-07 11:57:52 -070015831 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015832 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15833 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015834 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015835 */
15836 public void onCloseSystemDialogs(String reason) {
15837 }
Joe Malin32736f02011-01-19 16:14:20 -080015838
Dianne Hackbornffa42482009-09-23 22:20:11 -070015839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015840 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015841 * update a Region being computed for
15842 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015843 * that any non-transparent parts of the Drawable are removed from the
15844 * given transparent region.
15845 *
15846 * @param dr The Drawable whose transparency is to be applied to the region.
15847 * @param region A Region holding the current transparency information,
15848 * where any parts of the region that are set are considered to be
15849 * transparent. On return, this region will be modified to have the
15850 * transparency information reduced by the corresponding parts of the
15851 * Drawable that are not transparent.
15852 * {@hide}
15853 */
15854 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15855 if (DBG) {
15856 Log.i("View", "Getting transparent region for: " + this);
15857 }
15858 final Region r = dr.getTransparentRegion();
15859 final Rect db = dr.getBounds();
15860 final AttachInfo attachInfo = mAttachInfo;
15861 if (r != null && attachInfo != null) {
15862 final int w = getRight()-getLeft();
15863 final int h = getBottom()-getTop();
15864 if (db.left > 0) {
15865 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15866 r.op(0, 0, db.left, h, Region.Op.UNION);
15867 }
15868 if (db.right < w) {
15869 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15870 r.op(db.right, 0, w, h, Region.Op.UNION);
15871 }
15872 if (db.top > 0) {
15873 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15874 r.op(0, 0, w, db.top, Region.Op.UNION);
15875 }
15876 if (db.bottom < h) {
15877 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15878 r.op(0, db.bottom, w, h, Region.Op.UNION);
15879 }
15880 final int[] location = attachInfo.mTransparentLocation;
15881 getLocationInWindow(location);
15882 r.translate(location[0], location[1]);
15883 region.op(r, Region.Op.INTERSECT);
15884 } else {
15885 region.op(db, Region.Op.DIFFERENCE);
15886 }
15887 }
15888
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015889 private void checkForLongClick(int delayOffset) {
15890 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15891 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015892
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015893 if (mPendingCheckForLongPress == null) {
15894 mPendingCheckForLongPress = new CheckForLongPress();
15895 }
15896 mPendingCheckForLongPress.rememberWindowAttachCount();
15897 postDelayed(mPendingCheckForLongPress,
15898 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015900 }
15901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015902 /**
15903 * Inflate a view from an XML resource. This convenience method wraps the {@link
15904 * LayoutInflater} class, which provides a full range of options for view inflation.
15905 *
15906 * @param context The Context object for your activity or application.
15907 * @param resource The resource ID to inflate
15908 * @param root A view group that will be the parent. Used to properly inflate the
15909 * layout_* parameters.
15910 * @see LayoutInflater
15911 */
15912 public static View inflate(Context context, int resource, ViewGroup root) {
15913 LayoutInflater factory = LayoutInflater.from(context);
15914 return factory.inflate(resource, root);
15915 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015917 /**
Adam Powell637d3372010-08-25 14:37:03 -070015918 * Scroll the view with standard behavior for scrolling beyond the normal
15919 * content boundaries. Views that call this method should override
15920 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15921 * results of an over-scroll operation.
15922 *
15923 * Views can use this method to handle any touch or fling-based scrolling.
15924 *
15925 * @param deltaX Change in X in pixels
15926 * @param deltaY Change in Y in pixels
15927 * @param scrollX Current X scroll value in pixels before applying deltaX
15928 * @param scrollY Current Y scroll value in pixels before applying deltaY
15929 * @param scrollRangeX Maximum content scroll range along the X axis
15930 * @param scrollRangeY Maximum content scroll range along the Y axis
15931 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15932 * along the X axis.
15933 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15934 * along the Y axis.
15935 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15936 * @return true if scrolling was clamped to an over-scroll boundary along either
15937 * axis, false otherwise.
15938 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015939 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015940 protected boolean overScrollBy(int deltaX, int deltaY,
15941 int scrollX, int scrollY,
15942 int scrollRangeX, int scrollRangeY,
15943 int maxOverScrollX, int maxOverScrollY,
15944 boolean isTouchEvent) {
15945 final int overScrollMode = mOverScrollMode;
15946 final boolean canScrollHorizontal =
15947 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15948 final boolean canScrollVertical =
15949 computeVerticalScrollRange() > computeVerticalScrollExtent();
15950 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15951 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15952 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15953 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15954
15955 int newScrollX = scrollX + deltaX;
15956 if (!overScrollHorizontal) {
15957 maxOverScrollX = 0;
15958 }
15959
15960 int newScrollY = scrollY + deltaY;
15961 if (!overScrollVertical) {
15962 maxOverScrollY = 0;
15963 }
15964
15965 // Clamp values if at the limits and record
15966 final int left = -maxOverScrollX;
15967 final int right = maxOverScrollX + scrollRangeX;
15968 final int top = -maxOverScrollY;
15969 final int bottom = maxOverScrollY + scrollRangeY;
15970
15971 boolean clampedX = false;
15972 if (newScrollX > right) {
15973 newScrollX = right;
15974 clampedX = true;
15975 } else if (newScrollX < left) {
15976 newScrollX = left;
15977 clampedX = true;
15978 }
15979
15980 boolean clampedY = false;
15981 if (newScrollY > bottom) {
15982 newScrollY = bottom;
15983 clampedY = true;
15984 } else if (newScrollY < top) {
15985 newScrollY = top;
15986 clampedY = true;
15987 }
15988
15989 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15990
15991 return clampedX || clampedY;
15992 }
15993
15994 /**
15995 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15996 * respond to the results of an over-scroll operation.
15997 *
15998 * @param scrollX New X scroll value in pixels
15999 * @param scrollY New Y scroll value in pixels
16000 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16001 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16002 */
16003 protected void onOverScrolled(int scrollX, int scrollY,
16004 boolean clampedX, boolean clampedY) {
16005 // Intentionally empty.
16006 }
16007
16008 /**
16009 * Returns the over-scroll mode for this view. The result will be
16010 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16011 * (allow over-scrolling only if the view content is larger than the container),
16012 * or {@link #OVER_SCROLL_NEVER}.
16013 *
16014 * @return This view's over-scroll mode.
16015 */
16016 public int getOverScrollMode() {
16017 return mOverScrollMode;
16018 }
16019
16020 /**
16021 * Set the over-scroll mode for this view. Valid over-scroll modes are
16022 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16023 * (allow over-scrolling only if the view content is larger than the container),
16024 * or {@link #OVER_SCROLL_NEVER}.
16025 *
16026 * Setting the over-scroll mode of a view will have an effect only if the
16027 * view is capable of scrolling.
16028 *
16029 * @param overScrollMode The new over-scroll mode for this view.
16030 */
16031 public void setOverScrollMode(int overScrollMode) {
16032 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16033 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16034 overScrollMode != OVER_SCROLL_NEVER) {
16035 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16036 }
16037 mOverScrollMode = overScrollMode;
16038 }
16039
16040 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016041 * Gets a scale factor that determines the distance the view should scroll
16042 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16043 * @return The vertical scroll scale factor.
16044 * @hide
16045 */
16046 protected float getVerticalScrollFactor() {
16047 if (mVerticalScrollFactor == 0) {
16048 TypedValue outValue = new TypedValue();
16049 if (!mContext.getTheme().resolveAttribute(
16050 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16051 throw new IllegalStateException(
16052 "Expected theme to define listPreferredItemHeight.");
16053 }
16054 mVerticalScrollFactor = outValue.getDimension(
16055 mContext.getResources().getDisplayMetrics());
16056 }
16057 return mVerticalScrollFactor;
16058 }
16059
16060 /**
16061 * Gets a scale factor that determines the distance the view should scroll
16062 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16063 * @return The horizontal scroll scale factor.
16064 * @hide
16065 */
16066 protected float getHorizontalScrollFactor() {
16067 // TODO: Should use something else.
16068 return getVerticalScrollFactor();
16069 }
16070
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016071 /**
16072 * Return the value specifying the text direction or policy that was set with
16073 * {@link #setTextDirection(int)}.
16074 *
16075 * @return the defined text direction. It can be one of:
16076 *
16077 * {@link #TEXT_DIRECTION_INHERIT},
16078 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16079 * {@link #TEXT_DIRECTION_ANY_RTL},
16080 * {@link #TEXT_DIRECTION_LTR},
16081 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016082 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016083 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016084 @ViewDebug.ExportedProperty(category = "text", mapping = {
16085 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16086 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16087 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16088 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16089 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16090 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16091 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016092 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016093 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016094 }
16095
16096 /**
16097 * Set the text direction.
16098 *
16099 * @param textDirection the direction to set. Should be one of:
16100 *
16101 * {@link #TEXT_DIRECTION_INHERIT},
16102 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16103 * {@link #TEXT_DIRECTION_ANY_RTL},
16104 * {@link #TEXT_DIRECTION_LTR},
16105 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016106 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016107 */
16108 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016109 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016110 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016111 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016112 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016113 // Set the new text direction
16114 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016115 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016116 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016117 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016118 }
16119 }
16120
16121 /**
16122 * Return the resolved text direction.
16123 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016124 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16125 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16126 * up the parent chain of the view. if there is no parent, then it will return the default
16127 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16128 *
16129 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016130 *
Doug Feltcb3791202011-07-07 11:57:48 -070016131 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16132 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016133 * {@link #TEXT_DIRECTION_LTR},
16134 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016135 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016136 */
16137 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016138 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016139 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016140 resolveTextDirection();
16141 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016142 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016143 }
16144
16145 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016146 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16147 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016148 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016149 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016150 // Reset any previous text direction resolution
16151 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16152
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016153 if (hasRtlSupport()) {
16154 // Set resolved text direction flag depending on text direction flag
16155 final int textDirection = getTextDirection();
16156 switch(textDirection) {
16157 case TEXT_DIRECTION_INHERIT:
16158 if (canResolveTextDirection()) {
16159 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016160
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016161 // Set current resolved direction to the same value as the parent's one
16162 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16163 switch (parentResolvedDirection) {
16164 case TEXT_DIRECTION_FIRST_STRONG:
16165 case TEXT_DIRECTION_ANY_RTL:
16166 case TEXT_DIRECTION_LTR:
16167 case TEXT_DIRECTION_RTL:
16168 case TEXT_DIRECTION_LOCALE:
16169 mPrivateFlags2 |=
16170 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16171 break;
16172 default:
16173 // Default resolved direction is "first strong" heuristic
16174 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16175 }
16176 } else {
16177 // We cannot do the resolution if there is no parent, so use the default one
16178 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016179 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016180 break;
16181 case TEXT_DIRECTION_FIRST_STRONG:
16182 case TEXT_DIRECTION_ANY_RTL:
16183 case TEXT_DIRECTION_LTR:
16184 case TEXT_DIRECTION_RTL:
16185 case TEXT_DIRECTION_LOCALE:
16186 // Resolved direction is the same as text direction
16187 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16188 break;
16189 default:
16190 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016191 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016192 }
16193 } else {
16194 // Default resolved direction is "first strong" heuristic
16195 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016196 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016197
16198 // Set to resolved
16199 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016200 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016201 }
16202
16203 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016204 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016205 * resolution should override this method.
16206 *
16207 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016208 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016209 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016210 }
16211
16212 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016213 * Check if text direction resolution can be done.
16214 *
16215 * @return true if text direction resolution can be done otherwise return false.
16216 */
16217 public boolean canResolveTextDirection() {
16218 switch (getTextDirection()) {
16219 case TEXT_DIRECTION_INHERIT:
16220 return (mParent != null) && (mParent instanceof ViewGroup);
16221 default:
16222 return true;
16223 }
16224 }
16225
16226 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016227 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016228 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016229 * reset is done.
16230 */
16231 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016232 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016233 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016234 }
16235
16236 /**
16237 * Called when text direction is reset. Subclasses that care about text direction reset should
16238 * override this method and do a reset of the text direction of their children. The default
16239 * implementation does nothing.
16240 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016241 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016242 }
16243
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016244 /**
16245 * Return the value specifying the text alignment or policy that was set with
16246 * {@link #setTextAlignment(int)}.
16247 *
16248 * @return the defined text alignment. It can be one of:
16249 *
16250 * {@link #TEXT_ALIGNMENT_INHERIT},
16251 * {@link #TEXT_ALIGNMENT_GRAVITY},
16252 * {@link #TEXT_ALIGNMENT_CENTER},
16253 * {@link #TEXT_ALIGNMENT_TEXT_START},
16254 * {@link #TEXT_ALIGNMENT_TEXT_END},
16255 * {@link #TEXT_ALIGNMENT_VIEW_START},
16256 * {@link #TEXT_ALIGNMENT_VIEW_END}
16257 */
16258 @ViewDebug.ExportedProperty(category = "text", mapping = {
16259 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16260 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16261 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16262 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16263 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16264 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16265 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16266 })
16267 public int getTextAlignment() {
16268 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16269 }
16270
16271 /**
16272 * Set the text alignment.
16273 *
16274 * @param textAlignment The text alignment to set. Should be one of
16275 *
16276 * {@link #TEXT_ALIGNMENT_INHERIT},
16277 * {@link #TEXT_ALIGNMENT_GRAVITY},
16278 * {@link #TEXT_ALIGNMENT_CENTER},
16279 * {@link #TEXT_ALIGNMENT_TEXT_START},
16280 * {@link #TEXT_ALIGNMENT_TEXT_END},
16281 * {@link #TEXT_ALIGNMENT_VIEW_START},
16282 * {@link #TEXT_ALIGNMENT_VIEW_END}
16283 *
16284 * @attr ref android.R.styleable#View_textAlignment
16285 */
16286 public void setTextAlignment(int textAlignment) {
16287 if (textAlignment != getTextAlignment()) {
16288 // Reset the current and resolved text alignment
16289 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16290 resetResolvedTextAlignment();
16291 // Set the new text alignment
16292 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16293 // Refresh
16294 requestLayout();
16295 invalidate(true);
16296 }
16297 }
16298
16299 /**
16300 * Return the resolved text alignment.
16301 *
16302 * The resolved text alignment. This needs resolution if the value is
16303 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16304 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16305 *
16306 * @return the resolved text alignment. Returns one of:
16307 *
16308 * {@link #TEXT_ALIGNMENT_GRAVITY},
16309 * {@link #TEXT_ALIGNMENT_CENTER},
16310 * {@link #TEXT_ALIGNMENT_TEXT_START},
16311 * {@link #TEXT_ALIGNMENT_TEXT_END},
16312 * {@link #TEXT_ALIGNMENT_VIEW_START},
16313 * {@link #TEXT_ALIGNMENT_VIEW_END}
16314 */
16315 @ViewDebug.ExportedProperty(category = "text", mapping = {
16316 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16317 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16318 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16319 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16320 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16321 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16322 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16323 })
16324 public int getResolvedTextAlignment() {
16325 // If text alignment is not resolved, then resolve it
16326 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16327 resolveTextAlignment();
16328 }
16329 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16330 }
16331
16332 /**
16333 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16334 * resolution is done.
16335 */
16336 public void resolveTextAlignment() {
16337 // Reset any previous text alignment resolution
16338 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16339
16340 if (hasRtlSupport()) {
16341 // Set resolved text alignment flag depending on text alignment flag
16342 final int textAlignment = getTextAlignment();
16343 switch (textAlignment) {
16344 case TEXT_ALIGNMENT_INHERIT:
16345 // Check if we can resolve the text alignment
16346 if (canResolveLayoutDirection() && mParent instanceof View) {
16347 View view = (View) mParent;
16348
16349 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16350 switch (parentResolvedTextAlignment) {
16351 case TEXT_ALIGNMENT_GRAVITY:
16352 case TEXT_ALIGNMENT_TEXT_START:
16353 case TEXT_ALIGNMENT_TEXT_END:
16354 case TEXT_ALIGNMENT_CENTER:
16355 case TEXT_ALIGNMENT_VIEW_START:
16356 case TEXT_ALIGNMENT_VIEW_END:
16357 // Resolved text alignment is the same as the parent resolved
16358 // text alignment
16359 mPrivateFlags2 |=
16360 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16361 break;
16362 default:
16363 // Use default resolved text alignment
16364 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16365 }
16366 }
16367 else {
16368 // We cannot do the resolution if there is no parent so use the default
16369 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16370 }
16371 break;
16372 case TEXT_ALIGNMENT_GRAVITY:
16373 case TEXT_ALIGNMENT_TEXT_START:
16374 case TEXT_ALIGNMENT_TEXT_END:
16375 case TEXT_ALIGNMENT_CENTER:
16376 case TEXT_ALIGNMENT_VIEW_START:
16377 case TEXT_ALIGNMENT_VIEW_END:
16378 // Resolved text alignment is the same as text alignment
16379 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16380 break;
16381 default:
16382 // Use default resolved text alignment
16383 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16384 }
16385 } else {
16386 // Use default resolved text alignment
16387 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16388 }
16389
16390 // Set the resolved
16391 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16392 onResolvedTextAlignmentChanged();
16393 }
16394
16395 /**
16396 * Check if text alignment resolution can be done.
16397 *
16398 * @return true if text alignment resolution can be done otherwise return false.
16399 */
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.
16414 */
16415 public void onResolvedTextAlignmentChanged() {
16416 }
16417
16418 /**
16419 * Reset resolved text alignment. Text alignment can be resolved with a call to
16420 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16421 * reset is done.
16422 */
16423 public void resetResolvedTextAlignment() {
16424 // Reset any previous text alignment resolution
16425 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16426 onResolvedTextAlignmentReset();
16427 }
16428
16429 /**
16430 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16431 * override this method and do a reset of the text alignment of their children. The default
16432 * implementation does nothing.
16433 */
16434 public void onResolvedTextAlignmentReset() {
16435 }
16436
Chet Haaseb39f0512011-05-24 14:36:40 -070016437 //
16438 // Properties
16439 //
16440 /**
16441 * A Property wrapper around the <code>alpha</code> functionality handled by the
16442 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16443 */
Chet Haased47f1532011-12-16 11:18:52 -080016444 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016445 @Override
16446 public void setValue(View object, float value) {
16447 object.setAlpha(value);
16448 }
16449
16450 @Override
16451 public Float get(View object) {
16452 return object.getAlpha();
16453 }
16454 };
16455
16456 /**
16457 * A Property wrapper around the <code>translationX</code> functionality handled by the
16458 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16459 */
Chet Haased47f1532011-12-16 11:18:52 -080016460 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016461 @Override
16462 public void setValue(View object, float value) {
16463 object.setTranslationX(value);
16464 }
16465
16466 @Override
16467 public Float get(View object) {
16468 return object.getTranslationX();
16469 }
16470 };
16471
16472 /**
16473 * A Property wrapper around the <code>translationY</code> functionality handled by the
16474 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16475 */
Chet Haased47f1532011-12-16 11:18:52 -080016476 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016477 @Override
16478 public void setValue(View object, float value) {
16479 object.setTranslationY(value);
16480 }
16481
16482 @Override
16483 public Float get(View object) {
16484 return object.getTranslationY();
16485 }
16486 };
16487
16488 /**
16489 * A Property wrapper around the <code>x</code> functionality handled by the
16490 * {@link View#setX(float)} and {@link View#getX()} methods.
16491 */
Chet Haased47f1532011-12-16 11:18:52 -080016492 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016493 @Override
16494 public void setValue(View object, float value) {
16495 object.setX(value);
16496 }
16497
16498 @Override
16499 public Float get(View object) {
16500 return object.getX();
16501 }
16502 };
16503
16504 /**
16505 * A Property wrapper around the <code>y</code> functionality handled by the
16506 * {@link View#setY(float)} and {@link View#getY()} methods.
16507 */
Chet Haased47f1532011-12-16 11:18:52 -080016508 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016509 @Override
16510 public void setValue(View object, float value) {
16511 object.setY(value);
16512 }
16513
16514 @Override
16515 public Float get(View object) {
16516 return object.getY();
16517 }
16518 };
16519
16520 /**
16521 * A Property wrapper around the <code>rotation</code> functionality handled by the
16522 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16523 */
Chet Haased47f1532011-12-16 11:18:52 -080016524 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016525 @Override
16526 public void setValue(View object, float value) {
16527 object.setRotation(value);
16528 }
16529
16530 @Override
16531 public Float get(View object) {
16532 return object.getRotation();
16533 }
16534 };
16535
16536 /**
16537 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16538 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16539 */
Chet Haased47f1532011-12-16 11:18:52 -080016540 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016541 @Override
16542 public void setValue(View object, float value) {
16543 object.setRotationX(value);
16544 }
16545
16546 @Override
16547 public Float get(View object) {
16548 return object.getRotationX();
16549 }
16550 };
16551
16552 /**
16553 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16554 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16555 */
Chet Haased47f1532011-12-16 11:18:52 -080016556 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016557 @Override
16558 public void setValue(View object, float value) {
16559 object.setRotationY(value);
16560 }
16561
16562 @Override
16563 public Float get(View object) {
16564 return object.getRotationY();
16565 }
16566 };
16567
16568 /**
16569 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16570 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16571 */
Chet Haased47f1532011-12-16 11:18:52 -080016572 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016573 @Override
16574 public void setValue(View object, float value) {
16575 object.setScaleX(value);
16576 }
16577
16578 @Override
16579 public Float get(View object) {
16580 return object.getScaleX();
16581 }
16582 };
16583
16584 /**
16585 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16586 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16587 */
Chet Haased47f1532011-12-16 11:18:52 -080016588 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016589 @Override
16590 public void setValue(View object, float value) {
16591 object.setScaleY(value);
16592 }
16593
16594 @Override
16595 public Float get(View object) {
16596 return object.getScaleY();
16597 }
16598 };
16599
Jeff Brown33bbfd22011-02-24 20:55:35 -080016600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016601 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16602 * Each MeasureSpec represents a requirement for either the width or the height.
16603 * A MeasureSpec is comprised of a size and a mode. There are three possible
16604 * modes:
16605 * <dl>
16606 * <dt>UNSPECIFIED</dt>
16607 * <dd>
16608 * The parent has not imposed any constraint on the child. It can be whatever size
16609 * it wants.
16610 * </dd>
16611 *
16612 * <dt>EXACTLY</dt>
16613 * <dd>
16614 * The parent has determined an exact size for the child. The child is going to be
16615 * given those bounds regardless of how big it wants to be.
16616 * </dd>
16617 *
16618 * <dt>AT_MOST</dt>
16619 * <dd>
16620 * The child can be as large as it wants up to the specified size.
16621 * </dd>
16622 * </dl>
16623 *
16624 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16625 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16626 */
16627 public static class MeasureSpec {
16628 private static final int MODE_SHIFT = 30;
16629 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16630
16631 /**
16632 * Measure specification mode: The parent has not imposed any constraint
16633 * on the child. It can be whatever size it wants.
16634 */
16635 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16636
16637 /**
16638 * Measure specification mode: The parent has determined an exact size
16639 * for the child. The child is going to be given those bounds regardless
16640 * of how big it wants to be.
16641 */
16642 public static final int EXACTLY = 1 << MODE_SHIFT;
16643
16644 /**
16645 * Measure specification mode: The child can be as large as it wants up
16646 * to the specified size.
16647 */
16648 public static final int AT_MOST = 2 << MODE_SHIFT;
16649
16650 /**
16651 * Creates a measure specification based on the supplied size and mode.
16652 *
16653 * The mode must always be one of the following:
16654 * <ul>
16655 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16656 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16657 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16658 * </ul>
16659 *
16660 * @param size the size of the measure specification
16661 * @param mode the mode of the measure specification
16662 * @return the measure specification based on size and mode
16663 */
16664 public static int makeMeasureSpec(int size, int mode) {
16665 return size + mode;
16666 }
16667
16668 /**
16669 * Extracts the mode from the supplied measure specification.
16670 *
16671 * @param measureSpec the measure specification to extract the mode from
16672 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16673 * {@link android.view.View.MeasureSpec#AT_MOST} or
16674 * {@link android.view.View.MeasureSpec#EXACTLY}
16675 */
16676 public static int getMode(int measureSpec) {
16677 return (measureSpec & MODE_MASK);
16678 }
16679
16680 /**
16681 * Extracts the size from the supplied measure specification.
16682 *
16683 * @param measureSpec the measure specification to extract the size from
16684 * @return the size in pixels defined in the supplied measure specification
16685 */
16686 public static int getSize(int measureSpec) {
16687 return (measureSpec & ~MODE_MASK);
16688 }
16689
16690 /**
16691 * Returns a String representation of the specified measure
16692 * specification.
16693 *
16694 * @param measureSpec the measure specification to convert to a String
16695 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16696 */
16697 public static String toString(int measureSpec) {
16698 int mode = getMode(measureSpec);
16699 int size = getSize(measureSpec);
16700
16701 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16702
16703 if (mode == UNSPECIFIED)
16704 sb.append("UNSPECIFIED ");
16705 else if (mode == EXACTLY)
16706 sb.append("EXACTLY ");
16707 else if (mode == AT_MOST)
16708 sb.append("AT_MOST ");
16709 else
16710 sb.append(mode).append(" ");
16711
16712 sb.append(size);
16713 return sb.toString();
16714 }
16715 }
16716
16717 class CheckForLongPress implements Runnable {
16718
16719 private int mOriginalWindowAttachCount;
16720
16721 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016722 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016723 && mOriginalWindowAttachCount == mWindowAttachCount) {
16724 if (performLongClick()) {
16725 mHasPerformedLongPress = true;
16726 }
16727 }
16728 }
16729
16730 public void rememberWindowAttachCount() {
16731 mOriginalWindowAttachCount = mWindowAttachCount;
16732 }
16733 }
Joe Malin32736f02011-01-19 16:14:20 -080016734
Adam Powelle14579b2009-12-16 18:39:52 -080016735 private final class CheckForTap implements Runnable {
16736 public void run() {
16737 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016738 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016739 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016740 }
16741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016742
Adam Powella35d7682010-03-12 14:48:13 -080016743 private final class PerformClick implements Runnable {
16744 public void run() {
16745 performClick();
16746 }
16747 }
16748
Dianne Hackborn63042d62011-01-26 18:56:29 -080016749 /** @hide */
16750 public void hackTurnOffWindowResizeAnim(boolean off) {
16751 mAttachInfo.mTurnOffWindowResizeAnim = off;
16752 }
Joe Malin32736f02011-01-19 16:14:20 -080016753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016754 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016755 * This method returns a ViewPropertyAnimator object, which can be used to animate
16756 * specific properties on this View.
16757 *
16758 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16759 */
16760 public ViewPropertyAnimator animate() {
16761 if (mAnimator == null) {
16762 mAnimator = new ViewPropertyAnimator(this);
16763 }
16764 return mAnimator;
16765 }
16766
16767 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016768 * Interface definition for a callback to be invoked when a hardware key event is
16769 * dispatched to this view. The callback will be invoked before the key event is
16770 * given to the view. This is only useful for hardware keyboards; a software input
16771 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016772 */
16773 public interface OnKeyListener {
16774 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016775 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016776 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016777 * <p>Key presses in software keyboards will generally NOT trigger this method,
16778 * although some may elect to do so in some situations. Do not assume a
16779 * software input method has to be key-based; even if it is, it may use key presses
16780 * in a different way than you expect, so there is no way to reliably catch soft
16781 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016782 *
16783 * @param v The view the key has been dispatched to.
16784 * @param keyCode The code for the physical key that was pressed
16785 * @param event The KeyEvent object containing full information about
16786 * the event.
16787 * @return True if the listener has consumed the event, false otherwise.
16788 */
16789 boolean onKey(View v, int keyCode, KeyEvent event);
16790 }
16791
16792 /**
16793 * Interface definition for a callback to be invoked when a touch event is
16794 * dispatched to this view. The callback will be invoked before the touch
16795 * event is given to the view.
16796 */
16797 public interface OnTouchListener {
16798 /**
16799 * Called when a touch event is dispatched to a view. This allows listeners to
16800 * get a chance to respond before the target view.
16801 *
16802 * @param v The view the touch event has been dispatched to.
16803 * @param event The MotionEvent object containing full information about
16804 * the event.
16805 * @return True if the listener has consumed the event, false otherwise.
16806 */
16807 boolean onTouch(View v, MotionEvent event);
16808 }
16809
16810 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016811 * Interface definition for a callback to be invoked when a hover event is
16812 * dispatched to this view. The callback will be invoked before the hover
16813 * event is given to the view.
16814 */
16815 public interface OnHoverListener {
16816 /**
16817 * Called when a hover event is dispatched to a view. This allows listeners to
16818 * get a chance to respond before the target view.
16819 *
16820 * @param v The view the hover event has been dispatched to.
16821 * @param event The MotionEvent object containing full information about
16822 * the event.
16823 * @return True if the listener has consumed the event, false otherwise.
16824 */
16825 boolean onHover(View v, MotionEvent event);
16826 }
16827
16828 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016829 * Interface definition for a callback to be invoked when a generic motion event is
16830 * dispatched to this view. The callback will be invoked before the generic motion
16831 * event is given to the view.
16832 */
16833 public interface OnGenericMotionListener {
16834 /**
16835 * Called when a generic motion event is dispatched to a view. This allows listeners to
16836 * get a chance to respond before the target view.
16837 *
16838 * @param v The view the generic motion event has been dispatched to.
16839 * @param event The MotionEvent object containing full information about
16840 * the event.
16841 * @return True if the listener has consumed the event, false otherwise.
16842 */
16843 boolean onGenericMotion(View v, MotionEvent event);
16844 }
16845
16846 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016847 * Interface definition for a callback to be invoked when a view has been clicked and held.
16848 */
16849 public interface OnLongClickListener {
16850 /**
16851 * Called when a view has been clicked and held.
16852 *
16853 * @param v The view that was clicked and held.
16854 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016855 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016856 */
16857 boolean onLongClick(View v);
16858 }
16859
16860 /**
Chris Tate32affef2010-10-18 15:29:21 -070016861 * Interface definition for a callback to be invoked when a drag is being dispatched
16862 * to this view. The callback will be invoked before the hosting view's own
16863 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16864 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016865 *
16866 * <div class="special reference">
16867 * <h3>Developer Guides</h3>
16868 * <p>For a guide to implementing drag and drop features, read the
16869 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16870 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016871 */
16872 public interface OnDragListener {
16873 /**
16874 * Called when a drag event is dispatched to a view. This allows listeners
16875 * to get a chance to override base View behavior.
16876 *
Joe Malin32736f02011-01-19 16:14:20 -080016877 * @param v The View that received the drag event.
16878 * @param event The {@link android.view.DragEvent} object for the drag event.
16879 * @return {@code true} if the drag event was handled successfully, or {@code false}
16880 * if the drag event was not handled. Note that {@code false} will trigger the View
16881 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016882 */
16883 boolean onDrag(View v, DragEvent event);
16884 }
16885
16886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016887 * Interface definition for a callback to be invoked when the focus state of
16888 * a view changed.
16889 */
16890 public interface OnFocusChangeListener {
16891 /**
16892 * Called when the focus state of a view has changed.
16893 *
16894 * @param v The view whose state has changed.
16895 * @param hasFocus The new focus state of v.
16896 */
16897 void onFocusChange(View v, boolean hasFocus);
16898 }
16899
16900 /**
16901 * Interface definition for a callback to be invoked when a view is clicked.
16902 */
16903 public interface OnClickListener {
16904 /**
16905 * Called when a view has been clicked.
16906 *
16907 * @param v The view that was clicked.
16908 */
16909 void onClick(View v);
16910 }
16911
16912 /**
16913 * Interface definition for a callback to be invoked when the context menu
16914 * for this view is being built.
16915 */
16916 public interface OnCreateContextMenuListener {
16917 /**
16918 * Called when the context menu for this view is being built. It is not
16919 * safe to hold onto the menu after this method returns.
16920 *
16921 * @param menu The context menu that is being built
16922 * @param v The view for which the context menu is being built
16923 * @param menuInfo Extra information about the item for which the
16924 * context menu should be shown. This information will vary
16925 * depending on the class of v.
16926 */
16927 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16928 }
16929
Joe Onorato664644d2011-01-23 17:53:23 -080016930 /**
16931 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016932 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070016933 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016934 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016935 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016936 */
16937 public interface OnSystemUiVisibilityChangeListener {
16938 /**
16939 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016940 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016941 *
Dianne Hackborncf675782012-05-10 15:07:24 -070016942 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16943 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
16944 * This tells you the <strong>global</strong> state of these UI visibility
16945 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016946 */
16947 public void onSystemUiVisibilityChange(int visibility);
16948 }
16949
Adam Powell4afd62b2011-02-18 15:02:18 -080016950 /**
16951 * Interface definition for a callback to be invoked when this view is attached
16952 * or detached from its window.
16953 */
16954 public interface OnAttachStateChangeListener {
16955 /**
16956 * Called when the view is attached to a window.
16957 * @param v The view that was attached
16958 */
16959 public void onViewAttachedToWindow(View v);
16960 /**
16961 * Called when the view is detached from a window.
16962 * @param v The view that was detached
16963 */
16964 public void onViewDetachedFromWindow(View v);
16965 }
16966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016967 private final class UnsetPressedState implements Runnable {
16968 public void run() {
16969 setPressed(false);
16970 }
16971 }
16972
16973 /**
16974 * Base class for derived classes that want to save and restore their own
16975 * state in {@link android.view.View#onSaveInstanceState()}.
16976 */
16977 public static class BaseSavedState extends AbsSavedState {
16978 /**
16979 * Constructor used when reading from a parcel. Reads the state of the superclass.
16980 *
16981 * @param source
16982 */
16983 public BaseSavedState(Parcel source) {
16984 super(source);
16985 }
16986
16987 /**
16988 * Constructor called by derived classes when creating their SavedState objects
16989 *
16990 * @param superState The state of the superclass of this view
16991 */
16992 public BaseSavedState(Parcelable superState) {
16993 super(superState);
16994 }
16995
16996 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16997 new Parcelable.Creator<BaseSavedState>() {
16998 public BaseSavedState createFromParcel(Parcel in) {
16999 return new BaseSavedState(in);
17000 }
17001
17002 public BaseSavedState[] newArray(int size) {
17003 return new BaseSavedState[size];
17004 }
17005 };
17006 }
17007
17008 /**
17009 * A set of information given to a view when it is attached to its parent
17010 * window.
17011 */
17012 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017013 interface Callbacks {
17014 void playSoundEffect(int effectId);
17015 boolean performHapticFeedback(int effectId, boolean always);
17016 }
17017
17018 /**
17019 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17020 * to a Handler. This class contains the target (View) to invalidate and
17021 * the coordinates of the dirty rectangle.
17022 *
17023 * For performance purposes, this class also implements a pool of up to
17024 * POOL_LIMIT objects that get reused. This reduces memory allocations
17025 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026 */
Romain Guyd928d682009-03-31 17:52:16 -070017027 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017028 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017029 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17030 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017031 public InvalidateInfo newInstance() {
17032 return new InvalidateInfo();
17033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017034
Romain Guyd928d682009-03-31 17:52:16 -070017035 public void onAcquired(InvalidateInfo element) {
17036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017037
Romain Guyd928d682009-03-31 17:52:16 -070017038 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017039 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017040 }
17041 }, POOL_LIMIT)
17042 );
17043
17044 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017045 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017046
17047 View target;
17048
17049 int left;
17050 int top;
17051 int right;
17052 int bottom;
17053
Romain Guyd928d682009-03-31 17:52:16 -070017054 public void setNextPoolable(InvalidateInfo element) {
17055 mNext = element;
17056 }
17057
17058 public InvalidateInfo getNextPoolable() {
17059 return mNext;
17060 }
17061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017062 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017063 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017064 }
17065
17066 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017067 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017068 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017069
17070 public boolean isPooled() {
17071 return mIsPooled;
17072 }
17073
17074 public void setPooled(boolean isPooled) {
17075 mIsPooled = isPooled;
17076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017077 }
17078
17079 final IWindowSession mSession;
17080
17081 final IWindow mWindow;
17082
17083 final IBinder mWindowToken;
17084
17085 final Callbacks mRootCallbacks;
17086
Romain Guy59a12ca2011-06-09 17:48:21 -070017087 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017089 /**
17090 * The top view of the hierarchy.
17091 */
17092 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017094 IBinder mPanelParentWindowToken;
17095 Surface mSurface;
17096
Romain Guyb051e892010-09-28 19:09:36 -070017097 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017098 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017099 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017100
Romain Guy7e4e5612012-03-05 14:37:29 -080017101 boolean mScreenOn;
17102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017103 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017104 * Scale factor used by the compatibility mode
17105 */
17106 float mApplicationScale;
17107
17108 /**
17109 * Indicates whether the application is in compatibility mode
17110 */
17111 boolean mScalingRequired;
17112
17113 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017114 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017115 */
17116 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017117
Dianne Hackborn63042d62011-01-26 18:56:29 -080017118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017119 * Left position of this view's window
17120 */
17121 int mWindowLeft;
17122
17123 /**
17124 * Top position of this view's window
17125 */
17126 int mWindowTop;
17127
17128 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -070017129 * Left actual position of this view's window.
17130 *
17131 * TODO: This is a workaround for 6623031. Remove when fixed.
17132 */
17133 int mActualWindowLeft;
17134
17135 /**
17136 * Actual top position of this view's window.
17137 *
17138 * TODO: This is a workaround for 6623031. Remove when fixed.
17139 */
17140 int mActualWindowTop;
17141
17142 /**
Adam Powell26153a32010-11-08 15:22:27 -080017143 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017144 */
Adam Powell26153a32010-11-08 15:22:27 -080017145 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017146
17147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017148 * For windows that are full-screen but using insets to layout inside
17149 * of the screen decorations, these are the current insets for the
17150 * content of the window.
17151 */
17152 final Rect mContentInsets = new Rect();
17153
17154 /**
17155 * For windows that are full-screen but using insets to layout inside
17156 * of the screen decorations, these are the current insets for the
17157 * actual visible parts of the window.
17158 */
17159 final Rect mVisibleInsets = new Rect();
17160
17161 /**
17162 * The internal insets given by this window. This value is
17163 * supplied by the client (through
17164 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17165 * be given to the window manager when changed to be used in laying
17166 * out windows behind it.
17167 */
17168 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17169 = new ViewTreeObserver.InternalInsetsInfo();
17170
17171 /**
17172 * All views in the window's hierarchy that serve as scroll containers,
17173 * used to determine if the window can be resized or must be panned
17174 * to adjust for a soft input area.
17175 */
17176 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17177
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017178 final KeyEvent.DispatcherState mKeyDispatchState
17179 = new KeyEvent.DispatcherState();
17180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017181 /**
17182 * Indicates whether the view's window currently has the focus.
17183 */
17184 boolean mHasWindowFocus;
17185
17186 /**
17187 * The current visibility of the window.
17188 */
17189 int mWindowVisibility;
17190
17191 /**
17192 * Indicates the time at which drawing started to occur.
17193 */
17194 long mDrawingTime;
17195
17196 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017197 * Indicates whether or not ignoring the DIRTY_MASK flags.
17198 */
17199 boolean mIgnoreDirtyState;
17200
17201 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017202 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17203 * to avoid clearing that flag prematurely.
17204 */
17205 boolean mSetIgnoreDirtyState = false;
17206
17207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017208 * Indicates whether the view's window is currently in touch mode.
17209 */
17210 boolean mInTouchMode;
17211
17212 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017213 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017214 * the next time it performs a traversal
17215 */
17216 boolean mRecomputeGlobalAttributes;
17217
17218 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017219 * Always report new attributes at next traversal.
17220 */
17221 boolean mForceReportNewAttributes;
17222
17223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017224 * Set during a traveral if any views want to keep the screen on.
17225 */
17226 boolean mKeepScreenOn;
17227
17228 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017229 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17230 */
17231 int mSystemUiVisibility;
17232
17233 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017234 * Hack to force certain system UI visibility flags to be cleared.
17235 */
17236 int mDisabledSystemUiVisibility;
17237
17238 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017239 * Last global system UI visibility reported by the window manager.
17240 */
17241 int mGlobalSystemUiVisibility;
17242
17243 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017244 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17245 * attached.
17246 */
17247 boolean mHasSystemUiListeners;
17248
17249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017250 * Set if the visibility of any views has changed.
17251 */
17252 boolean mViewVisibilityChanged;
17253
17254 /**
17255 * Set to true if a view has been scrolled.
17256 */
17257 boolean mViewScrollChanged;
17258
17259 /**
17260 * Global to the view hierarchy used as a temporary for dealing with
17261 * x/y points in the transparent region computations.
17262 */
17263 final int[] mTransparentLocation = new int[2];
17264
17265 /**
17266 * Global to the view hierarchy used as a temporary for dealing with
17267 * x/y points in the ViewGroup.invalidateChild implementation.
17268 */
17269 final int[] mInvalidateChildLocation = new int[2];
17270
Chet Haasec3aa3612010-06-17 08:50:37 -070017271
17272 /**
17273 * Global to the view hierarchy used as a temporary for dealing with
17274 * x/y location when view is transformed.
17275 */
17276 final float[] mTmpTransformLocation = new float[2];
17277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017278 /**
17279 * The view tree observer used to dispatch global events like
17280 * layout, pre-draw, touch mode change, etc.
17281 */
17282 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17283
17284 /**
17285 * A Canvas used by the view hierarchy to perform bitmap caching.
17286 */
17287 Canvas mCanvas;
17288
17289 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017290 * The view root impl.
17291 */
17292 final ViewRootImpl mViewRootImpl;
17293
17294 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017295 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017296 * handler can be used to pump events in the UI events queue.
17297 */
17298 final Handler mHandler;
17299
17300 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017301 * Temporary for use in computing invalidate rectangles while
17302 * calling up the hierarchy.
17303 */
17304 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017305
17306 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017307 * Temporary for use in computing hit areas with transformed views
17308 */
17309 final RectF mTmpTransformRect = new RectF();
17310
17311 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017312 * Temporary list for use in collecting focusable descendents of a view.
17313 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017314 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017316 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017317 * The id of the window for accessibility purposes.
17318 */
17319 int mAccessibilityWindowId = View.NO_ID;
17320
17321 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017322 * Whether to ingore not exposed for accessibility Views when
17323 * reporting the view tree to accessibility services.
17324 */
17325 boolean mIncludeNotImportantViews;
17326
17327 /**
17328 * The drawable for highlighting accessibility focus.
17329 */
17330 Drawable mAccessibilityFocusDrawable;
17331
17332 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017333 * Show where the margins, bounds and layout bounds are for each view.
17334 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017335 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017336
17337 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017338 * Point used to compute visible regions.
17339 */
17340 final Point mPoint = new Point();
17341
17342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017343 * Creates a new set of attachment information with the specified
17344 * events handler and thread.
17345 *
17346 * @param handler the events handler the view must use
17347 */
17348 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017349 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017350 mSession = session;
17351 mWindow = window;
17352 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017353 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017354 mHandler = handler;
17355 mRootCallbacks = effectPlayer;
17356 }
17357 }
17358
17359 /**
17360 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17361 * is supported. This avoids keeping too many unused fields in most
17362 * instances of View.</p>
17363 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017364 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017365
Mike Cleronf116bf82009-09-27 19:14:12 -070017366 /**
17367 * Scrollbars are not visible
17368 */
17369 public static final int OFF = 0;
17370
17371 /**
17372 * Scrollbars are visible
17373 */
17374 public static final int ON = 1;
17375
17376 /**
17377 * Scrollbars are fading away
17378 */
17379 public static final int FADING = 2;
17380
17381 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017383 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017384 public int scrollBarDefaultDelayBeforeFade;
17385 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017386
17387 public int scrollBarSize;
17388 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017389 public float[] interpolatorValues;
17390 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017391
17392 public final Paint paint;
17393 public final Matrix matrix;
17394 public Shader shader;
17395
Mike Cleronf116bf82009-09-27 19:14:12 -070017396 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17397
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017398 private static final float[] OPAQUE = { 255 };
17399 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017400
Mike Cleronf116bf82009-09-27 19:14:12 -070017401 /**
17402 * When fading should start. This time moves into the future every time
17403 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17404 */
17405 public long fadeStartTime;
17406
17407
17408 /**
17409 * The current state of the scrollbars: ON, OFF, or FADING
17410 */
17411 public int state = OFF;
17412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017413 private int mLastColor;
17414
Mike Cleronf116bf82009-09-27 19:14:12 -070017415 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017416 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17417 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017418 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17419 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017420
17421 paint = new Paint();
17422 matrix = new Matrix();
17423 // use use a height of 1, and then wack the matrix each time we
17424 // actually use it.
17425 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017427 paint.setShader(shader);
17428 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017429 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017430 }
Romain Guy8506ab42009-06-11 17:35:47 -070017431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017432 public void setFadeColor(int color) {
17433 if (color != 0 && color != mLastColor) {
17434 mLastColor = color;
17435 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017436
Romain Guye55e1a72009-08-27 10:42:26 -070017437 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17438 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017440 paint.setShader(shader);
17441 // Restore the default transfer mode (src_over)
17442 paint.setXfermode(null);
17443 }
17444 }
Joe Malin32736f02011-01-19 16:14:20 -080017445
Mike Cleronf116bf82009-09-27 19:14:12 -070017446 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017447 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017448 if (now >= fadeStartTime) {
17449
17450 // the animation fades the scrollbars out by changing
17451 // the opacity (alpha) from fully opaque to fully
17452 // transparent
17453 int nextFrame = (int) now;
17454 int framesCount = 0;
17455
17456 Interpolator interpolator = scrollBarInterpolator;
17457
17458 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017459 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017460
17461 // End transparent
17462 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017463 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017464
17465 state = FADING;
17466
17467 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017468 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017469 }
17470 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017471 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017472
Svetoslav Ganova0156172011-06-26 17:55:44 -070017473 /**
17474 * Resuable callback for sending
17475 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17476 */
17477 private class SendViewScrolledAccessibilityEvent implements Runnable {
17478 public volatile boolean mIsPending;
17479
17480 public void run() {
17481 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17482 mIsPending = false;
17483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017484 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017485
17486 /**
17487 * <p>
17488 * This class represents a delegate that can be registered in a {@link View}
17489 * to enhance accessibility support via composition rather via inheritance.
17490 * It is specifically targeted to widget developers that extend basic View
17491 * classes i.e. classes in package android.view, that would like their
17492 * applications to be backwards compatible.
17493 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017494 * <div class="special reference">
17495 * <h3>Developer Guides</h3>
17496 * <p>For more information about making applications accessible, read the
17497 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17498 * developer guide.</p>
17499 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017500 * <p>
17501 * A scenario in which a developer would like to use an accessibility delegate
17502 * is overriding a method introduced in a later API version then the minimal API
17503 * version supported by the application. For example, the method
17504 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17505 * in API version 4 when the accessibility APIs were first introduced. If a
17506 * developer would like his application to run on API version 4 devices (assuming
17507 * all other APIs used by the application are version 4 or lower) and take advantage
17508 * of this method, instead of overriding the method which would break the application's
17509 * backwards compatibility, he can override the corresponding method in this
17510 * delegate and register the delegate in the target View if the API version of
17511 * the system is high enough i.e. the API version is same or higher to the API
17512 * version that introduced
17513 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17514 * </p>
17515 * <p>
17516 * Here is an example implementation:
17517 * </p>
17518 * <code><pre><p>
17519 * if (Build.VERSION.SDK_INT >= 14) {
17520 * // If the API version is equal of higher than the version in
17521 * // which onInitializeAccessibilityNodeInfo was introduced we
17522 * // register a delegate with a customized implementation.
17523 * View view = findViewById(R.id.view_id);
17524 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17525 * public void onInitializeAccessibilityNodeInfo(View host,
17526 * AccessibilityNodeInfo info) {
17527 * // Let the default implementation populate the info.
17528 * super.onInitializeAccessibilityNodeInfo(host, info);
17529 * // Set some other information.
17530 * info.setEnabled(host.isEnabled());
17531 * }
17532 * });
17533 * }
17534 * </code></pre></p>
17535 * <p>
17536 * This delegate contains methods that correspond to the accessibility methods
17537 * in View. If a delegate has been specified the implementation in View hands
17538 * off handling to the corresponding method in this delegate. The default
17539 * implementation the delegate methods behaves exactly as the corresponding
17540 * method in View for the case of no accessibility delegate been set. Hence,
17541 * to customize the behavior of a View method, clients can override only the
17542 * corresponding delegate method without altering the behavior of the rest
17543 * accessibility related methods of the host view.
17544 * </p>
17545 */
17546 public static class AccessibilityDelegate {
17547
17548 /**
17549 * Sends an accessibility event of the given type. If accessibility is not
17550 * enabled this method has no effect.
17551 * <p>
17552 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17553 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17554 * been set.
17555 * </p>
17556 *
17557 * @param host The View hosting the delegate.
17558 * @param eventType The type of the event to send.
17559 *
17560 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17561 */
17562 public void sendAccessibilityEvent(View host, int eventType) {
17563 host.sendAccessibilityEventInternal(eventType);
17564 }
17565
17566 /**
alanv8eeefef2012-05-07 16:57:53 -070017567 * Performs the specified accessibility action on the view. For
17568 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17569 * <p>
17570 * The default implementation behaves as
17571 * {@link View#performAccessibilityAction(int, Bundle)
17572 * View#performAccessibilityAction(int, Bundle)} for the case of
17573 * no accessibility delegate been set.
17574 * </p>
17575 *
17576 * @param action The action to perform.
17577 * @return Whether the action was performed.
17578 *
17579 * @see View#performAccessibilityAction(int, Bundle)
17580 * View#performAccessibilityAction(int, Bundle)
17581 */
17582 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17583 return host.performAccessibilityActionInternal(action, args);
17584 }
17585
17586 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017587 * Sends an accessibility event. This method behaves exactly as
17588 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17589 * empty {@link AccessibilityEvent} and does not perform a check whether
17590 * accessibility is enabled.
17591 * <p>
17592 * The default implementation behaves as
17593 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17594 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17595 * the case of no accessibility delegate been set.
17596 * </p>
17597 *
17598 * @param host The View hosting the delegate.
17599 * @param event The event to send.
17600 *
17601 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17602 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17603 */
17604 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17605 host.sendAccessibilityEventUncheckedInternal(event);
17606 }
17607
17608 /**
17609 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17610 * to its children for adding their text content to the event.
17611 * <p>
17612 * The default implementation behaves as
17613 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17614 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17615 * the case of no accessibility delegate been set.
17616 * </p>
17617 *
17618 * @param host The View hosting the delegate.
17619 * @param event The event.
17620 * @return True if the event population was completed.
17621 *
17622 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17623 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17624 */
17625 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17626 return host.dispatchPopulateAccessibilityEventInternal(event);
17627 }
17628
17629 /**
17630 * Gives a chance to the host View to populate the accessibility event with its
17631 * text content.
17632 * <p>
17633 * The default implementation behaves as
17634 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17635 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17636 * the case of no accessibility delegate been set.
17637 * </p>
17638 *
17639 * @param host The View hosting the delegate.
17640 * @param event The accessibility event which to populate.
17641 *
17642 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17643 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17644 */
17645 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17646 host.onPopulateAccessibilityEventInternal(event);
17647 }
17648
17649 /**
17650 * Initializes an {@link AccessibilityEvent} with information about the
17651 * the host View which is the event source.
17652 * <p>
17653 * The default implementation behaves as
17654 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17655 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17656 * the case of no accessibility delegate been set.
17657 * </p>
17658 *
17659 * @param host The View hosting the delegate.
17660 * @param event The event to initialize.
17661 *
17662 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17663 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17664 */
17665 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17666 host.onInitializeAccessibilityEventInternal(event);
17667 }
17668
17669 /**
17670 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17671 * <p>
17672 * The default implementation behaves as
17673 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17674 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17675 * the case of no accessibility delegate been set.
17676 * </p>
17677 *
17678 * @param host The View hosting the delegate.
17679 * @param info The instance to initialize.
17680 *
17681 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17682 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17683 */
17684 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17685 host.onInitializeAccessibilityNodeInfoInternal(info);
17686 }
17687
17688 /**
17689 * Called when a child of the host View has requested sending an
17690 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17691 * to augment the event.
17692 * <p>
17693 * The default implementation behaves as
17694 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17695 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17696 * the case of no accessibility delegate been set.
17697 * </p>
17698 *
17699 * @param host The View hosting the delegate.
17700 * @param child The child which requests sending the event.
17701 * @param event The event to be sent.
17702 * @return True if the event should be sent
17703 *
17704 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17705 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17706 */
17707 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17708 AccessibilityEvent event) {
17709 return host.onRequestSendAccessibilityEventInternal(child, event);
17710 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017711
17712 /**
17713 * Gets the provider for managing a virtual view hierarchy rooted at this View
17714 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17715 * that explore the window content.
17716 * <p>
17717 * The default implementation behaves as
17718 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17719 * the case of no accessibility delegate been set.
17720 * </p>
17721 *
17722 * @return The provider.
17723 *
17724 * @see AccessibilityNodeProvider
17725 */
17726 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17727 return null;
17728 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017730}