blob: 75241f614799ae7ac1088468cb17d72c24111bd3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700669 * When set to true, apps will draw debugging information about their layouts.
670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
1599 private int mAccessibilityCursorPosition = -1;
1600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
2089 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002131 /**
2132 * Flag indicating that view has an animation set on it. This is used to track whether an
2133 * animation is cleared between successive frames, in order to tell the associated
2134 * DisplayList to clear its animation matrix.
2135 */
2136 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2137
Christopher Tate3d4bf172011-03-28 16:16:46 -07002138 /* End of masks for mPrivateFlags2 */
2139
2140 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2141
Chet Haasedaf98e92011-01-10 14:10:36 -08002142 /**
Adam Powell637d3372010-08-25 14:37:03 -07002143 * Always allow a user to over-scroll this view, provided it is a
2144 * view that can scroll.
2145 *
2146 * @see #getOverScrollMode()
2147 * @see #setOverScrollMode(int)
2148 */
2149 public static final int OVER_SCROLL_ALWAYS = 0;
2150
2151 /**
2152 * Allow a user to over-scroll this view only if the content is large
2153 * enough to meaningfully scroll, provided it is a view that can scroll.
2154 *
2155 * @see #getOverScrollMode()
2156 * @see #setOverScrollMode(int)
2157 */
2158 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2159
2160 /**
2161 * Never allow a user to over-scroll this view.
2162 *
2163 * @see #getOverScrollMode()
2164 * @see #setOverScrollMode(int)
2165 */
2166 public static final int OVER_SCROLL_NEVER = 2;
2167
2168 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002169 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2170 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002171 *
Joe Malin32736f02011-01-19 16:14:20 -08002172 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002173 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002174 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002175
2176 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002177 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2178 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002179 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002180 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002181 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002182 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002183 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002184 *
Joe Malin32736f02011-01-19 16:14:20 -08002185 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002186 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002187 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2188
2189 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002190 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2191 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002192 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002193 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002194 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2195 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2196 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002197 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002198 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2199 * window flags) for displaying content using every last pixel on the display.
2200 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002201 * <p>There is a limitation: because navigation controls are so important, the least user
2202 * interaction will cause them to reappear immediately. When this happens, both
2203 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2204 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002205 *
2206 * @see #setSystemUiVisibility(int)
2207 */
2208 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2209
2210 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002211 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2212 * into the normal fullscreen mode so that its content can take over the screen
2213 * while still allowing the user to interact with the application.
2214 *
2215 * <p>This has the same visual effect as
2216 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2217 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2218 * meaning that non-critical screen decorations (such as the status bar) will be
2219 * hidden while the user is in the View's window, focusing the experience on
2220 * that content. Unlike the window flag, if you are using ActionBar in
2221 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2222 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2223 * hide the action bar.
2224 *
2225 * <p>This approach to going fullscreen is best used over the window flag when
2226 * it is a transient state -- that is, the application does this at certain
2227 * points in its user interaction where it wants to allow the user to focus
2228 * on content, but not as a continuous state. For situations where the application
2229 * would like to simply stay full screen the entire time (such as a game that
2230 * wants to take over the screen), the
2231 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2232 * is usually a better approach. The state set here will be removed by the system
2233 * in various situations (such as the user moving to another application) like
2234 * the other system UI states.
2235 *
2236 * <p>When using this flag, the application should provide some easy facility
2237 * for the user to go out of it. A common example would be in an e-book
2238 * reader, where tapping on the screen brings back whatever screen and UI
2239 * decorations that had been hidden while the user was immersed in reading
2240 * the book.
2241 *
2242 * @see #setSystemUiVisibility(int)
2243 */
2244 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2245
2246 /**
2247 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2248 * flags, we would like a stable view of the content insets given to
2249 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2250 * will always represent the worst case that the application can expect
2251 * as a continue state. In practice this means with any of system bar,
2252 * nav bar, and status bar shown, but not the space that would be needed
2253 * for an input method.
2254 *
2255 * <p>If you are using ActionBar in
2256 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2257 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2258 * insets it adds to those given to the application.
2259 */
2260 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2261
2262 /**
2263 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2264 * to be layed out as if it has requested
2265 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2266 * allows it to avoid artifacts when switching in and out of that mode, at
2267 * the expense that some of its user interface may be covered by screen
2268 * decorations when they are shown. You can perform layout of your inner
2269 * UI elements to account for the navagation system UI through the
2270 * {@link #fitSystemWindows(Rect)} method.
2271 */
2272 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2273
2274 /**
2275 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2276 * to be layed out as if it has requested
2277 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2278 * allows it to avoid artifacts when switching in and out of that mode, at
2279 * the expense that some of its user interface may be covered by screen
2280 * decorations when they are shown. You can perform layout of your inner
2281 * UI elements to account for non-fullscreen system UI through the
2282 * {@link #fitSystemWindows(Rect)} method.
2283 */
2284 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2285
2286 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002287 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2288 */
2289 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2290
2291 /**
2292 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2293 */
2294 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002295
2296 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002297 * @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 make the status bar not expandable. Unless you also
2303 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2304 */
2305 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
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 notification icons and scrolling ticker text.
2314 */
2315 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2316
2317 /**
2318 * @hide
2319 *
2320 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2321 * out of the public fields to keep the undefined bits out of the developer's way.
2322 *
2323 * Flag to disable incoming notification alerts. This will not block
2324 * icons, but it will block sound, vibrating and other visual or aural notifications.
2325 */
2326 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2327
2328 /**
2329 * @hide
2330 *
2331 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2332 * out of the public fields to keep the undefined bits out of the developer's way.
2333 *
2334 * Flag to hide only the scrolling ticker. Note that
2335 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2336 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2337 */
2338 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2339
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 *
2346 * Flag to hide the center system info area.
2347 */
2348 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2349
2350 /**
2351 * @hide
2352 *
2353 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2354 * out of the public fields to keep the undefined bits out of the developer's way.
2355 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002356 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002357 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2358 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002359 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002360
2361 /**
2362 * @hide
2363 *
2364 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2365 * out of the public fields to keep the undefined bits out of the developer's way.
2366 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002367 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002368 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2369 */
2370 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2371
2372 /**
2373 * @hide
2374 *
2375 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2376 * out of the public fields to keep the undefined bits out of the developer's way.
2377 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002378 * Flag to hide only the clock. You might use this if your activity has
2379 * its own clock making the status bar's clock redundant.
2380 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002381 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2382
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002383 /**
2384 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002385 *
2386 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2387 * out of the public fields to keep the undefined bits out of the developer's way.
2388 *
2389 * Flag to hide only the recent apps button. Don't use this
2390 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2391 */
2392 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2393
2394 /**
2395 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002396 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002397 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002398
2399 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002400 * These are the system UI flags that can be cleared by events outside
2401 * of an application. Currently this is just the ability to tap on the
2402 * screen while hiding the navigation bar to have it return.
2403 * @hide
2404 */
2405 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002406 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2407 | SYSTEM_UI_FLAG_FULLSCREEN;
2408
2409 /**
2410 * Flags that can impact the layout in relation to system UI.
2411 */
2412 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2413 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2414 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002415
2416 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002417 * Find views that render the specified text.
2418 *
2419 * @see #findViewsWithText(ArrayList, CharSequence, int)
2420 */
2421 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2422
2423 /**
2424 * Find find views that contain the specified content description.
2425 *
2426 * @see #findViewsWithText(ArrayList, CharSequence, int)
2427 */
2428 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2429
2430 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002431 * Find views that contain {@link AccessibilityNodeProvider}. Such
2432 * a View is a root of virtual view hierarchy and may contain the searched
2433 * text. If this flag is set Views with providers are automatically
2434 * added and it is a responsibility of the client to call the APIs of
2435 * the provider to determine whether the virtual tree rooted at this View
2436 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2437 * represeting the virtual views with this text.
2438 *
2439 * @see #findViewsWithText(ArrayList, CharSequence, int)
2440 *
2441 * @hide
2442 */
2443 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2444
2445 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002446 * Indicates that the screen has changed state and is now off.
2447 *
2448 * @see #onScreenStateChanged(int)
2449 */
2450 public static final int SCREEN_STATE_OFF = 0x0;
2451
2452 /**
2453 * Indicates that the screen has changed state and is now on.
2454 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002455 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002456 */
2457 public static final int SCREEN_STATE_ON = 0x1;
2458
2459 /**
Adam Powell637d3372010-08-25 14:37:03 -07002460 * Controls the over-scroll mode for this view.
2461 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2462 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2463 * and {@link #OVER_SCROLL_NEVER}.
2464 */
2465 private int mOverScrollMode;
2466
2467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 * The parent this view is attached to.
2469 * {@hide}
2470 *
2471 * @see #getParent()
2472 */
2473 protected ViewParent mParent;
2474
2475 /**
2476 * {@hide}
2477 */
2478 AttachInfo mAttachInfo;
2479
2480 /**
2481 * {@hide}
2482 */
Romain Guy809a7f62009-05-14 15:44:42 -07002483 @ViewDebug.ExportedProperty(flagMapping = {
2484 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2485 name = "FORCE_LAYOUT"),
2486 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2487 name = "LAYOUT_REQUIRED"),
2488 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002489 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002490 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2491 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2492 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2493 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2494 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002496 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497
2498 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002499 * This view's request for the visibility of the status bar.
2500 * @hide
2501 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002502 @ViewDebug.ExportedProperty(flagMapping = {
2503 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2504 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2505 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2506 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2507 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2508 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2509 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2510 equals = SYSTEM_UI_FLAG_VISIBLE,
2511 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2512 })
Joe Onorato664644d2011-01-23 17:53:23 -08002513 int mSystemUiVisibility;
2514
2515 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002516 * Reference count for transient state.
2517 * @see #setHasTransientState(boolean)
2518 */
2519 int mTransientStateCount = 0;
2520
2521 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 * Count of how many windows this view has been attached to.
2523 */
2524 int mWindowAttachCount;
2525
2526 /**
2527 * The layout parameters associated with this view and used by the parent
2528 * {@link android.view.ViewGroup} to determine how this view should be
2529 * laid out.
2530 * {@hide}
2531 */
2532 protected ViewGroup.LayoutParams mLayoutParams;
2533
2534 /**
2535 * The view flags hold various views states.
2536 * {@hide}
2537 */
2538 @ViewDebug.ExportedProperty
2539 int mViewFlags;
2540
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002541 static class TransformationInfo {
2542 /**
2543 * The transform matrix for the View. This transform is calculated internally
2544 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2545 * is used by default. Do *not* use this variable directly; instead call
2546 * getMatrix(), which will automatically recalculate the matrix if necessary
2547 * to get the correct matrix based on the latest rotation and scale properties.
2548 */
2549 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002550
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002551 /**
2552 * The transform matrix for the View. This transform is calculated internally
2553 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2554 * is used by default. Do *not* use this variable directly; instead call
2555 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2556 * to get the correct matrix based on the latest rotation and scale properties.
2557 */
2558 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002559
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002560 /**
2561 * An internal variable that tracks whether we need to recalculate the
2562 * transform matrix, based on whether the rotation or scaleX/Y properties
2563 * have changed since the matrix was last calculated.
2564 */
2565 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002566
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002567 /**
2568 * An internal variable that tracks whether we need to recalculate the
2569 * transform matrix, based on whether the rotation or scaleX/Y properties
2570 * have changed since the matrix was last calculated.
2571 */
2572 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002573
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002574 /**
2575 * A variable that tracks whether we need to recalculate the
2576 * transform matrix, based on whether the rotation or scaleX/Y properties
2577 * have changed since the matrix was last calculated. This variable
2578 * is only valid after a call to updateMatrix() or to a function that
2579 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2580 */
2581 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002582
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002583 /**
2584 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2585 */
2586 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002587
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002588 /**
2589 * This matrix is used when computing the matrix for 3D rotations.
2590 */
2591 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002592
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002593 /**
2594 * These prev values are used to recalculate a centered pivot point when necessary. The
2595 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2596 * set), so thes values are only used then as well.
2597 */
2598 private int mPrevWidth = -1;
2599 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002600
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002601 /**
2602 * The degrees rotation around the vertical axis through the pivot point.
2603 */
2604 @ViewDebug.ExportedProperty
2605 float mRotationY = 0f;
2606
2607 /**
2608 * The degrees rotation around the horizontal axis through the pivot point.
2609 */
2610 @ViewDebug.ExportedProperty
2611 float mRotationX = 0f;
2612
2613 /**
2614 * The degrees rotation around the pivot point.
2615 */
2616 @ViewDebug.ExportedProperty
2617 float mRotation = 0f;
2618
2619 /**
2620 * The amount of translation of the object away from its left property (post-layout).
2621 */
2622 @ViewDebug.ExportedProperty
2623 float mTranslationX = 0f;
2624
2625 /**
2626 * The amount of translation of the object away from its top property (post-layout).
2627 */
2628 @ViewDebug.ExportedProperty
2629 float mTranslationY = 0f;
2630
2631 /**
2632 * The amount of scale in the x direction around the pivot point. A
2633 * value of 1 means no scaling is applied.
2634 */
2635 @ViewDebug.ExportedProperty
2636 float mScaleX = 1f;
2637
2638 /**
2639 * The amount of scale in the y direction around the pivot point. A
2640 * value of 1 means no scaling is applied.
2641 */
2642 @ViewDebug.ExportedProperty
2643 float mScaleY = 1f;
2644
2645 /**
Chet Haasea33de552012-02-03 16:28:24 -08002646 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002647 */
2648 @ViewDebug.ExportedProperty
2649 float mPivotX = 0f;
2650
2651 /**
Chet Haasea33de552012-02-03 16:28:24 -08002652 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002653 */
2654 @ViewDebug.ExportedProperty
2655 float mPivotY = 0f;
2656
2657 /**
2658 * The opacity of the View. This is a value from 0 to 1, where 0 means
2659 * completely transparent and 1 means completely opaque.
2660 */
2661 @ViewDebug.ExportedProperty
2662 float mAlpha = 1f;
2663 }
2664
2665 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002666
Joe Malin32736f02011-01-19 16:14:20 -08002667 private boolean mLastIsOpaque;
2668
Chet Haasefd2b0022010-08-06 13:08:56 -07002669 /**
2670 * Convenience value to check for float values that are close enough to zero to be considered
2671 * zero.
2672 */
Romain Guy2542d192010-08-18 11:47:12 -07002673 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002674
2675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 * The distance in pixels from the left edge of this view's parent
2677 * to the left edge of this view.
2678 * {@hide}
2679 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002680 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 protected int mLeft;
2682 /**
2683 * The distance in pixels from the left edge of this view's parent
2684 * to the right edge of this view.
2685 * {@hide}
2686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 protected int mRight;
2689 /**
2690 * The distance in pixels from the top edge of this view's parent
2691 * to the top edge of this view.
2692 * {@hide}
2693 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002694 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 protected int mTop;
2696 /**
2697 * The distance in pixels from the top edge of this view's parent
2698 * to the bottom edge of this view.
2699 * {@hide}
2700 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002701 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 protected int mBottom;
2703
2704 /**
2705 * The offset, in pixels, by which the content of this view is scrolled
2706 * horizontally.
2707 * {@hide}
2708 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002709 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 protected int mScrollX;
2711 /**
2712 * The offset, in pixels, by which the content of this view is scrolled
2713 * vertically.
2714 * {@hide}
2715 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002716 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 protected int mScrollY;
2718
2719 /**
2720 * The left padding in pixels, that is the distance in pixels between the
2721 * left edge of this view and the left edge of its content.
2722 * {@hide}
2723 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002724 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 protected int mPaddingLeft;
2726 /**
2727 * The right padding in pixels, that is the distance in pixels between the
2728 * right edge of this view and the right edge of its content.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mPaddingRight;
2733 /**
2734 * The top padding in pixels, that is the distance in pixels between the
2735 * top edge of this view and the top edge of its content.
2736 * {@hide}
2737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002738 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 protected int mPaddingTop;
2740 /**
2741 * The bottom padding in pixels, that is the distance in pixels between the
2742 * bottom edge of this view and the bottom edge of its content.
2743 * {@hide}
2744 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002745 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 protected int mPaddingBottom;
2747
2748 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002749 * The layout insets in pixels, that is the distance in pixels between the
2750 * visible edges of this view its bounds.
2751 */
2752 private Insets mLayoutInsets;
2753
2754 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002755 * Briefly describes the view and is primarily used for accessibility support.
2756 */
2757 private CharSequence mContentDescription;
2758
2759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002761 *
2762 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002764 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002765 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766
2767 /**
2768 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002769 *
2770 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002772 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002773 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002775 /**
Adam Powell20232d02010-12-08 21:08:53 -08002776 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002777 *
2778 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002779 */
2780 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002781 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002782
2783 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002784 * Cache if the user padding is relative.
2785 *
2786 */
2787 @ViewDebug.ExportedProperty(category = "padding")
2788 boolean mUserPaddingRelative;
2789
2790 /**
2791 * Cache the paddingStart set by the user to append to the scrollbar's size.
2792 *
2793 */
2794 @ViewDebug.ExportedProperty(category = "padding")
2795 int mUserPaddingStart;
2796
2797 /**
2798 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2799 *
2800 */
2801 @ViewDebug.ExportedProperty(category = "padding")
2802 int mUserPaddingEnd;
2803
2804 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002805 * @hide
2806 */
2807 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2808 /**
2809 * @hide
2810 */
2811 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812
Philip Milne6c8ea062012-04-03 17:38:43 -07002813 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814
2815 private int mBackgroundResource;
2816 private boolean mBackgroundSizeChanged;
2817
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002818 static class ListenerInfo {
2819 /**
2820 * Listener used to dispatch focus change events.
2821 * This field should be made private, so it is hidden from the SDK.
2822 * {@hide}
2823 */
2824 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002826 /**
2827 * Listeners for layout change events.
2828 */
2829 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002830
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002831 /**
2832 * Listeners for attach events.
2833 */
2834 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002835
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002836 /**
2837 * Listener used to dispatch click events.
2838 * This field should be made private, so it is hidden from the SDK.
2839 * {@hide}
2840 */
2841 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 /**
2844 * Listener used to dispatch long click events.
2845 * This field should be made private, so it is hidden from the SDK.
2846 * {@hide}
2847 */
2848 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 /**
2851 * Listener used to build the context menu.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002859 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002861 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002862
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002863 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002864
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002865 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002866
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002867 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2868 }
2869
2870 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 /**
2873 * The application environment this view lives in.
2874 * This field should be made private, so it is hidden from the SDK.
2875 * {@hide}
2876 */
2877 protected Context mContext;
2878
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002879 private final Resources mResources;
2880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 private ScrollabilityCache mScrollCache;
2882
2883 private int[] mDrawableState = null;
2884
Romain Guy0211a0a2011-02-14 16:34:59 -08002885 /**
2886 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002887 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002888 * @hide
2889 */
2890 public boolean mCachingFailed;
2891
Romain Guy02890fd2010-08-06 17:58:44 -07002892 private Bitmap mDrawingCache;
2893 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002894 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002895 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896
2897 /**
2898 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2899 * the user may specify which view to go to next.
2900 */
2901 private int mNextFocusLeftId = View.NO_ID;
2902
2903 /**
2904 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2905 * the user may specify which view to go to next.
2906 */
2907 private int mNextFocusRightId = View.NO_ID;
2908
2909 /**
2910 * When this view has focus and the next focus is {@link #FOCUS_UP},
2911 * the user may specify which view to go to next.
2912 */
2913 private int mNextFocusUpId = View.NO_ID;
2914
2915 /**
2916 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2917 * the user may specify which view to go to next.
2918 */
2919 private int mNextFocusDownId = View.NO_ID;
2920
Jeff Brown4e6319b2010-12-13 10:36:51 -08002921 /**
2922 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2923 * the user may specify which view to go to next.
2924 */
2925 int mNextFocusForwardId = View.NO_ID;
2926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002928 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002929 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002930 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 private UnsetPressedState mUnsetPressedState;
2933
2934 /**
2935 * Whether the long press's action has been invoked. The tap's action is invoked on the
2936 * up event while a long press is invoked as soon as the long press duration is reached, so
2937 * a long press could be performed before the tap is checked, in which case the tap's action
2938 * should not be invoked.
2939 */
2940 private boolean mHasPerformedLongPress;
2941
2942 /**
2943 * The minimum height of the view. We'll try our best to have the height
2944 * of this view to at least this amount.
2945 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002946 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 private int mMinHeight;
2948
2949 /**
2950 * The minimum width of the view. We'll try our best to have the width
2951 * of this view to at least this amount.
2952 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002953 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 private int mMinWidth;
2955
2956 /**
2957 * The delegate to handle touch events that are physically in this view
2958 * but should be handled by another view.
2959 */
2960 private TouchDelegate mTouchDelegate = null;
2961
2962 /**
2963 * Solid color to use as a background when creating the drawing cache. Enables
2964 * the cache to use 16 bit bitmaps instead of 32 bit.
2965 */
2966 private int mDrawingCacheBackgroundColor = 0;
2967
2968 /**
2969 * Special tree observer used when mAttachInfo is null.
2970 */
2971 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002972
Adam Powelle14579b2009-12-16 18:39:52 -08002973 /**
2974 * Cache the touch slop from the context that created the view.
2975 */
2976 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002979 * Object that handles automatic animation of view properties.
2980 */
2981 private ViewPropertyAnimator mAnimator = null;
2982
2983 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002984 * Flag indicating that a drag can cross window boundaries. When
2985 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2986 * with this flag set, all visible applications will be able to participate
2987 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002988 *
2989 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002990 */
2991 public static final int DRAG_FLAG_GLOBAL = 1;
2992
2993 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002994 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2995 */
2996 private float mVerticalScrollFactor;
2997
2998 /**
Adam Powell20232d02010-12-08 21:08:53 -08002999 * Position of the vertical scroll bar.
3000 */
3001 private int mVerticalScrollbarPosition;
3002
3003 /**
3004 * Position the scroll bar at the default position as determined by the system.
3005 */
3006 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3007
3008 /**
3009 * Position the scroll bar along the left edge.
3010 */
3011 public static final int SCROLLBAR_POSITION_LEFT = 1;
3012
3013 /**
3014 * Position the scroll bar along the right edge.
3015 */
3016 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3017
3018 /**
Romain Guy171c5922011-01-06 10:04:23 -08003019 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003020 *
3021 * @see #getLayerType()
3022 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003023 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003024 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003025 */
3026 public static final int LAYER_TYPE_NONE = 0;
3027
3028 /**
3029 * <p>Indicates that the view has a software layer. A software layer is backed
3030 * by a bitmap and causes the view to be rendered using Android's software
3031 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003032 *
Romain Guy171c5922011-01-06 10:04:23 -08003033 * <p>Software layers have various usages:</p>
3034 * <p>When the application is not using hardware acceleration, a software layer
3035 * is useful to apply a specific color filter and/or blending mode and/or
3036 * translucency to a view and all its children.</p>
3037 * <p>When the application is using hardware acceleration, a software layer
3038 * is useful to render drawing primitives not supported by the hardware
3039 * accelerated pipeline. It can also be used to cache a complex view tree
3040 * into a texture and reduce the complexity of drawing operations. For instance,
3041 * when animating a complex view tree with a translation, a software layer can
3042 * be used to render the view tree only once.</p>
3043 * <p>Software layers should be avoided when the affected view tree updates
3044 * often. Every update will require to re-render the software layer, which can
3045 * potentially be slow (particularly when hardware acceleration is turned on
3046 * since the layer will have to be uploaded into a hardware texture after every
3047 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003048 *
3049 * @see #getLayerType()
3050 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003051 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003052 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003053 */
3054 public static final int LAYER_TYPE_SOFTWARE = 1;
3055
3056 /**
3057 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3058 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3059 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3060 * rendering pipeline, but only if hardware acceleration is turned on for the
3061 * view hierarchy. When hardware acceleration is turned off, hardware layers
3062 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003063 *
Romain Guy171c5922011-01-06 10:04:23 -08003064 * <p>A hardware layer is useful to apply a specific color filter and/or
3065 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003066 * <p>A hardware layer can be used to cache a complex view tree into a
3067 * texture and reduce the complexity of drawing operations. For instance,
3068 * when animating a complex view tree with a translation, a hardware layer can
3069 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003070 * <p>A hardware layer can also be used to increase the rendering quality when
3071 * rotation transformations are applied on a view. It can also be used to
3072 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003073 *
3074 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003075 * @see #setLayerType(int, android.graphics.Paint)
3076 * @see #LAYER_TYPE_NONE
3077 * @see #LAYER_TYPE_SOFTWARE
3078 */
3079 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003080
Romain Guy3aaff3a2011-01-12 14:18:47 -08003081 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3082 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3083 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3084 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3085 })
Romain Guy171c5922011-01-06 10:04:23 -08003086 int mLayerType = LAYER_TYPE_NONE;
3087 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003088 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003089
3090 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003091 * Set to true when the view is sending hover accessibility events because it
3092 * is the innermost hovered view.
3093 */
3094 private boolean mSendingHoverAccessibilityEvents;
3095
3096 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 * Simple constructor to use when creating a view from code.
3098 *
3099 * @param context The Context the view is running in, through which it can
3100 * access the current theme, resources, etc.
3101 */
3102 public View(Context context) {
3103 mContext = context;
3104 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003105 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003106 // Set layout and text direction defaults
3107 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003108 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003109 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3110 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003111 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003112 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003113 mUserPaddingStart = -1;
3114 mUserPaddingEnd = -1;
3115 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 }
3117
3118 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003119 * Delegate for injecting accessiblity functionality.
3120 */
3121 AccessibilityDelegate mAccessibilityDelegate;
3122
3123 /**
3124 * Consistency verifier for debugging purposes.
3125 * @hide
3126 */
3127 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3128 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3129 new InputEventConsistencyVerifier(this, 0) : null;
3130
3131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 * Constructor that is called when inflating a view from XML. This is called
3133 * when a view is being constructed from an XML file, supplying attributes
3134 * that were specified in the XML file. This version uses a default style of
3135 * 0, so the only attribute values applied are those in the Context's Theme
3136 * and the given AttributeSet.
3137 *
3138 * <p>
3139 * The method onFinishInflate() will be called after all children have been
3140 * added.
3141 *
3142 * @param context The Context the view is running in, through which it can
3143 * access the current theme, resources, etc.
3144 * @param attrs The attributes of the XML tag that is inflating the view.
3145 * @see #View(Context, AttributeSet, int)
3146 */
3147 public View(Context context, AttributeSet attrs) {
3148 this(context, attrs, 0);
3149 }
3150
3151 /**
3152 * Perform inflation from XML and apply a class-specific base style. This
3153 * constructor of View allows subclasses to use their own base style when
3154 * they are inflating. For example, a Button class's constructor would call
3155 * this version of the super class constructor and supply
3156 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3157 * the theme's button style to modify all of the base view attributes (in
3158 * particular its background) as well as the Button class's attributes.
3159 *
3160 * @param context The Context the view is running in, through which it can
3161 * access the current theme, resources, etc.
3162 * @param attrs The attributes of the XML tag that is inflating the view.
3163 * @param defStyle The default style to apply to this view. If 0, no style
3164 * will be applied (beyond what is included in the theme). This may
3165 * either be an attribute resource, whose value will be retrieved
3166 * from the current theme, or an explicit style resource.
3167 * @see #View(Context, AttributeSet)
3168 */
3169 public View(Context context, AttributeSet attrs, int defStyle) {
3170 this(context);
3171
3172 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3173 defStyle, 0);
3174
3175 Drawable background = null;
3176
3177 int leftPadding = -1;
3178 int topPadding = -1;
3179 int rightPadding = -1;
3180 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003181 int startPadding = -1;
3182 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183
3184 int padding = -1;
3185
3186 int viewFlagValues = 0;
3187 int viewFlagMasks = 0;
3188
3189 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003191 int x = 0;
3192 int y = 0;
3193
Chet Haase73066682010-11-29 15:55:32 -08003194 float tx = 0;
3195 float ty = 0;
3196 float rotation = 0;
3197 float rotationX = 0;
3198 float rotationY = 0;
3199 float sx = 1f;
3200 float sy = 1f;
3201 boolean transformSet = false;
3202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3204
Adam Powell637d3372010-08-25 14:37:03 -07003205 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 final int N = a.getIndexCount();
3207 for (int i = 0; i < N; i++) {
3208 int attr = a.getIndex(i);
3209 switch (attr) {
3210 case com.android.internal.R.styleable.View_background:
3211 background = a.getDrawable(attr);
3212 break;
3213 case com.android.internal.R.styleable.View_padding:
3214 padding = a.getDimensionPixelSize(attr, -1);
3215 break;
3216 case com.android.internal.R.styleable.View_paddingLeft:
3217 leftPadding = a.getDimensionPixelSize(attr, -1);
3218 break;
3219 case com.android.internal.R.styleable.View_paddingTop:
3220 topPadding = a.getDimensionPixelSize(attr, -1);
3221 break;
3222 case com.android.internal.R.styleable.View_paddingRight:
3223 rightPadding = a.getDimensionPixelSize(attr, -1);
3224 break;
3225 case com.android.internal.R.styleable.View_paddingBottom:
3226 bottomPadding = a.getDimensionPixelSize(attr, -1);
3227 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003228 case com.android.internal.R.styleable.View_paddingStart:
3229 startPadding = a.getDimensionPixelSize(attr, -1);
3230 break;
3231 case com.android.internal.R.styleable.View_paddingEnd:
3232 endPadding = a.getDimensionPixelSize(attr, -1);
3233 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 case com.android.internal.R.styleable.View_scrollX:
3235 x = a.getDimensionPixelOffset(attr, 0);
3236 break;
3237 case com.android.internal.R.styleable.View_scrollY:
3238 y = a.getDimensionPixelOffset(attr, 0);
3239 break;
Chet Haase73066682010-11-29 15:55:32 -08003240 case com.android.internal.R.styleable.View_alpha:
3241 setAlpha(a.getFloat(attr, 1f));
3242 break;
3243 case com.android.internal.R.styleable.View_transformPivotX:
3244 setPivotX(a.getDimensionPixelOffset(attr, 0));
3245 break;
3246 case com.android.internal.R.styleable.View_transformPivotY:
3247 setPivotY(a.getDimensionPixelOffset(attr, 0));
3248 break;
3249 case com.android.internal.R.styleable.View_translationX:
3250 tx = a.getDimensionPixelOffset(attr, 0);
3251 transformSet = true;
3252 break;
3253 case com.android.internal.R.styleable.View_translationY:
3254 ty = a.getDimensionPixelOffset(attr, 0);
3255 transformSet = true;
3256 break;
3257 case com.android.internal.R.styleable.View_rotation:
3258 rotation = a.getFloat(attr, 0);
3259 transformSet = true;
3260 break;
3261 case com.android.internal.R.styleable.View_rotationX:
3262 rotationX = a.getFloat(attr, 0);
3263 transformSet = true;
3264 break;
3265 case com.android.internal.R.styleable.View_rotationY:
3266 rotationY = a.getFloat(attr, 0);
3267 transformSet = true;
3268 break;
3269 case com.android.internal.R.styleable.View_scaleX:
3270 sx = a.getFloat(attr, 1f);
3271 transformSet = true;
3272 break;
3273 case com.android.internal.R.styleable.View_scaleY:
3274 sy = a.getFloat(attr, 1f);
3275 transformSet = true;
3276 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 case com.android.internal.R.styleable.View_id:
3278 mID = a.getResourceId(attr, NO_ID);
3279 break;
3280 case com.android.internal.R.styleable.View_tag:
3281 mTag = a.getText(attr);
3282 break;
3283 case com.android.internal.R.styleable.View_fitsSystemWindows:
3284 if (a.getBoolean(attr, false)) {
3285 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3286 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3287 }
3288 break;
3289 case com.android.internal.R.styleable.View_focusable:
3290 if (a.getBoolean(attr, false)) {
3291 viewFlagValues |= FOCUSABLE;
3292 viewFlagMasks |= FOCUSABLE_MASK;
3293 }
3294 break;
3295 case com.android.internal.R.styleable.View_focusableInTouchMode:
3296 if (a.getBoolean(attr, false)) {
3297 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3298 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3299 }
3300 break;
3301 case com.android.internal.R.styleable.View_clickable:
3302 if (a.getBoolean(attr, false)) {
3303 viewFlagValues |= CLICKABLE;
3304 viewFlagMasks |= CLICKABLE;
3305 }
3306 break;
3307 case com.android.internal.R.styleable.View_longClickable:
3308 if (a.getBoolean(attr, false)) {
3309 viewFlagValues |= LONG_CLICKABLE;
3310 viewFlagMasks |= LONG_CLICKABLE;
3311 }
3312 break;
3313 case com.android.internal.R.styleable.View_saveEnabled:
3314 if (!a.getBoolean(attr, true)) {
3315 viewFlagValues |= SAVE_DISABLED;
3316 viewFlagMasks |= SAVE_DISABLED_MASK;
3317 }
3318 break;
3319 case com.android.internal.R.styleable.View_duplicateParentState:
3320 if (a.getBoolean(attr, false)) {
3321 viewFlagValues |= DUPLICATE_PARENT_STATE;
3322 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3323 }
3324 break;
3325 case com.android.internal.R.styleable.View_visibility:
3326 final int visibility = a.getInt(attr, 0);
3327 if (visibility != 0) {
3328 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3329 viewFlagMasks |= VISIBILITY_MASK;
3330 }
3331 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003332 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003333 // Clear any layout direction flags (included resolved bits) already set
3334 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3335 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003336 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003337 final int value = (layoutDirection != -1) ?
3338 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3339 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003340 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 case com.android.internal.R.styleable.View_drawingCacheQuality:
3342 final int cacheQuality = a.getInt(attr, 0);
3343 if (cacheQuality != 0) {
3344 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3345 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3346 }
3347 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003348 case com.android.internal.R.styleable.View_contentDescription:
3349 mContentDescription = a.getString(attr);
3350 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3352 if (!a.getBoolean(attr, true)) {
3353 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3354 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3355 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003356 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3358 if (!a.getBoolean(attr, true)) {
3359 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3360 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3361 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003362 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 case R.styleable.View_scrollbars:
3364 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3365 if (scrollbars != SCROLLBARS_NONE) {
3366 viewFlagValues |= scrollbars;
3367 viewFlagMasks |= SCROLLBARS_MASK;
3368 initializeScrollbars(a);
3369 }
3370 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003371 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003373 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3374 // Ignore the attribute starting with ICS
3375 break;
3376 }
3377 // With builds < ICS, fall through and apply fading edges
3378 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3380 if (fadingEdge != FADING_EDGE_NONE) {
3381 viewFlagValues |= fadingEdge;
3382 viewFlagMasks |= FADING_EDGE_MASK;
3383 initializeFadingEdge(a);
3384 }
3385 break;
3386 case R.styleable.View_scrollbarStyle:
3387 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3388 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3389 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3390 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3391 }
3392 break;
3393 case R.styleable.View_isScrollContainer:
3394 setScrollContainer = true;
3395 if (a.getBoolean(attr, false)) {
3396 setScrollContainer(true);
3397 }
3398 break;
3399 case com.android.internal.R.styleable.View_keepScreenOn:
3400 if (a.getBoolean(attr, false)) {
3401 viewFlagValues |= KEEP_SCREEN_ON;
3402 viewFlagMasks |= KEEP_SCREEN_ON;
3403 }
3404 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003405 case R.styleable.View_filterTouchesWhenObscured:
3406 if (a.getBoolean(attr, false)) {
3407 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3408 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3409 }
3410 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 case R.styleable.View_nextFocusLeft:
3412 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3413 break;
3414 case R.styleable.View_nextFocusRight:
3415 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3416 break;
3417 case R.styleable.View_nextFocusUp:
3418 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3419 break;
3420 case R.styleable.View_nextFocusDown:
3421 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3422 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003423 case R.styleable.View_nextFocusForward:
3424 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3425 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 case R.styleable.View_minWidth:
3427 mMinWidth = a.getDimensionPixelSize(attr, 0);
3428 break;
3429 case R.styleable.View_minHeight:
3430 mMinHeight = a.getDimensionPixelSize(attr, 0);
3431 break;
Romain Guy9a817362009-05-01 10:57:14 -07003432 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003433 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003434 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003435 + "be used within a restricted context");
3436 }
3437
Romain Guy9a817362009-05-01 10:57:14 -07003438 final String handlerName = a.getString(attr);
3439 if (handlerName != null) {
3440 setOnClickListener(new OnClickListener() {
3441 private Method mHandler;
3442
3443 public void onClick(View v) {
3444 if (mHandler == null) {
3445 try {
3446 mHandler = getContext().getClass().getMethod(handlerName,
3447 View.class);
3448 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003449 int id = getId();
3450 String idText = id == NO_ID ? "" : " with id '"
3451 + getContext().getResources().getResourceEntryName(
3452 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003453 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003454 handlerName + "(View) in the activity "
3455 + getContext().getClass() + " for onClick handler"
3456 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003457 }
3458 }
3459
3460 try {
3461 mHandler.invoke(getContext(), View.this);
3462 } catch (IllegalAccessException e) {
3463 throw new IllegalStateException("Could not execute non "
3464 + "public method of the activity", e);
3465 } catch (InvocationTargetException e) {
3466 throw new IllegalStateException("Could not execute "
3467 + "method of the activity", e);
3468 }
3469 }
3470 });
3471 }
3472 break;
Adam Powell637d3372010-08-25 14:37:03 -07003473 case R.styleable.View_overScrollMode:
3474 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3475 break;
Adam Powell20232d02010-12-08 21:08:53 -08003476 case R.styleable.View_verticalScrollbarPosition:
3477 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3478 break;
Romain Guy171c5922011-01-06 10:04:23 -08003479 case R.styleable.View_layerType:
3480 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3481 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003482 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003483 // Clear any text direction flag already set
3484 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3485 // Set the text direction flags depending on the value of the attribute
3486 final int textDirection = a.getInt(attr, -1);
3487 if (textDirection != -1) {
3488 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3489 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003490 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003491 case R.styleable.View_textAlignment:
3492 // Clear any text alignment flag already set
3493 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3494 // Set the text alignment flag depending on the value of the attribute
3495 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3496 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3497 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003498 case R.styleable.View_importantForAccessibility:
3499 setImportantForAccessibility(a.getInt(attr,
3500 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 }
3502 }
3503
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003504 a.recycle();
3505
Adam Powell637d3372010-08-25 14:37:03 -07003506 setOverScrollMode(overScrollMode);
3507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003509 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 }
3511
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003512 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3513 // layout direction). Those cached values will be used later during padding resolution.
3514 mUserPaddingStart = startPadding;
3515 mUserPaddingEnd = endPadding;
3516
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003517 updateUserPaddingRelative();
3518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 if (padding >= 0) {
3520 leftPadding = padding;
3521 topPadding = padding;
3522 rightPadding = padding;
3523 bottomPadding = padding;
3524 }
3525
3526 // If the user specified the padding (either with android:padding or
3527 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3528 // use the default padding or the padding from the background drawable
3529 // (stored at this point in mPadding*)
3530 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3531 topPadding >= 0 ? topPadding : mPaddingTop,
3532 rightPadding >= 0 ? rightPadding : mPaddingRight,
3533 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3534
3535 if (viewFlagMasks != 0) {
3536 setFlags(viewFlagValues, viewFlagMasks);
3537 }
3538
3539 // Needs to be called after mViewFlags is set
3540 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3541 recomputePadding();
3542 }
3543
3544 if (x != 0 || y != 0) {
3545 scrollTo(x, y);
3546 }
3547
Chet Haase73066682010-11-29 15:55:32 -08003548 if (transformSet) {
3549 setTranslationX(tx);
3550 setTranslationY(ty);
3551 setRotation(rotation);
3552 setRotationX(rotationX);
3553 setRotationY(rotationY);
3554 setScaleX(sx);
3555 setScaleY(sy);
3556 }
3557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3559 setScrollContainer(true);
3560 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003561
3562 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
3564
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003565 private void updateUserPaddingRelative() {
3566 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3567 }
3568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 /**
3570 * Non-public constructor for use in testing
3571 */
3572 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003573 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
3575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 /**
3577 * <p>
3578 * Initializes the fading edges from a given set of styled attributes. This
3579 * method should be called by subclasses that need fading edges and when an
3580 * instance of these subclasses is created programmatically rather than
3581 * being inflated from XML. This method is automatically called when the XML
3582 * is inflated.
3583 * </p>
3584 *
3585 * @param a the styled attributes set to initialize the fading edges from
3586 */
3587 protected void initializeFadingEdge(TypedArray a) {
3588 initScrollCache();
3589
3590 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3591 R.styleable.View_fadingEdgeLength,
3592 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3593 }
3594
3595 /**
3596 * Returns the size of the vertical faded edges used to indicate that more
3597 * content in this view is visible.
3598 *
3599 * @return The size in pixels of the vertical faded edge or 0 if vertical
3600 * faded edges are not enabled for this view.
3601 * @attr ref android.R.styleable#View_fadingEdgeLength
3602 */
3603 public int getVerticalFadingEdgeLength() {
3604 if (isVerticalFadingEdgeEnabled()) {
3605 ScrollabilityCache cache = mScrollCache;
3606 if (cache != null) {
3607 return cache.fadingEdgeLength;
3608 }
3609 }
3610 return 0;
3611 }
3612
3613 /**
3614 * Set the size of the faded edge used to indicate that more content in this
3615 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003616 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3617 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3618 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 *
3620 * @param length The size in pixels of the faded edge used to indicate that more
3621 * content in this view is visible.
3622 */
3623 public void setFadingEdgeLength(int length) {
3624 initScrollCache();
3625 mScrollCache.fadingEdgeLength = length;
3626 }
3627
3628 /**
3629 * Returns the size of the horizontal faded edges used to indicate that more
3630 * content in this view is visible.
3631 *
3632 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3633 * faded edges are not enabled for this view.
3634 * @attr ref android.R.styleable#View_fadingEdgeLength
3635 */
3636 public int getHorizontalFadingEdgeLength() {
3637 if (isHorizontalFadingEdgeEnabled()) {
3638 ScrollabilityCache cache = mScrollCache;
3639 if (cache != null) {
3640 return cache.fadingEdgeLength;
3641 }
3642 }
3643 return 0;
3644 }
3645
3646 /**
3647 * Returns the width of the vertical scrollbar.
3648 *
3649 * @return The width in pixels of the vertical scrollbar or 0 if there
3650 * is no vertical scrollbar.
3651 */
3652 public int getVerticalScrollbarWidth() {
3653 ScrollabilityCache cache = mScrollCache;
3654 if (cache != null) {
3655 ScrollBarDrawable scrollBar = cache.scrollBar;
3656 if (scrollBar != null) {
3657 int size = scrollBar.getSize(true);
3658 if (size <= 0) {
3659 size = cache.scrollBarSize;
3660 }
3661 return size;
3662 }
3663 return 0;
3664 }
3665 return 0;
3666 }
3667
3668 /**
3669 * Returns the height of the horizontal scrollbar.
3670 *
3671 * @return The height in pixels of the horizontal scrollbar or 0 if
3672 * there is no horizontal scrollbar.
3673 */
3674 protected int getHorizontalScrollbarHeight() {
3675 ScrollabilityCache cache = mScrollCache;
3676 if (cache != null) {
3677 ScrollBarDrawable scrollBar = cache.scrollBar;
3678 if (scrollBar != null) {
3679 int size = scrollBar.getSize(false);
3680 if (size <= 0) {
3681 size = cache.scrollBarSize;
3682 }
3683 return size;
3684 }
3685 return 0;
3686 }
3687 return 0;
3688 }
3689
3690 /**
3691 * <p>
3692 * Initializes the scrollbars from a given set of styled attributes. This
3693 * method should be called by subclasses that need scrollbars and when an
3694 * instance of these subclasses is created programmatically rather than
3695 * being inflated from XML. This method is automatically called when the XML
3696 * is inflated.
3697 * </p>
3698 *
3699 * @param a the styled attributes set to initialize the scrollbars from
3700 */
3701 protected void initializeScrollbars(TypedArray a) {
3702 initScrollCache();
3703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003705
Mike Cleronf116bf82009-09-27 19:14:12 -07003706 if (scrollabilityCache.scrollBar == null) {
3707 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3708 }
Joe Malin32736f02011-01-19 16:14:20 -08003709
Romain Guy8bda2482010-03-02 11:42:11 -08003710 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711
Mike Cleronf116bf82009-09-27 19:14:12 -07003712 if (!fadeScrollbars) {
3713 scrollabilityCache.state = ScrollabilityCache.ON;
3714 }
3715 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003716
3717
Mike Cleronf116bf82009-09-27 19:14:12 -07003718 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3719 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3720 .getScrollBarFadeDuration());
3721 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3722 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3723 ViewConfiguration.getScrollDefaultDelay());
3724
Joe Malin32736f02011-01-19 16:14:20 -08003725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3727 com.android.internal.R.styleable.View_scrollbarSize,
3728 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3729
3730 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3731 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3732
3733 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3734 if (thumb != null) {
3735 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3736 }
3737
3738 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3739 false);
3740 if (alwaysDraw) {
3741 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3742 }
3743
3744 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3745 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3746
3747 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3748 if (thumb != null) {
3749 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3750 }
3751
3752 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3753 false);
3754 if (alwaysDraw) {
3755 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3756 }
3757
3758 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003759 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761
3762 /**
3763 * <p>
3764 * Initalizes the scrollability cache if necessary.
3765 * </p>
3766 */
3767 private void initScrollCache() {
3768 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003769 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771 }
3772
Philip Milne6c8ea062012-04-03 17:38:43 -07003773 private ScrollabilityCache getScrollCache() {
3774 initScrollCache();
3775 return mScrollCache;
3776 }
3777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003778 /**
Adam Powell20232d02010-12-08 21:08:53 -08003779 * Set the position of the vertical scroll bar. Should be one of
3780 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3781 * {@link #SCROLLBAR_POSITION_RIGHT}.
3782 *
3783 * @param position Where the vertical scroll bar should be positioned.
3784 */
3785 public void setVerticalScrollbarPosition(int position) {
3786 if (mVerticalScrollbarPosition != position) {
3787 mVerticalScrollbarPosition = position;
3788 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003789 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003790 }
3791 }
3792
3793 /**
3794 * @return The position where the vertical scroll bar will show, if applicable.
3795 * @see #setVerticalScrollbarPosition(int)
3796 */
3797 public int getVerticalScrollbarPosition() {
3798 return mVerticalScrollbarPosition;
3799 }
3800
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003801 ListenerInfo getListenerInfo() {
3802 if (mListenerInfo != null) {
3803 return mListenerInfo;
3804 }
3805 mListenerInfo = new ListenerInfo();
3806 return mListenerInfo;
3807 }
3808
Adam Powell20232d02010-12-08 21:08:53 -08003809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 * Register a callback to be invoked when focus of this view changed.
3811 *
3812 * @param l The callback that will run.
3813 */
3814 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003815 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
3817
3818 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003819 * Add a listener that will be called when the bounds of the view change due to
3820 * layout processing.
3821 *
3822 * @param listener The listener that will be called when layout bounds change.
3823 */
3824 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003825 ListenerInfo li = getListenerInfo();
3826 if (li.mOnLayoutChangeListeners == null) {
3827 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003828 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003829 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3830 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003831 }
Chet Haase21cd1382010-09-01 17:42:29 -07003832 }
3833
3834 /**
3835 * Remove a listener for layout changes.
3836 *
3837 * @param listener The listener for layout bounds change.
3838 */
3839 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003840 ListenerInfo li = mListenerInfo;
3841 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003842 return;
3843 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003844 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003845 }
3846
3847 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003848 * Add a listener for attach state changes.
3849 *
3850 * This listener will be called whenever this view is attached or detached
3851 * from a window. Remove the listener using
3852 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3853 *
3854 * @param listener Listener to attach
3855 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3856 */
3857 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003858 ListenerInfo li = getListenerInfo();
3859 if (li.mOnAttachStateChangeListeners == null) {
3860 li.mOnAttachStateChangeListeners
3861 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003862 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003863 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003864 }
3865
3866 /**
3867 * Remove a listener for attach state changes. The listener will receive no further
3868 * notification of window attach/detach events.
3869 *
3870 * @param listener Listener to remove
3871 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3872 */
3873 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003874 ListenerInfo li = mListenerInfo;
3875 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003876 return;
3877 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003878 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003879 }
3880
3881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 * Returns the focus-change callback registered for this view.
3883 *
3884 * @return The callback, or null if one is not registered.
3885 */
3886 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003887 ListenerInfo li = mListenerInfo;
3888 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 }
3890
3891 /**
3892 * Register a callback to be invoked when this view is clicked. If this view is not
3893 * clickable, it becomes clickable.
3894 *
3895 * @param l The callback that will run
3896 *
3897 * @see #setClickable(boolean)
3898 */
3899 public void setOnClickListener(OnClickListener l) {
3900 if (!isClickable()) {
3901 setClickable(true);
3902 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003903 getListenerInfo().mOnClickListener = l;
3904 }
3905
3906 /**
3907 * Return whether this view has an attached OnClickListener. Returns
3908 * true if there is a listener, false if there is none.
3909 */
3910 public boolean hasOnClickListeners() {
3911 ListenerInfo li = mListenerInfo;
3912 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 }
3914
3915 /**
3916 * Register a callback to be invoked when this view is clicked and held. If this view is not
3917 * long clickable, it becomes long clickable.
3918 *
3919 * @param l The callback that will run
3920 *
3921 * @see #setLongClickable(boolean)
3922 */
3923 public void setOnLongClickListener(OnLongClickListener l) {
3924 if (!isLongClickable()) {
3925 setLongClickable(true);
3926 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003927 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 }
3929
3930 /**
3931 * Register a callback to be invoked when the context menu for this view is
3932 * being built. If this view is not long clickable, it becomes long clickable.
3933 *
3934 * @param l The callback that will run
3935 *
3936 */
3937 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3938 if (!isLongClickable()) {
3939 setLongClickable(true);
3940 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003941 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942 }
3943
3944 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003945 * Call this view's OnClickListener, if it is defined. Performs all normal
3946 * actions associated with clicking: reporting accessibility event, playing
3947 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 *
3949 * @return True there was an assigned OnClickListener that was called, false
3950 * otherwise is returned.
3951 */
3952 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003953 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3954
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 ListenerInfo li = mListenerInfo;
3956 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003958 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 return true;
3960 }
3961
3962 return false;
3963 }
3964
3965 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003966 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3967 * this only calls the listener, and does not do any associated clicking
3968 * actions like reporting an accessibility event.
3969 *
3970 * @return True there was an assigned OnClickListener that was called, false
3971 * otherwise is returned.
3972 */
3973 public boolean callOnClick() {
3974 ListenerInfo li = mListenerInfo;
3975 if (li != null && li.mOnClickListener != null) {
3976 li.mOnClickListener.onClick(this);
3977 return true;
3978 }
3979 return false;
3980 }
3981
3982 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003983 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3984 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003986 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 */
3988 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003989 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003992 ListenerInfo li = mListenerInfo;
3993 if (li != null && li.mOnLongClickListener != null) {
3994 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996 if (!handled) {
3997 handled = showContextMenu();
3998 }
3999 if (handled) {
4000 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4001 }
4002 return handled;
4003 }
4004
4005 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004006 * Performs button-related actions during a touch down event.
4007 *
4008 * @param event The event.
4009 * @return True if the down was consumed.
4010 *
4011 * @hide
4012 */
4013 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4014 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4015 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4016 return true;
4017 }
4018 }
4019 return false;
4020 }
4021
4022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 * Bring up the context menu for this view.
4024 *
4025 * @return Whether a context menu was displayed.
4026 */
4027 public boolean showContextMenu() {
4028 return getParent().showContextMenuForChild(this);
4029 }
4030
4031 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004032 * Bring up the context menu for this view, referring to the item under the specified point.
4033 *
4034 * @param x The referenced x coordinate.
4035 * @param y The referenced y coordinate.
4036 * @param metaState The keyboard modifiers that were pressed.
4037 * @return Whether a context menu was displayed.
4038 *
4039 * @hide
4040 */
4041 public boolean showContextMenu(float x, float y, int metaState) {
4042 return showContextMenu();
4043 }
4044
4045 /**
Adam Powell6e346362010-07-23 10:18:23 -07004046 * Start an action mode.
4047 *
4048 * @param callback Callback that will control the lifecycle of the action mode
4049 * @return The new action mode if it is started, null otherwise
4050 *
4051 * @see ActionMode
4052 */
4053 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004054 ViewParent parent = getParent();
4055 if (parent == null) return null;
4056 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004057 }
4058
4059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 * Register a callback to be invoked when a key is pressed in this view.
4061 * @param l the key listener to attach to this view
4062 */
4063 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 }
4066
4067 /**
4068 * Register a callback to be invoked when a touch event is sent to this view.
4069 * @param l the touch listener to attach to this view
4070 */
4071 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004072 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 }
4074
4075 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004076 * Register a callback to be invoked when a generic motion event is sent to this view.
4077 * @param l the generic motion listener to attach to this view
4078 */
4079 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004080 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004081 }
4082
4083 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004084 * Register a callback to be invoked when a hover event is sent to this view.
4085 * @param l the hover listener to attach to this view
4086 */
4087 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004088 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004089 }
4090
4091 /**
Joe Malin32736f02011-01-19 16:14:20 -08004092 * Register a drag event listener callback object for this View. The parameter is
4093 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4094 * View, the system calls the
4095 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4096 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004097 */
4098 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004099 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004100 }
4101
4102 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004103 * Give this view focus. This will cause
4104 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 *
4106 * Note: this does not check whether this {@link View} should get focus, it just
4107 * gives it focus no matter what. It should only be called internally by framework
4108 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4109 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004110 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4111 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 * focus moved when requestFocus() is called. It may not always
4113 * apply, in which case use the default View.FOCUS_DOWN.
4114 * @param previouslyFocusedRect The rectangle of the view that had focus
4115 * prior in this View's coordinate system.
4116 */
4117 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4118 if (DBG) {
4119 System.out.println(this + " requestFocus()");
4120 }
4121
4122 if ((mPrivateFlags & FOCUSED) == 0) {
4123 mPrivateFlags |= FOCUSED;
4124
4125 if (mParent != null) {
4126 mParent.requestChildFocus(this, this);
4127 }
4128
4129 onFocusChanged(true, direction, previouslyFocusedRect);
4130 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004131
4132 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4133 notifyAccessibilityStateChanged();
4134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
4136 }
4137
4138 /**
4139 * Request that a rectangle of this view be visible on the screen,
4140 * scrolling if necessary just enough.
4141 *
4142 * <p>A View should call this if it maintains some notion of which part
4143 * of its content is interesting. For example, a text editing view
4144 * should call this when its cursor moves.
4145 *
4146 * @param rectangle The rectangle.
4147 * @return Whether any parent scrolled.
4148 */
4149 public boolean requestRectangleOnScreen(Rect rectangle) {
4150 return requestRectangleOnScreen(rectangle, false);
4151 }
4152
4153 /**
4154 * Request that a rectangle of this view be visible on the screen,
4155 * scrolling if necessary just enough.
4156 *
4157 * <p>A View should call this if it maintains some notion of which part
4158 * of its content is interesting. For example, a text editing view
4159 * should call this when its cursor moves.
4160 *
4161 * <p>When <code>immediate</code> is set to true, scrolling will not be
4162 * animated.
4163 *
4164 * @param rectangle The rectangle.
4165 * @param immediate True to forbid animated scrolling, false otherwise
4166 * @return Whether any parent scrolled.
4167 */
4168 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4169 View child = this;
4170 ViewParent parent = mParent;
4171 boolean scrolled = false;
4172 while (parent != null) {
4173 scrolled |= parent.requestChildRectangleOnScreen(child,
4174 rectangle, immediate);
4175
4176 // offset rect so next call has the rectangle in the
4177 // coordinate system of its direct child.
4178 rectangle.offset(child.getLeft(), child.getTop());
4179 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4180
4181 if (!(parent instanceof View)) {
4182 break;
4183 }
Romain Guy8506ab42009-06-11 17:35:47 -07004184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 child = (View) parent;
4186 parent = child.getParent();
4187 }
4188 return scrolled;
4189 }
4190
4191 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004192 * Called when this view wants to give up focus. If focus is cleared
4193 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4194 * <p>
4195 * <strong>Note:</strong> When a View clears focus the framework is trying
4196 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004197 * View is the first from the top that can take focus, then all callbacks
4198 * related to clearing focus will be invoked after wich the framework will
4199 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004200 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 */
4202 public void clearFocus() {
4203 if (DBG) {
4204 System.out.println(this + " clearFocus()");
4205 }
4206
4207 if ((mPrivateFlags & FOCUSED) != 0) {
4208 mPrivateFlags &= ~FOCUSED;
4209
4210 if (mParent != null) {
4211 mParent.clearChildFocus(this);
4212 }
4213
4214 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004217
4218 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004219
4220 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4221 notifyAccessibilityStateChanged();
4222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
4224 }
4225
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004226 void ensureInputFocusOnFirstFocusable() {
4227 View root = getRootView();
4228 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004229 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 }
4231 }
4232
4233 /**
4234 * Called internally by the view system when a new view is getting focus.
4235 * This is what clears the old focus.
4236 */
4237 void unFocus() {
4238 if (DBG) {
4239 System.out.println(this + " unFocus()");
4240 }
4241
4242 if ((mPrivateFlags & FOCUSED) != 0) {
4243 mPrivateFlags &= ~FOCUSED;
4244
4245 onFocusChanged(false, 0, null);
4246 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004247
4248 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4249 notifyAccessibilityStateChanged();
4250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004251 }
4252 }
4253
4254 /**
4255 * Returns true if this view has focus iteself, or is the ancestor of the
4256 * view that has focus.
4257 *
4258 * @return True if this view has or contains focus, false otherwise.
4259 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004260 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 public boolean hasFocus() {
4262 return (mPrivateFlags & FOCUSED) != 0;
4263 }
4264
4265 /**
4266 * Returns true if this view is focusable or if it contains a reachable View
4267 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4268 * is a View whose parents do not block descendants focus.
4269 *
4270 * Only {@link #VISIBLE} views are considered focusable.
4271 *
4272 * @return True if the view is focusable or if the view contains a focusable
4273 * View, false otherwise.
4274 *
4275 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4276 */
4277 public boolean hasFocusable() {
4278 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4279 }
4280
4281 /**
4282 * Called by the view system when the focus state of this view changes.
4283 * When the focus change event is caused by directional navigation, direction
4284 * and previouslyFocusedRect provide insight into where the focus is coming from.
4285 * When overriding, be sure to call up through to the super class so that
4286 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004287 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 * @param gainFocus True if the View has focus; false otherwise.
4289 * @param direction The direction focus has moved when requestFocus()
4290 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004291 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4292 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4293 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4295 * system, of the previously focused view. If applicable, this will be
4296 * passed in as finer grained information about where the focus is coming
4297 * from (in addition to direction). Will be <code>null</code> otherwise.
4298 */
4299 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004300 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004301 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4302 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4303 requestAccessibilityFocus();
4304 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004305 }
4306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 InputMethodManager imm = InputMethodManager.peekInstance();
4308 if (!gainFocus) {
4309 if (isPressed()) {
4310 setPressed(false);
4311 }
4312 if (imm != null && mAttachInfo != null
4313 && mAttachInfo.mHasWindowFocus) {
4314 imm.focusOut(this);
4315 }
Romain Guya2431d02009-04-30 16:30:00 -07004316 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 } else if (imm != null && mAttachInfo != null
4318 && mAttachInfo.mHasWindowFocus) {
4319 imm.focusIn(this);
4320 }
Romain Guy8506ab42009-06-11 17:35:47 -07004321
Romain Guy0fd89bf2011-01-26 15:41:30 -08004322 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004323 ListenerInfo li = mListenerInfo;
4324 if (li != null && li.mOnFocusChangeListener != null) {
4325 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 }
Joe Malin32736f02011-01-19 16:14:20 -08004327
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004328 if (mAttachInfo != null) {
4329 mAttachInfo.mKeyDispatchState.reset(this);
4330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
4332
4333 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004334 * Sends an accessibility event of the given type. If accessiiblity is
4335 * not enabled this method has no effect. The default implementation calls
4336 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4337 * to populate information about the event source (this View), then calls
4338 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4339 * populate the text content of the event source including its descendants,
4340 * and last calls
4341 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4342 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004343 * <p>
4344 * If an {@link AccessibilityDelegate} has been specified via calling
4345 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4346 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4347 * responsible for handling this call.
4348 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004349 *
Scott Mainb303d832011-10-12 16:45:18 -07004350 * @param eventType The type of the event to send, as defined by several types from
4351 * {@link android.view.accessibility.AccessibilityEvent}, such as
4352 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4353 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004354 *
4355 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4356 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4357 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004358 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004359 */
4360 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004361 if (mAccessibilityDelegate != null) {
4362 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4363 } else {
4364 sendAccessibilityEventInternal(eventType);
4365 }
4366 }
4367
4368 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004369 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4370 * {@link AccessibilityEvent} to make an announcement which is related to some
4371 * sort of a context change for which none of the events representing UI transitions
4372 * is a good fit. For example, announcing a new page in a book. If accessibility
4373 * is not enabled this method does nothing.
4374 *
4375 * @param text The announcement text.
4376 */
4377 public void announceForAccessibility(CharSequence text) {
4378 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4379 AccessibilityEvent event = AccessibilityEvent.obtain(
4380 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4381 event.getText().add(text);
4382 sendAccessibilityEventUnchecked(event);
4383 }
4384 }
4385
4386 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004387 * @see #sendAccessibilityEvent(int)
4388 *
4389 * Note: Called from the default {@link AccessibilityDelegate}.
4390 */
4391 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004392 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4393 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4394 }
4395 }
4396
4397 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004398 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4399 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004400 * perform a check whether accessibility is enabled.
4401 * <p>
4402 * If an {@link AccessibilityDelegate} has been specified via calling
4403 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4404 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4405 * is responsible for handling this call.
4406 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004407 *
4408 * @param event The event to send.
4409 *
4410 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004411 */
4412 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004413 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004414 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004415 } else {
4416 sendAccessibilityEventUncheckedInternal(event);
4417 }
4418 }
4419
4420 /**
4421 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4422 *
4423 * Note: Called from the default {@link AccessibilityDelegate}.
4424 */
4425 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004426 if (!isShown()) {
4427 return;
4428 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004429 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004430 // Only a subset of accessibility events populates text content.
4431 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4432 dispatchPopulateAccessibilityEvent(event);
4433 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004434 // Intercept accessibility focus events fired by virtual nodes to keep
4435 // track of accessibility focus position in such nodes.
4436 final int eventType = event.getEventType();
4437 switch (eventType) {
4438 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4439 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4440 event.getSourceNodeId());
4441 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4442 ViewRootImpl viewRootImpl = getViewRootImpl();
4443 if (viewRootImpl != null) {
4444 viewRootImpl.setAccessibilityFocusedHost(this);
4445 }
4446 }
4447 } break;
4448 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4449 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4450 event.getSourceNodeId());
4451 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4452 ViewRootImpl viewRootImpl = getViewRootImpl();
4453 if (viewRootImpl != null) {
4454 viewRootImpl.setAccessibilityFocusedHost(null);
4455 }
4456 }
4457 } break;
4458 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004459 // In the beginning we called #isShown(), so we know that getParent() is not null.
4460 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004461 }
4462
4463 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004464 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4465 * to its children for adding their text content to the event. Note that the
4466 * event text is populated in a separate dispatch path since we add to the
4467 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004468 * A typical implementation will call
4469 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4470 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4471 * on each child. Override this method if custom population of the event text
4472 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004473 * <p>
4474 * If an {@link AccessibilityDelegate} has been specified via calling
4475 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4476 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4477 * is responsible for handling this call.
4478 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004479 * <p>
4480 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4481 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4482 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004483 *
4484 * @param event The event.
4485 *
4486 * @return True if the event population was completed.
4487 */
4488 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004489 if (mAccessibilityDelegate != null) {
4490 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4491 } else {
4492 return dispatchPopulateAccessibilityEventInternal(event);
4493 }
4494 }
4495
4496 /**
4497 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4498 *
4499 * Note: Called from the default {@link AccessibilityDelegate}.
4500 */
4501 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004502 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004503 return false;
4504 }
4505
4506 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004507 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004508 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004509 * text content. While this method is free to modify event
4510 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004511 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4512 * <p>
4513 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004514 * to the text added by the super implementation:
4515 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004516 * super.onPopulateAccessibilityEvent(event);
4517 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4518 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4519 * mCurrentDate.getTimeInMillis(), flags);
4520 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004521 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004522 * <p>
4523 * If an {@link AccessibilityDelegate} has been specified via calling
4524 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4525 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4526 * is responsible for handling this call.
4527 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004528 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4529 * information to the event, in case the default implementation has basic information to add.
4530 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004531 *
4532 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004533 *
4534 * @see #sendAccessibilityEvent(int)
4535 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004536 */
4537 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004538 if (mAccessibilityDelegate != null) {
4539 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4540 } else {
4541 onPopulateAccessibilityEventInternal(event);
4542 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004543 }
4544
4545 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004546 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4547 *
4548 * Note: Called from the default {@link AccessibilityDelegate}.
4549 */
4550 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4551
4552 }
4553
4554 /**
4555 * Initializes an {@link AccessibilityEvent} with information about
4556 * this View which is the event source. In other words, the source of
4557 * an accessibility event is the view whose state change triggered firing
4558 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004559 * <p>
4560 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004561 * to properties set by the super implementation:
4562 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4563 * super.onInitializeAccessibilityEvent(event);
4564 * event.setPassword(true);
4565 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004566 * <p>
4567 * If an {@link AccessibilityDelegate} has been specified via calling
4568 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4569 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4570 * is responsible for handling this call.
4571 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004572 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4573 * information to the event, in case the default implementation has basic information to add.
4574 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004575 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004576 *
4577 * @see #sendAccessibilityEvent(int)
4578 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4579 */
4580 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004581 if (mAccessibilityDelegate != null) {
4582 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4583 } else {
4584 onInitializeAccessibilityEventInternal(event);
4585 }
4586 }
4587
4588 /**
4589 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4590 *
4591 * Note: Called from the default {@link AccessibilityDelegate}.
4592 */
4593 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004594 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004595 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004596 event.setPackageName(getContext().getPackageName());
4597 event.setEnabled(isEnabled());
4598 event.setContentDescription(mContentDescription);
4599
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004600 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004601 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004602 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4603 FOCUSABLES_ALL);
4604 event.setItemCount(focusablesTempList.size());
4605 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4606 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004607 }
4608 }
4609
4610 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004611 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4612 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4613 * This method is responsible for obtaining an accessibility node info from a
4614 * pool of reusable instances and calling
4615 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4616 * initialize the former.
4617 * <p>
4618 * Note: The client is responsible for recycling the obtained instance by calling
4619 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4620 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004621 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004622 * @return A populated {@link AccessibilityNodeInfo}.
4623 *
4624 * @see AccessibilityNodeInfo
4625 */
4626 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004627 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4628 if (provider != null) {
4629 return provider.createAccessibilityNodeInfo(View.NO_ID);
4630 } else {
4631 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4632 onInitializeAccessibilityNodeInfo(info);
4633 return info;
4634 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004635 }
4636
4637 /**
4638 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4639 * The base implementation sets:
4640 * <ul>
4641 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004642 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4643 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004644 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4645 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4646 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4647 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4648 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4649 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4650 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4651 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4652 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4653 * </ul>
4654 * <p>
4655 * Subclasses should override this method, call the super implementation,
4656 * and set additional attributes.
4657 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * <p>
4659 * If an {@link AccessibilityDelegate} has been specified via calling
4660 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4661 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4662 * is responsible for handling this call.
4663 * </p>
4664 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004665 * @param info The instance to initialize.
4666 */
4667 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004668 if (mAccessibilityDelegate != null) {
4669 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4670 } else {
4671 onInitializeAccessibilityNodeInfoInternal(info);
4672 }
4673 }
4674
4675 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004676 * Gets the location of this view in screen coordintates.
4677 *
4678 * @param outRect The output location
4679 */
4680 private void getBoundsOnScreen(Rect outRect) {
4681 if (mAttachInfo == null) {
4682 return;
4683 }
4684
4685 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004686 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004687
4688 if (!hasIdentityMatrix()) {
4689 getMatrix().mapRect(position);
4690 }
4691
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004692 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004693
4694 ViewParent parent = mParent;
4695 while (parent instanceof View) {
4696 View parentView = (View) parent;
4697
4698 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4699
4700 if (!parentView.hasIdentityMatrix()) {
4701 parentView.getMatrix().mapRect(position);
4702 }
4703
4704 position.offset(parentView.mLeft, parentView.mTop);
4705
4706 parent = parentView.mParent;
4707 }
4708
4709 if (parent instanceof ViewRootImpl) {
4710 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4711 position.offset(0, -viewRootImpl.mCurScrollY);
4712 }
4713
4714 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4715
4716 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4717 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4718 }
4719
4720 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004721 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4722 *
4723 * Note: Called from the default {@link AccessibilityDelegate}.
4724 */
4725 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004726 Rect bounds = mAttachInfo.mTmpInvalRect;
4727 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004728 info.setBoundsInParent(bounds);
4729
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004730 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004731 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004732
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004733 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004734 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004735 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004736 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004737 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004738 }
4739
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004740 info.setVisibleToUser(isVisibleToUser());
4741
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004742 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004743 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004744 info.setContentDescription(getContentDescription());
4745
4746 info.setEnabled(isEnabled());
4747 info.setClickable(isClickable());
4748 info.setFocusable(isFocusable());
4749 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004750 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004751 info.setSelected(isSelected());
4752 info.setLongClickable(isLongClickable());
4753
4754 // TODO: These make sense only if we are in an AdapterView but all
4755 // views can be selected. Maybe from accessiiblity perspective
4756 // we should report as selectable view in an AdapterView.
4757 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4758 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4759
4760 if (isFocusable()) {
4761 if (isFocused()) {
4762 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4763 } else {
4764 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4765 }
4766 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004767
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004768 if (!isAccessibilityFocused()) {
4769 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4770 } else {
4771 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4772 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004773
4774 if (isClickable()) {
4775 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4776 }
4777
4778 if (isLongClickable()) {
4779 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4780 }
4781
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004782 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004783 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4784 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4785 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004786 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4787 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004788 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004789 }
4790
4791 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004792 * Computes whether this view is visible to the user. Such a view is
4793 * attached, visible, all its predecessors are visible, it is not clipped
4794 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004795 *
4796 * @return Whether the view is visible on the screen.
4797 */
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004798 private boolean isVisibleToUser() {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004799 // The first two checks are made also made by isShown() which
4800 // however traverses the tree up to the parent to catch that.
4801 // Therefore, we do some fail fast check to minimize the up
4802 // tree traversal.
4803 return (mAttachInfo != null
4804 && mAttachInfo.mWindowVisibility == View.VISIBLE
4805 && getAlpha() > 0
4806 && isShown()
4807 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4808 }
4809
4810 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004811 * Sets a delegate for implementing accessibility support via compositon as
4812 * opposed to inheritance. The delegate's primary use is for implementing
4813 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4814 *
4815 * @param delegate The delegate instance.
4816 *
4817 * @see AccessibilityDelegate
4818 */
4819 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4820 mAccessibilityDelegate = delegate;
4821 }
4822
4823 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004824 * Gets the provider for managing a virtual view hierarchy rooted at this View
4825 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4826 * that explore the window content.
4827 * <p>
4828 * If this method returns an instance, this instance is responsible for managing
4829 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4830 * View including the one representing the View itself. Similarly the returned
4831 * instance is responsible for performing accessibility actions on any virtual
4832 * view or the root view itself.
4833 * </p>
4834 * <p>
4835 * If an {@link AccessibilityDelegate} has been specified via calling
4836 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4837 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4838 * is responsible for handling this call.
4839 * </p>
4840 *
4841 * @return The provider.
4842 *
4843 * @see AccessibilityNodeProvider
4844 */
4845 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4846 if (mAccessibilityDelegate != null) {
4847 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4848 } else {
4849 return null;
4850 }
4851 }
4852
4853 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854 * Gets the unique identifier of this view on the screen for accessibility purposes.
4855 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4856 *
4857 * @return The view accessibility id.
4858 *
4859 * @hide
4860 */
4861 public int getAccessibilityViewId() {
4862 if (mAccessibilityViewId == NO_ID) {
4863 mAccessibilityViewId = sNextAccessibilityViewId++;
4864 }
4865 return mAccessibilityViewId;
4866 }
4867
4868 /**
4869 * Gets the unique identifier of the window in which this View reseides.
4870 *
4871 * @return The window accessibility id.
4872 *
4873 * @hide
4874 */
4875 public int getAccessibilityWindowId() {
4876 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4877 }
4878
4879 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004880 * Gets the {@link View} description. It briefly describes the view and is
4881 * primarily used for accessibility support. Set this property to enable
4882 * better accessibility support for your application. This is especially
4883 * true for views that do not have textual representation (For example,
4884 * ImageButton).
4885 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004886 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004887 *
4888 * @attr ref android.R.styleable#View_contentDescription
4889 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004890 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004891 public CharSequence getContentDescription() {
4892 return mContentDescription;
4893 }
4894
4895 /**
4896 * Sets the {@link View} description. It briefly describes the view and is
4897 * primarily used for accessibility support. Set this property to enable
4898 * better accessibility support for your application. This is especially
4899 * true for views that do not have textual representation (For example,
4900 * ImageButton).
4901 *
4902 * @param contentDescription The content description.
4903 *
4904 * @attr ref android.R.styleable#View_contentDescription
4905 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004906 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004907 public void setContentDescription(CharSequence contentDescription) {
4908 mContentDescription = contentDescription;
4909 }
4910
4911 /**
Romain Guya2431d02009-04-30 16:30:00 -07004912 * Invoked whenever this view loses focus, either by losing window focus or by losing
4913 * focus within its window. This method can be used to clear any state tied to the
4914 * focus. For instance, if a button is held pressed with the trackball and the window
4915 * loses focus, this method can be used to cancel the press.
4916 *
4917 * Subclasses of View overriding this method should always call super.onFocusLost().
4918 *
4919 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004920 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004921 *
4922 * @hide pending API council approval
4923 */
4924 protected void onFocusLost() {
4925 resetPressedState();
4926 }
4927
4928 private void resetPressedState() {
4929 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4930 return;
4931 }
4932
4933 if (isPressed()) {
4934 setPressed(false);
4935
4936 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004937 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004938 }
4939 }
4940 }
4941
4942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004943 * Returns true if this view has focus
4944 *
4945 * @return True if this view has focus, false otherwise.
4946 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004947 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 public boolean isFocused() {
4949 return (mPrivateFlags & FOCUSED) != 0;
4950 }
4951
4952 /**
4953 * Find the view in the hierarchy rooted at this view that currently has
4954 * focus.
4955 *
4956 * @return The view that currently has focus, or null if no focused view can
4957 * be found.
4958 */
4959 public View findFocus() {
4960 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4961 }
4962
4963 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004964 * Indicates whether this view is one of the set of scrollable containers in
4965 * its window.
4966 *
4967 * @return whether this view is one of the set of scrollable containers in
4968 * its window
4969 *
4970 * @attr ref android.R.styleable#View_isScrollContainer
4971 */
4972 public boolean isScrollContainer() {
4973 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4974 }
4975
4976 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 * Change whether this view is one of the set of scrollable containers in
4978 * its window. This will be used to determine whether the window can
4979 * resize or must pan when a soft input area is open -- scrollable
4980 * containers allow the window to use resize mode since the container
4981 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004982 *
4983 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004984 */
4985 public void setScrollContainer(boolean isScrollContainer) {
4986 if (isScrollContainer) {
4987 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4988 mAttachInfo.mScrollContainers.add(this);
4989 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4990 }
4991 mPrivateFlags |= SCROLL_CONTAINER;
4992 } else {
4993 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4994 mAttachInfo.mScrollContainers.remove(this);
4995 }
4996 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4997 }
4998 }
4999
5000 /**
5001 * Returns the quality of the drawing cache.
5002 *
5003 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5004 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5005 *
5006 * @see #setDrawingCacheQuality(int)
5007 * @see #setDrawingCacheEnabled(boolean)
5008 * @see #isDrawingCacheEnabled()
5009 *
5010 * @attr ref android.R.styleable#View_drawingCacheQuality
5011 */
5012 public int getDrawingCacheQuality() {
5013 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5014 }
5015
5016 /**
5017 * Set the drawing cache quality of this view. This value is used only when the
5018 * drawing cache is enabled
5019 *
5020 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5021 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5022 *
5023 * @see #getDrawingCacheQuality()
5024 * @see #setDrawingCacheEnabled(boolean)
5025 * @see #isDrawingCacheEnabled()
5026 *
5027 * @attr ref android.R.styleable#View_drawingCacheQuality
5028 */
5029 public void setDrawingCacheQuality(int quality) {
5030 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5031 }
5032
5033 /**
5034 * Returns whether the screen should remain on, corresponding to the current
5035 * value of {@link #KEEP_SCREEN_ON}.
5036 *
5037 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5038 *
5039 * @see #setKeepScreenOn(boolean)
5040 *
5041 * @attr ref android.R.styleable#View_keepScreenOn
5042 */
5043 public boolean getKeepScreenOn() {
5044 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5045 }
5046
5047 /**
5048 * Controls whether the screen should remain on, modifying the
5049 * value of {@link #KEEP_SCREEN_ON}.
5050 *
5051 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5052 *
5053 * @see #getKeepScreenOn()
5054 *
5055 * @attr ref android.R.styleable#View_keepScreenOn
5056 */
5057 public void setKeepScreenOn(boolean keepScreenOn) {
5058 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5059 }
5060
5061 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005062 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5063 * @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 -08005064 *
5065 * @attr ref android.R.styleable#View_nextFocusLeft
5066 */
5067 public int getNextFocusLeftId() {
5068 return mNextFocusLeftId;
5069 }
5070
5071 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005072 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5073 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5074 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 *
5076 * @attr ref android.R.styleable#View_nextFocusLeft
5077 */
5078 public void setNextFocusLeftId(int nextFocusLeftId) {
5079 mNextFocusLeftId = nextFocusLeftId;
5080 }
5081
5082 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005083 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5084 * @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 -08005085 *
5086 * @attr ref android.R.styleable#View_nextFocusRight
5087 */
5088 public int getNextFocusRightId() {
5089 return mNextFocusRightId;
5090 }
5091
5092 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005093 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5094 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5095 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 *
5097 * @attr ref android.R.styleable#View_nextFocusRight
5098 */
5099 public void setNextFocusRightId(int nextFocusRightId) {
5100 mNextFocusRightId = nextFocusRightId;
5101 }
5102
5103 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005104 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5105 * @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 -08005106 *
5107 * @attr ref android.R.styleable#View_nextFocusUp
5108 */
5109 public int getNextFocusUpId() {
5110 return mNextFocusUpId;
5111 }
5112
5113 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005114 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5115 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5116 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 *
5118 * @attr ref android.R.styleable#View_nextFocusUp
5119 */
5120 public void setNextFocusUpId(int nextFocusUpId) {
5121 mNextFocusUpId = nextFocusUpId;
5122 }
5123
5124 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005125 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5126 * @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 -08005127 *
5128 * @attr ref android.R.styleable#View_nextFocusDown
5129 */
5130 public int getNextFocusDownId() {
5131 return mNextFocusDownId;
5132 }
5133
5134 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005135 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5136 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5137 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 *
5139 * @attr ref android.R.styleable#View_nextFocusDown
5140 */
5141 public void setNextFocusDownId(int nextFocusDownId) {
5142 mNextFocusDownId = nextFocusDownId;
5143 }
5144
5145 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005146 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5147 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5148 *
5149 * @attr ref android.R.styleable#View_nextFocusForward
5150 */
5151 public int getNextFocusForwardId() {
5152 return mNextFocusForwardId;
5153 }
5154
5155 /**
5156 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5157 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5158 * decide automatically.
5159 *
5160 * @attr ref android.R.styleable#View_nextFocusForward
5161 */
5162 public void setNextFocusForwardId(int nextFocusForwardId) {
5163 mNextFocusForwardId = nextFocusForwardId;
5164 }
5165
5166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 * Returns the visibility of this view and all of its ancestors
5168 *
5169 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5170 */
5171 public boolean isShown() {
5172 View current = this;
5173 //noinspection ConstantConditions
5174 do {
5175 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5176 return false;
5177 }
5178 ViewParent parent = current.mParent;
5179 if (parent == null) {
5180 return false; // We are not attached to the view root
5181 }
5182 if (!(parent instanceof View)) {
5183 return true;
5184 }
5185 current = (View) parent;
5186 } while (current != null);
5187
5188 return false;
5189 }
5190
5191 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005192 * Called by the view hierarchy when the content insets for a window have
5193 * changed, to allow it to adjust its content to fit within those windows.
5194 * The content insets tell you the space that the status bar, input method,
5195 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005197 * <p>You do not normally need to deal with this function, since the default
5198 * window decoration given to applications takes care of applying it to the
5199 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5200 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5201 * and your content can be placed under those system elements. You can then
5202 * use this method within your view hierarchy if you have parts of your UI
5203 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005205 * <p>The default implementation of this method simply applies the content
5206 * inset's to the view's padding. This can be enabled through
5207 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5208 * the method and handle the insets however you would like. Note that the
5209 * insets provided by the framework are always relative to the far edges
5210 * of the window, not accounting for the location of the called view within
5211 * that window. (In fact when this method is called you do not yet know
5212 * where the layout will place the view, as it is done before layout happens.)
5213 *
5214 * <p>Note: unlike many View methods, there is no dispatch phase to this
5215 * call. If you are overriding it in a ViewGroup and want to allow the
5216 * call to continue to your children, you must be sure to call the super
5217 * implementation.
5218 *
5219 * @param insets Current content insets of the window. Prior to
5220 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5221 * the insets or else you and Android will be unhappy.
5222 *
5223 * @return Return true if this view applied the insets and it should not
5224 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 */
5226 protected boolean fitSystemWindows(Rect insets) {
5227 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005228 mUserPaddingStart = -1;
5229 mUserPaddingEnd = -1;
5230 mUserPaddingRelative = false;
5231 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5232 || mAttachInfo == null
5233 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5234 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5235 return true;
5236 } else {
5237 internalSetPadding(0, 0, 0, 0);
5238 return false;
5239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
5241 return false;
5242 }
5243
5244 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005245 * Set whether or not this view should account for system screen decorations
5246 * such as the status bar and inset its content. This allows this view to be
5247 * positioned in absolute screen coordinates and remain visible to the user.
5248 *
5249 * <p>This should only be used by top-level window decor views.
5250 *
5251 * @param fitSystemWindows true to inset content for system screen decorations, false for
5252 * default behavior.
5253 *
5254 * @attr ref android.R.styleable#View_fitsSystemWindows
5255 */
5256 public void setFitsSystemWindows(boolean fitSystemWindows) {
5257 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5258 }
5259
5260 /**
5261 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5262 * will account for system screen decorations such as the status bar and inset its
5263 * content. This allows the view to be positioned in absolute screen coordinates
5264 * and remain visible to the user.
5265 *
5266 * @return true if this view will adjust its content bounds for system screen decorations.
5267 *
5268 * @attr ref android.R.styleable#View_fitsSystemWindows
5269 */
5270 public boolean fitsSystemWindows() {
5271 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5272 }
5273
5274 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005275 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5276 */
5277 public void requestFitSystemWindows() {
5278 if (mParent != null) {
5279 mParent.requestFitSystemWindows();
5280 }
5281 }
5282
5283 /**
5284 * For use by PhoneWindow to make its own system window fitting optional.
5285 * @hide
5286 */
5287 public void makeOptionalFitsSystemWindows() {
5288 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5289 }
5290
5291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 * Returns the visibility status for this view.
5293 *
5294 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5295 * @attr ref android.R.styleable#View_visibility
5296 */
5297 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005298 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5299 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5300 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 })
5302 public int getVisibility() {
5303 return mViewFlags & VISIBILITY_MASK;
5304 }
5305
5306 /**
5307 * Set the enabled state of this view.
5308 *
5309 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5310 * @attr ref android.R.styleable#View_visibility
5311 */
5312 @RemotableViewMethod
5313 public void setVisibility(int visibility) {
5314 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005315 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 }
5317
5318 /**
5319 * Returns the enabled status for this view. The interpretation of the
5320 * enabled state varies by subclass.
5321 *
5322 * @return True if this view is enabled, false otherwise.
5323 */
5324 @ViewDebug.ExportedProperty
5325 public boolean isEnabled() {
5326 return (mViewFlags & ENABLED_MASK) == ENABLED;
5327 }
5328
5329 /**
5330 * Set the enabled state of this view. The interpretation of the enabled
5331 * state varies by subclass.
5332 *
5333 * @param enabled True if this view is enabled, false otherwise.
5334 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005335 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005337 if (enabled == isEnabled()) return;
5338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5340
5341 /*
5342 * The View most likely has to change its appearance, so refresh
5343 * the drawable state.
5344 */
5345 refreshDrawableState();
5346
5347 // Invalidate too, since the default behavior for views is to be
5348 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005349 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 }
5351
5352 /**
5353 * Set whether this view can receive the focus.
5354 *
5355 * Setting this to false will also ensure that this view is not focusable
5356 * in touch mode.
5357 *
5358 * @param focusable If true, this view can receive the focus.
5359 *
5360 * @see #setFocusableInTouchMode(boolean)
5361 * @attr ref android.R.styleable#View_focusable
5362 */
5363 public void setFocusable(boolean focusable) {
5364 if (!focusable) {
5365 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5366 }
5367 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5368 }
5369
5370 /**
5371 * Set whether this view can receive focus while in touch mode.
5372 *
5373 * Setting this to true will also ensure that this view is focusable.
5374 *
5375 * @param focusableInTouchMode If true, this view can receive the focus while
5376 * in touch mode.
5377 *
5378 * @see #setFocusable(boolean)
5379 * @attr ref android.R.styleable#View_focusableInTouchMode
5380 */
5381 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5382 // Focusable in touch mode should always be set before the focusable flag
5383 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5384 // which, in touch mode, will not successfully request focus on this view
5385 // because the focusable in touch mode flag is not set
5386 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5387 if (focusableInTouchMode) {
5388 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5389 }
5390 }
5391
5392 /**
5393 * Set whether this view should have sound effects enabled for events such as
5394 * clicking and touching.
5395 *
5396 * <p>You may wish to disable sound effects for a view if you already play sounds,
5397 * for instance, a dial key that plays dtmf tones.
5398 *
5399 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5400 * @see #isSoundEffectsEnabled()
5401 * @see #playSoundEffect(int)
5402 * @attr ref android.R.styleable#View_soundEffectsEnabled
5403 */
5404 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5405 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5406 }
5407
5408 /**
5409 * @return whether this view should have sound effects enabled for events such as
5410 * clicking and touching.
5411 *
5412 * @see #setSoundEffectsEnabled(boolean)
5413 * @see #playSoundEffect(int)
5414 * @attr ref android.R.styleable#View_soundEffectsEnabled
5415 */
5416 @ViewDebug.ExportedProperty
5417 public boolean isSoundEffectsEnabled() {
5418 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5419 }
5420
5421 /**
5422 * Set whether this view should have haptic feedback for events such as
5423 * long presses.
5424 *
5425 * <p>You may wish to disable haptic feedback if your view already controls
5426 * its own haptic feedback.
5427 *
5428 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5429 * @see #isHapticFeedbackEnabled()
5430 * @see #performHapticFeedback(int)
5431 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5432 */
5433 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5434 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5435 }
5436
5437 /**
5438 * @return whether this view should have haptic feedback enabled for events
5439 * long presses.
5440 *
5441 * @see #setHapticFeedbackEnabled(boolean)
5442 * @see #performHapticFeedback(int)
5443 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5444 */
5445 @ViewDebug.ExportedProperty
5446 public boolean isHapticFeedbackEnabled() {
5447 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5448 }
5449
5450 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005451 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005452 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005453 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5454 * {@link #LAYOUT_DIRECTION_RTL},
5455 * {@link #LAYOUT_DIRECTION_INHERIT} or
5456 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005457 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005458 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005459 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005460 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005461 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005462 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5463 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5464 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5465 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005466 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005467 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005468 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005469 }
5470
5471 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005472 * Set the layout direction for this view. This will propagate a reset of layout direction
5473 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005474 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005475 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5476 * {@link #LAYOUT_DIRECTION_RTL},
5477 * {@link #LAYOUT_DIRECTION_INHERIT} or
5478 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005479 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005480 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005481 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005482 */
5483 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005484 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005485 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005486 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005487 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005488 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005489 // Set the new layout direction (filtered) and ask for a layout pass
5490 mPrivateFlags2 |=
5491 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5492 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005493 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005494 }
5495
5496 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005497 * Returns the resolved layout direction for this view.
5498 *
5499 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005500 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005501 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005502 */
5503 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005504 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5505 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005506 })
5507 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005508 // The layout diretion will be resolved only if needed
5509 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5510 resolveLayoutDirection();
5511 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005512 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005513 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5514 }
5515
5516 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005517 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5518 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005519 *
5520 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005521 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005522 */
5523 @ViewDebug.ExportedProperty(category = "layout")
5524 public boolean isLayoutRtl() {
5525 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5526 }
5527
5528 /**
Adam Powell539ee872012-02-03 19:00:49 -08005529 * Indicates whether the view is currently tracking transient state that the
5530 * app should not need to concern itself with saving and restoring, but that
5531 * the framework should take special note to preserve when possible.
5532 *
Adam Powell785c4472012-05-02 21:25:39 -07005533 * <p>A view with transient state cannot be trivially rebound from an external
5534 * data source, such as an adapter binding item views in a list. This may be
5535 * because the view is performing an animation, tracking user selection
5536 * of content, or similar.</p>
5537 *
Adam Powell539ee872012-02-03 19:00:49 -08005538 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005539 */
5540 @ViewDebug.ExportedProperty(category = "layout")
5541 public boolean hasTransientState() {
5542 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5543 }
5544
5545 /**
5546 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005547 * framework should attempt to preserve when possible. This flag is reference counted,
5548 * so every call to setHasTransientState(true) should be paired with a later call
5549 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005550 *
Adam Powell785c4472012-05-02 21:25:39 -07005551 * <p>A view with transient state cannot be trivially rebound from an external
5552 * data source, such as an adapter binding item views in a list. This may be
5553 * because the view is performing an animation, tracking user selection
5554 * of content, or similar.</p>
5555 *
Adam Powell539ee872012-02-03 19:00:49 -08005556 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005557 */
5558 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005559 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5560 mTransientStateCount - 1;
5561 if (mTransientStateCount < 0) {
5562 mTransientStateCount = 0;
5563 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5564 "unmatched pair of setHasTransientState calls");
5565 }
5566 if ((hasTransientState && mTransientStateCount == 1) ||
5567 (hasTransientState && mTransientStateCount == 0)) {
5568 // update flag if we've just incremented up from 0 or decremented down to 0
5569 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5570 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5571 if (mParent != null) {
5572 try {
5573 mParent.childHasTransientStateChanged(this, hasTransientState);
5574 } catch (AbstractMethodError e) {
5575 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5576 " does not fully implement ViewParent", e);
5577 }
Adam Powell539ee872012-02-03 19:00:49 -08005578 }
5579 }
5580 }
5581
5582 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 * If this view doesn't do any drawing on its own, set this flag to
5584 * allow further optimizations. By default, this flag is not set on
5585 * View, but could be set on some View subclasses such as ViewGroup.
5586 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005587 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5588 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 *
5590 * @param willNotDraw whether or not this View draw on its own
5591 */
5592 public void setWillNotDraw(boolean willNotDraw) {
5593 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5594 }
5595
5596 /**
5597 * Returns whether or not this View draws on its own.
5598 *
5599 * @return true if this view has nothing to draw, false otherwise
5600 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005601 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 public boolean willNotDraw() {
5603 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5604 }
5605
5606 /**
5607 * When a View's drawing cache is enabled, drawing is redirected to an
5608 * offscreen bitmap. Some views, like an ImageView, must be able to
5609 * bypass this mechanism if they already draw a single bitmap, to avoid
5610 * unnecessary usage of the memory.
5611 *
5612 * @param willNotCacheDrawing true if this view does not cache its
5613 * drawing, false otherwise
5614 */
5615 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5616 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5617 }
5618
5619 /**
5620 * Returns whether or not this View can cache its drawing or not.
5621 *
5622 * @return true if this view does not cache its drawing, false otherwise
5623 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005624 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 public boolean willNotCacheDrawing() {
5626 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5627 }
5628
5629 /**
5630 * Indicates whether this view reacts to click events or not.
5631 *
5632 * @return true if the view is clickable, false otherwise
5633 *
5634 * @see #setClickable(boolean)
5635 * @attr ref android.R.styleable#View_clickable
5636 */
5637 @ViewDebug.ExportedProperty
5638 public boolean isClickable() {
5639 return (mViewFlags & CLICKABLE) == CLICKABLE;
5640 }
5641
5642 /**
5643 * Enables or disables click events for this view. When a view
5644 * is clickable it will change its state to "pressed" on every click.
5645 * Subclasses should set the view clickable to visually react to
5646 * user's clicks.
5647 *
5648 * @param clickable true to make the view clickable, false otherwise
5649 *
5650 * @see #isClickable()
5651 * @attr ref android.R.styleable#View_clickable
5652 */
5653 public void setClickable(boolean clickable) {
5654 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5655 }
5656
5657 /**
5658 * Indicates whether this view reacts to long click events or not.
5659 *
5660 * @return true if the view is long clickable, false otherwise
5661 *
5662 * @see #setLongClickable(boolean)
5663 * @attr ref android.R.styleable#View_longClickable
5664 */
5665 public boolean isLongClickable() {
5666 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5667 }
5668
5669 /**
5670 * Enables or disables long click events for this view. When a view is long
5671 * clickable it reacts to the user holding down the button for a longer
5672 * duration than a tap. This event can either launch the listener or a
5673 * context menu.
5674 *
5675 * @param longClickable true to make the view long clickable, false otherwise
5676 * @see #isLongClickable()
5677 * @attr ref android.R.styleable#View_longClickable
5678 */
5679 public void setLongClickable(boolean longClickable) {
5680 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5681 }
5682
5683 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005684 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 *
5686 * @see #isClickable()
5687 * @see #setClickable(boolean)
5688 *
5689 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5690 * the View's internal state from a previously set "pressed" state.
5691 */
5692 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005693 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 if (pressed) {
5696 mPrivateFlags |= PRESSED;
5697 } else {
5698 mPrivateFlags &= ~PRESSED;
5699 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005700
5701 if (needsRefresh) {
5702 refreshDrawableState();
5703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704 dispatchSetPressed(pressed);
5705 }
5706
5707 /**
5708 * Dispatch setPressed to all of this View's children.
5709 *
5710 * @see #setPressed(boolean)
5711 *
5712 * @param pressed The new pressed state
5713 */
5714 protected void dispatchSetPressed(boolean pressed) {
5715 }
5716
5717 /**
5718 * Indicates whether the view is currently in pressed state. Unless
5719 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5720 * the pressed state.
5721 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005722 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 * @see #isClickable()
5724 * @see #setClickable(boolean)
5725 *
5726 * @return true if the view is currently pressed, false otherwise
5727 */
5728 public boolean isPressed() {
5729 return (mPrivateFlags & PRESSED) == PRESSED;
5730 }
5731
5732 /**
5733 * Indicates whether this view will save its state (that is,
5734 * whether its {@link #onSaveInstanceState} method will be called).
5735 *
5736 * @return Returns true if the view state saving is enabled, else false.
5737 *
5738 * @see #setSaveEnabled(boolean)
5739 * @attr ref android.R.styleable#View_saveEnabled
5740 */
5741 public boolean isSaveEnabled() {
5742 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5743 }
5744
5745 /**
5746 * Controls whether the saving of this view's state is
5747 * enabled (that is, whether its {@link #onSaveInstanceState} method
5748 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005749 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 * for its state to be saved. This flag can only disable the
5751 * saving of this view; any child views may still have their state saved.
5752 *
5753 * @param enabled Set to false to <em>disable</em> state saving, or true
5754 * (the default) to allow it.
5755 *
5756 * @see #isSaveEnabled()
5757 * @see #setId(int)
5758 * @see #onSaveInstanceState()
5759 * @attr ref android.R.styleable#View_saveEnabled
5760 */
5761 public void setSaveEnabled(boolean enabled) {
5762 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5763 }
5764
Jeff Brown85a31762010-09-01 17:01:00 -07005765 /**
5766 * Gets whether the framework should discard touches when the view's
5767 * window is obscured by another visible window.
5768 * Refer to the {@link View} security documentation for more details.
5769 *
5770 * @return True if touch filtering is enabled.
5771 *
5772 * @see #setFilterTouchesWhenObscured(boolean)
5773 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5774 */
5775 @ViewDebug.ExportedProperty
5776 public boolean getFilterTouchesWhenObscured() {
5777 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5778 }
5779
5780 /**
5781 * Sets whether the framework should discard touches when the view's
5782 * window is obscured by another visible window.
5783 * Refer to the {@link View} security documentation for more details.
5784 *
5785 * @param enabled True if touch filtering should be enabled.
5786 *
5787 * @see #getFilterTouchesWhenObscured
5788 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5789 */
5790 public void setFilterTouchesWhenObscured(boolean enabled) {
5791 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5792 FILTER_TOUCHES_WHEN_OBSCURED);
5793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794
5795 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005796 * Indicates whether the entire hierarchy under this view will save its
5797 * state when a state saving traversal occurs from its parent. The default
5798 * is true; if false, these views will not be saved unless
5799 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5800 *
5801 * @return Returns true if the view state saving from parent is enabled, else false.
5802 *
5803 * @see #setSaveFromParentEnabled(boolean)
5804 */
5805 public boolean isSaveFromParentEnabled() {
5806 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5807 }
5808
5809 /**
5810 * Controls whether the entire hierarchy under this view will save its
5811 * state when a state saving traversal occurs from its parent. The default
5812 * is true; if false, these views will not be saved unless
5813 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5814 *
5815 * @param enabled Set to false to <em>disable</em> state saving, or true
5816 * (the default) to allow it.
5817 *
5818 * @see #isSaveFromParentEnabled()
5819 * @see #setId(int)
5820 * @see #onSaveInstanceState()
5821 */
5822 public void setSaveFromParentEnabled(boolean enabled) {
5823 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5824 }
5825
5826
5827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 * Returns whether this View is able to take focus.
5829 *
5830 * @return True if this view can take focus, or false otherwise.
5831 * @attr ref android.R.styleable#View_focusable
5832 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005833 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 public final boolean isFocusable() {
5835 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5836 }
5837
5838 /**
5839 * When a view is focusable, it may not want to take focus when in touch mode.
5840 * For example, a button would like focus when the user is navigating via a D-pad
5841 * so that the user can click on it, but once the user starts touching the screen,
5842 * the button shouldn't take focus
5843 * @return Whether the view is focusable in touch mode.
5844 * @attr ref android.R.styleable#View_focusableInTouchMode
5845 */
5846 @ViewDebug.ExportedProperty
5847 public final boolean isFocusableInTouchMode() {
5848 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5849 }
5850
5851 /**
5852 * Find the nearest view in the specified direction that can take focus.
5853 * This does not actually give focus to that view.
5854 *
5855 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5856 *
5857 * @return The nearest focusable in the specified direction, or null if none
5858 * can be found.
5859 */
5860 public View focusSearch(int direction) {
5861 if (mParent != null) {
5862 return mParent.focusSearch(this, direction);
5863 } else {
5864 return null;
5865 }
5866 }
5867
5868 /**
5869 * This method is the last chance for the focused view and its ancestors to
5870 * respond to an arrow key. This is called when the focused view did not
5871 * consume the key internally, nor could the view system find a new view in
5872 * the requested direction to give focus to.
5873 *
5874 * @param focused The currently focused view.
5875 * @param direction The direction focus wants to move. One of FOCUS_UP,
5876 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5877 * @return True if the this view consumed this unhandled move.
5878 */
5879 public boolean dispatchUnhandledMove(View focused, int direction) {
5880 return false;
5881 }
5882
5883 /**
5884 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005885 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005887 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5888 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005889 * @return The user specified next view, or null if there is none.
5890 */
5891 View findUserSetNextFocus(View root, int direction) {
5892 switch (direction) {
5893 case FOCUS_LEFT:
5894 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005895 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 case FOCUS_RIGHT:
5897 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005898 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 case FOCUS_UP:
5900 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005901 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 case FOCUS_DOWN:
5903 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005904 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005905 case FOCUS_FORWARD:
5906 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005907 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005908 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005909 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005910 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005911 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005912 @Override
5913 public boolean apply(View t) {
5914 return t.mNextFocusForwardId == id;
5915 }
5916 });
5917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 }
5919 return null;
5920 }
5921
Jeff Brown4dfbec22011-08-15 14:55:37 -07005922 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5923 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5924 @Override
5925 public boolean apply(View t) {
5926 return t.mID == childViewId;
5927 }
5928 });
5929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 if (result == null) {
5931 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5932 + "by user for id " + childViewId);
5933 }
5934 return result;
5935 }
5936
5937 /**
5938 * Find and return all focusable views that are descendants of this view,
5939 * possibly including this view if it is focusable itself.
5940 *
5941 * @param direction The direction of the focus
5942 * @return A list of focusable views
5943 */
5944 public ArrayList<View> getFocusables(int direction) {
5945 ArrayList<View> result = new ArrayList<View>(24);
5946 addFocusables(result, direction);
5947 return result;
5948 }
5949
5950 /**
5951 * Add any focusable views that are descendants of this view (possibly
5952 * including this view if it is focusable itself) to views. If we are in touch mode,
5953 * only add views that are also focusable in touch mode.
5954 *
5955 * @param views Focusable views found so far
5956 * @param direction The direction of the focus
5957 */
5958 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005959 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961
svetoslavganov75986cf2009-05-14 22:28:01 -07005962 /**
5963 * Adds any focusable views that are descendants of this view (possibly
5964 * including this view if it is focusable itself) to views. This method
5965 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005966 * only views focusable in touch mode if we are in touch mode or
5967 * only views that can take accessibility focus if accessibility is enabeld
5968 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005969 *
5970 * @param views Focusable views found so far or null if all we are interested is
5971 * the number of focusables.
5972 * @param direction The direction of the focus.
5973 * @param focusableMode The type of focusables to be added.
5974 *
5975 * @see #FOCUSABLES_ALL
5976 * @see #FOCUSABLES_TOUCH_MODE
5977 */
5978 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005979 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005980 return;
5981 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005982 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5983 if (AccessibilityManager.getInstance(mContext).isEnabled()
5984 && includeForAccessibility()) {
5985 views.add(this);
5986 return;
5987 }
5988 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005989 if (!isFocusable()) {
5990 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005991 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005992 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5993 && isInTouchMode() && !isFocusableInTouchMode()) {
5994 return;
5995 }
5996 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 }
5998
5999 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006000 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006001 * The search is performed by either the text that the View renders or the content
6002 * description that describes the view for accessibility purposes and the view does
6003 * not render or both. Clients can specify how the search is to be performed via
6004 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6005 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006006 *
6007 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006008 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006009 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006010 * @see #FIND_VIEWS_WITH_TEXT
6011 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6012 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006013 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006014 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006015 if (getAccessibilityNodeProvider() != null) {
6016 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6017 outViews.add(this);
6018 }
6019 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006020 && (searched != null && searched.length() > 0)
6021 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006022 String searchedLowerCase = searched.toString().toLowerCase();
6023 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6024 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6025 outViews.add(this);
6026 }
6027 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006028 }
6029
6030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 * Find and return all touchable views that are descendants of this view,
6032 * possibly including this view if it is touchable itself.
6033 *
6034 * @return A list of touchable views
6035 */
6036 public ArrayList<View> getTouchables() {
6037 ArrayList<View> result = new ArrayList<View>();
6038 addTouchables(result);
6039 return result;
6040 }
6041
6042 /**
6043 * Add any touchable views that are descendants of this view (possibly
6044 * including this view if it is touchable itself) to views.
6045 *
6046 * @param views Touchable views found so far
6047 */
6048 public void addTouchables(ArrayList<View> views) {
6049 final int viewFlags = mViewFlags;
6050
6051 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6052 && (viewFlags & ENABLED_MASK) == ENABLED) {
6053 views.add(this);
6054 }
6055 }
6056
6057 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006058 * Returns whether this View is accessibility focused.
6059 *
6060 * @return True if this View is accessibility focused.
6061 */
6062 boolean isAccessibilityFocused() {
6063 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6064 }
6065
6066 /**
6067 * Call this to try to give accessibility focus to this view.
6068 *
6069 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6070 * returns false or the view is no visible or the view already has accessibility
6071 * focus.
6072 *
6073 * See also {@link #focusSearch(int)}, which is what you call to say that you
6074 * have focus, and you want your parent to look for the next one.
6075 *
6076 * @return Whether this view actually took accessibility focus.
6077 *
6078 * @hide
6079 */
6080 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006081 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6082 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006083 return false;
6084 }
6085 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6086 return false;
6087 }
6088 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6089 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6090 ViewRootImpl viewRootImpl = getViewRootImpl();
6091 if (viewRootImpl != null) {
6092 viewRootImpl.setAccessibilityFocusedHost(this);
6093 }
6094 invalidate();
6095 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6096 notifyAccessibilityStateChanged();
6097 // Try to give input focus to this view - not a descendant.
6098 requestFocusNoSearch(View.FOCUS_DOWN, null);
6099 return true;
6100 }
6101 return false;
6102 }
6103
6104 /**
6105 * Call this to try to clear accessibility focus of this view.
6106 *
6107 * See also {@link #focusSearch(int)}, which is what you call to say that you
6108 * have focus, and you want your parent to look for the next one.
6109 *
6110 * @hide
6111 */
6112 public void clearAccessibilityFocus() {
6113 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6114 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6115 ViewRootImpl viewRootImpl = getViewRootImpl();
6116 if (viewRootImpl != null) {
6117 viewRootImpl.setAccessibilityFocusedHost(null);
6118 }
6119 invalidate();
6120 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6121 notifyAccessibilityStateChanged();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006122
6123 // Clear the text navigation state.
6124 setAccessibilityCursorPosition(-1);
6125
Svetoslav Ganov42138042012-03-20 11:51:39 -07006126 // Try to move accessibility focus to the input focus.
6127 View rootView = getRootView();
6128 if (rootView != null) {
6129 View inputFocus = rootView.findFocus();
6130 if (inputFocus != null) {
6131 inputFocus.requestAccessibilityFocus();
6132 }
6133 }
6134 }
6135 }
6136
6137 /**
6138 * Find the best view to take accessibility focus from a hover.
6139 * This function finds the deepest actionable view and if that
6140 * fails ask the parent to take accessibility focus from hover.
6141 *
6142 * @param x The X hovered location in this view coorditantes.
6143 * @param y The Y hovered location in this view coorditantes.
6144 * @return Whether the request was handled.
6145 *
6146 * @hide
6147 */
6148 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6149 if (onRequestAccessibilityFocusFromHover(x, y)) {
6150 return true;
6151 }
6152 ViewParent parent = mParent;
6153 if (parent instanceof View) {
6154 View parentView = (View) parent;
6155
6156 float[] position = mAttachInfo.mTmpTransformLocation;
6157 position[0] = x;
6158 position[1] = y;
6159
6160 // Compensate for the transformation of the current matrix.
6161 if (!hasIdentityMatrix()) {
6162 getMatrix().mapPoints(position);
6163 }
6164
6165 // Compensate for the parent scroll and the offset
6166 // of this view stop from the parent top.
6167 position[0] += mLeft - parentView.mScrollX;
6168 position[1] += mTop - parentView.mScrollY;
6169
6170 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6171 }
6172 return false;
6173 }
6174
6175 /**
6176 * Requests to give this View focus from hover.
6177 *
6178 * @param x The X hovered location in this view coorditantes.
6179 * @param y The Y hovered location in this view coorditantes.
6180 * @return Whether the request was handled.
6181 *
6182 * @hide
6183 */
6184 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6185 if (includeForAccessibility()
6186 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6187 return requestAccessibilityFocus();
6188 }
6189 return false;
6190 }
6191
6192 /**
6193 * Clears accessibility focus without calling any callback methods
6194 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6195 * is used for clearing accessibility focus when giving this focus to
6196 * another view.
6197 */
6198 void clearAccessibilityFocusNoCallbacks() {
6199 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6200 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6201 invalidate();
6202 }
6203 }
6204
6205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 * Call this to try to give focus to a specific view or to one of its
6207 * descendants.
6208 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006209 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6210 * false), or if it is focusable and it is not focusable in touch mode
6211 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006213 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 * have focus, and you want your parent to look for the next one.
6215 *
6216 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6217 * {@link #FOCUS_DOWN} and <code>null</code>.
6218 *
6219 * @return Whether this view or one of its descendants actually took focus.
6220 */
6221 public final boolean requestFocus() {
6222 return requestFocus(View.FOCUS_DOWN);
6223 }
6224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 /**
6226 * Call this to try to give focus to a specific view or to one of its
6227 * descendants and give it a hint about what direction focus is heading.
6228 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006229 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6230 * false), or if it is focusable and it is not focusable in touch mode
6231 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006233 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 * have focus, and you want your parent to look for the next one.
6235 *
6236 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6237 * <code>null</code> set for the previously focused rectangle.
6238 *
6239 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6240 * @return Whether this view or one of its descendants actually took focus.
6241 */
6242 public final boolean requestFocus(int direction) {
6243 return requestFocus(direction, null);
6244 }
6245
6246 /**
6247 * Call this to try to give focus to a specific view or to one of its descendants
6248 * and give it hints about the direction and a specific rectangle that the focus
6249 * is coming from. The rectangle can help give larger views a finer grained hint
6250 * about where focus is coming from, and therefore, where to show selection, or
6251 * forward focus change internally.
6252 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006253 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6254 * false), or if it is focusable and it is not focusable in touch mode
6255 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 *
6257 * A View will not take focus if it is not visible.
6258 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006259 * A View will not take focus if one of its parents has
6260 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6261 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006263 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 * have focus, and you want your parent to look for the next one.
6265 *
6266 * You may wish to override this method if your custom {@link View} has an internal
6267 * {@link View} that it wishes to forward the request to.
6268 *
6269 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6270 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6271 * to give a finer grained hint about where focus is coming from. May be null
6272 * if there is no hint.
6273 * @return Whether this view or one of its descendants actually took focus.
6274 */
6275 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006276 return requestFocusNoSearch(direction, previouslyFocusedRect);
6277 }
6278
6279 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 // need to be focusable
6281 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6282 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6283 return false;
6284 }
6285
6286 // need to be focusable in touch mode if in touch mode
6287 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006288 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6289 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 }
6291
6292 // need to not have any parents blocking us
6293 if (hasAncestorThatBlocksDescendantFocus()) {
6294 return false;
6295 }
6296
6297 handleFocusGainInternal(direction, previouslyFocusedRect);
6298 return true;
6299 }
6300
6301 /**
6302 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6303 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6304 * touch mode to request focus when they are touched.
6305 *
6306 * @return Whether this view or one of its descendants actually took focus.
6307 *
6308 * @see #isInTouchMode()
6309 *
6310 */
6311 public final boolean requestFocusFromTouch() {
6312 // Leave touch mode if we need to
6313 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006314 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006315 if (viewRoot != null) {
6316 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 }
6318 }
6319 return requestFocus(View.FOCUS_DOWN);
6320 }
6321
6322 /**
6323 * @return Whether any ancestor of this view blocks descendant focus.
6324 */
6325 private boolean hasAncestorThatBlocksDescendantFocus() {
6326 ViewParent ancestor = mParent;
6327 while (ancestor instanceof ViewGroup) {
6328 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6329 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6330 return true;
6331 } else {
6332 ancestor = vgAncestor.getParent();
6333 }
6334 }
6335 return false;
6336 }
6337
6338 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006339 * Gets the mode for determining whether this View is important for accessibility
6340 * which is if it fires accessibility events and if it is reported to
6341 * accessibility services that query the screen.
6342 *
6343 * @return The mode for determining whether a View is important for accessibility.
6344 *
6345 * @attr ref android.R.styleable#View_importantForAccessibility
6346 *
6347 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6348 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6349 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6350 */
6351 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6352 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6353 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6354 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6355 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6356 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6357 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6358 })
6359 public int getImportantForAccessibility() {
6360 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6361 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6362 }
6363
6364 /**
6365 * Sets how to determine whether this view is important for accessibility
6366 * which is if it fires accessibility events and if it is reported to
6367 * accessibility services that query the screen.
6368 *
6369 * @param mode How to determine whether this view is important for accessibility.
6370 *
6371 * @attr ref android.R.styleable#View_importantForAccessibility
6372 *
6373 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6374 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6375 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6376 */
6377 public void setImportantForAccessibility(int mode) {
6378 if (mode != getImportantForAccessibility()) {
6379 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6380 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6381 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6382 notifyAccessibilityStateChanged();
6383 }
6384 }
6385
6386 /**
6387 * Gets whether this view should be exposed for accessibility.
6388 *
6389 * @return Whether the view is exposed for accessibility.
6390 *
6391 * @hide
6392 */
6393 public boolean isImportantForAccessibility() {
6394 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6395 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6396 switch (mode) {
6397 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6398 return true;
6399 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6400 return false;
6401 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6402 return isActionableForAccessibility() || hasListenersForAccessibility();
6403 default:
6404 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6405 + mode);
6406 }
6407 }
6408
6409 /**
6410 * Gets the parent for accessibility purposes. Note that the parent for
6411 * accessibility is not necessary the immediate parent. It is the first
6412 * predecessor that is important for accessibility.
6413 *
6414 * @return The parent for accessibility purposes.
6415 */
6416 public ViewParent getParentForAccessibility() {
6417 if (mParent instanceof View) {
6418 View parentView = (View) mParent;
6419 if (parentView.includeForAccessibility()) {
6420 return mParent;
6421 } else {
6422 return mParent.getParentForAccessibility();
6423 }
6424 }
6425 return null;
6426 }
6427
6428 /**
6429 * Adds the children of a given View for accessibility. Since some Views are
6430 * not important for accessibility the children for accessibility are not
6431 * necessarily direct children of the riew, rather they are the first level of
6432 * descendants important for accessibility.
6433 *
6434 * @param children The list of children for accessibility.
6435 */
6436 public void addChildrenForAccessibility(ArrayList<View> children) {
6437 if (includeForAccessibility()) {
6438 children.add(this);
6439 }
6440 }
6441
6442 /**
6443 * Whether to regard this view for accessibility. A view is regarded for
6444 * accessibility if it is important for accessibility or the querying
6445 * accessibility service has explicitly requested that view not
6446 * important for accessibility are regarded.
6447 *
6448 * @return Whether to regard the view for accessibility.
6449 */
6450 boolean includeForAccessibility() {
6451 if (mAttachInfo != null) {
6452 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006453 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006454 } else {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006455 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006456 }
6457 }
6458 return false;
6459 }
6460
6461 /**
6462 * Returns whether the View is considered actionable from
6463 * accessibility perspective. Such view are important for
6464 * accessiiblity.
6465 *
6466 * @return True if the view is actionable for accessibility.
6467 */
6468 private boolean isActionableForAccessibility() {
6469 return (isClickable() || isLongClickable() || isFocusable());
6470 }
6471
6472 /**
6473 * Returns whether the View has registered callbacks wich makes it
6474 * important for accessiiblity.
6475 *
6476 * @return True if the view is actionable for accessibility.
6477 */
6478 private boolean hasListenersForAccessibility() {
6479 ListenerInfo info = getListenerInfo();
6480 return mTouchDelegate != null || info.mOnKeyListener != null
6481 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6482 || info.mOnHoverListener != null || info.mOnDragListener != null;
6483 }
6484
6485 /**
6486 * Notifies accessibility services that some view's important for
6487 * accessibility state has changed. Note that such notifications
6488 * are made at most once every
6489 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6490 * to avoid unnecessary load to the system. Also once a view has
6491 * made a notifucation this method is a NOP until the notification has
6492 * been sent to clients.
6493 *
6494 * @hide
6495 *
6496 * TODO: Makse sure this method is called for any view state change
6497 * that is interesting for accessilility purposes.
6498 */
6499 public void notifyAccessibilityStateChanged() {
6500 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6501 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6502 if (mParent != null) {
6503 mParent.childAccessibilityStateChanged(this);
6504 }
6505 }
6506 }
6507
6508 /**
6509 * Reset the state indicating the this view has requested clients
6510 * interested in its accessiblity state to be notified.
6511 *
6512 * @hide
6513 */
6514 public void resetAccessibilityStateChanged() {
6515 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6516 }
6517
6518 /**
6519 * Performs the specified accessibility action on the view. For
6520 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006521 * <p>
6522 * If an {@link AccessibilityDelegate} has been specified via calling
6523 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6524 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6525 * is responsible for handling this call.
6526 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006527 *
6528 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006529 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006530 * @return Whether the action was performed.
6531 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006532 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006533 if (mAccessibilityDelegate != null) {
6534 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6535 } else {
6536 return performAccessibilityActionInternal(action, arguments);
6537 }
6538 }
6539
6540 /**
6541 * @see #performAccessibilityAction(int, Bundle)
6542 *
6543 * Note: Called from the default {@link AccessibilityDelegate}.
6544 */
6545 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006546 switch (action) {
6547 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006548 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006549 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006550 }
6551 } break;
6552 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6553 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006554 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006555 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006556 } break;
6557 case AccessibilityNodeInfo.ACTION_FOCUS: {
6558 if (!hasFocus()) {
6559 // Get out of touch mode since accessibility
6560 // wants to move focus around.
6561 getViewRootImpl().ensureTouchMode(false);
6562 return requestFocus();
6563 }
6564 } break;
6565 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6566 if (hasFocus()) {
6567 clearFocus();
6568 return !isFocused();
6569 }
6570 } break;
6571 case AccessibilityNodeInfo.ACTION_SELECT: {
6572 if (!isSelected()) {
6573 setSelected(true);
6574 return isSelected();
6575 }
6576 } break;
6577 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6578 if (isSelected()) {
6579 setSelected(false);
6580 return !isSelected();
6581 }
6582 } break;
6583 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6584 if (!isAccessibilityFocused()) {
6585 return requestAccessibilityFocus();
6586 }
6587 } break;
6588 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6589 if (isAccessibilityFocused()) {
6590 clearAccessibilityFocus();
6591 return true;
6592 }
6593 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006594 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6595 if (arguments != null) {
6596 final int granularity = arguments.getInt(
6597 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6598 return nextAtGranularity(granularity);
6599 }
6600 } break;
6601 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6602 if (arguments != null) {
6603 final int granularity = arguments.getInt(
6604 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6605 return previousAtGranularity(granularity);
6606 }
6607 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006608 }
6609 return false;
6610 }
6611
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006612 private boolean nextAtGranularity(int granularity) {
6613 CharSequence text = getIterableTextForAccessibility();
6614 if (text != null && text.length() > 0) {
6615 return false;
6616 }
6617 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6618 if (iterator == null) {
6619 return false;
6620 }
6621 final int current = getAccessibilityCursorPosition();
6622 final int[] range = iterator.following(current);
6623 if (range == null) {
6624 setAccessibilityCursorPosition(-1);
6625 return false;
6626 }
6627 final int start = range[0];
6628 final int end = range[1];
6629 setAccessibilityCursorPosition(start);
6630 sendViewTextTraversedAtGranularityEvent(
6631 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6632 granularity, start, end);
6633 return true;
6634 }
6635
6636 private boolean previousAtGranularity(int granularity) {
6637 CharSequence text = getIterableTextForAccessibility();
6638 if (text != null && text.length() > 0) {
6639 return false;
6640 }
6641 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6642 if (iterator == null) {
6643 return false;
6644 }
6645 final int selectionStart = getAccessibilityCursorPosition();
6646 final int current = selectionStart >= 0 ? selectionStart : text.length() + 1;
6647 final int[] range = iterator.preceding(current);
6648 if (range == null) {
6649 setAccessibilityCursorPosition(-1);
6650 return false;
6651 }
6652 final int start = range[0];
6653 final int end = range[1];
6654 setAccessibilityCursorPosition(end);
6655 sendViewTextTraversedAtGranularityEvent(
6656 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6657 granularity, start, end);
6658 return true;
6659 }
6660
6661 /**
6662 * Gets the text reported for accessibility purposes.
6663 *
6664 * @return The accessibility text.
6665 *
6666 * @hide
6667 */
6668 public CharSequence getIterableTextForAccessibility() {
6669 return mContentDescription;
6670 }
6671
6672 /**
6673 * @hide
6674 */
6675 public int getAccessibilityCursorPosition() {
6676 return mAccessibilityCursorPosition;
6677 }
6678
6679 /**
6680 * @hide
6681 */
6682 public void setAccessibilityCursorPosition(int position) {
6683 mAccessibilityCursorPosition = position;
6684 }
6685
6686 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6687 int fromIndex, int toIndex) {
6688 if (mParent == null) {
6689 return;
6690 }
6691 AccessibilityEvent event = AccessibilityEvent.obtain(
6692 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6693 onInitializeAccessibilityEvent(event);
6694 onPopulateAccessibilityEvent(event);
6695 event.setFromIndex(fromIndex);
6696 event.setToIndex(toIndex);
6697 event.setAction(action);
6698 event.setMovementGranularity(granularity);
6699 mParent.requestSendAccessibilityEvent(this, event);
6700 }
6701
6702 /**
6703 * @hide
6704 */
6705 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6706 switch (granularity) {
6707 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6708 CharSequence text = getIterableTextForAccessibility();
6709 if (text != null && text.length() > 0) {
6710 CharacterTextSegmentIterator iterator =
6711 CharacterTextSegmentIterator.getInstance(mContext);
6712 iterator.initialize(text.toString());
6713 return iterator;
6714 }
6715 } break;
6716 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6717 CharSequence text = getIterableTextForAccessibility();
6718 if (text != null && text.length() > 0) {
6719 WordTextSegmentIterator iterator =
6720 WordTextSegmentIterator.getInstance(mContext);
6721 iterator.initialize(text.toString());
6722 return iterator;
6723 }
6724 } break;
6725 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6726 CharSequence text = getIterableTextForAccessibility();
6727 if (text != null && text.length() > 0) {
6728 ParagraphTextSegmentIterator iterator =
6729 ParagraphTextSegmentIterator.getInstance();
6730 iterator.initialize(text.toString());
6731 return iterator;
6732 }
6733 } break;
6734 }
6735 return null;
6736 }
6737
Svetoslav Ganov42138042012-03-20 11:51:39 -07006738 /**
Romain Guya440b002010-02-24 15:57:54 -08006739 * @hide
6740 */
6741 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006742 clearAccessibilityFocus();
Romain Guya440b002010-02-24 15:57:54 -08006743 onStartTemporaryDetach();
6744 }
6745
6746 /**
6747 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006748 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6749 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006750 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 */
6752 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006753 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006754 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006755 }
6756
6757 /**
6758 * @hide
6759 */
6760 public void dispatchFinishTemporaryDetach() {
6761 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 }
Romain Guy8506ab42009-06-11 17:35:47 -07006763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 /**
6765 * Called after {@link #onStartTemporaryDetach} when the container is done
6766 * changing the view.
6767 */
6768 public void onFinishTemporaryDetach() {
6769 }
Romain Guy8506ab42009-06-11 17:35:47 -07006770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006771 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006772 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6773 * for this view's window. Returns null if the view is not currently attached
6774 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006775 * just use the standard high-level event callbacks like
6776 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006777 */
6778 public KeyEvent.DispatcherState getKeyDispatcherState() {
6779 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6780 }
Joe Malin32736f02011-01-19 16:14:20 -08006781
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 * Dispatch a key event before it is processed by any input method
6784 * associated with the view hierarchy. This can be used to intercept
6785 * key events in special situations before the IME consumes them; a
6786 * typical example would be handling the BACK key to update the application's
6787 * UI instead of allowing the IME to see it and close itself.
6788 *
6789 * @param event The key event to be dispatched.
6790 * @return True if the event was handled, false otherwise.
6791 */
6792 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6793 return onKeyPreIme(event.getKeyCode(), event);
6794 }
6795
6796 /**
6797 * Dispatch a key event to the next view on the focus path. This path runs
6798 * from the top of the view tree down to the currently focused view. If this
6799 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6800 * the next node down the focus path. This method also fires any key
6801 * listeners.
6802 *
6803 * @param event The key event to be dispatched.
6804 * @return True if the event was handled, false otherwise.
6805 */
6806 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006807 if (mInputEventConsistencyVerifier != null) {
6808 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810
Jeff Brown21bc5c92011-02-28 18:27:14 -08006811 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006812 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006813 ListenerInfo li = mListenerInfo;
6814 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6815 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006816 return true;
6817 }
6818
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006819 if (event.dispatch(this, mAttachInfo != null
6820 ? mAttachInfo.mKeyDispatchState : null, this)) {
6821 return true;
6822 }
6823
6824 if (mInputEventConsistencyVerifier != null) {
6825 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6826 }
6827 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 }
6829
6830 /**
6831 * Dispatches a key shortcut event.
6832 *
6833 * @param event The key event to be dispatched.
6834 * @return True if the event was handled by the view, false otherwise.
6835 */
6836 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6837 return onKeyShortcut(event.getKeyCode(), event);
6838 }
6839
6840 /**
6841 * Pass the touch screen motion event down to the target view, or this
6842 * view if it is the target.
6843 *
6844 * @param event The motion event to be dispatched.
6845 * @return True if the event was handled by the view, false otherwise.
6846 */
6847 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006848 if (mInputEventConsistencyVerifier != null) {
6849 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6850 }
6851
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006852 if (onFilterTouchEventForSecurity(event)) {
6853 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006854 ListenerInfo li = mListenerInfo;
6855 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6856 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006857 return true;
6858 }
6859
6860 if (onTouchEvent(event)) {
6861 return true;
6862 }
Jeff Brown85a31762010-09-01 17:01:00 -07006863 }
6864
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006865 if (mInputEventConsistencyVerifier != null) {
6866 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006867 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006868 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 }
6870
6871 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006872 * Filter the touch event to apply security policies.
6873 *
6874 * @param event The motion event to be filtered.
6875 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006876 *
Jeff Brown85a31762010-09-01 17:01:00 -07006877 * @see #getFilterTouchesWhenObscured
6878 */
6879 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006880 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006881 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6882 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6883 // Window is obscured, drop this touch.
6884 return false;
6885 }
6886 return true;
6887 }
6888
6889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 * Pass a trackball motion event down to the focused view.
6891 *
6892 * @param event The motion event to be dispatched.
6893 * @return True if the event was handled by the view, false otherwise.
6894 */
6895 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006896 if (mInputEventConsistencyVerifier != null) {
6897 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6898 }
6899
Romain Guy02ccac62011-06-24 13:20:23 -07006900 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 }
6902
6903 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006904 * Dispatch a generic motion event.
6905 * <p>
6906 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6907 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006908 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006909 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006910 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006911 *
6912 * @param event The motion event to be dispatched.
6913 * @return True if the event was handled by the view, false otherwise.
6914 */
6915 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006916 if (mInputEventConsistencyVerifier != null) {
6917 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6918 }
6919
Jeff Browna032cc02011-03-07 16:56:21 -08006920 final int source = event.getSource();
6921 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6922 final int action = event.getAction();
6923 if (action == MotionEvent.ACTION_HOVER_ENTER
6924 || action == MotionEvent.ACTION_HOVER_MOVE
6925 || action == MotionEvent.ACTION_HOVER_EXIT) {
6926 if (dispatchHoverEvent(event)) {
6927 return true;
6928 }
6929 } else if (dispatchGenericPointerEvent(event)) {
6930 return true;
6931 }
6932 } else if (dispatchGenericFocusedEvent(event)) {
6933 return true;
6934 }
6935
Jeff Brown10b62902011-06-20 16:40:37 -07006936 if (dispatchGenericMotionEventInternal(event)) {
6937 return true;
6938 }
6939
6940 if (mInputEventConsistencyVerifier != null) {
6941 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6942 }
6943 return false;
6944 }
6945
6946 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006947 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006948 ListenerInfo li = mListenerInfo;
6949 if (li != null && li.mOnGenericMotionListener != null
6950 && (mViewFlags & ENABLED_MASK) == ENABLED
6951 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006952 return true;
6953 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006954
6955 if (onGenericMotionEvent(event)) {
6956 return true;
6957 }
6958
6959 if (mInputEventConsistencyVerifier != null) {
6960 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6961 }
6962 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006963 }
6964
6965 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006966 * Dispatch a hover event.
6967 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006968 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006969 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006970 * </p>
6971 *
6972 * @param event The motion event to be dispatched.
6973 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006974 */
6975 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006976 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006977 ListenerInfo li = mListenerInfo;
6978 if (li != null && li.mOnHoverListener != null
6979 && (mViewFlags & ENABLED_MASK) == ENABLED
6980 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006981 return true;
6982 }
6983
Jeff Browna032cc02011-03-07 16:56:21 -08006984 return onHoverEvent(event);
6985 }
6986
6987 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006988 * Returns true if the view has a child to which it has recently sent
6989 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6990 * it does not have a hovered child, then it must be the innermost hovered view.
6991 * @hide
6992 */
6993 protected boolean hasHoveredChild() {
6994 return false;
6995 }
6996
6997 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006998 * Dispatch a generic motion event to the view under the first pointer.
6999 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007000 * Do not call this method directly.
7001 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007002 * </p>
7003 *
7004 * @param event The motion event to be dispatched.
7005 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007006 */
7007 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7008 return false;
7009 }
7010
7011 /**
7012 * Dispatch a generic motion event to the currently focused view.
7013 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007014 * Do not call this method directly.
7015 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007016 * </p>
7017 *
7018 * @param event The motion event to be dispatched.
7019 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007020 */
7021 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7022 return false;
7023 }
7024
7025 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007026 * Dispatch a pointer event.
7027 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007028 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7029 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7030 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007031 * and should not be expected to handle other pointing device features.
7032 * </p>
7033 *
7034 * @param event The motion event to be dispatched.
7035 * @return True if the event was handled by the view, false otherwise.
7036 * @hide
7037 */
7038 public final boolean dispatchPointerEvent(MotionEvent event) {
7039 if (event.isTouchEvent()) {
7040 return dispatchTouchEvent(event);
7041 } else {
7042 return dispatchGenericMotionEvent(event);
7043 }
7044 }
7045
7046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 * Called when the window containing this view gains or loses window focus.
7048 * ViewGroups should override to route to their children.
7049 *
7050 * @param hasFocus True if the window containing this view now has focus,
7051 * false otherwise.
7052 */
7053 public void dispatchWindowFocusChanged(boolean hasFocus) {
7054 onWindowFocusChanged(hasFocus);
7055 }
7056
7057 /**
7058 * Called when the window containing this view gains or loses focus. Note
7059 * that this is separate from view focus: to receive key events, both
7060 * your view and its window must have focus. If a window is displayed
7061 * on top of yours that takes input focus, then your own window will lose
7062 * focus but the view focus will remain unchanged.
7063 *
7064 * @param hasWindowFocus True if the window containing this view now has
7065 * focus, false otherwise.
7066 */
7067 public void onWindowFocusChanged(boolean hasWindowFocus) {
7068 InputMethodManager imm = InputMethodManager.peekInstance();
7069 if (!hasWindowFocus) {
7070 if (isPressed()) {
7071 setPressed(false);
7072 }
7073 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7074 imm.focusOut(this);
7075 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007076 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007077 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007078 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007079 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7080 imm.focusIn(this);
7081 }
7082 refreshDrawableState();
7083 }
7084
7085 /**
7086 * Returns true if this view is in a window that currently has window focus.
7087 * Note that this is not the same as the view itself having focus.
7088 *
7089 * @return True if this view is in a window that currently has window focus.
7090 */
7091 public boolean hasWindowFocus() {
7092 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7093 }
7094
7095 /**
Adam Powell326d8082009-12-09 15:10:07 -08007096 * Dispatch a view visibility change down the view hierarchy.
7097 * ViewGroups should override to route to their children.
7098 * @param changedView The view whose visibility changed. Could be 'this' or
7099 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007100 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7101 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007102 */
7103 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7104 onVisibilityChanged(changedView, visibility);
7105 }
7106
7107 /**
7108 * Called when the visibility of the view or an ancestor of the view is changed.
7109 * @param changedView The view whose visibility changed. Could be 'this' or
7110 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007111 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7112 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007113 */
7114 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007115 if (visibility == VISIBLE) {
7116 if (mAttachInfo != null) {
7117 initialAwakenScrollBars();
7118 } else {
7119 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7120 }
7121 }
Adam Powell326d8082009-12-09 15:10:07 -08007122 }
7123
7124 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007125 * Dispatch a hint about whether this view is displayed. For instance, when
7126 * a View moves out of the screen, it might receives a display hint indicating
7127 * the view is not displayed. Applications should not <em>rely</em> on this hint
7128 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007129 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007130 * @param hint A hint about whether or not this view is displayed:
7131 * {@link #VISIBLE} or {@link #INVISIBLE}.
7132 */
7133 public void dispatchDisplayHint(int hint) {
7134 onDisplayHint(hint);
7135 }
7136
7137 /**
7138 * Gives this view a hint about whether is displayed or not. For instance, when
7139 * a View moves out of the screen, it might receives a display hint indicating
7140 * the view is not displayed. Applications should not <em>rely</em> on this hint
7141 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007142 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007143 * @param hint A hint about whether or not this view is displayed:
7144 * {@link #VISIBLE} or {@link #INVISIBLE}.
7145 */
7146 protected void onDisplayHint(int hint) {
7147 }
7148
7149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 * Dispatch a window visibility change down the view hierarchy.
7151 * ViewGroups should override to route to their children.
7152 *
7153 * @param visibility The new visibility of the window.
7154 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007155 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 */
7157 public void dispatchWindowVisibilityChanged(int visibility) {
7158 onWindowVisibilityChanged(visibility);
7159 }
7160
7161 /**
7162 * Called when the window containing has change its visibility
7163 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7164 * that this tells you whether or not your window is being made visible
7165 * to the window manager; this does <em>not</em> tell you whether or not
7166 * your window is obscured by other windows on the screen, even if it
7167 * is itself visible.
7168 *
7169 * @param visibility The new visibility of the window.
7170 */
7171 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007172 if (visibility == VISIBLE) {
7173 initialAwakenScrollBars();
7174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007175 }
7176
7177 /**
7178 * Returns the current visibility of the window this view is attached to
7179 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7180 *
7181 * @return Returns the current visibility of the view's window.
7182 */
7183 public int getWindowVisibility() {
7184 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7185 }
7186
7187 /**
7188 * Retrieve the overall visible display size in which the window this view is
7189 * attached to has been positioned in. This takes into account screen
7190 * decorations above the window, for both cases where the window itself
7191 * is being position inside of them or the window is being placed under
7192 * then and covered insets are used for the window to position its content
7193 * inside. In effect, this tells you the available area where content can
7194 * be placed and remain visible to users.
7195 *
7196 * <p>This function requires an IPC back to the window manager to retrieve
7197 * the requested information, so should not be used in performance critical
7198 * code like drawing.
7199 *
7200 * @param outRect Filled in with the visible display frame. If the view
7201 * is not attached to a window, this is simply the raw display size.
7202 */
7203 public void getWindowVisibleDisplayFrame(Rect outRect) {
7204 if (mAttachInfo != null) {
7205 try {
7206 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7207 } catch (RemoteException e) {
7208 return;
7209 }
7210 // XXX This is really broken, and probably all needs to be done
7211 // in the window manager, and we need to know more about whether
7212 // we want the area behind or in front of the IME.
7213 final Rect insets = mAttachInfo.mVisibleInsets;
7214 outRect.left += insets.left;
7215 outRect.top += insets.top;
7216 outRect.right -= insets.right;
7217 outRect.bottom -= insets.bottom;
7218 return;
7219 }
7220 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007221 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007222 }
7223
7224 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007225 * Dispatch a notification about a resource configuration change down
7226 * the view hierarchy.
7227 * ViewGroups should override to route to their children.
7228 *
7229 * @param newConfig The new resource configuration.
7230 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007231 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007232 */
7233 public void dispatchConfigurationChanged(Configuration newConfig) {
7234 onConfigurationChanged(newConfig);
7235 }
7236
7237 /**
7238 * Called when the current configuration of the resources being used
7239 * by the application have changed. You can use this to decide when
7240 * to reload resources that can changed based on orientation and other
7241 * configuration characterstics. You only need to use this if you are
7242 * not relying on the normal {@link android.app.Activity} mechanism of
7243 * recreating the activity instance upon a configuration change.
7244 *
7245 * @param newConfig The new resource configuration.
7246 */
7247 protected void onConfigurationChanged(Configuration newConfig) {
7248 }
7249
7250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251 * Private function to aggregate all per-view attributes in to the view
7252 * root.
7253 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007254 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7255 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256 }
7257
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007258 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7259 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007260 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007261 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007262 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007263 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007264 ListenerInfo li = mListenerInfo;
7265 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007266 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 }
7269 }
7270
7271 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007272 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007273 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007274 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7275 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 ai.mRecomputeGlobalAttributes = true;
7277 }
7278 }
7279 }
7280
7281 /**
7282 * Returns whether the device is currently in touch mode. Touch mode is entered
7283 * once the user begins interacting with the device by touch, and affects various
7284 * things like whether focus is always visible to the user.
7285 *
7286 * @return Whether the device is in touch mode.
7287 */
7288 @ViewDebug.ExportedProperty
7289 public boolean isInTouchMode() {
7290 if (mAttachInfo != null) {
7291 return mAttachInfo.mInTouchMode;
7292 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007293 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007294 }
7295 }
7296
7297 /**
7298 * Returns the context the view is running in, through which it can
7299 * access the current theme, resources, etc.
7300 *
7301 * @return The view's Context.
7302 */
7303 @ViewDebug.CapturedViewProperty
7304 public final Context getContext() {
7305 return mContext;
7306 }
7307
7308 /**
7309 * Handle a key event before it is processed by any input method
7310 * associated with the view hierarchy. This can be used to intercept
7311 * key events in special situations before the IME consumes them; a
7312 * typical example would be handling the BACK key to update the application's
7313 * UI instead of allowing the IME to see it and close itself.
7314 *
7315 * @param keyCode The value in event.getKeyCode().
7316 * @param event Description of the key event.
7317 * @return If you handled the event, return true. If you want to allow the
7318 * event to be handled by the next receiver, return false.
7319 */
7320 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7321 return false;
7322 }
7323
7324 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007325 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7326 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7328 * is released, if the view is enabled and clickable.
7329 *
7330 * @param keyCode A key code that represents the button pressed, from
7331 * {@link android.view.KeyEvent}.
7332 * @param event The KeyEvent object that defines the button action.
7333 */
7334 public boolean onKeyDown(int keyCode, KeyEvent event) {
7335 boolean result = false;
7336
7337 switch (keyCode) {
7338 case KeyEvent.KEYCODE_DPAD_CENTER:
7339 case KeyEvent.KEYCODE_ENTER: {
7340 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7341 return true;
7342 }
7343 // Long clickable items don't necessarily have to be clickable
7344 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7345 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7346 (event.getRepeatCount() == 0)) {
7347 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007348 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 return true;
7350 }
7351 break;
7352 }
7353 }
7354 return result;
7355 }
7356
7357 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007358 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7359 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7360 * the event).
7361 */
7362 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7363 return false;
7364 }
7365
7366 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007367 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7368 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7370 * {@link KeyEvent#KEYCODE_ENTER} is released.
7371 *
7372 * @param keyCode A key code that represents the button pressed, from
7373 * {@link android.view.KeyEvent}.
7374 * @param event The KeyEvent object that defines the button action.
7375 */
7376 public boolean onKeyUp(int keyCode, KeyEvent event) {
7377 boolean result = false;
7378
7379 switch (keyCode) {
7380 case KeyEvent.KEYCODE_DPAD_CENTER:
7381 case KeyEvent.KEYCODE_ENTER: {
7382 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7383 return true;
7384 }
7385 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7386 setPressed(false);
7387
7388 if (!mHasPerformedLongPress) {
7389 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007390 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391
7392 result = performClick();
7393 }
7394 }
7395 break;
7396 }
7397 }
7398 return result;
7399 }
7400
7401 /**
7402 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7403 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7404 * the event).
7405 *
7406 * @param keyCode A key code that represents the button pressed, from
7407 * {@link android.view.KeyEvent}.
7408 * @param repeatCount The number of times the action was made.
7409 * @param event The KeyEvent object that defines the button action.
7410 */
7411 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7412 return false;
7413 }
7414
7415 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007416 * Called on the focused view when a key shortcut event is not handled.
7417 * Override this method to implement local key shortcuts for the View.
7418 * Key shortcuts can also be implemented by setting the
7419 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 *
7421 * @param keyCode The value in event.getKeyCode().
7422 * @param event Description of the key event.
7423 * @return If you handled the event, return true. If you want to allow the
7424 * event to be handled by the next receiver, return false.
7425 */
7426 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7427 return false;
7428 }
7429
7430 /**
7431 * Check whether the called view is a text editor, in which case it
7432 * would make sense to automatically display a soft input window for
7433 * it. Subclasses should override this if they implement
7434 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007435 * a call on that method would return a non-null InputConnection, and
7436 * they are really a first-class editor that the user would normally
7437 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007438 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007439 * <p>The default implementation always returns false. This does
7440 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7441 * will not be called or the user can not otherwise perform edits on your
7442 * view; it is just a hint to the system that this is not the primary
7443 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 * @return Returns true if this view is a text editor, else false.
7446 */
7447 public boolean onCheckIsTextEditor() {
7448 return false;
7449 }
Romain Guy8506ab42009-06-11 17:35:47 -07007450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 /**
7452 * Create a new InputConnection for an InputMethod to interact
7453 * with the view. The default implementation returns null, since it doesn't
7454 * support input methods. You can override this to implement such support.
7455 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007456 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 * <p>When implementing this, you probably also want to implement
7458 * {@link #onCheckIsTextEditor()} to indicate you will return a
7459 * non-null InputConnection.
7460 *
7461 * @param outAttrs Fill in with attribute information about the connection.
7462 */
7463 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7464 return null;
7465 }
7466
7467 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007468 * Called by the {@link android.view.inputmethod.InputMethodManager}
7469 * when a view who is not the current
7470 * input connection target is trying to make a call on the manager. The
7471 * default implementation returns false; you can override this to return
7472 * true for certain views if you are performing InputConnection proxying
7473 * to them.
7474 * @param view The View that is making the InputMethodManager call.
7475 * @return Return true to allow the call, false to reject.
7476 */
7477 public boolean checkInputConnectionProxy(View view) {
7478 return false;
7479 }
Romain Guy8506ab42009-06-11 17:35:47 -07007480
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 * Show the context menu for this view. It is not safe to hold on to the
7483 * menu after returning from this method.
7484 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007485 * You should normally not overload this method. Overload
7486 * {@link #onCreateContextMenu(ContextMenu)} or define an
7487 * {@link OnCreateContextMenuListener} to add items to the context menu.
7488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 * @param menu The context menu to populate
7490 */
7491 public void createContextMenu(ContextMenu menu) {
7492 ContextMenuInfo menuInfo = getContextMenuInfo();
7493
7494 // Sets the current menu info so all items added to menu will have
7495 // my extra info set.
7496 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7497
7498 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007499 ListenerInfo li = mListenerInfo;
7500 if (li != null && li.mOnCreateContextMenuListener != null) {
7501 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 }
7503
7504 // Clear the extra information so subsequent items that aren't mine don't
7505 // have my extra info.
7506 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7507
7508 if (mParent != null) {
7509 mParent.createContextMenu(menu);
7510 }
7511 }
7512
7513 /**
7514 * Views should implement this if they have extra information to associate
7515 * with the context menu. The return result is supplied as a parameter to
7516 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7517 * callback.
7518 *
7519 * @return Extra information about the item for which the context menu
7520 * should be shown. This information will vary across different
7521 * subclasses of View.
7522 */
7523 protected ContextMenuInfo getContextMenuInfo() {
7524 return null;
7525 }
7526
7527 /**
7528 * Views should implement this if the view itself is going to add items to
7529 * the context menu.
7530 *
7531 * @param menu the context menu to populate
7532 */
7533 protected void onCreateContextMenu(ContextMenu menu) {
7534 }
7535
7536 /**
7537 * Implement this method to handle trackball motion events. The
7538 * <em>relative</em> movement of the trackball since the last event
7539 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7540 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7541 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7542 * they will often be fractional values, representing the more fine-grained
7543 * movement information available from a trackball).
7544 *
7545 * @param event The motion event.
7546 * @return True if the event was handled, false otherwise.
7547 */
7548 public boolean onTrackballEvent(MotionEvent event) {
7549 return false;
7550 }
7551
7552 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007553 * Implement this method to handle generic motion events.
7554 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007555 * Generic motion events describe joystick movements, mouse hovers, track pad
7556 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007557 * {@link MotionEvent#getSource() source} of the motion event specifies
7558 * the class of input that was received. Implementations of this method
7559 * must examine the bits in the source before processing the event.
7560 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007561 * </p><p>
7562 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7563 * are delivered to the view under the pointer. All other generic motion events are
7564 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007565 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007566 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007567 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007568 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7569 * // process the joystick movement...
7570 * return true;
7571 * }
7572 * }
7573 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7574 * switch (event.getAction()) {
7575 * case MotionEvent.ACTION_HOVER_MOVE:
7576 * // process the mouse hover movement...
7577 * return true;
7578 * case MotionEvent.ACTION_SCROLL:
7579 * // process the scroll wheel movement...
7580 * return true;
7581 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007582 * }
7583 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007584 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007585 *
7586 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007587 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007588 */
7589 public boolean onGenericMotionEvent(MotionEvent event) {
7590 return false;
7591 }
7592
7593 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007594 * Implement this method to handle hover events.
7595 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007596 * This method is called whenever a pointer is hovering into, over, or out of the
7597 * bounds of a view and the view is not currently being touched.
7598 * Hover events are represented as pointer events with action
7599 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7600 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7601 * </p>
7602 * <ul>
7603 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7604 * when the pointer enters the bounds of the view.</li>
7605 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7606 * when the pointer has already entered the bounds of the view and has moved.</li>
7607 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7608 * when the pointer has exited the bounds of the view or when the pointer is
7609 * about to go down due to a button click, tap, or similar user action that
7610 * causes the view to be touched.</li>
7611 * </ul>
7612 * <p>
7613 * The view should implement this method to return true to indicate that it is
7614 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007615 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007616 * The default implementation calls {@link #setHovered} to update the hovered state
7617 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007618 * is enabled and is clickable. The default implementation also sends hover
7619 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007620 * </p>
7621 *
7622 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007623 * @return True if the view handled the hover event.
7624 *
7625 * @see #isHovered
7626 * @see #setHovered
7627 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007628 */
7629 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007630 // The root view may receive hover (or touch) events that are outside the bounds of
7631 // the window. This code ensures that we only send accessibility events for
7632 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007633 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007634 if (!mSendingHoverAccessibilityEvents) {
7635 if ((action == MotionEvent.ACTION_HOVER_ENTER
7636 || action == MotionEvent.ACTION_HOVER_MOVE)
7637 && !hasHoveredChild()
7638 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007639 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007640 mSendingHoverAccessibilityEvents = true;
7641 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007642 }
7643 } else {
7644 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007645 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007646 && !pointInView(event.getX(), event.getY()))) {
7647 mSendingHoverAccessibilityEvents = false;
7648 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007649 // If the window does not have input focus we take away accessibility
7650 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007651 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007652 getViewRootImpl().setAccessibilityFocusedHost(null);
7653 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007654 }
Jeff Browna1b24182011-07-28 13:38:24 -07007655 }
7656
Jeff Brown87b7f802011-06-21 18:35:45 -07007657 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007658 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007659 case MotionEvent.ACTION_HOVER_ENTER:
7660 setHovered(true);
7661 break;
7662 case MotionEvent.ACTION_HOVER_EXIT:
7663 setHovered(false);
7664 break;
7665 }
Jeff Browna1b24182011-07-28 13:38:24 -07007666
7667 // Dispatch the event to onGenericMotionEvent before returning true.
7668 // This is to provide compatibility with existing applications that
7669 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7670 // break because of the new default handling for hoverable views
7671 // in onHoverEvent.
7672 // Note that onGenericMotionEvent will be called by default when
7673 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7674 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007675 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007676 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007677
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007678 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007679 }
7680
7681 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007682 * Returns true if the view should handle {@link #onHoverEvent}
7683 * by calling {@link #setHovered} to change its hovered state.
7684 *
7685 * @return True if the view is hoverable.
7686 */
7687 private boolean isHoverable() {
7688 final int viewFlags = mViewFlags;
7689 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7690 return false;
7691 }
7692
7693 return (viewFlags & CLICKABLE) == CLICKABLE
7694 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7695 }
7696
7697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007698 * Returns true if the view is currently hovered.
7699 *
7700 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007701 *
7702 * @see #setHovered
7703 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007704 */
Jeff Brown10b62902011-06-20 16:40:37 -07007705 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007706 public boolean isHovered() {
7707 return (mPrivateFlags & HOVERED) != 0;
7708 }
7709
7710 /**
7711 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007712 * <p>
7713 * Calling this method also changes the drawable state of the view. This
7714 * enables the view to react to hover by using different drawable resources
7715 * to change its appearance.
7716 * </p><p>
7717 * The {@link #onHoverChanged} method is called when the hovered state changes.
7718 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007719 *
7720 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007721 *
7722 * @see #isHovered
7723 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007724 */
7725 public void setHovered(boolean hovered) {
7726 if (hovered) {
7727 if ((mPrivateFlags & HOVERED) == 0) {
7728 mPrivateFlags |= HOVERED;
7729 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007730 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007731 }
7732 } else {
7733 if ((mPrivateFlags & HOVERED) != 0) {
7734 mPrivateFlags &= ~HOVERED;
7735 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007736 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007737 }
7738 }
7739 }
7740
7741 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007742 * Implement this method to handle hover state changes.
7743 * <p>
7744 * This method is called whenever the hover state changes as a result of a
7745 * call to {@link #setHovered}.
7746 * </p>
7747 *
7748 * @param hovered The current hover state, as returned by {@link #isHovered}.
7749 *
7750 * @see #isHovered
7751 * @see #setHovered
7752 */
7753 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007754 }
7755
7756 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 * Implement this method to handle touch screen motion events.
7758 *
7759 * @param event The motion event.
7760 * @return True if the event was handled, false otherwise.
7761 */
7762 public boolean onTouchEvent(MotionEvent event) {
7763 final int viewFlags = mViewFlags;
7764
7765 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007766 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007767 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 // A disabled view that is clickable still consumes the touch
7770 // events, it just doesn't respond to them.
7771 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7772 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7773 }
7774
7775 if (mTouchDelegate != null) {
7776 if (mTouchDelegate.onTouchEvent(event)) {
7777 return true;
7778 }
7779 }
7780
7781 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7782 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7783 switch (event.getAction()) {
7784 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007785 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7786 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 // take focus if we don't have it already and we should in
7788 // touch mode.
7789 boolean focusTaken = false;
7790 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7791 focusTaken = requestFocus();
7792 }
7793
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007794 if (prepressed) {
7795 // The button is being released before we actually
7796 // showed it as pressed. Make it show the pressed
7797 // state now (before scheduling the click) to ensure
7798 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007799 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007800 }
Joe Malin32736f02011-01-19 16:14:20 -08007801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 if (!mHasPerformedLongPress) {
7803 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007804 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805
7806 // Only perform take click actions if we were in the pressed state
7807 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007808 // Use a Runnable and post this rather than calling
7809 // performClick directly. This lets other visual state
7810 // of the view update before click actions start.
7811 if (mPerformClick == null) {
7812 mPerformClick = new PerformClick();
7813 }
7814 if (!post(mPerformClick)) {
7815 performClick();
7816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 }
7818 }
7819
7820 if (mUnsetPressedState == null) {
7821 mUnsetPressedState = new UnsetPressedState();
7822 }
7823
Adam Powelle14579b2009-12-16 18:39:52 -08007824 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007825 postDelayed(mUnsetPressedState,
7826 ViewConfiguration.getPressedStateDuration());
7827 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 // If the post failed, unpress right now
7829 mUnsetPressedState.run();
7830 }
Adam Powelle14579b2009-12-16 18:39:52 -08007831 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007832 }
7833 break;
7834
7835 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007836 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007837
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007838 if (performButtonActionOnTouchDown(event)) {
7839 break;
7840 }
7841
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007842 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007843 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007844
7845 // For views inside a scrolling container, delay the pressed feedback for
7846 // a short period in case this is a scroll.
7847 if (isInScrollingContainer) {
7848 mPrivateFlags |= PREPRESSED;
7849 if (mPendingCheckForTap == null) {
7850 mPendingCheckForTap = new CheckForTap();
7851 }
7852 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7853 } else {
7854 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007855 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007856 checkForLongClick(0);
7857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 break;
7859
7860 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007861 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007862 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 break;
7864
7865 case MotionEvent.ACTION_MOVE:
7866 final int x = (int) event.getX();
7867 final int y = (int) event.getY();
7868
7869 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007870 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007871 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007872 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007874 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007875 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876
Adam Powell4d6f0662012-02-21 15:11:11 -08007877 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007879 }
7880 break;
7881 }
7882 return true;
7883 }
7884
7885 return false;
7886 }
7887
7888 /**
Adam Powell10298662011-08-14 18:26:30 -07007889 * @hide
7890 */
7891 public boolean isInScrollingContainer() {
7892 ViewParent p = getParent();
7893 while (p != null && p instanceof ViewGroup) {
7894 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7895 return true;
7896 }
7897 p = p.getParent();
7898 }
7899 return false;
7900 }
7901
7902 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007903 * Remove the longpress detection timer.
7904 */
7905 private void removeLongPressCallback() {
7906 if (mPendingCheckForLongPress != null) {
7907 removeCallbacks(mPendingCheckForLongPress);
7908 }
7909 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007910
7911 /**
7912 * Remove the pending click action
7913 */
7914 private void removePerformClickCallback() {
7915 if (mPerformClick != null) {
7916 removeCallbacks(mPerformClick);
7917 }
7918 }
7919
Adam Powelle14579b2009-12-16 18:39:52 -08007920 /**
Romain Guya440b002010-02-24 15:57:54 -08007921 * Remove the prepress detection timer.
7922 */
7923 private void removeUnsetPressCallback() {
7924 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7925 setPressed(false);
7926 removeCallbacks(mUnsetPressedState);
7927 }
7928 }
7929
7930 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007931 * Remove the tap detection timer.
7932 */
7933 private void removeTapCallback() {
7934 if (mPendingCheckForTap != null) {
7935 mPrivateFlags &= ~PREPRESSED;
7936 removeCallbacks(mPendingCheckForTap);
7937 }
7938 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007939
7940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 * Cancels a pending long press. Your subclass can use this if you
7942 * want the context menu to come up if the user presses and holds
7943 * at the same place, but you don't want it to come up if they press
7944 * and then move around enough to cause scrolling.
7945 */
7946 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007947 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007948
7949 /*
7950 * The prepressed state handled by the tap callback is a display
7951 * construct, but the tap callback will post a long press callback
7952 * less its own timeout. Remove it here.
7953 */
7954 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007955 }
7956
7957 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007958 * Remove the pending callback for sending a
7959 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7960 */
7961 private void removeSendViewScrolledAccessibilityEventCallback() {
7962 if (mSendViewScrolledAccessibilityEvent != null) {
7963 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7964 }
7965 }
7966
7967 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 * Sets the TouchDelegate for this View.
7969 */
7970 public void setTouchDelegate(TouchDelegate delegate) {
7971 mTouchDelegate = delegate;
7972 }
7973
7974 /**
7975 * Gets the TouchDelegate for this View.
7976 */
7977 public TouchDelegate getTouchDelegate() {
7978 return mTouchDelegate;
7979 }
7980
7981 /**
7982 * Set flags controlling behavior of this view.
7983 *
7984 * @param flags Constant indicating the value which should be set
7985 * @param mask Constant indicating the bit range that should be changed
7986 */
7987 void setFlags(int flags, int mask) {
7988 int old = mViewFlags;
7989 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7990
7991 int changed = mViewFlags ^ old;
7992 if (changed == 0) {
7993 return;
7994 }
7995 int privateFlags = mPrivateFlags;
7996
7997 /* Check if the FOCUSABLE bit has changed */
7998 if (((changed & FOCUSABLE_MASK) != 0) &&
7999 ((privateFlags & HAS_BOUNDS) !=0)) {
8000 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8001 && ((privateFlags & FOCUSED) != 0)) {
8002 /* Give up focus if we are no longer focusable */
8003 clearFocus();
8004 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8005 && ((privateFlags & FOCUSED) == 0)) {
8006 /*
8007 * Tell the view system that we are now available to take focus
8008 * if no one else already has it.
8009 */
8010 if (mParent != null) mParent.focusableViewAvailable(this);
8011 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008012 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8013 notifyAccessibilityStateChanged();
8014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 }
8016
8017 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8018 if ((changed & VISIBILITY_MASK) != 0) {
8019 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008020 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008021 * it was not visible. Marking it drawn ensures that the invalidation will
8022 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008024 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008025 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026
8027 needGlobalAttributesUpdate(true);
8028
8029 // a view becoming visible is worth notifying the parent
8030 // about in case nothing has focus. even if this specific view
8031 // isn't focusable, it may contain something that is, so let
8032 // the root view try to give this focus if nothing else does.
8033 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8034 mParent.focusableViewAvailable(this);
8035 }
8036 }
8037 }
8038
8039 /* Check if the GONE bit has changed */
8040 if ((changed & GONE) != 0) {
8041 needGlobalAttributesUpdate(false);
8042 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043
Romain Guyecd80ee2009-12-03 17:13:02 -08008044 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8045 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008046 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008047 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008048 if (mParent instanceof View) {
8049 // GONE views noop invalidation, so invalidate the parent
8050 ((View) mParent).invalidate(true);
8051 }
8052 // Mark the view drawn to ensure that it gets invalidated properly the next
8053 // time it is visible and gets invalidated
8054 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 }
8056 if (mAttachInfo != null) {
8057 mAttachInfo.mViewVisibilityChanged = true;
8058 }
8059 }
8060
8061 /* Check if the VISIBLE bit has changed */
8062 if ((changed & INVISIBLE) != 0) {
8063 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008064 /*
8065 * If this view is becoming invisible, set the DRAWN flag so that
8066 * the next invalidate() will not be skipped.
8067 */
8068 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069
8070 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008071 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 if (getRootView() != this) {
8073 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008074 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 }
8076 }
8077 if (mAttachInfo != null) {
8078 mAttachInfo.mViewVisibilityChanged = true;
8079 }
8080 }
8081
Adam Powell326d8082009-12-09 15:10:07 -08008082 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008083 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008084 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8085 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008086 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008087 } else if (mParent != null) {
8088 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008089 }
Adam Powell326d8082009-12-09 15:10:07 -08008090 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8091 }
8092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008093 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8094 destroyDrawingCache();
8095 }
8096
8097 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8098 destroyDrawingCache();
8099 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008100 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 }
8102
8103 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8104 destroyDrawingCache();
8105 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8106 }
8107
8108 if ((changed & DRAW_MASK) != 0) {
8109 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008110 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 mPrivateFlags &= ~SKIP_DRAW;
8112 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8113 } else {
8114 mPrivateFlags |= SKIP_DRAW;
8115 }
8116 } else {
8117 mPrivateFlags &= ~SKIP_DRAW;
8118 }
8119 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008120 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122
8123 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008124 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008125 mParent.recomputeViewAttributes(this);
8126 }
8127 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008128
8129 if (AccessibilityManager.getInstance(mContext).isEnabled()
8130 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8131 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8132 notifyAccessibilityStateChanged();
8133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 }
8135
8136 /**
8137 * Change the view's z order in the tree, so it's on top of other sibling
8138 * views
8139 */
8140 public void bringToFront() {
8141 if (mParent != null) {
8142 mParent.bringChildToFront(this);
8143 }
8144 }
8145
8146 /**
8147 * This is called in response to an internal scroll in this view (i.e., the
8148 * view scrolled its own contents). This is typically as a result of
8149 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8150 * called.
8151 *
8152 * @param l Current horizontal scroll origin.
8153 * @param t Current vertical scroll origin.
8154 * @param oldl Previous horizontal scroll origin.
8155 * @param oldt Previous vertical scroll origin.
8156 */
8157 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008158 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8159 postSendViewScrolledAccessibilityEventCallback();
8160 }
8161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 mBackgroundSizeChanged = true;
8163
8164 final AttachInfo ai = mAttachInfo;
8165 if (ai != null) {
8166 ai.mViewScrollChanged = true;
8167 }
8168 }
8169
8170 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008171 * Interface definition for a callback to be invoked when the layout bounds of a view
8172 * changes due to layout processing.
8173 */
8174 public interface OnLayoutChangeListener {
8175 /**
8176 * Called when the focus state of a view has changed.
8177 *
8178 * @param v The view whose state has changed.
8179 * @param left The new value of the view's left property.
8180 * @param top The new value of the view's top property.
8181 * @param right The new value of the view's right property.
8182 * @param bottom The new value of the view's bottom property.
8183 * @param oldLeft The previous value of the view's left property.
8184 * @param oldTop The previous value of the view's top property.
8185 * @param oldRight The previous value of the view's right property.
8186 * @param oldBottom The previous value of the view's bottom property.
8187 */
8188 void onLayoutChange(View v, int left, int top, int right, int bottom,
8189 int oldLeft, int oldTop, int oldRight, int oldBottom);
8190 }
8191
8192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 * This is called during layout when the size of this view has changed. If
8194 * you were just added to the view hierarchy, you're called with the old
8195 * values of 0.
8196 *
8197 * @param w Current width of this view.
8198 * @param h Current height of this view.
8199 * @param oldw Old width of this view.
8200 * @param oldh Old height of this view.
8201 */
8202 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8203 }
8204
8205 /**
8206 * Called by draw to draw the child views. This may be overridden
8207 * by derived classes to gain control just before its children are drawn
8208 * (but after its own view has been drawn).
8209 * @param canvas the canvas on which to draw the view
8210 */
8211 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 }
8214
8215 /**
8216 * Gets the parent of this view. Note that the parent is a
8217 * ViewParent and not necessarily a View.
8218 *
8219 * @return Parent of this view.
8220 */
8221 public final ViewParent getParent() {
8222 return mParent;
8223 }
8224
8225 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008226 * Set the horizontal scrolled position of your view. This will cause a call to
8227 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8228 * invalidated.
8229 * @param value the x position to scroll to
8230 */
8231 public void setScrollX(int value) {
8232 scrollTo(value, mScrollY);
8233 }
8234
8235 /**
8236 * Set the vertical scrolled position of your view. This will cause a call to
8237 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8238 * invalidated.
8239 * @param value the y position to scroll to
8240 */
8241 public void setScrollY(int value) {
8242 scrollTo(mScrollX, value);
8243 }
8244
8245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 * Return the scrolled left position of this view. This is the left edge of
8247 * the displayed part of your view. You do not need to draw any pixels
8248 * farther left, since those are outside of the frame of your view on
8249 * screen.
8250 *
8251 * @return The left edge of the displayed part of your view, in pixels.
8252 */
8253 public final int getScrollX() {
8254 return mScrollX;
8255 }
8256
8257 /**
8258 * Return the scrolled top position of this view. This is the top edge of
8259 * the displayed part of your view. You do not need to draw any pixels above
8260 * it, since those are outside of the frame of your view on screen.
8261 *
8262 * @return The top edge of the displayed part of your view, in pixels.
8263 */
8264 public final int getScrollY() {
8265 return mScrollY;
8266 }
8267
8268 /**
8269 * Return the width of the your view.
8270 *
8271 * @return The width of your view, in pixels.
8272 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008273 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 public final int getWidth() {
8275 return mRight - mLeft;
8276 }
8277
8278 /**
8279 * Return the height of your view.
8280 *
8281 * @return The height of your view, in pixels.
8282 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008283 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008284 public final int getHeight() {
8285 return mBottom - mTop;
8286 }
8287
8288 /**
8289 * Return the visible drawing bounds of your view. Fills in the output
8290 * rectangle with the values from getScrollX(), getScrollY(),
8291 * getWidth(), and getHeight().
8292 *
8293 * @param outRect The (scrolled) drawing bounds of the view.
8294 */
8295 public void getDrawingRect(Rect outRect) {
8296 outRect.left = mScrollX;
8297 outRect.top = mScrollY;
8298 outRect.right = mScrollX + (mRight - mLeft);
8299 outRect.bottom = mScrollY + (mBottom - mTop);
8300 }
8301
8302 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008303 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8304 * raw width component (that is the result is masked by
8305 * {@link #MEASURED_SIZE_MASK}).
8306 *
8307 * @return The raw measured width of this view.
8308 */
8309 public final int getMeasuredWidth() {
8310 return mMeasuredWidth & MEASURED_SIZE_MASK;
8311 }
8312
8313 /**
8314 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008315 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008316 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 * This should be used during measurement and layout calculations only. Use
8318 * {@link #getWidth()} to see how wide a view is after layout.
8319 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008320 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008322 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 return mMeasuredWidth;
8324 }
8325
8326 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008327 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8328 * raw width component (that is the result is masked by
8329 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008331 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 */
8333 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008334 return mMeasuredHeight & MEASURED_SIZE_MASK;
8335 }
8336
8337 /**
8338 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008339 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008340 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8341 * This should be used during measurement and layout calculations only. Use
8342 * {@link #getHeight()} to see how wide a view is after layout.
8343 *
8344 * @return The measured width of this view as a bit mask.
8345 */
8346 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 return mMeasuredHeight;
8348 }
8349
8350 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008351 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8352 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8353 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8354 * and the height component is at the shifted bits
8355 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8356 */
8357 public final int getMeasuredState() {
8358 return (mMeasuredWidth&MEASURED_STATE_MASK)
8359 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8360 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8361 }
8362
8363 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008364 * The transform matrix of this view, which is calculated based on the current
8365 * roation, scale, and pivot properties.
8366 *
8367 * @see #getRotation()
8368 * @see #getScaleX()
8369 * @see #getScaleY()
8370 * @see #getPivotX()
8371 * @see #getPivotY()
8372 * @return The current transform matrix for the view
8373 */
8374 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008375 if (mTransformationInfo != null) {
8376 updateMatrix();
8377 return mTransformationInfo.mMatrix;
8378 }
8379 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008380 }
8381
8382 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008383 * Utility function to determine if the value is far enough away from zero to be
8384 * considered non-zero.
8385 * @param value A floating point value to check for zero-ness
8386 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8387 */
8388 private static boolean nonzero(float value) {
8389 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8390 }
8391
8392 /**
Jeff Brown86671742010-09-30 20:00:15 -07008393 * Returns true if the transform matrix is the identity matrix.
8394 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008395 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008396 * @return True if the transform matrix is the identity matrix, false otherwise.
8397 */
Jeff Brown86671742010-09-30 20:00:15 -07008398 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008399 if (mTransformationInfo != null) {
8400 updateMatrix();
8401 return mTransformationInfo.mMatrixIsIdentity;
8402 }
8403 return true;
8404 }
8405
8406 void ensureTransformationInfo() {
8407 if (mTransformationInfo == null) {
8408 mTransformationInfo = new TransformationInfo();
8409 }
Jeff Brown86671742010-09-30 20:00:15 -07008410 }
8411
8412 /**
8413 * Recomputes the transform matrix if necessary.
8414 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008415 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008416 final TransformationInfo info = mTransformationInfo;
8417 if (info == null) {
8418 return;
8419 }
8420 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008421 // transform-related properties have changed since the last time someone
8422 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008423
8424 // Figure out if we need to update the pivot point
8425 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008426 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8427 info.mPrevWidth = mRight - mLeft;
8428 info.mPrevHeight = mBottom - mTop;
8429 info.mPivotX = info.mPrevWidth / 2f;
8430 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008431 }
8432 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008433 info.mMatrix.reset();
8434 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8435 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8436 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8437 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008438 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008439 if (info.mCamera == null) {
8440 info.mCamera = new Camera();
8441 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008442 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008443 info.mCamera.save();
8444 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8445 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8446 info.mCamera.getMatrix(info.matrix3D);
8447 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8448 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8449 info.mPivotY + info.mTranslationY);
8450 info.mMatrix.postConcat(info.matrix3D);
8451 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008452 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008453 info.mMatrixDirty = false;
8454 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8455 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008456 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008457 }
8458
8459 /**
8460 * Utility method to retrieve the inverse of the current mMatrix property.
8461 * We cache the matrix to avoid recalculating it when transform properties
8462 * have not changed.
8463 *
8464 * @return The inverse of the current matrix of this view.
8465 */
Jeff Brown86671742010-09-30 20:00:15 -07008466 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008467 final TransformationInfo info = mTransformationInfo;
8468 if (info != null) {
8469 updateMatrix();
8470 if (info.mInverseMatrixDirty) {
8471 if (info.mInverseMatrix == null) {
8472 info.mInverseMatrix = new Matrix();
8473 }
8474 info.mMatrix.invert(info.mInverseMatrix);
8475 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008476 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008477 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008478 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008479 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008480 }
8481
8482 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008483 * Gets the distance along the Z axis from the camera to this view.
8484 *
8485 * @see #setCameraDistance(float)
8486 *
8487 * @return The distance along the Z axis.
8488 */
8489 public float getCameraDistance() {
8490 ensureTransformationInfo();
8491 final float dpi = mResources.getDisplayMetrics().densityDpi;
8492 final TransformationInfo info = mTransformationInfo;
8493 if (info.mCamera == null) {
8494 info.mCamera = new Camera();
8495 info.matrix3D = new Matrix();
8496 }
8497 return -(info.mCamera.getLocationZ() * dpi);
8498 }
8499
8500 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008501 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8502 * views are drawn) from the camera to this view. The camera's distance
8503 * affects 3D transformations, for instance rotations around the X and Y
8504 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008505 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008506 * use a camera distance that's greater than the height (X axis rotation) or
8507 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008508 *
Romain Guya5364ee2011-02-24 14:46:04 -08008509 * <p>The distance of the camera from the view plane can have an affect on the
8510 * perspective distortion of the view when it is rotated around the x or y axis.
8511 * For example, a large distance will result in a large viewing angle, and there
8512 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008513 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008514 * also result in some drawing artifacts if the rotated view ends up partially
8515 * behind the camera (which is why the recommendation is to use a distance at
8516 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008517 *
Romain Guya5364ee2011-02-24 14:46:04 -08008518 * <p>The distance is expressed in "depth pixels." The default distance depends
8519 * on the screen density. For instance, on a medium density display, the
8520 * default distance is 1280. On a high density display, the default distance
8521 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008522 *
Romain Guya5364ee2011-02-24 14:46:04 -08008523 * <p>If you want to specify a distance that leads to visually consistent
8524 * results across various densities, use the following formula:</p>
8525 * <pre>
8526 * float scale = context.getResources().getDisplayMetrics().density;
8527 * view.setCameraDistance(distance * scale);
8528 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008529 *
Romain Guya5364ee2011-02-24 14:46:04 -08008530 * <p>The density scale factor of a high density display is 1.5,
8531 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008532 *
Romain Guya5364ee2011-02-24 14:46:04 -08008533 * @param distance The distance in "depth pixels", if negative the opposite
8534 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008535 *
8536 * @see #setRotationX(float)
8537 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008538 */
8539 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008540 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008541
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008542 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008543 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008544 final TransformationInfo info = mTransformationInfo;
8545 if (info.mCamera == null) {
8546 info.mCamera = new Camera();
8547 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008548 }
8549
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008550 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8551 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008552
Chet Haase9d1992d2012-03-13 11:03:25 -07008553 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008554 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008555 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008556 }
Romain Guya5364ee2011-02-24 14:46:04 -08008557 }
8558
8559 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008560 * The degrees that the view is rotated around the pivot point.
8561 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008562 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008563 * @see #getPivotX()
8564 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008565 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008566 * @return The degrees of rotation.
8567 */
Chet Haasea5531132012-02-02 13:41:44 -08008568 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008569 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008570 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008571 }
8572
8573 /**
Chet Haase897247b2010-09-09 14:54:47 -07008574 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8575 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008576 *
8577 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008578 *
8579 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008580 * @see #getPivotX()
8581 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008582 * @see #setRotationX(float)
8583 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008584 *
8585 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008586 */
8587 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008588 ensureTransformationInfo();
8589 final TransformationInfo info = mTransformationInfo;
8590 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008591 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008592 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008593 info.mRotation = rotation;
8594 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008595 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008596 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008597 mDisplayList.setRotation(rotation);
8598 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 }
8600 }
8601
8602 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008603 * The degrees that the view is rotated around the vertical axis through the pivot point.
8604 *
8605 * @see #getPivotX()
8606 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008607 * @see #setRotationY(float)
8608 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008609 * @return The degrees of Y rotation.
8610 */
Chet Haasea5531132012-02-02 13:41:44 -08008611 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008612 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008613 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008614 }
8615
8616 /**
Chet Haase897247b2010-09-09 14:54:47 -07008617 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8618 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8619 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008620 *
Romain Guya5364ee2011-02-24 14:46:04 -08008621 * When rotating large views, it is recommended to adjust the camera distance
8622 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008623 *
8624 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008625 *
8626 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008627 * @see #getPivotX()
8628 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008629 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008630 * @see #setRotationX(float)
8631 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008632 *
8633 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008634 */
8635 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008636 ensureTransformationInfo();
8637 final TransformationInfo info = mTransformationInfo;
8638 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008639 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008640 info.mRotationY = rotationY;
8641 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008642 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008643 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008644 mDisplayList.setRotationY(rotationY);
8645 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008646 }
8647 }
8648
8649 /**
8650 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8651 *
8652 * @see #getPivotX()
8653 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008654 * @see #setRotationX(float)
8655 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008656 * @return The degrees of X rotation.
8657 */
Chet Haasea5531132012-02-02 13:41:44 -08008658 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008659 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008660 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008661 }
8662
8663 /**
Chet Haase897247b2010-09-09 14:54:47 -07008664 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8665 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8666 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008667 *
Romain Guya5364ee2011-02-24 14:46:04 -08008668 * When rotating large views, it is recommended to adjust the camera distance
8669 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008670 *
8671 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008672 *
8673 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008674 * @see #getPivotX()
8675 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008676 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008677 * @see #setRotationY(float)
8678 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008679 *
8680 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008681 */
8682 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008683 ensureTransformationInfo();
8684 final TransformationInfo info = mTransformationInfo;
8685 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008686 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008687 info.mRotationX = rotationX;
8688 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008689 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008690 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008691 mDisplayList.setRotationX(rotationX);
8692 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008693 }
8694 }
8695
8696 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008697 * The amount that the view is scaled in x around the pivot point, as a proportion of
8698 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8699 *
Joe Onorato93162322010-09-16 15:42:01 -04008700 * <p>By default, this is 1.0f.
8701 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008702 * @see #getPivotX()
8703 * @see #getPivotY()
8704 * @return The scaling factor.
8705 */
Chet Haasea5531132012-02-02 13:41:44 -08008706 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008707 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008708 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008709 }
8710
8711 /**
8712 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8713 * the view's unscaled width. A value of 1 means that no scaling is applied.
8714 *
8715 * @param scaleX The scaling factor.
8716 * @see #getPivotX()
8717 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008718 *
8719 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008720 */
8721 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008722 ensureTransformationInfo();
8723 final TransformationInfo info = mTransformationInfo;
8724 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008725 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008726 info.mScaleX = scaleX;
8727 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008728 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008729 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008730 mDisplayList.setScaleX(scaleX);
8731 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008732 }
8733 }
8734
8735 /**
8736 * The amount that the view is scaled in y around the pivot point, as a proportion of
8737 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8738 *
Joe Onorato93162322010-09-16 15:42:01 -04008739 * <p>By default, this is 1.0f.
8740 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008741 * @see #getPivotX()
8742 * @see #getPivotY()
8743 * @return The scaling factor.
8744 */
Chet Haasea5531132012-02-02 13:41:44 -08008745 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008746 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008747 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008748 }
8749
8750 /**
8751 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8752 * the view's unscaled width. A value of 1 means that no scaling is applied.
8753 *
8754 * @param scaleY The scaling factor.
8755 * @see #getPivotX()
8756 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008757 *
8758 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008759 */
8760 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008761 ensureTransformationInfo();
8762 final TransformationInfo info = mTransformationInfo;
8763 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008764 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008765 info.mScaleY = scaleY;
8766 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008767 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008768 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008769 mDisplayList.setScaleY(scaleY);
8770 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008771 }
8772 }
8773
8774 /**
8775 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8776 * and {@link #setScaleX(float) scaled}.
8777 *
8778 * @see #getRotation()
8779 * @see #getScaleX()
8780 * @see #getScaleY()
8781 * @see #getPivotY()
8782 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008783 *
8784 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008785 */
Chet Haasea5531132012-02-02 13:41:44 -08008786 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008787 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008788 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008789 }
8790
8791 /**
8792 * Sets the x location of the point around which the view is
8793 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008794 * By default, the pivot point is centered on the object.
8795 * Setting this property disables this behavior and causes the view to use only the
8796 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008797 *
8798 * @param pivotX The x location of the pivot point.
8799 * @see #getRotation()
8800 * @see #getScaleX()
8801 * @see #getScaleY()
8802 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008803 *
8804 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008805 */
8806 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008807 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008808 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008809 final TransformationInfo info = mTransformationInfo;
8810 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008811 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008812 info.mPivotX = pivotX;
8813 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008814 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008815 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008816 mDisplayList.setPivotX(pivotX);
8817 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008818 }
8819 }
8820
8821 /**
8822 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8823 * and {@link #setScaleY(float) scaled}.
8824 *
8825 * @see #getRotation()
8826 * @see #getScaleX()
8827 * @see #getScaleY()
8828 * @see #getPivotY()
8829 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008830 *
8831 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008832 */
Chet Haasea5531132012-02-02 13:41:44 -08008833 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008834 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008835 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008836 }
8837
8838 /**
8839 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008840 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8841 * Setting this property disables this behavior and causes the view to use only the
8842 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008843 *
8844 * @param pivotY The y location of the pivot point.
8845 * @see #getRotation()
8846 * @see #getScaleX()
8847 * @see #getScaleY()
8848 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008849 *
8850 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008851 */
8852 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008854 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008855 final TransformationInfo info = mTransformationInfo;
8856 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008857 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008858 info.mPivotY = pivotY;
8859 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008860 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008861 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008862 mDisplayList.setPivotY(pivotY);
8863 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008864 }
8865 }
8866
8867 /**
8868 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8869 * completely transparent and 1 means the view is completely opaque.
8870 *
Joe Onorato93162322010-09-16 15:42:01 -04008871 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 * @return The opacity of the view.
8873 */
Chet Haasea5531132012-02-02 13:41:44 -08008874 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008876 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008877 }
8878
8879 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008880 * Returns whether this View has content which overlaps. This function, intended to be
8881 * overridden by specific View types, is an optimization when alpha is set on a view. If
8882 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8883 * and then composited it into place, which can be expensive. If the view has no overlapping
8884 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8885 * An example of overlapping rendering is a TextView with a background image, such as a
8886 * Button. An example of non-overlapping rendering is a TextView with no background, or
8887 * an ImageView with only the foreground image. The default implementation returns true;
8888 * subclasses should override if they have cases which can be optimized.
8889 *
8890 * @return true if the content in this view might overlap, false otherwise.
8891 */
8892 public boolean hasOverlappingRendering() {
8893 return true;
8894 }
8895
8896 /**
Romain Guy171c5922011-01-06 10:04:23 -08008897 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8898 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008899 *
Romain Guy171c5922011-01-06 10:04:23 -08008900 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8901 * responsible for applying the opacity itself. Otherwise, calling this method is
8902 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008903 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008904 *
Chet Haasea5531132012-02-02 13:41:44 -08008905 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8906 * performance implications. It is generally best to use the alpha property sparingly and
8907 * transiently, as in the case of fading animations.</p>
8908 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008909 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008910 *
Joe Malin32736f02011-01-19 16:14:20 -08008911 * @see #setLayerType(int, android.graphics.Paint)
8912 *
Chet Haase73066682010-11-29 15:55:32 -08008913 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008914 */
8915 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008916 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008917 if (mTransformationInfo.mAlpha != alpha) {
8918 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008919 if (onSetAlpha((int) (alpha * 255))) {
8920 mPrivateFlags |= ALPHA_SET;
8921 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008922 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008923 invalidate(true);
8924 } else {
8925 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008926 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008927 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008928 mDisplayList.setAlpha(alpha);
8929 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008930 }
Chet Haaseed032702010-10-01 14:05:54 -07008931 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008932 }
8933
8934 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008935 * Faster version of setAlpha() which performs the same steps except there are
8936 * no calls to invalidate(). The caller of this function should perform proper invalidation
8937 * on the parent and this object. The return value indicates whether the subclass handles
8938 * alpha (the return value for onSetAlpha()).
8939 *
8940 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008941 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8942 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008943 */
8944 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008945 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008946 if (mTransformationInfo.mAlpha != alpha) {
8947 mTransformationInfo.mAlpha = alpha;
8948 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8949 if (subclassHandlesAlpha) {
8950 mPrivateFlags |= ALPHA_SET;
8951 return true;
8952 } else {
8953 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008954 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008955 mDisplayList.setAlpha(alpha);
8956 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008957 }
Chet Haasea00f3862011-02-22 06:34:40 -08008958 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008959 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008960 }
8961
8962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008963 * Top position of this view relative to its parent.
8964 *
8965 * @return The top of this view, in pixels.
8966 */
8967 @ViewDebug.CapturedViewProperty
8968 public final int getTop() {
8969 return mTop;
8970 }
8971
8972 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008973 * Sets the top position of this view relative to its parent. This method is meant to be called
8974 * by the layout system and should not generally be called otherwise, because the property
8975 * may be changed at any time by the layout.
8976 *
8977 * @param top The top of this view, in pixels.
8978 */
8979 public final void setTop(int top) {
8980 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008981 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008982 final boolean matrixIsIdentity = mTransformationInfo == null
8983 || mTransformationInfo.mMatrixIsIdentity;
8984 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008985 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008986 int minTop;
8987 int yLoc;
8988 if (top < mTop) {
8989 minTop = top;
8990 yLoc = top - mTop;
8991 } else {
8992 minTop = mTop;
8993 yLoc = 0;
8994 }
Chet Haasee9140a72011-02-16 16:23:29 -08008995 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008996 }
8997 } else {
8998 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008999 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009000 }
9001
Chet Haaseed032702010-10-01 14:05:54 -07009002 int width = mRight - mLeft;
9003 int oldHeight = mBottom - mTop;
9004
Chet Haase21cd1382010-09-01 17:42:29 -07009005 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009006 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009007 mDisplayList.setTop(mTop);
9008 }
Chet Haase21cd1382010-09-01 17:42:29 -07009009
Chet Haaseed032702010-10-01 14:05:54 -07009010 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9011
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009012 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009013 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9014 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009015 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009016 }
Chet Haase21cd1382010-09-01 17:42:29 -07009017 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009018 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009019 }
Chet Haase55dbb652010-12-21 20:15:08 -08009020 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009021 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009022 }
9023 }
9024
9025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 * Bottom position of this view relative to its parent.
9027 *
9028 * @return The bottom of this view, in pixels.
9029 */
9030 @ViewDebug.CapturedViewProperty
9031 public final int getBottom() {
9032 return mBottom;
9033 }
9034
9035 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009036 * True if this view has changed since the last time being drawn.
9037 *
9038 * @return The dirty state of this view.
9039 */
9040 public boolean isDirty() {
9041 return (mPrivateFlags & DIRTY_MASK) != 0;
9042 }
9043
9044 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009045 * Sets the bottom position of this view relative to its parent. This method is meant to be
9046 * called by the layout system and should not generally be called otherwise, because the
9047 * property may be changed at any time by the layout.
9048 *
9049 * @param bottom The bottom of this view, in pixels.
9050 */
9051 public final void setBottom(int bottom) {
9052 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009053 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 final boolean matrixIsIdentity = mTransformationInfo == null
9055 || mTransformationInfo.mMatrixIsIdentity;
9056 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009057 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009058 int maxBottom;
9059 if (bottom < mBottom) {
9060 maxBottom = mBottom;
9061 } else {
9062 maxBottom = bottom;
9063 }
Chet Haasee9140a72011-02-16 16:23:29 -08009064 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009065 }
9066 } else {
9067 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009068 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009069 }
9070
Chet Haaseed032702010-10-01 14:05:54 -07009071 int width = mRight - mLeft;
9072 int oldHeight = mBottom - mTop;
9073
Chet Haase21cd1382010-09-01 17:42:29 -07009074 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009075 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009076 mDisplayList.setBottom(mBottom);
9077 }
Chet Haase21cd1382010-09-01 17:42:29 -07009078
Chet Haaseed032702010-10-01 14:05:54 -07009079 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9080
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009081 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009082 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9083 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009084 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009085 }
Chet Haase21cd1382010-09-01 17:42:29 -07009086 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009087 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009088 }
Chet Haase55dbb652010-12-21 20:15:08 -08009089 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009090 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009091 }
9092 }
9093
9094 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009095 * Left position of this view relative to its parent.
9096 *
9097 * @return The left edge of this view, in pixels.
9098 */
9099 @ViewDebug.CapturedViewProperty
9100 public final int getLeft() {
9101 return mLeft;
9102 }
9103
9104 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009105 * Sets the left position of this view relative to its parent. This method is meant to be called
9106 * by the layout system and should not generally be called otherwise, because the property
9107 * may be changed at any time by the layout.
9108 *
9109 * @param left The bottom of this view, in pixels.
9110 */
9111 public final void setLeft(int left) {
9112 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009113 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009114 final boolean matrixIsIdentity = mTransformationInfo == null
9115 || mTransformationInfo.mMatrixIsIdentity;
9116 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009117 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009118 int minLeft;
9119 int xLoc;
9120 if (left < mLeft) {
9121 minLeft = left;
9122 xLoc = left - mLeft;
9123 } else {
9124 minLeft = mLeft;
9125 xLoc = 0;
9126 }
Chet Haasee9140a72011-02-16 16:23:29 -08009127 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009128 }
9129 } else {
9130 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009131 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009132 }
9133
Chet Haaseed032702010-10-01 14:05:54 -07009134 int oldWidth = mRight - mLeft;
9135 int height = mBottom - mTop;
9136
Chet Haase21cd1382010-09-01 17:42:29 -07009137 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009138 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009139 mDisplayList.setLeft(left);
9140 }
Chet Haase21cd1382010-09-01 17:42:29 -07009141
Chet Haaseed032702010-10-01 14:05:54 -07009142 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9143
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009144 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009145 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9146 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009147 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009148 }
Chet Haase21cd1382010-09-01 17:42:29 -07009149 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009150 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009151 }
Chet Haase55dbb652010-12-21 20:15:08 -08009152 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009153 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009154 }
9155 }
9156
9157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 * Right position of this view relative to its parent.
9159 *
9160 * @return The right edge of this view, in pixels.
9161 */
9162 @ViewDebug.CapturedViewProperty
9163 public final int getRight() {
9164 return mRight;
9165 }
9166
9167 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009168 * Sets the right position of this view relative to its parent. This method is meant to be called
9169 * by the layout system and should not generally be called otherwise, because the property
9170 * may be changed at any time by the layout.
9171 *
9172 * @param right The bottom of this view, in pixels.
9173 */
9174 public final void setRight(int right) {
9175 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009176 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009177 final boolean matrixIsIdentity = mTransformationInfo == null
9178 || mTransformationInfo.mMatrixIsIdentity;
9179 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009180 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009181 int maxRight;
9182 if (right < mRight) {
9183 maxRight = mRight;
9184 } else {
9185 maxRight = right;
9186 }
Chet Haasee9140a72011-02-16 16:23:29 -08009187 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009188 }
9189 } else {
9190 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009191 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009192 }
9193
Chet Haaseed032702010-10-01 14:05:54 -07009194 int oldWidth = mRight - mLeft;
9195 int height = mBottom - mTop;
9196
Chet Haase21cd1382010-09-01 17:42:29 -07009197 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009198 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009199 mDisplayList.setRight(mRight);
9200 }
Chet Haase21cd1382010-09-01 17:42:29 -07009201
Chet Haaseed032702010-10-01 14:05:54 -07009202 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9203
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009204 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009205 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9206 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009207 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009208 }
Chet Haase21cd1382010-09-01 17:42:29 -07009209 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009210 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009211 }
Chet Haase55dbb652010-12-21 20:15:08 -08009212 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009213 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009214 }
9215 }
9216
9217 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009218 * The visual x position of this view, in pixels. This is equivalent to the
9219 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009220 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009221 *
Chet Haasedf030d22010-07-30 17:22:38 -07009222 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009223 */
Chet Haasea5531132012-02-02 13:41:44 -08009224 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009225 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009226 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009227 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009228
Chet Haasedf030d22010-07-30 17:22:38 -07009229 /**
9230 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9231 * {@link #setTranslationX(float) translationX} property to be the difference between
9232 * the x value passed in and the current {@link #getLeft() left} property.
9233 *
9234 * @param x The visual x position of this view, in pixels.
9235 */
9236 public void setX(float x) {
9237 setTranslationX(x - mLeft);
9238 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009239
Chet Haasedf030d22010-07-30 17:22:38 -07009240 /**
9241 * The visual y position of this view, in pixels. This is equivalent to the
9242 * {@link #setTranslationY(float) translationY} property plus the current
9243 * {@link #getTop() top} property.
9244 *
9245 * @return The visual y position of this view, in pixels.
9246 */
Chet Haasea5531132012-02-02 13:41:44 -08009247 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009248 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009249 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009250 }
9251
9252 /**
9253 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9254 * {@link #setTranslationY(float) translationY} property to be the difference between
9255 * the y value passed in and the current {@link #getTop() top} property.
9256 *
9257 * @param y The visual y position of this view, in pixels.
9258 */
9259 public void setY(float y) {
9260 setTranslationY(y - mTop);
9261 }
9262
9263
9264 /**
9265 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9266 * This position is post-layout, in addition to wherever the object's
9267 * layout placed it.
9268 *
9269 * @return The horizontal position of this view relative to its left position, in pixels.
9270 */
Chet Haasea5531132012-02-02 13:41:44 -08009271 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009272 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009273 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009274 }
9275
9276 /**
9277 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9278 * This effectively positions the object post-layout, in addition to wherever the object's
9279 * layout placed it.
9280 *
9281 * @param translationX The horizontal position of this view relative to its left position,
9282 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009283 *
9284 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009285 */
9286 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009287 ensureTransformationInfo();
9288 final TransformationInfo info = mTransformationInfo;
9289 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009290 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009291 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009292 info.mTranslationX = translationX;
9293 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009294 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009295 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009296 mDisplayList.setTranslationX(translationX);
9297 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009298 }
9299 }
9300
9301 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009302 * The horizontal location of this view relative to its {@link #getTop() top} position.
9303 * This position is post-layout, in addition to wherever the object's
9304 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009305 *
Chet Haasedf030d22010-07-30 17:22:38 -07009306 * @return The vertical position of this view relative to its top position,
9307 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009308 */
Chet Haasea5531132012-02-02 13:41:44 -08009309 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009310 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009311 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009312 }
9313
9314 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009315 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9316 * This effectively positions the object post-layout, in addition to wherever the object's
9317 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009318 *
Chet Haasedf030d22010-07-30 17:22:38 -07009319 * @param translationY The vertical position of this view relative to its top position,
9320 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009321 *
9322 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009323 */
Chet Haasedf030d22010-07-30 17:22:38 -07009324 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009325 ensureTransformationInfo();
9326 final TransformationInfo info = mTransformationInfo;
9327 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009328 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009329 info.mTranslationY = translationY;
9330 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009331 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009332 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009333 mDisplayList.setTranslationY(translationY);
9334 }
Chet Haasedf030d22010-07-30 17:22:38 -07009335 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009336 }
9337
9338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 * Hit rectangle in parent's coordinates
9340 *
9341 * @param outRect The hit rectangle of the view.
9342 */
9343 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009344 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009345 final TransformationInfo info = mTransformationInfo;
9346 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009347 outRect.set(mLeft, mTop, mRight, mBottom);
9348 } else {
9349 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009350 tmpRect.set(-info.mPivotX, -info.mPivotY,
9351 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9352 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009353 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9354 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009355 }
9356 }
9357
9358 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009359 * Determines whether the given point, in local coordinates is inside the view.
9360 */
9361 /*package*/ final boolean pointInView(float localX, float localY) {
9362 return localX >= 0 && localX < (mRight - mLeft)
9363 && localY >= 0 && localY < (mBottom - mTop);
9364 }
9365
9366 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009367 * Utility method to determine whether the given point, in local coordinates,
9368 * is inside the view, where the area of the view is expanded by the slop factor.
9369 * This method is called while processing touch-move events to determine if the event
9370 * is still within the view.
9371 */
9372 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009373 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009374 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 }
9376
9377 /**
9378 * When a view has focus and the user navigates away from it, the next view is searched for
9379 * starting from the rectangle filled in by this method.
9380 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009381 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9382 * of the view. However, if your view maintains some idea of internal selection,
9383 * such as a cursor, or a selected row or column, you should override this method and
9384 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385 *
9386 * @param r The rectangle to fill in, in this view's coordinates.
9387 */
9388 public void getFocusedRect(Rect r) {
9389 getDrawingRect(r);
9390 }
9391
9392 /**
9393 * If some part of this view is not clipped by any of its parents, then
9394 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009395 * coordinates (without taking possible View rotations into account), offset
9396 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9397 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009398 *
9399 * @param r If true is returned, r holds the global coordinates of the
9400 * visible portion of this view.
9401 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9402 * between this view and its root. globalOffet may be null.
9403 * @return true if r is non-empty (i.e. part of the view is visible at the
9404 * root level.
9405 */
9406 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9407 int width = mRight - mLeft;
9408 int height = mBottom - mTop;
9409 if (width > 0 && height > 0) {
9410 r.set(0, 0, width, height);
9411 if (globalOffset != null) {
9412 globalOffset.set(-mScrollX, -mScrollY);
9413 }
9414 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9415 }
9416 return false;
9417 }
9418
9419 public final boolean getGlobalVisibleRect(Rect r) {
9420 return getGlobalVisibleRect(r, null);
9421 }
9422
9423 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009424 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009425 if (getGlobalVisibleRect(r, offset)) {
9426 r.offset(-offset.x, -offset.y); // make r local
9427 return true;
9428 }
9429 return false;
9430 }
9431
9432 /**
9433 * Offset this view's vertical location by the specified number of pixels.
9434 *
9435 * @param offset the number of pixels to offset the view by
9436 */
9437 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009438 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009439 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009440 final boolean matrixIsIdentity = mTransformationInfo == null
9441 || mTransformationInfo.mMatrixIsIdentity;
9442 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009443 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009444 invalidateViewProperty(false, false);
9445 } else {
9446 final ViewParent p = mParent;
9447 if (p != null && mAttachInfo != null) {
9448 final Rect r = mAttachInfo.mTmpInvalRect;
9449 int minTop;
9450 int maxBottom;
9451 int yLoc;
9452 if (offset < 0) {
9453 minTop = mTop + offset;
9454 maxBottom = mBottom;
9455 yLoc = offset;
9456 } else {
9457 minTop = mTop;
9458 maxBottom = mBottom + offset;
9459 yLoc = 0;
9460 }
9461 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9462 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009463 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009464 }
9465 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009466 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009467 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009468
Chet Haasec3aa3612010-06-17 08:50:37 -07009469 mTop += offset;
9470 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009471 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009472 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009473 invalidateViewProperty(false, false);
9474 } else {
9475 if (!matrixIsIdentity) {
9476 invalidateViewProperty(false, true);
9477 }
9478 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009479 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009481 }
9482
9483 /**
9484 * Offset this view's horizontal location by the specified amount of pixels.
9485 *
9486 * @param offset the numer of pixels to offset the view by
9487 */
9488 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009489 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009490 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009491 final boolean matrixIsIdentity = mTransformationInfo == null
9492 || mTransformationInfo.mMatrixIsIdentity;
9493 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009494 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009495 invalidateViewProperty(false, false);
9496 } else {
9497 final ViewParent p = mParent;
9498 if (p != null && mAttachInfo != null) {
9499 final Rect r = mAttachInfo.mTmpInvalRect;
9500 int minLeft;
9501 int maxRight;
9502 if (offset < 0) {
9503 minLeft = mLeft + offset;
9504 maxRight = mRight;
9505 } else {
9506 minLeft = mLeft;
9507 maxRight = mRight + offset;
9508 }
9509 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9510 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009511 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009512 }
9513 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009514 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009515 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009516
Chet Haasec3aa3612010-06-17 08:50:37 -07009517 mLeft += offset;
9518 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009519 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009520 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009521 invalidateViewProperty(false, false);
9522 } else {
9523 if (!matrixIsIdentity) {
9524 invalidateViewProperty(false, true);
9525 }
9526 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009527 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009529 }
9530
9531 /**
9532 * Get the LayoutParams associated with this view. All views should have
9533 * layout parameters. These supply parameters to the <i>parent</i> of this
9534 * view specifying how it should be arranged. There are many subclasses of
9535 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9536 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009537 *
9538 * This method may return null if this View is not attached to a parent
9539 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9540 * was not invoked successfully. When a View is attached to a parent
9541 * ViewGroup, this method must not return null.
9542 *
9543 * @return The LayoutParams associated with this view, or null if no
9544 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009545 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009546 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 public ViewGroup.LayoutParams getLayoutParams() {
9548 return mLayoutParams;
9549 }
9550
9551 /**
9552 * Set the layout parameters associated with this view. These supply
9553 * parameters to the <i>parent</i> of this view specifying how it should be
9554 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9555 * correspond to the different subclasses of ViewGroup that are responsible
9556 * for arranging their children.
9557 *
Romain Guy01c174b2011-02-22 11:51:06 -08009558 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009559 */
9560 public void setLayoutParams(ViewGroup.LayoutParams params) {
9561 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009562 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 }
9564 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009565 if (mParent instanceof ViewGroup) {
9566 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 requestLayout();
9569 }
9570
9571 /**
9572 * Set the scrolled position of your view. This will cause a call to
9573 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9574 * invalidated.
9575 * @param x the x position to scroll to
9576 * @param y the y position to scroll to
9577 */
9578 public void scrollTo(int x, int y) {
9579 if (mScrollX != x || mScrollY != y) {
9580 int oldX = mScrollX;
9581 int oldY = mScrollY;
9582 mScrollX = x;
9583 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009584 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009586 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009587 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 }
9590 }
9591
9592 /**
9593 * Move the scrolled position of your view. This will cause a call to
9594 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9595 * invalidated.
9596 * @param x the amount of pixels to scroll by horizontally
9597 * @param y the amount of pixels to scroll by vertically
9598 */
9599 public void scrollBy(int x, int y) {
9600 scrollTo(mScrollX + x, mScrollY + y);
9601 }
9602
9603 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009604 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9605 * animation to fade the scrollbars out after a default delay. If a subclass
9606 * provides animated scrolling, the start delay should equal the duration
9607 * of the scrolling animation.</p>
9608 *
9609 * <p>The animation starts only if at least one of the scrollbars is
9610 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9611 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9612 * this method returns true, and false otherwise. If the animation is
9613 * started, this method calls {@link #invalidate()}; in that case the
9614 * caller should not call {@link #invalidate()}.</p>
9615 *
9616 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009617 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009618 *
9619 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9620 * and {@link #scrollTo(int, int)}.</p>
9621 *
9622 * @return true if the animation is played, false otherwise
9623 *
9624 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009625 * @see #scrollBy(int, int)
9626 * @see #scrollTo(int, int)
9627 * @see #isHorizontalScrollBarEnabled()
9628 * @see #isVerticalScrollBarEnabled()
9629 * @see #setHorizontalScrollBarEnabled(boolean)
9630 * @see #setVerticalScrollBarEnabled(boolean)
9631 */
9632 protected boolean awakenScrollBars() {
9633 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009634 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009635 }
9636
9637 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009638 * Trigger the scrollbars to draw.
9639 * This method differs from awakenScrollBars() only in its default duration.
9640 * initialAwakenScrollBars() will show the scroll bars for longer than
9641 * usual to give the user more of a chance to notice them.
9642 *
9643 * @return true if the animation is played, false otherwise.
9644 */
9645 private boolean initialAwakenScrollBars() {
9646 return mScrollCache != null &&
9647 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9648 }
9649
9650 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009651 * <p>
9652 * Trigger the scrollbars to draw. When invoked this method starts an
9653 * animation to fade the scrollbars out after a fixed delay. If a subclass
9654 * provides animated scrolling, the start delay should equal the duration of
9655 * the scrolling animation.
9656 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009657 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009658 * <p>
9659 * The animation starts only if at least one of the scrollbars is enabled,
9660 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9661 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9662 * this method returns true, and false otherwise. If the animation is
9663 * started, this method calls {@link #invalidate()}; in that case the caller
9664 * should not call {@link #invalidate()}.
9665 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009666 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009667 * <p>
9668 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009669 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009670 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009671 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009672 * @param startDelay the delay, in milliseconds, after which the animation
9673 * should start; when the delay is 0, the animation starts
9674 * immediately
9675 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009676 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009677 * @see #scrollBy(int, int)
9678 * @see #scrollTo(int, int)
9679 * @see #isHorizontalScrollBarEnabled()
9680 * @see #isVerticalScrollBarEnabled()
9681 * @see #setHorizontalScrollBarEnabled(boolean)
9682 * @see #setVerticalScrollBarEnabled(boolean)
9683 */
9684 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009685 return awakenScrollBars(startDelay, true);
9686 }
Joe Malin32736f02011-01-19 16:14:20 -08009687
Mike Cleron290947b2009-09-29 18:34:32 -07009688 /**
9689 * <p>
9690 * Trigger the scrollbars to draw. When invoked this method starts an
9691 * animation to fade the scrollbars out after a fixed delay. If a subclass
9692 * provides animated scrolling, the start delay should equal the duration of
9693 * the scrolling animation.
9694 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009695 *
Mike Cleron290947b2009-09-29 18:34:32 -07009696 * <p>
9697 * The animation starts only if at least one of the scrollbars is enabled,
9698 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9699 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9700 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009701 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009702 * is set to true; in that case the caller
9703 * should not call {@link #invalidate()}.
9704 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009705 *
Mike Cleron290947b2009-09-29 18:34:32 -07009706 * <p>
9707 * This method should be invoked everytime a subclass directly updates the
9708 * scroll parameters.
9709 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009710 *
Mike Cleron290947b2009-09-29 18:34:32 -07009711 * @param startDelay the delay, in milliseconds, after which the animation
9712 * should start; when the delay is 0, the animation starts
9713 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009714 *
Mike Cleron290947b2009-09-29 18:34:32 -07009715 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009716 *
Mike Cleron290947b2009-09-29 18:34:32 -07009717 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009718 *
Mike Cleron290947b2009-09-29 18:34:32 -07009719 * @see #scrollBy(int, int)
9720 * @see #scrollTo(int, int)
9721 * @see #isHorizontalScrollBarEnabled()
9722 * @see #isVerticalScrollBarEnabled()
9723 * @see #setHorizontalScrollBarEnabled(boolean)
9724 * @see #setVerticalScrollBarEnabled(boolean)
9725 */
9726 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009727 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009728
Mike Cleronf116bf82009-09-27 19:14:12 -07009729 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9730 return false;
9731 }
9732
9733 if (scrollCache.scrollBar == null) {
9734 scrollCache.scrollBar = new ScrollBarDrawable();
9735 }
9736
9737 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9738
Mike Cleron290947b2009-09-29 18:34:32 -07009739 if (invalidate) {
9740 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009741 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009742 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009743
9744 if (scrollCache.state == ScrollabilityCache.OFF) {
9745 // FIXME: this is copied from WindowManagerService.
9746 // We should get this value from the system when it
9747 // is possible to do so.
9748 final int KEY_REPEAT_FIRST_DELAY = 750;
9749 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9750 }
9751
9752 // Tell mScrollCache when we should start fading. This may
9753 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009754 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009755 scrollCache.fadeStartTime = fadeStartTime;
9756 scrollCache.state = ScrollabilityCache.ON;
9757
9758 // Schedule our fader to run, unscheduling any old ones first
9759 if (mAttachInfo != null) {
9760 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9761 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9762 }
9763
9764 return true;
9765 }
9766
9767 return false;
9768 }
9769
9770 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009771 * Do not invalidate views which are not visible and which are not running an animation. They
9772 * will not get drawn and they should not set dirty flags as if they will be drawn
9773 */
9774 private boolean skipInvalidate() {
9775 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9776 (!(mParent instanceof ViewGroup) ||
9777 !((ViewGroup) mParent).isViewTransitioning(this));
9778 }
9779 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009780 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009781 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9782 * in the future. This must be called from a UI thread. To call from a non-UI
9783 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 *
9785 * WARNING: This method is destructive to dirty.
9786 * @param dirty the rectangle representing the bounds of the dirty region
9787 */
9788 public void invalidate(Rect dirty) {
9789 if (ViewDebug.TRACE_HIERARCHY) {
9790 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9791 }
9792
Chet Haaseaceafe62011-08-26 15:44:33 -07009793 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009794 return;
9795 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009796 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009797 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9798 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009800 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009801 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 final ViewParent p = mParent;
9803 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009804 //noinspection PointlessBooleanExpression,ConstantConditions
9805 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9806 if (p != null && ai != null && ai.mHardwareAccelerated) {
9807 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009808 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009809 p.invalidateChild(this, null);
9810 return;
9811 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 if (p != null && ai != null) {
9814 final int scrollX = mScrollX;
9815 final int scrollY = mScrollY;
9816 final Rect r = ai.mTmpInvalRect;
9817 r.set(dirty.left - scrollX, dirty.top - scrollY,
9818 dirty.right - scrollX, dirty.bottom - scrollY);
9819 mParent.invalidateChild(this, r);
9820 }
9821 }
9822 }
9823
9824 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009825 * 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 -08009826 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009827 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9828 * will be called at some point in the future. This must be called from
9829 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 * @param l the left position of the dirty region
9831 * @param t the top position of the dirty region
9832 * @param r the right position of the dirty region
9833 * @param b the bottom position of the dirty region
9834 */
9835 public void invalidate(int l, int t, int r, int b) {
9836 if (ViewDebug.TRACE_HIERARCHY) {
9837 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9838 }
9839
Chet Haaseaceafe62011-08-26 15:44:33 -07009840 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009841 return;
9842 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009843 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009844 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9845 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009846 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009847 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009848 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 final ViewParent p = mParent;
9850 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009851 //noinspection PointlessBooleanExpression,ConstantConditions
9852 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9853 if (p != null && ai != null && ai.mHardwareAccelerated) {
9854 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009855 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009856 p.invalidateChild(this, null);
9857 return;
9858 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009860 if (p != null && ai != null && l < r && t < b) {
9861 final int scrollX = mScrollX;
9862 final int scrollY = mScrollY;
9863 final Rect tmpr = ai.mTmpInvalRect;
9864 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9865 p.invalidateChild(this, tmpr);
9866 }
9867 }
9868 }
9869
9870 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009871 * Invalidate the whole view. If the view is visible,
9872 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9873 * the future. This must be called from a UI thread. To call from a non-UI thread,
9874 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 */
9876 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009877 invalidate(true);
9878 }
Joe Malin32736f02011-01-19 16:14:20 -08009879
Chet Haaseed032702010-10-01 14:05:54 -07009880 /**
9881 * This is where the invalidate() work actually happens. A full invalidate()
9882 * causes the drawing cache to be invalidated, but this function can be called with
9883 * invalidateCache set to false to skip that invalidation step for cases that do not
9884 * need it (for example, a component that remains at the same dimensions with the same
9885 * content).
9886 *
9887 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9888 * well. This is usually true for a full invalidate, but may be set to false if the
9889 * View's contents or dimensions have not changed.
9890 */
Romain Guy849d0a32011-02-01 17:20:48 -08009891 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009892 if (ViewDebug.TRACE_HIERARCHY) {
9893 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9894 }
9895
Chet Haaseaceafe62011-08-26 15:44:33 -07009896 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009897 return;
9898 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009899 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009900 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009901 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9902 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009903 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009904 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009905 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009906 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009907 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009910 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009911 //noinspection PointlessBooleanExpression,ConstantConditions
9912 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9913 if (p != null && ai != null && ai.mHardwareAccelerated) {
9914 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009915 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009916 p.invalidateChild(this, null);
9917 return;
9918 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009919 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 if (p != null && ai != null) {
9922 final Rect r = ai.mTmpInvalRect;
9923 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9924 // Don't call invalidate -- we don't want to internally scroll
9925 // our own bounds
9926 p.invalidateChild(this, r);
9927 }
9928 }
9929 }
9930
9931 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009932 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9933 * set any flags or handle all of the cases handled by the default invalidation methods.
9934 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9935 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9936 * walk up the hierarchy, transforming the dirty rect as necessary.
9937 *
9938 * The method also handles normal invalidation logic if display list properties are not
9939 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9940 * backup approach, to handle these cases used in the various property-setting methods.
9941 *
9942 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9943 * are not being used in this view
9944 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9945 * list properties are not being used in this view
9946 */
9947 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009948 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009949 if (invalidateParent) {
9950 invalidateParentCaches();
9951 }
9952 if (forceRedraw) {
9953 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9954 }
9955 invalidate(false);
9956 } else {
9957 final AttachInfo ai = mAttachInfo;
9958 final ViewParent p = mParent;
9959 if (p != null && ai != null) {
9960 final Rect r = ai.mTmpInvalRect;
9961 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9962 if (mParent instanceof ViewGroup) {
9963 ((ViewGroup) mParent).invalidateChildFast(this, r);
9964 } else {
9965 mParent.invalidateChild(this, r);
9966 }
9967 }
9968 }
9969 }
9970
9971 /**
9972 * Utility method to transform a given Rect by the current matrix of this view.
9973 */
9974 void transformRect(final Rect rect) {
9975 if (!getMatrix().isIdentity()) {
9976 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9977 boundingRect.set(rect);
9978 getMatrix().mapRect(boundingRect);
9979 rect.set((int) (boundingRect.left - 0.5f),
9980 (int) (boundingRect.top - 0.5f),
9981 (int) (boundingRect.right + 0.5f),
9982 (int) (boundingRect.bottom + 0.5f));
9983 }
9984 }
9985
9986 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009987 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009988 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9989 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009990 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9991 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009992 *
9993 * @hide
9994 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009995 protected void invalidateParentCaches() {
9996 if (mParent instanceof View) {
9997 ((View) mParent).mPrivateFlags |= INVALIDATED;
9998 }
9999 }
Joe Malin32736f02011-01-19 16:14:20 -080010000
Romain Guy0fd89bf2011-01-26 15:41:30 -080010001 /**
10002 * Used to indicate that the parent of this view should be invalidated. This functionality
10003 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10004 * which is necessary when various parent-managed properties of the view change, such as
10005 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10006 * an invalidation event to the parent.
10007 *
10008 * @hide
10009 */
10010 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010011 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010012 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010013 }
10014 }
10015
10016 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010017 * Indicates whether this View is opaque. An opaque View guarantees that it will
10018 * draw all the pixels overlapping its bounds using a fully opaque color.
10019 *
10020 * Subclasses of View should override this method whenever possible to indicate
10021 * whether an instance is opaque. Opaque Views are treated in a special way by
10022 * the View hierarchy, possibly allowing it to perform optimizations during
10023 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010024 *
Romain Guy24443ea2009-05-11 11:56:30 -070010025 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010026 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010027 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010028 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010029 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010030 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10031 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010032 }
10033
Adam Powell20232d02010-12-08 21:08:53 -080010034 /**
10035 * @hide
10036 */
10037 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010038 // Opaque if:
10039 // - Has a background
10040 // - Background is opaque
10041 // - Doesn't have scrollbars or scrollbars are inside overlay
10042
Philip Milne6c8ea062012-04-03 17:38:43 -070010043 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010044 mPrivateFlags |= OPAQUE_BACKGROUND;
10045 } else {
10046 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10047 }
10048
10049 final int flags = mViewFlags;
10050 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10051 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10052 mPrivateFlags |= OPAQUE_SCROLLBARS;
10053 } else {
10054 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10055 }
10056 }
10057
10058 /**
10059 * @hide
10060 */
10061 protected boolean hasOpaqueScrollbars() {
10062 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010063 }
10064
10065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 * @return A handler associated with the thread running the View. This
10067 * handler can be used to pump events in the UI events queue.
10068 */
10069 public Handler getHandler() {
10070 if (mAttachInfo != null) {
10071 return mAttachInfo.mHandler;
10072 }
10073 return null;
10074 }
10075
10076 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010077 * Gets the view root associated with the View.
10078 * @return The view root, or null if none.
10079 * @hide
10080 */
10081 public ViewRootImpl getViewRootImpl() {
10082 if (mAttachInfo != null) {
10083 return mAttachInfo.mViewRootImpl;
10084 }
10085 return null;
10086 }
10087
10088 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010089 * <p>Causes the Runnable to be added to the message queue.
10090 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010091 *
Romain Guye63a4f32011-08-11 11:33:31 -070010092 * <p>This method can be invoked from outside of the UI thread
10093 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010094 *
10095 * @param action The Runnable that will be executed.
10096 *
10097 * @return Returns true if the Runnable was successfully placed in to the
10098 * message queue. Returns false on failure, usually because the
10099 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010100 *
10101 * @see #postDelayed
10102 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 */
10104 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010105 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010106 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010107 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010109 // Assume that post will succeed later
10110 ViewRootImpl.getRunQueue().post(action);
10111 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 }
10113
10114 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010115 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010117 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010118 *
Romain Guye63a4f32011-08-11 11:33:31 -070010119 * <p>This method can be invoked from outside of the UI thread
10120 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 *
10122 * @param action The Runnable that will be executed.
10123 * @param delayMillis The delay (in milliseconds) until the Runnable
10124 * will be executed.
10125 *
10126 * @return true if the Runnable was successfully placed in to the
10127 * message queue. Returns false on failure, usually because the
10128 * looper processing the message queue is exiting. Note that a
10129 * result of true does not mean the Runnable will be processed --
10130 * if the looper is quit before the delivery time of the message
10131 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010132 *
10133 * @see #post
10134 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 */
10136 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010137 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010138 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010139 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010140 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010141 // Assume that post will succeed later
10142 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10143 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010144 }
10145
10146 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010147 * <p>Causes the Runnable to execute on the next animation time step.
10148 * The runnable will be run on the user interface thread.</p>
10149 *
10150 * <p>This method can be invoked from outside of the UI thread
10151 * only when this View is attached to a window.</p>
10152 *
10153 * @param action The Runnable that will be executed.
10154 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010155 * @see #postOnAnimationDelayed
10156 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010157 */
10158 public void postOnAnimation(Runnable action) {
10159 final AttachInfo attachInfo = mAttachInfo;
10160 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010161 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10162 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010163 } else {
10164 // Assume that post will succeed later
10165 ViewRootImpl.getRunQueue().post(action);
10166 }
10167 }
10168
10169 /**
10170 * <p>Causes the Runnable to execute on the next animation time step,
10171 * after the specified amount of time elapses.
10172 * The runnable will be run on the user interface thread.</p>
10173 *
10174 * <p>This method can be invoked from outside of the UI thread
10175 * only when this View is attached to a window.</p>
10176 *
10177 * @param action The Runnable that will be executed.
10178 * @param delayMillis The delay (in milliseconds) until the Runnable
10179 * will be executed.
10180 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010181 * @see #postOnAnimation
10182 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010183 */
10184 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10185 final AttachInfo attachInfo = mAttachInfo;
10186 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010187 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10188 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010189 } else {
10190 // Assume that post will succeed later
10191 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10192 }
10193 }
10194
10195 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010196 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010197 *
Romain Guye63a4f32011-08-11 11:33:31 -070010198 * <p>This method can be invoked from outside of the UI thread
10199 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200 *
10201 * @param action The Runnable to remove from the message handling queue
10202 *
10203 * @return true if this view could ask the Handler to remove the Runnable,
10204 * false otherwise. When the returned value is true, the Runnable
10205 * may or may not have been actually removed from the message queue
10206 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010207 *
10208 * @see #post
10209 * @see #postDelayed
10210 * @see #postOnAnimation
10211 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 */
10213 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010214 if (action != null) {
10215 final AttachInfo attachInfo = mAttachInfo;
10216 if (attachInfo != null) {
10217 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010218 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10219 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010220 } else {
10221 // Assume that post will succeed later
10222 ViewRootImpl.getRunQueue().removeCallbacks(action);
10223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010225 return true;
10226 }
10227
10228 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010229 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10230 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 *
Romain Guye63a4f32011-08-11 11:33:31 -070010232 * <p>This method can be invoked from outside of the UI thread
10233 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010236 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 */
10238 public void postInvalidate() {
10239 postInvalidateDelayed(0);
10240 }
10241
10242 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010243 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10244 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010245 *
Romain Guye63a4f32011-08-11 11:33:31 -070010246 * <p>This method can be invoked from outside of the UI thread
10247 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 *
10249 * @param left The left coordinate of the rectangle to invalidate.
10250 * @param top The top coordinate of the rectangle to invalidate.
10251 * @param right The right coordinate of the rectangle to invalidate.
10252 * @param bottom The bottom coordinate of the rectangle to invalidate.
10253 *
10254 * @see #invalidate(int, int, int, int)
10255 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010256 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010257 */
10258 public void postInvalidate(int left, int top, int right, int bottom) {
10259 postInvalidateDelayed(0, left, top, right, bottom);
10260 }
10261
10262 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010263 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10264 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010265 *
Romain Guye63a4f32011-08-11 11:33:31 -070010266 * <p>This method can be invoked from outside of the UI thread
10267 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010268 *
10269 * @param delayMilliseconds the duration in milliseconds to delay the
10270 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010271 *
10272 * @see #invalidate()
10273 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 */
10275 public void postInvalidateDelayed(long delayMilliseconds) {
10276 // We try only with the AttachInfo because there's no point in invalidating
10277 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010278 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010279 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010280 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 }
10282 }
10283
10284 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010285 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10286 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010287 *
Romain Guye63a4f32011-08-11 11:33:31 -070010288 * <p>This method can be invoked from outside of the UI thread
10289 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 *
10291 * @param delayMilliseconds the duration in milliseconds to delay the
10292 * invalidation by
10293 * @param left The left coordinate of the rectangle to invalidate.
10294 * @param top The top coordinate of the rectangle to invalidate.
10295 * @param right The right coordinate of the rectangle to invalidate.
10296 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010297 *
10298 * @see #invalidate(int, int, int, int)
10299 * @see #invalidate(Rect)
10300 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 */
10302 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10303 int right, int bottom) {
10304
10305 // We try only with the AttachInfo because there's no point in invalidating
10306 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010307 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010308 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10310 info.target = this;
10311 info.left = left;
10312 info.top = top;
10313 info.right = right;
10314 info.bottom = bottom;
10315
Jeff Browna175a5b2012-02-15 19:18:31 -080010316 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 }
10318 }
10319
10320 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010321 * <p>Cause an invalidate to happen on the next animation time step, typically the
10322 * next display frame.</p>
10323 *
10324 * <p>This method can be invoked from outside of the UI thread
10325 * only when this View is attached to a window.</p>
10326 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010327 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010328 */
10329 public void postInvalidateOnAnimation() {
10330 // We try only with the AttachInfo because there's no point in invalidating
10331 // if we are not attached to our window
10332 final AttachInfo attachInfo = mAttachInfo;
10333 if (attachInfo != null) {
10334 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10335 }
10336 }
10337
10338 /**
10339 * <p>Cause an invalidate of the specified area to happen on the next animation
10340 * time step, typically the next display frame.</p>
10341 *
10342 * <p>This method can be invoked from outside of the UI thread
10343 * only when this View is attached to a window.</p>
10344 *
10345 * @param left The left coordinate of the rectangle to invalidate.
10346 * @param top The top coordinate of the rectangle to invalidate.
10347 * @param right The right coordinate of the rectangle to invalidate.
10348 * @param bottom The bottom coordinate of the rectangle to invalidate.
10349 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010350 * @see #invalidate(int, int, int, int)
10351 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010352 */
10353 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10354 // We try only with the AttachInfo because there's no point in invalidating
10355 // if we are not attached to our window
10356 final AttachInfo attachInfo = mAttachInfo;
10357 if (attachInfo != null) {
10358 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10359 info.target = this;
10360 info.left = left;
10361 info.top = top;
10362 info.right = right;
10363 info.bottom = bottom;
10364
10365 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10366 }
10367 }
10368
10369 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010370 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10371 * This event is sent at most once every
10372 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10373 */
10374 private void postSendViewScrolledAccessibilityEventCallback() {
10375 if (mSendViewScrolledAccessibilityEvent == null) {
10376 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10377 }
10378 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10379 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10380 postDelayed(mSendViewScrolledAccessibilityEvent,
10381 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10382 }
10383 }
10384
10385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 * Called by a parent to request that a child update its values for mScrollX
10387 * and mScrollY if necessary. This will typically be done if the child is
10388 * animating a scroll using a {@link android.widget.Scroller Scroller}
10389 * object.
10390 */
10391 public void computeScroll() {
10392 }
10393
10394 /**
10395 * <p>Indicate whether the horizontal edges are faded when the view is
10396 * scrolled horizontally.</p>
10397 *
10398 * @return true if the horizontal edges should are faded on scroll, false
10399 * otherwise
10400 *
10401 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010402 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010403 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 */
10405 public boolean isHorizontalFadingEdgeEnabled() {
10406 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10407 }
10408
10409 /**
10410 * <p>Define whether the horizontal edges should be faded when this view
10411 * is scrolled horizontally.</p>
10412 *
10413 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10414 * be faded when the view is scrolled
10415 * horizontally
10416 *
10417 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010418 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010419 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 */
10421 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10422 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10423 if (horizontalFadingEdgeEnabled) {
10424 initScrollCache();
10425 }
10426
10427 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10428 }
10429 }
10430
10431 /**
10432 * <p>Indicate whether the vertical edges are faded when the view is
10433 * scrolled horizontally.</p>
10434 *
10435 * @return true if the vertical edges should are faded on scroll, false
10436 * otherwise
10437 *
10438 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010439 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010440 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 */
10442 public boolean isVerticalFadingEdgeEnabled() {
10443 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10444 }
10445
10446 /**
10447 * <p>Define whether the vertical edges should be faded when this view
10448 * is scrolled vertically.</p>
10449 *
10450 * @param verticalFadingEdgeEnabled true if the vertical edges should
10451 * be faded when the view is scrolled
10452 * vertically
10453 *
10454 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010455 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010456 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010457 */
10458 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10459 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10460 if (verticalFadingEdgeEnabled) {
10461 initScrollCache();
10462 }
10463
10464 mViewFlags ^= FADING_EDGE_VERTICAL;
10465 }
10466 }
10467
10468 /**
10469 * Returns the strength, or intensity, of the top faded edge. The strength is
10470 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10471 * returns 0.0 or 1.0 but no value in between.
10472 *
10473 * Subclasses should override this method to provide a smoother fade transition
10474 * when scrolling occurs.
10475 *
10476 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10477 */
10478 protected float getTopFadingEdgeStrength() {
10479 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10480 }
10481
10482 /**
10483 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10484 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10485 * returns 0.0 or 1.0 but no value in between.
10486 *
10487 * Subclasses should override this method to provide a smoother fade transition
10488 * when scrolling occurs.
10489 *
10490 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10491 */
10492 protected float getBottomFadingEdgeStrength() {
10493 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10494 computeVerticalScrollRange() ? 1.0f : 0.0f;
10495 }
10496
10497 /**
10498 * Returns the strength, or intensity, of the left faded edge. The strength is
10499 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10500 * returns 0.0 or 1.0 but no value in between.
10501 *
10502 * Subclasses should override this method to provide a smoother fade transition
10503 * when scrolling occurs.
10504 *
10505 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10506 */
10507 protected float getLeftFadingEdgeStrength() {
10508 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10509 }
10510
10511 /**
10512 * Returns the strength, or intensity, of the right faded edge. The strength is
10513 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10514 * returns 0.0 or 1.0 but no value in between.
10515 *
10516 * Subclasses should override this method to provide a smoother fade transition
10517 * when scrolling occurs.
10518 *
10519 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10520 */
10521 protected float getRightFadingEdgeStrength() {
10522 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10523 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10524 }
10525
10526 /**
10527 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10528 * scrollbar is not drawn by default.</p>
10529 *
10530 * @return true if the horizontal scrollbar should be painted, false
10531 * otherwise
10532 *
10533 * @see #setHorizontalScrollBarEnabled(boolean)
10534 */
10535 public boolean isHorizontalScrollBarEnabled() {
10536 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10537 }
10538
10539 /**
10540 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10541 * scrollbar is not drawn by default.</p>
10542 *
10543 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10544 * be painted
10545 *
10546 * @see #isHorizontalScrollBarEnabled()
10547 */
10548 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10549 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10550 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010551 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010552 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 }
10554 }
10555
10556 /**
10557 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10558 * scrollbar is not drawn by default.</p>
10559 *
10560 * @return true if the vertical scrollbar should be painted, false
10561 * otherwise
10562 *
10563 * @see #setVerticalScrollBarEnabled(boolean)
10564 */
10565 public boolean isVerticalScrollBarEnabled() {
10566 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10567 }
10568
10569 /**
10570 * <p>Define whether the vertical scrollbar should be drawn or not. The
10571 * scrollbar is not drawn by default.</p>
10572 *
10573 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10574 * be painted
10575 *
10576 * @see #isVerticalScrollBarEnabled()
10577 */
10578 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10579 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10580 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010581 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010582 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 }
10584 }
10585
Adam Powell20232d02010-12-08 21:08:53 -080010586 /**
10587 * @hide
10588 */
10589 protected void recomputePadding() {
10590 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 }
Joe Malin32736f02011-01-19 16:14:20 -080010592
Mike Cleronfe81d382009-09-28 14:22:16 -070010593 /**
10594 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010595 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010596 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010597 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010598 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010599 */
10600 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10601 initScrollCache();
10602 final ScrollabilityCache scrollabilityCache = mScrollCache;
10603 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010604 if (fadeScrollbars) {
10605 scrollabilityCache.state = ScrollabilityCache.OFF;
10606 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010607 scrollabilityCache.state = ScrollabilityCache.ON;
10608 }
10609 }
Joe Malin32736f02011-01-19 16:14:20 -080010610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 /**
Joe Malin32736f02011-01-19 16:14:20 -080010612 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010613 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010614 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010615 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010616 *
10617 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010618 */
10619 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010620 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010621 }
Joe Malin32736f02011-01-19 16:14:20 -080010622
Mike Cleron52f0a642009-09-28 18:21:37 -070010623 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010624 *
10625 * Returns the delay before scrollbars fade.
10626 *
10627 * @return the delay before scrollbars fade
10628 *
10629 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10630 */
10631 public int getScrollBarDefaultDelayBeforeFade() {
10632 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10633 mScrollCache.scrollBarDefaultDelayBeforeFade;
10634 }
10635
10636 /**
10637 * Define the delay before scrollbars fade.
10638 *
10639 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10640 *
10641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10642 */
10643 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10644 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10645 }
10646
10647 /**
10648 *
10649 * Returns the scrollbar fade duration.
10650 *
10651 * @return the scrollbar fade duration
10652 *
10653 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10654 */
10655 public int getScrollBarFadeDuration() {
10656 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10657 mScrollCache.scrollBarFadeDuration;
10658 }
10659
10660 /**
10661 * Define the scrollbar fade duration.
10662 *
10663 * @param scrollBarFadeDuration - the scrollbar fade duration
10664 *
10665 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10666 */
10667 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10668 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10669 }
10670
10671 /**
10672 *
10673 * Returns the scrollbar size.
10674 *
10675 * @return the scrollbar size
10676 *
10677 * @attr ref android.R.styleable#View_scrollbarSize
10678 */
10679 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010680 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010681 mScrollCache.scrollBarSize;
10682 }
10683
10684 /**
10685 * Define the scrollbar size.
10686 *
10687 * @param scrollBarSize - the scrollbar size
10688 *
10689 * @attr ref android.R.styleable#View_scrollbarSize
10690 */
10691 public void setScrollBarSize(int scrollBarSize) {
10692 getScrollCache().scrollBarSize = scrollBarSize;
10693 }
10694
10695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10697 * inset. When inset, they add to the padding of the view. And the scrollbars
10698 * can be drawn inside the padding area or on the edge of the view. For example,
10699 * if a view has a background drawable and you want to draw the scrollbars
10700 * inside the padding specified by the drawable, you can use
10701 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10702 * appear at the edge of the view, ignoring the padding, then you can use
10703 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10704 * @param style the style of the scrollbars. Should be one of
10705 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10706 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10707 * @see #SCROLLBARS_INSIDE_OVERLAY
10708 * @see #SCROLLBARS_INSIDE_INSET
10709 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10710 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010711 *
10712 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 */
10714 public void setScrollBarStyle(int style) {
10715 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10716 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010717 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010718 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 }
10720 }
10721
10722 /**
10723 * <p>Returns the current scrollbar style.</p>
10724 * @return the current scrollbar style
10725 * @see #SCROLLBARS_INSIDE_OVERLAY
10726 * @see #SCROLLBARS_INSIDE_INSET
10727 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10728 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010729 *
10730 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010731 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010732 @ViewDebug.ExportedProperty(mapping = {
10733 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10734 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10735 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10736 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10737 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010738 public int getScrollBarStyle() {
10739 return mViewFlags & SCROLLBARS_STYLE_MASK;
10740 }
10741
10742 /**
10743 * <p>Compute the horizontal range that the horizontal scrollbar
10744 * represents.</p>
10745 *
10746 * <p>The range is expressed in arbitrary units that must be the same as the
10747 * units used by {@link #computeHorizontalScrollExtent()} and
10748 * {@link #computeHorizontalScrollOffset()}.</p>
10749 *
10750 * <p>The default range is the drawing width of this view.</p>
10751 *
10752 * @return the total horizontal range represented by the horizontal
10753 * scrollbar
10754 *
10755 * @see #computeHorizontalScrollExtent()
10756 * @see #computeHorizontalScrollOffset()
10757 * @see android.widget.ScrollBarDrawable
10758 */
10759 protected int computeHorizontalScrollRange() {
10760 return getWidth();
10761 }
10762
10763 /**
10764 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10765 * within the horizontal range. This value is used to compute the position
10766 * of the thumb within the scrollbar's track.</p>
10767 *
10768 * <p>The range is expressed in arbitrary units that must be the same as the
10769 * units used by {@link #computeHorizontalScrollRange()} and
10770 * {@link #computeHorizontalScrollExtent()}.</p>
10771 *
10772 * <p>The default offset is the scroll offset of this view.</p>
10773 *
10774 * @return the horizontal offset of the scrollbar's thumb
10775 *
10776 * @see #computeHorizontalScrollRange()
10777 * @see #computeHorizontalScrollExtent()
10778 * @see android.widget.ScrollBarDrawable
10779 */
10780 protected int computeHorizontalScrollOffset() {
10781 return mScrollX;
10782 }
10783
10784 /**
10785 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10786 * within the horizontal range. This value is used to compute the length
10787 * of the thumb within the scrollbar's track.</p>
10788 *
10789 * <p>The range is expressed in arbitrary units that must be the same as the
10790 * units used by {@link #computeHorizontalScrollRange()} and
10791 * {@link #computeHorizontalScrollOffset()}.</p>
10792 *
10793 * <p>The default extent is the drawing width of this view.</p>
10794 *
10795 * @return the horizontal extent of the scrollbar's thumb
10796 *
10797 * @see #computeHorizontalScrollRange()
10798 * @see #computeHorizontalScrollOffset()
10799 * @see android.widget.ScrollBarDrawable
10800 */
10801 protected int computeHorizontalScrollExtent() {
10802 return getWidth();
10803 }
10804
10805 /**
10806 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10807 *
10808 * <p>The range is expressed in arbitrary units that must be the same as the
10809 * units used by {@link #computeVerticalScrollExtent()} and
10810 * {@link #computeVerticalScrollOffset()}.</p>
10811 *
10812 * @return the total vertical range represented by the vertical scrollbar
10813 *
10814 * <p>The default range is the drawing height of this view.</p>
10815 *
10816 * @see #computeVerticalScrollExtent()
10817 * @see #computeVerticalScrollOffset()
10818 * @see android.widget.ScrollBarDrawable
10819 */
10820 protected int computeVerticalScrollRange() {
10821 return getHeight();
10822 }
10823
10824 /**
10825 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10826 * within the horizontal range. This value is used to compute the position
10827 * of the thumb within the scrollbar's track.</p>
10828 *
10829 * <p>The range is expressed in arbitrary units that must be the same as the
10830 * units used by {@link #computeVerticalScrollRange()} and
10831 * {@link #computeVerticalScrollExtent()}.</p>
10832 *
10833 * <p>The default offset is the scroll offset of this view.</p>
10834 *
10835 * @return the vertical offset of the scrollbar's thumb
10836 *
10837 * @see #computeVerticalScrollRange()
10838 * @see #computeVerticalScrollExtent()
10839 * @see android.widget.ScrollBarDrawable
10840 */
10841 protected int computeVerticalScrollOffset() {
10842 return mScrollY;
10843 }
10844
10845 /**
10846 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10847 * within the vertical range. This value is used to compute the length
10848 * of the thumb within the scrollbar's track.</p>
10849 *
10850 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010851 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 * {@link #computeVerticalScrollOffset()}.</p>
10853 *
10854 * <p>The default extent is the drawing height of this view.</p>
10855 *
10856 * @return the vertical extent of the scrollbar's thumb
10857 *
10858 * @see #computeVerticalScrollRange()
10859 * @see #computeVerticalScrollOffset()
10860 * @see android.widget.ScrollBarDrawable
10861 */
10862 protected int computeVerticalScrollExtent() {
10863 return getHeight();
10864 }
10865
10866 /**
Adam Powell69159442011-06-13 17:53:06 -070010867 * Check if this view can be scrolled horizontally in a certain direction.
10868 *
10869 * @param direction Negative to check scrolling left, positive to check scrolling right.
10870 * @return true if this view can be scrolled in the specified direction, false otherwise.
10871 */
10872 public boolean canScrollHorizontally(int direction) {
10873 final int offset = computeHorizontalScrollOffset();
10874 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10875 if (range == 0) return false;
10876 if (direction < 0) {
10877 return offset > 0;
10878 } else {
10879 return offset < range - 1;
10880 }
10881 }
10882
10883 /**
10884 * Check if this view can be scrolled vertically in a certain direction.
10885 *
10886 * @param direction Negative to check scrolling up, positive to check scrolling down.
10887 * @return true if this view can be scrolled in the specified direction, false otherwise.
10888 */
10889 public boolean canScrollVertically(int direction) {
10890 final int offset = computeVerticalScrollOffset();
10891 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10892 if (range == 0) return false;
10893 if (direction < 0) {
10894 return offset > 0;
10895 } else {
10896 return offset < range - 1;
10897 }
10898 }
10899
10900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010901 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10902 * scrollbars are painted only if they have been awakened first.</p>
10903 *
10904 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010905 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010906 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010908 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010909 // scrollbars are drawn only when the animation is running
10910 final ScrollabilityCache cache = mScrollCache;
10911 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010912
Mike Cleronf116bf82009-09-27 19:14:12 -070010913 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010914
Mike Cleronf116bf82009-09-27 19:14:12 -070010915 if (state == ScrollabilityCache.OFF) {
10916 return;
10917 }
Joe Malin32736f02011-01-19 16:14:20 -080010918
Mike Cleronf116bf82009-09-27 19:14:12 -070010919 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010920
Mike Cleronf116bf82009-09-27 19:14:12 -070010921 if (state == ScrollabilityCache.FADING) {
10922 // We're fading -- get our fade interpolation
10923 if (cache.interpolatorValues == null) {
10924 cache.interpolatorValues = new float[1];
10925 }
Joe Malin32736f02011-01-19 16:14:20 -080010926
Mike Cleronf116bf82009-09-27 19:14:12 -070010927 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010928
Mike Cleronf116bf82009-09-27 19:14:12 -070010929 // Stops the animation if we're done
10930 if (cache.scrollBarInterpolator.timeToValues(values) ==
10931 Interpolator.Result.FREEZE_END) {
10932 cache.state = ScrollabilityCache.OFF;
10933 } else {
10934 cache.scrollBar.setAlpha(Math.round(values[0]));
10935 }
Joe Malin32736f02011-01-19 16:14:20 -080010936
10937 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010938 // drawing. We only want this when we're fading so that
10939 // we prevent excessive redraws
10940 invalidate = true;
10941 } else {
10942 // We're just on -- but we may have been fading before so
10943 // reset alpha
10944 cache.scrollBar.setAlpha(255);
10945 }
10946
Joe Malin32736f02011-01-19 16:14:20 -080010947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 final int viewFlags = mViewFlags;
10949
10950 final boolean drawHorizontalScrollBar =
10951 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10952 final boolean drawVerticalScrollBar =
10953 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10954 && !isVerticalScrollBarHidden();
10955
10956 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10957 final int width = mRight - mLeft;
10958 final int height = mBottom - mTop;
10959
10960 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961
Mike Reede8853fc2009-09-04 14:01:48 -040010962 final int scrollX = mScrollX;
10963 final int scrollY = mScrollY;
10964 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10965
Mike Cleronf116bf82009-09-27 19:14:12 -070010966 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010969 int size = scrollBar.getSize(false);
10970 if (size <= 0) {
10971 size = cache.scrollBarSize;
10972 }
10973
Mike Cleronf116bf82009-09-27 19:14:12 -070010974 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010975 computeHorizontalScrollOffset(),
10976 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010977 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010978 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010979 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010980 left = scrollX + (mPaddingLeft & inside);
10981 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10982 bottom = top + size;
10983 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10984 if (invalidate) {
10985 invalidate(left, top, right, bottom);
10986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987 }
10988
10989 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010990 int size = scrollBar.getSize(true);
10991 if (size <= 0) {
10992 size = cache.scrollBarSize;
10993 }
10994
Mike Reede8853fc2009-09-04 14:01:48 -040010995 scrollBar.setParameters(computeVerticalScrollRange(),
10996 computeVerticalScrollOffset(),
10997 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010998 switch (mVerticalScrollbarPosition) {
10999 default:
11000 case SCROLLBAR_POSITION_DEFAULT:
11001 case SCROLLBAR_POSITION_RIGHT:
11002 left = scrollX + width - size - (mUserPaddingRight & inside);
11003 break;
11004 case SCROLLBAR_POSITION_LEFT:
11005 left = scrollX + (mUserPaddingLeft & inside);
11006 break;
11007 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011008 top = scrollY + (mPaddingTop & inside);
11009 right = left + size;
11010 bottom = scrollY + height - (mUserPaddingBottom & inside);
11011 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11012 if (invalidate) {
11013 invalidate(left, top, right, bottom);
11014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011015 }
11016 }
11017 }
11018 }
Romain Guy8506ab42009-06-11 17:35:47 -070011019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011021 * 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 -080011022 * FastScroller is visible.
11023 * @return whether to temporarily hide the vertical scrollbar
11024 * @hide
11025 */
11026 protected boolean isVerticalScrollBarHidden() {
11027 return false;
11028 }
11029
11030 /**
11031 * <p>Draw the horizontal scrollbar if
11032 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11033 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034 * @param canvas the canvas on which to draw the scrollbar
11035 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011036 *
11037 * @see #isHorizontalScrollBarEnabled()
11038 * @see #computeHorizontalScrollRange()
11039 * @see #computeHorizontalScrollExtent()
11040 * @see #computeHorizontalScrollOffset()
11041 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011042 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011043 */
Romain Guy8fb95422010-08-17 18:38:51 -070011044 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11045 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011046 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011047 scrollBar.draw(canvas);
11048 }
Mike Reede8853fc2009-09-04 14:01:48 -040011049
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011050 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011051 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11052 * returns true.</p>
11053 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 * @param canvas the canvas on which to draw the scrollbar
11055 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011056 *
11057 * @see #isVerticalScrollBarEnabled()
11058 * @see #computeVerticalScrollRange()
11059 * @see #computeVerticalScrollExtent()
11060 * @see #computeVerticalScrollOffset()
11061 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011062 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 */
Romain Guy8fb95422010-08-17 18:38:51 -070011064 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11065 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011066 scrollBar.setBounds(l, t, r, b);
11067 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068 }
11069
11070 /**
11071 * Implement this to do your drawing.
11072 *
11073 * @param canvas the canvas on which the background will be drawn
11074 */
11075 protected void onDraw(Canvas canvas) {
11076 }
11077
11078 /*
11079 * Caller is responsible for calling requestLayout if necessary.
11080 * (This allows addViewInLayout to not request a new layout.)
11081 */
11082 void assignParent(ViewParent parent) {
11083 if (mParent == null) {
11084 mParent = parent;
11085 } else if (parent == null) {
11086 mParent = null;
11087 } else {
11088 throw new RuntimeException("view " + this + " being added, but"
11089 + " it already has a parent");
11090 }
11091 }
11092
11093 /**
11094 * This is called when the view is attached to a window. At this point it
11095 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011096 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11097 * however it may be called any time before the first onDraw -- including
11098 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011099 *
11100 * @see #onDetachedFromWindow()
11101 */
11102 protected void onAttachedToWindow() {
11103 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11104 mParent.requestTransparentRegion(this);
11105 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011106
Adam Powell8568c3a2010-04-19 14:26:11 -070011107 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11108 initialAwakenScrollBars();
11109 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11110 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011111
Chet Haasea9b61ac2010-12-20 07:40:25 -080011112 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011113
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011114 // Order is important here: LayoutDirection MUST be resolved before Padding
11115 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011116 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011117 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011118 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011119 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011120
Svetoslav Ganov42138042012-03-20 11:51:39 -070011121 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011122 if (isFocused()) {
11123 InputMethodManager imm = InputMethodManager.peekInstance();
11124 imm.focusIn(this);
11125 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011126
11127 if (mAttachInfo != null && mDisplayList != null) {
11128 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11129 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011130 }
Cibu Johny86666632010-02-22 13:01:02 -080011131
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011132 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011133 * @see #onScreenStateChanged(int)
11134 */
11135 void dispatchScreenStateChanged(int screenState) {
11136 onScreenStateChanged(screenState);
11137 }
11138
11139 /**
11140 * This method is called whenever the state of the screen this view is
11141 * attached to changes. A state change will usually occurs when the screen
11142 * turns on or off (whether it happens automatically or the user does it
11143 * manually.)
11144 *
11145 * @param screenState The new state of the screen. Can be either
11146 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11147 */
11148 public void onScreenStateChanged(int screenState) {
11149 }
11150
11151 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011152 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11153 */
11154 private boolean hasRtlSupport() {
11155 return mContext.getApplicationInfo().hasRtlSupport();
11156 }
11157
11158 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011159 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11160 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011161 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011162 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011163 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011164 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011165 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011166 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011167
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011168 if (hasRtlSupport()) {
11169 // Set resolved depending on layout direction
11170 switch (getLayoutDirection()) {
11171 case LAYOUT_DIRECTION_INHERIT:
11172 // If this is root view, no need to look at parent's layout dir.
11173 if (canResolveLayoutDirection()) {
11174 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011175
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011176 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11177 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11178 }
11179 } else {
11180 // Nothing to do, LTR by default
11181 }
11182 break;
11183 case LAYOUT_DIRECTION_RTL:
11184 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11185 break;
11186 case LAYOUT_DIRECTION_LOCALE:
11187 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011188 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11189 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011190 break;
11191 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011192 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011193 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011194 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011195
11196 // Set to resolved
11197 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011198 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011199 // Resolve padding
11200 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011201 }
11202
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011203 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011204 * Called when layout direction has been resolved.
11205 *
11206 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011207 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011208 */
11209 public void onResolvedLayoutDirectionChanged() {
11210 }
11211
11212 /**
11213 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011214 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011215 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011216 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011217 // If the user specified the absolute padding (either with android:padding or
11218 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11219 // use the default padding or the padding from the background drawable
11220 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011221 int resolvedLayoutDirection = getResolvedLayoutDirection();
11222 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011223 case LAYOUT_DIRECTION_RTL:
11224 // Start user padding override Right user padding. Otherwise, if Right user
11225 // padding is not defined, use the default Right padding. If Right user padding
11226 // is defined, just use it.
11227 if (mUserPaddingStart >= 0) {
11228 mUserPaddingRight = mUserPaddingStart;
11229 } else if (mUserPaddingRight < 0) {
11230 mUserPaddingRight = mPaddingRight;
11231 }
11232 if (mUserPaddingEnd >= 0) {
11233 mUserPaddingLeft = mUserPaddingEnd;
11234 } else if (mUserPaddingLeft < 0) {
11235 mUserPaddingLeft = mPaddingLeft;
11236 }
11237 break;
11238 case LAYOUT_DIRECTION_LTR:
11239 default:
11240 // Start user padding override Left user padding. Otherwise, if Left user
11241 // padding is not defined, use the default left padding. If Left user padding
11242 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011243 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011244 mUserPaddingLeft = mUserPaddingStart;
11245 } else if (mUserPaddingLeft < 0) {
11246 mUserPaddingLeft = mPaddingLeft;
11247 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011248 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011249 mUserPaddingRight = mUserPaddingEnd;
11250 } else if (mUserPaddingRight < 0) {
11251 mUserPaddingRight = mPaddingRight;
11252 }
11253 }
11254
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011255 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11256
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011257 if(isPaddingRelative()) {
11258 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11259 } else {
11260 recomputePadding();
11261 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011262 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011263 }
11264
11265 /**
11266 * Resolve padding depending on the layout direction. Subclasses that care about
11267 * padding resolution should override this method. The default implementation does
11268 * nothing.
11269 *
11270 * @param layoutDirection the direction of the layout
11271 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011272 * @see {@link #LAYOUT_DIRECTION_LTR}
11273 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011274 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011275 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011276 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011277 }
11278
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011279 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011280 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011281 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011282 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011283 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011284 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011285 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011286 switch (getLayoutDirection()) {
11287 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011288 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011289 default:
11290 return true;
11291 }
11292 }
11293
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011294 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011295 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11296 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011297 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011298 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011299 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011300 // Reset the current resolved bits
11301 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011302 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011303 // Reset also the text direction
11304 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011305 }
11306
11307 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011308 * Called during reset of resolved layout direction.
11309 *
11310 * Subclasses need to override this method to clear cached information that depends on the
11311 * resolved layout direction, or to inform child views that inherit their layout direction.
11312 *
11313 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011314 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011315 */
11316 public void onResolvedLayoutDirectionReset() {
11317 }
11318
11319 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011320 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011321 *
11322 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011323 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011324 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011325 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011326 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011327 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 }
11329
11330 /**
11331 * This is called when the view is detached from a window. At this point it
11332 * no longer has a surface for drawing.
11333 *
11334 * @see #onAttachedToWindow()
11335 */
11336 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011337 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011338
Romain Guya440b002010-02-24 15:57:54 -080011339 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011340 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011341 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011342 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011345
Romain Guya998dff2012-03-23 18:58:36 -070011346 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011347
11348 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011349 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011350 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011351 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011352 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011353 } else {
11354 if (mDisplayList != null) {
11355 // Should never happen
11356 mDisplayList.invalidate();
11357 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011358 }
11359
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011360 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011361
11362 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011363 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011364 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 }
11366
11367 /**
11368 * @return The number of times this view has been attached to a window
11369 */
11370 protected int getWindowAttachCount() {
11371 return mWindowAttachCount;
11372 }
11373
11374 /**
11375 * Retrieve a unique token identifying the window this view is attached to.
11376 * @return Return the window's token for use in
11377 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11378 */
11379 public IBinder getWindowToken() {
11380 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11381 }
11382
11383 /**
11384 * Retrieve a unique token identifying the top-level "real" window of
11385 * the window that this view is attached to. That is, this is like
11386 * {@link #getWindowToken}, except if the window this view in is a panel
11387 * window (attached to another containing window), then the token of
11388 * the containing window is returned instead.
11389 *
11390 * @return Returns the associated window token, either
11391 * {@link #getWindowToken()} or the containing window's token.
11392 */
11393 public IBinder getApplicationWindowToken() {
11394 AttachInfo ai = mAttachInfo;
11395 if (ai != null) {
11396 IBinder appWindowToken = ai.mPanelParentWindowToken;
11397 if (appWindowToken == null) {
11398 appWindowToken = ai.mWindowToken;
11399 }
11400 return appWindowToken;
11401 }
11402 return null;
11403 }
11404
11405 /**
11406 * Retrieve private session object this view hierarchy is using to
11407 * communicate with the window manager.
11408 * @return the session object to communicate with the window manager
11409 */
11410 /*package*/ IWindowSession getWindowSession() {
11411 return mAttachInfo != null ? mAttachInfo.mSession : null;
11412 }
11413
11414 /**
11415 * @param info the {@link android.view.View.AttachInfo} to associated with
11416 * this view
11417 */
11418 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11419 //System.out.println("Attached! " + this);
11420 mAttachInfo = info;
11421 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011422 // We will need to evaluate the drawable state at least once.
11423 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424 if (mFloatingTreeObserver != null) {
11425 info.mTreeObserver.merge(mFloatingTreeObserver);
11426 mFloatingTreeObserver = null;
11427 }
11428 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11429 mAttachInfo.mScrollContainers.add(this);
11430 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11431 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011432 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011434
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011435 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011436 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011437 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011438 if (listeners != null && listeners.size() > 0) {
11439 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11440 // perform the dispatching. The iterator is a safe guard against listeners that
11441 // could mutate the list by calling the various add/remove methods. This prevents
11442 // the array from being modified while we iterate it.
11443 for (OnAttachStateChangeListener listener : listeners) {
11444 listener.onViewAttachedToWindow(this);
11445 }
11446 }
11447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448 int vis = info.mWindowVisibility;
11449 if (vis != GONE) {
11450 onWindowVisibilityChanged(vis);
11451 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011452 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11453 // If nobody has evaluated the drawable state yet, then do it now.
11454 refreshDrawableState();
11455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011456 }
11457
11458 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 AttachInfo info = mAttachInfo;
11460 if (info != null) {
11461 int vis = info.mWindowVisibility;
11462 if (vis != GONE) {
11463 onWindowVisibilityChanged(GONE);
11464 }
11465 }
11466
11467 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011468
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011469 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011470 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011471 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011472 if (listeners != null && listeners.size() > 0) {
11473 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11474 // perform the dispatching. The iterator is a safe guard against listeners that
11475 // could mutate the list by calling the various add/remove methods. This prevents
11476 // the array from being modified while we iterate it.
11477 for (OnAttachStateChangeListener listener : listeners) {
11478 listener.onViewDetachedFromWindow(this);
11479 }
11480 }
11481
Romain Guy01d5edc2011-01-28 11:28:53 -080011482 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011483 mAttachInfo.mScrollContainers.remove(this);
11484 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11485 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 mAttachInfo = null;
11488 }
11489
11490 /**
11491 * Store this view hierarchy's frozen state into the given container.
11492 *
11493 * @param container The SparseArray in which to save the view's state.
11494 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011495 * @see #restoreHierarchyState(android.util.SparseArray)
11496 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11497 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 */
11499 public void saveHierarchyState(SparseArray<Parcelable> container) {
11500 dispatchSaveInstanceState(container);
11501 }
11502
11503 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011504 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11505 * this view and its children. May be overridden to modify how freezing happens to a
11506 * 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 -080011507 *
11508 * @param container The SparseArray in which to save the view's state.
11509 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011510 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11511 * @see #saveHierarchyState(android.util.SparseArray)
11512 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 */
11514 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11515 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11516 mPrivateFlags &= ~SAVE_STATE_CALLED;
11517 Parcelable state = onSaveInstanceState();
11518 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11519 throw new IllegalStateException(
11520 "Derived class did not call super.onSaveInstanceState()");
11521 }
11522 if (state != null) {
11523 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11524 // + ": " + state);
11525 container.put(mID, state);
11526 }
11527 }
11528 }
11529
11530 /**
11531 * Hook allowing a view to generate a representation of its internal state
11532 * that can later be used to create a new instance with that same state.
11533 * This state should only contain information that is not persistent or can
11534 * not be reconstructed later. For example, you will never store your
11535 * current position on screen because that will be computed again when a
11536 * new instance of the view is placed in its view hierarchy.
11537 * <p>
11538 * Some examples of things you may store here: the current cursor position
11539 * in a text view (but usually not the text itself since that is stored in a
11540 * content provider or other persistent storage), the currently selected
11541 * item in a list view.
11542 *
11543 * @return Returns a Parcelable object containing the view's current dynamic
11544 * state, or null if there is nothing interesting to save. The
11545 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011546 * @see #onRestoreInstanceState(android.os.Parcelable)
11547 * @see #saveHierarchyState(android.util.SparseArray)
11548 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 * @see #setSaveEnabled(boolean)
11550 */
11551 protected Parcelable onSaveInstanceState() {
11552 mPrivateFlags |= SAVE_STATE_CALLED;
11553 return BaseSavedState.EMPTY_STATE;
11554 }
11555
11556 /**
11557 * Restore this view hierarchy's frozen state from the given container.
11558 *
11559 * @param container The SparseArray which holds previously frozen states.
11560 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011561 * @see #saveHierarchyState(android.util.SparseArray)
11562 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11563 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011564 */
11565 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11566 dispatchRestoreInstanceState(container);
11567 }
11568
11569 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011570 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11571 * state for this view and its children. May be overridden to modify how restoring
11572 * happens to a view's children; for example, some views may want to not store state
11573 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011574 *
11575 * @param container The SparseArray which holds previously saved state.
11576 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011577 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11578 * @see #restoreHierarchyState(android.util.SparseArray)
11579 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 */
11581 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11582 if (mID != NO_ID) {
11583 Parcelable state = container.get(mID);
11584 if (state != null) {
11585 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11586 // + ": " + state);
11587 mPrivateFlags &= ~SAVE_STATE_CALLED;
11588 onRestoreInstanceState(state);
11589 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11590 throw new IllegalStateException(
11591 "Derived class did not call super.onRestoreInstanceState()");
11592 }
11593 }
11594 }
11595 }
11596
11597 /**
11598 * Hook allowing a view to re-apply a representation of its internal state that had previously
11599 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11600 * null state.
11601 *
11602 * @param state The frozen state that had previously been returned by
11603 * {@link #onSaveInstanceState}.
11604 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011605 * @see #onSaveInstanceState()
11606 * @see #restoreHierarchyState(android.util.SparseArray)
11607 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 */
11609 protected void onRestoreInstanceState(Parcelable state) {
11610 mPrivateFlags |= SAVE_STATE_CALLED;
11611 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011612 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11613 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011614 + "when two views of different type have the same id in the same hierarchy. "
11615 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011616 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011617 }
11618 }
11619
11620 /**
11621 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11622 *
11623 * @return the drawing start time in milliseconds
11624 */
11625 public long getDrawingTime() {
11626 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11627 }
11628
11629 /**
11630 * <p>Enables or disables the duplication of the parent's state into this view. When
11631 * duplication is enabled, this view gets its drawable state from its parent rather
11632 * than from its own internal properties.</p>
11633 *
11634 * <p>Note: in the current implementation, setting this property to true after the
11635 * view was added to a ViewGroup might have no effect at all. This property should
11636 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11637 *
11638 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11639 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011640 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011641 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11642 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011643 *
11644 * @param enabled True to enable duplication of the parent's drawable state, false
11645 * to disable it.
11646 *
11647 * @see #getDrawableState()
11648 * @see #isDuplicateParentStateEnabled()
11649 */
11650 public void setDuplicateParentStateEnabled(boolean enabled) {
11651 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11652 }
11653
11654 /**
11655 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11656 *
11657 * @return True if this view's drawable state is duplicated from the parent,
11658 * false otherwise
11659 *
11660 * @see #getDrawableState()
11661 * @see #setDuplicateParentStateEnabled(boolean)
11662 */
11663 public boolean isDuplicateParentStateEnabled() {
11664 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11665 }
11666
11667 /**
Romain Guy171c5922011-01-06 10:04:23 -080011668 * <p>Specifies the type of layer backing this view. The layer can be
11669 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11670 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011671 *
Romain Guy171c5922011-01-06 10:04:23 -080011672 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11673 * instance that controls how the layer is composed on screen. The following
11674 * properties of the paint are taken into account when composing the layer:</p>
11675 * <ul>
11676 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11677 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11678 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11679 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011680 *
Romain Guy171c5922011-01-06 10:04:23 -080011681 * <p>If this view has an alpha value set to < 1.0 by calling
11682 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11683 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11684 * equivalent to setting a hardware layer on this view and providing a paint with
11685 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011686 *
Romain Guy171c5922011-01-06 10:04:23 -080011687 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11688 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11689 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011690 *
Romain Guy171c5922011-01-06 10:04:23 -080011691 * @param layerType The ype of layer to use with this view, must be one of
11692 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11693 * {@link #LAYER_TYPE_HARDWARE}
11694 * @param paint The paint used to compose the layer. This argument is optional
11695 * and can be null. It is ignored when the layer type is
11696 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011697 *
11698 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011699 * @see #LAYER_TYPE_NONE
11700 * @see #LAYER_TYPE_SOFTWARE
11701 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011702 * @see #setAlpha(float)
11703 *
Romain Guy171c5922011-01-06 10:04:23 -080011704 * @attr ref android.R.styleable#View_layerType
11705 */
11706 public void setLayerType(int layerType, Paint paint) {
11707 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011708 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011709 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11710 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011711
Romain Guyd6cd5722011-01-17 14:42:41 -080011712 if (layerType == mLayerType) {
11713 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11714 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011715 invalidateParentCaches();
11716 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011717 }
11718 return;
11719 }
Romain Guy171c5922011-01-06 10:04:23 -080011720
11721 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011722 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011723 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011724 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011725 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011726 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011727 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011728 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011729 default:
11730 break;
Romain Guy171c5922011-01-06 10:04:23 -080011731 }
11732
11733 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011734 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11735 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11736 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011737
Romain Guy0fd89bf2011-01-26 15:41:30 -080011738 invalidateParentCaches();
11739 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011740 }
11741
11742 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011743 * Indicates whether this view has a static layer. A view with layer type
11744 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11745 * dynamic.
11746 */
11747 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011748 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011749 }
11750
11751 /**
Romain Guy171c5922011-01-06 10:04:23 -080011752 * Indicates what type of layer is currently associated with this view. By default
11753 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11754 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11755 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011756 *
Romain Guy171c5922011-01-06 10:04:23 -080011757 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11758 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011759 *
11760 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011761 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011762 * @see #LAYER_TYPE_NONE
11763 * @see #LAYER_TYPE_SOFTWARE
11764 * @see #LAYER_TYPE_HARDWARE
11765 */
11766 public int getLayerType() {
11767 return mLayerType;
11768 }
Joe Malin32736f02011-01-19 16:14:20 -080011769
Romain Guy6c319ca2011-01-11 14:29:25 -080011770 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011771 * Forces this view's layer to be created and this view to be rendered
11772 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11773 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011774 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011775 * This method can for instance be used to render a view into its layer before
11776 * starting an animation. If this view is complex, rendering into the layer
11777 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011778 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011779 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011780 *
11781 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011782 */
11783 public void buildLayer() {
11784 if (mLayerType == LAYER_TYPE_NONE) return;
11785
11786 if (mAttachInfo == null) {
11787 throw new IllegalStateException("This view must be attached to a window first");
11788 }
11789
11790 switch (mLayerType) {
11791 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011792 if (mAttachInfo.mHardwareRenderer != null &&
11793 mAttachInfo.mHardwareRenderer.isEnabled() &&
11794 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011795 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011796 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011797 break;
11798 case LAYER_TYPE_SOFTWARE:
11799 buildDrawingCache(true);
11800 break;
11801 }
11802 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011803
Romain Guy9c4b79a2011-11-10 19:23:58 -080011804 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11805 void flushLayer() {
11806 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11807 mHardwareLayer.flush();
11808 }
11809 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011810
11811 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011812 * <p>Returns a hardware layer that can be used to draw this view again
11813 * without executing its draw method.</p>
11814 *
11815 * @return A HardwareLayer ready to render, or null if an error occurred.
11816 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011817 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011818 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11819 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011820 return null;
11821 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011822
Romain Guy9c4b79a2011-11-10 19:23:58 -080011823 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011824
11825 final int width = mRight - mLeft;
11826 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011827
Romain Guy6c319ca2011-01-11 14:29:25 -080011828 if (width == 0 || height == 0) {
11829 return null;
11830 }
11831
11832 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11833 if (mHardwareLayer == null) {
11834 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11835 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011836 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011837 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11838 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011839 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011840 }
11841
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011842 // The layer is not valid if the underlying GPU resources cannot be allocated
11843 if (!mHardwareLayer.isValid()) {
11844 return null;
11845 }
11846
Chet Haasea1cff502012-02-21 13:43:44 -080011847 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011848 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011849 }
11850
11851 return mHardwareLayer;
11852 }
Romain Guy171c5922011-01-06 10:04:23 -080011853
Romain Guy589b0bb2011-10-10 13:57:47 -070011854 /**
11855 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011856 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011857 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011858 *
11859 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011860 * @see #LAYER_TYPE_HARDWARE
11861 */
Romain Guya998dff2012-03-23 18:58:36 -070011862 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011863 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011864 AttachInfo info = mAttachInfo;
11865 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011866 info.mHardwareRenderer.isEnabled() &&
11867 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011868 mHardwareLayer.destroy();
11869 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011870
Romain Guy9c4b79a2011-11-10 19:23:58 -080011871 invalidate(true);
11872 invalidateParentCaches();
11873 }
Romain Guy65b345f2011-07-27 18:51:50 -070011874 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011875 }
Romain Guy65b345f2011-07-27 18:51:50 -070011876 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011877 }
11878
Romain Guy171c5922011-01-06 10:04:23 -080011879 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011880 * Destroys all hardware rendering resources. This method is invoked
11881 * when the system needs to reclaim resources. Upon execution of this
11882 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011883 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011884 * Note: you <strong>must</strong> call
11885 * <code>super.destroyHardwareResources()</code> when overriding
11886 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011887 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011888 * @hide
11889 */
11890 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011891 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011892 }
11893
11894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011895 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11896 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11897 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11898 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11899 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11900 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011901 *
Romain Guy171c5922011-01-06 10:04:23 -080011902 * <p>Enabling the drawing cache is similar to
11903 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011904 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11905 * drawing cache has no effect on rendering because the system uses a different mechanism
11906 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11907 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11908 * for information on how to enable software and hardware layers.</p>
11909 *
11910 * <p>This API can be used to manually generate
11911 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11912 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 *
11914 * @param enabled true to enable the drawing cache, false otherwise
11915 *
11916 * @see #isDrawingCacheEnabled()
11917 * @see #getDrawingCache()
11918 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011919 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 */
11921 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011922 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011923 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11924 }
11925
11926 /**
11927 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11928 *
11929 * @return true if the drawing cache is enabled
11930 *
11931 * @see #setDrawingCacheEnabled(boolean)
11932 * @see #getDrawingCache()
11933 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011934 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 public boolean isDrawingCacheEnabled() {
11936 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11937 }
11938
11939 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011940 * Debugging utility which recursively outputs the dirty state of a view and its
11941 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011942 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011943 * @hide
11944 */
Romain Guy676b1732011-02-14 14:45:33 -080011945 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011946 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11947 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11948 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11949 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11950 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11951 if (clear) {
11952 mPrivateFlags &= clearMask;
11953 }
11954 if (this instanceof ViewGroup) {
11955 ViewGroup parent = (ViewGroup) this;
11956 final int count = parent.getChildCount();
11957 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011958 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011959 child.outputDirtyFlags(indent + " ", clear, clearMask);
11960 }
11961 }
11962 }
11963
11964 /**
11965 * This method is used by ViewGroup to cause its children to restore or recreate their
11966 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11967 * to recreate its own display list, which would happen if it went through the normal
11968 * draw/dispatchDraw mechanisms.
11969 *
11970 * @hide
11971 */
11972 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011973
11974 /**
11975 * A view that is not attached or hardware accelerated cannot create a display list.
11976 * This method checks these conditions and returns the appropriate result.
11977 *
11978 * @return true if view has the ability to create a display list, false otherwise.
11979 *
11980 * @hide
11981 */
11982 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011983 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011984 }
Joe Malin32736f02011-01-19 16:14:20 -080011985
Chet Haasedaf98e92011-01-10 14:10:36 -080011986 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011987 * @return The HardwareRenderer associated with that view or null if hardware rendering
11988 * is not supported or this this has not been attached to a window.
11989 *
11990 * @hide
11991 */
11992 public HardwareRenderer getHardwareRenderer() {
11993 if (mAttachInfo != null) {
11994 return mAttachInfo.mHardwareRenderer;
11995 }
11996 return null;
11997 }
11998
11999 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012000 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12001 * Otherwise, the same display list will be returned (after having been rendered into
12002 * along the way, depending on the invalidation state of the view).
12003 *
12004 * @param displayList The previous version of this displayList, could be null.
12005 * @param isLayer Whether the requester of the display list is a layer. If so,
12006 * the view will avoid creating a layer inside the resulting display list.
12007 * @return A new or reused DisplayList object.
12008 */
12009 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12010 if (!canHaveDisplayList()) {
12011 return null;
12012 }
12013
12014 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12015 displayList == null || !displayList.isValid() ||
12016 (!isLayer && mRecreateDisplayList))) {
12017 // Don't need to recreate the display list, just need to tell our
12018 // children to restore/recreate theirs
12019 if (displayList != null && displayList.isValid() &&
12020 !isLayer && !mRecreateDisplayList) {
12021 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12022 mPrivateFlags &= ~DIRTY_MASK;
12023 dispatchGetDisplayList();
12024
12025 return displayList;
12026 }
12027
12028 if (!isLayer) {
12029 // If we got here, we're recreating it. Mark it as such to ensure that
12030 // we copy in child display lists into ours in drawChild()
12031 mRecreateDisplayList = true;
12032 }
12033 if (displayList == null) {
12034 final String name = getClass().getSimpleName();
12035 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12036 // If we're creating a new display list, make sure our parent gets invalidated
12037 // since they will need to recreate their display list to account for this
12038 // new child display list.
12039 invalidateParentCaches();
12040 }
12041
12042 boolean caching = false;
12043 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012044 int width = mRight - mLeft;
12045 int height = mBottom - mTop;
12046
12047 try {
12048 canvas.setViewport(width, height);
12049 // The dirty rect should always be null for a display list
12050 canvas.onPreDraw(null);
12051 int layerType = (
12052 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12053 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012054 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012055 if (layerType == LAYER_TYPE_HARDWARE) {
12056 final HardwareLayer layer = getHardwareLayer();
12057 if (layer != null && layer.isValid()) {
12058 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12059 } else {
12060 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12061 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12062 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12063 }
12064 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012065 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012066 buildDrawingCache(true);
12067 Bitmap cache = getDrawingCache(true);
12068 if (cache != null) {
12069 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12070 caching = true;
12071 }
Chet Haasea1cff502012-02-21 13:43:44 -080012072 }
Chet Haasea1cff502012-02-21 13:43:44 -080012073 } else {
12074
12075 computeScroll();
12076
Chet Haasea1cff502012-02-21 13:43:44 -080012077 canvas.translate(-mScrollX, -mScrollY);
12078 if (!isLayer) {
12079 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12080 mPrivateFlags &= ~DIRTY_MASK;
12081 }
12082
12083 // Fast path for layouts with no backgrounds
12084 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12085 dispatchDraw(canvas);
12086 } else {
12087 draw(canvas);
12088 }
12089 }
12090 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012091 canvas.onPostDraw();
12092
12093 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012094 displayList.setCaching(caching);
12095 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012096 displayList.setLeftTopRightBottom(0, 0, width, height);
12097 } else {
12098 setDisplayListProperties(displayList);
12099 }
12100 }
12101 } else if (!isLayer) {
12102 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12103 mPrivateFlags &= ~DIRTY_MASK;
12104 }
12105
12106 return displayList;
12107 }
12108
12109 /**
12110 * Get the DisplayList for the HardwareLayer
12111 *
12112 * @param layer The HardwareLayer whose DisplayList we want
12113 * @return A DisplayList fopr the specified HardwareLayer
12114 */
12115 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12116 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12117 layer.setDisplayList(displayList);
12118 return displayList;
12119 }
12120
12121
12122 /**
Romain Guyb051e892010-09-28 19:09:36 -070012123 * <p>Returns a display list that can be used to draw this view again
12124 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012125 *
Romain Guyb051e892010-09-28 19:09:36 -070012126 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012127 *
12128 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012129 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012130 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012131 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012132 return mDisplayList;
12133 }
12134
12135 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012136 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012137 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012138 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012139 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012140 * @see #getDrawingCache(boolean)
12141 */
12142 public Bitmap getDrawingCache() {
12143 return getDrawingCache(false);
12144 }
12145
12146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012147 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12148 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12149 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12150 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12151 * request the drawing cache by calling this method and draw it on screen if the
12152 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012153 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012154 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12155 * this method will create a bitmap of the same size as this view. Because this bitmap
12156 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12157 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12158 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12159 * size than the view. This implies that your application must be able to handle this
12160 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012161 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012162 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12163 * the current density of the screen when the application is in compatibility
12164 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012165 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012166 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 * @see #setDrawingCacheEnabled(boolean)
12169 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012170 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 * @see #destroyDrawingCache()
12172 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012173 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012174 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12175 return null;
12176 }
12177 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012178 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 }
Romain Guy02890fd2010-08-06 17:58:44 -070012180 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012181 }
12182
12183 /**
12184 * <p>Frees the resources used by the drawing cache. If you call
12185 * {@link #buildDrawingCache()} manually without calling
12186 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12187 * should cleanup the cache with this method afterwards.</p>
12188 *
12189 * @see #setDrawingCacheEnabled(boolean)
12190 * @see #buildDrawingCache()
12191 * @see #getDrawingCache()
12192 */
12193 public void destroyDrawingCache() {
12194 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012195 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012196 mDrawingCache = null;
12197 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012198 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012199 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012200 mUnscaledDrawingCache = null;
12201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 }
12203
12204 /**
12205 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012206 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 * view will always be drawn on top of a solid color.
12208 *
12209 * @param color The background color to use for the drawing cache's bitmap
12210 *
12211 * @see #setDrawingCacheEnabled(boolean)
12212 * @see #buildDrawingCache()
12213 * @see #getDrawingCache()
12214 */
12215 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012216 if (color != mDrawingCacheBackgroundColor) {
12217 mDrawingCacheBackgroundColor = color;
12218 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012220 }
12221
12222 /**
12223 * @see #setDrawingCacheBackgroundColor(int)
12224 *
12225 * @return The background color to used for the drawing cache's bitmap
12226 */
12227 public int getDrawingCacheBackgroundColor() {
12228 return mDrawingCacheBackgroundColor;
12229 }
12230
12231 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012232 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012233 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012234 * @see #buildDrawingCache(boolean)
12235 */
12236 public void buildDrawingCache() {
12237 buildDrawingCache(false);
12238 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012239
Romain Guyfbd8f692009-06-26 14:51:58 -070012240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12242 *
12243 * <p>If you call {@link #buildDrawingCache()} manually without calling
12244 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12245 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012246 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012247 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12248 * this method will create a bitmap of the same size as this view. Because this bitmap
12249 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12250 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12251 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12252 * size than the view. This implies that your application must be able to handle this
12253 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012254 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012255 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12256 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012257 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012258 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012259 *
12260 * @see #getDrawingCache()
12261 * @see #destroyDrawingCache()
12262 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012263 public void buildDrawingCache(boolean autoScale) {
12264 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012265 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012266 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012267
12268 if (ViewDebug.TRACE_HIERARCHY) {
12269 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
12270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271
Romain Guy8506ab42009-06-11 17:35:47 -070012272 int width = mRight - mLeft;
12273 int height = mBottom - mTop;
12274
12275 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012276 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012277
Romain Guye1123222009-06-29 14:24:56 -070012278 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012279 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12280 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012282
12283 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012284 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012285 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012286
12287 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012288 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012289 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012290 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12291 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012292 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012293 return;
12294 }
12295
12296 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012297 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012298
12299 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012300 Bitmap.Config quality;
12301 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012302 // Never pick ARGB_4444 because it looks awful
12303 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12305 case DRAWING_CACHE_QUALITY_AUTO:
12306 quality = Bitmap.Config.ARGB_8888;
12307 break;
12308 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012309 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012310 break;
12311 case DRAWING_CACHE_QUALITY_HIGH:
12312 quality = Bitmap.Config.ARGB_8888;
12313 break;
12314 default:
12315 quality = Bitmap.Config.ARGB_8888;
12316 break;
12317 }
12318 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012319 // Optimization for translucent windows
12320 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012321 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012322 }
12323
12324 // Try to cleanup memory
12325 if (bitmap != null) bitmap.recycle();
12326
12327 try {
12328 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012329 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012330 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012331 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012332 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012333 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012334 }
Adam Powell26153a32010-11-08 15:22:27 -080012335 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012336 } catch (OutOfMemoryError e) {
12337 // If there is not enough memory to create the bitmap cache, just
12338 // ignore the issue as bitmap caches are not required to draw the
12339 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012340 if (autoScale) {
12341 mDrawingCache = null;
12342 } else {
12343 mUnscaledDrawingCache = null;
12344 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012345 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012346 return;
12347 }
12348
12349 clear = drawingCacheBackgroundColor != 0;
12350 }
12351
12352 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012353 if (attachInfo != null) {
12354 canvas = attachInfo.mCanvas;
12355 if (canvas == null) {
12356 canvas = new Canvas();
12357 }
12358 canvas.setBitmap(bitmap);
12359 // Temporarily clobber the cached Canvas in case one of our children
12360 // is also using a drawing cache. Without this, the children would
12361 // steal the canvas by attaching their own bitmap to it and bad, bad
12362 // thing would happen (invisible views, corrupted drawings, etc.)
12363 attachInfo.mCanvas = null;
12364 } else {
12365 // This case should hopefully never or seldom happen
12366 canvas = new Canvas(bitmap);
12367 }
12368
12369 if (clear) {
12370 bitmap.eraseColor(drawingCacheBackgroundColor);
12371 }
12372
12373 computeScroll();
12374 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012375
Romain Guye1123222009-06-29 14:24:56 -070012376 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012377 final float scale = attachInfo.mApplicationScale;
12378 canvas.scale(scale, scale);
12379 }
Joe Malin32736f02011-01-19 16:14:20 -080012380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 canvas.translate(-mScrollX, -mScrollY);
12382
Romain Guy5bcdff42009-05-14 21:27:18 -070012383 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012384 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12385 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012386 mPrivateFlags |= DRAWING_CACHE_VALID;
12387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388
12389 // Fast path for layouts with no backgrounds
12390 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12391 if (ViewDebug.TRACE_HIERARCHY) {
12392 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12393 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012394 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395 dispatchDraw(canvas);
12396 } else {
12397 draw(canvas);
12398 }
12399
12400 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012401 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012402
12403 if (attachInfo != null) {
12404 // Restore the cached Canvas for our siblings
12405 attachInfo.mCanvas = canvas;
12406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 }
12408 }
12409
12410 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012411 * Create a snapshot of the view into a bitmap. We should probably make
12412 * some form of this public, but should think about the API.
12413 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012414 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012415 int width = mRight - mLeft;
12416 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012417
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012418 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012419 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012420 width = (int) ((width * scale) + 0.5f);
12421 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012422
Romain Guy8c11e312009-09-14 15:15:30 -070012423 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012424 if (bitmap == null) {
12425 throw new OutOfMemoryError();
12426 }
12427
Romain Guyc529d8d2011-09-06 15:01:39 -070012428 Resources resources = getResources();
12429 if (resources != null) {
12430 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12431 }
Joe Malin32736f02011-01-19 16:14:20 -080012432
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012433 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012434 if (attachInfo != null) {
12435 canvas = attachInfo.mCanvas;
12436 if (canvas == null) {
12437 canvas = new Canvas();
12438 }
12439 canvas.setBitmap(bitmap);
12440 // Temporarily clobber the cached Canvas in case one of our children
12441 // is also using a drawing cache. Without this, the children would
12442 // steal the canvas by attaching their own bitmap to it and bad, bad
12443 // things would happen (invisible views, corrupted drawings, etc.)
12444 attachInfo.mCanvas = null;
12445 } else {
12446 // This case should hopefully never or seldom happen
12447 canvas = new Canvas(bitmap);
12448 }
12449
Romain Guy5bcdff42009-05-14 21:27:18 -070012450 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012451 bitmap.eraseColor(backgroundColor);
12452 }
12453
12454 computeScroll();
12455 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012456 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012457 canvas.translate(-mScrollX, -mScrollY);
12458
Romain Guy5bcdff42009-05-14 21:27:18 -070012459 // Temporarily remove the dirty mask
12460 int flags = mPrivateFlags;
12461 mPrivateFlags &= ~DIRTY_MASK;
12462
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012463 // Fast path for layouts with no backgrounds
12464 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12465 dispatchDraw(canvas);
12466 } else {
12467 draw(canvas);
12468 }
12469
Romain Guy5bcdff42009-05-14 21:27:18 -070012470 mPrivateFlags = flags;
12471
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012472 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012473 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012474
12475 if (attachInfo != null) {
12476 // Restore the cached Canvas for our siblings
12477 attachInfo.mCanvas = canvas;
12478 }
Romain Guy8506ab42009-06-11 17:35:47 -070012479
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012480 return bitmap;
12481 }
12482
12483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 * Indicates whether this View is currently in edit mode. A View is usually
12485 * in edit mode when displayed within a developer tool. For instance, if
12486 * this View is being drawn by a visual user interface builder, this method
12487 * should return true.
12488 *
12489 * Subclasses should check the return value of this method to provide
12490 * different behaviors if their normal behavior might interfere with the
12491 * host environment. For instance: the class spawns a thread in its
12492 * constructor, the drawing code relies on device-specific features, etc.
12493 *
12494 * This method is usually checked in the drawing code of custom widgets.
12495 *
12496 * @return True if this View is in edit mode, false otherwise.
12497 */
12498 public boolean isInEditMode() {
12499 return false;
12500 }
12501
12502 /**
12503 * If the View draws content inside its padding and enables fading edges,
12504 * it needs to support padding offsets. Padding offsets are added to the
12505 * fading edges to extend the length of the fade so that it covers pixels
12506 * drawn inside the padding.
12507 *
12508 * Subclasses of this class should override this method if they need
12509 * to draw content inside the padding.
12510 *
12511 * @return True if padding offset must be applied, false otherwise.
12512 *
12513 * @see #getLeftPaddingOffset()
12514 * @see #getRightPaddingOffset()
12515 * @see #getTopPaddingOffset()
12516 * @see #getBottomPaddingOffset()
12517 *
12518 * @since CURRENT
12519 */
12520 protected boolean isPaddingOffsetRequired() {
12521 return false;
12522 }
12523
12524 /**
12525 * Amount by which to extend the left fading region. Called only when
12526 * {@link #isPaddingOffsetRequired()} returns true.
12527 *
12528 * @return The left padding offset in pixels.
12529 *
12530 * @see #isPaddingOffsetRequired()
12531 *
12532 * @since CURRENT
12533 */
12534 protected int getLeftPaddingOffset() {
12535 return 0;
12536 }
12537
12538 /**
12539 * Amount by which to extend the right fading region. Called only when
12540 * {@link #isPaddingOffsetRequired()} returns true.
12541 *
12542 * @return The right padding offset in pixels.
12543 *
12544 * @see #isPaddingOffsetRequired()
12545 *
12546 * @since CURRENT
12547 */
12548 protected int getRightPaddingOffset() {
12549 return 0;
12550 }
12551
12552 /**
12553 * Amount by which to extend the top fading region. Called only when
12554 * {@link #isPaddingOffsetRequired()} returns true.
12555 *
12556 * @return The top padding offset in pixels.
12557 *
12558 * @see #isPaddingOffsetRequired()
12559 *
12560 * @since CURRENT
12561 */
12562 protected int getTopPaddingOffset() {
12563 return 0;
12564 }
12565
12566 /**
12567 * Amount by which to extend the bottom fading region. Called only when
12568 * {@link #isPaddingOffsetRequired()} returns true.
12569 *
12570 * @return The bottom padding offset in pixels.
12571 *
12572 * @see #isPaddingOffsetRequired()
12573 *
12574 * @since CURRENT
12575 */
12576 protected int getBottomPaddingOffset() {
12577 return 0;
12578 }
12579
12580 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012581 * @hide
12582 * @param offsetRequired
12583 */
12584 protected int getFadeTop(boolean offsetRequired) {
12585 int top = mPaddingTop;
12586 if (offsetRequired) top += getTopPaddingOffset();
12587 return top;
12588 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012589
Romain Guyf2fc4602011-07-19 15:20:03 -070012590 /**
12591 * @hide
12592 * @param offsetRequired
12593 */
12594 protected int getFadeHeight(boolean offsetRequired) {
12595 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012596 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012597 return mBottom - mTop - mPaddingBottom - padding;
12598 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012599
Romain Guyf2fc4602011-07-19 15:20:03 -070012600 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012601 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012602 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012603 *
Romain Guy2bffd262010-09-12 17:40:02 -070012604 * <p>Even if this method returns true, it does not mean that every call
12605 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12606 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012607 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012608 * window is hardware accelerated,
12609 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12610 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012611 *
Romain Guy2bffd262010-09-12 17:40:02 -070012612 * @return True if the view is attached to a window and the window is
12613 * hardware accelerated; false in any other case.
12614 */
12615 public boolean isHardwareAccelerated() {
12616 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12617 }
Joe Malin32736f02011-01-19 16:14:20 -080012618
Romain Guy2bffd262010-09-12 17:40:02 -070012619 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012620 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12621 * case of an active Animation being run on the view.
12622 */
12623 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12624 Animation a, boolean scalingRequired) {
12625 Transformation invalidationTransform;
12626 final int flags = parent.mGroupFlags;
12627 final boolean initialized = a.isInitialized();
12628 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012629 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012630 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12631 onAnimationStart();
12632 }
12633
12634 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12635 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12636 if (parent.mInvalidationTransformation == null) {
12637 parent.mInvalidationTransformation = new Transformation();
12638 }
12639 invalidationTransform = parent.mInvalidationTransformation;
12640 a.getTransformation(drawingTime, invalidationTransform, 1f);
12641 } else {
12642 invalidationTransform = parent.mChildTransformation;
12643 }
12644 if (more) {
12645 if (!a.willChangeBounds()) {
12646 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12647 parent.FLAG_OPTIMIZE_INVALIDATE) {
12648 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12649 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12650 // The child need to draw an animation, potentially offscreen, so
12651 // make sure we do not cancel invalidate requests
12652 parent.mPrivateFlags |= DRAW_ANIMATION;
12653 parent.invalidate(mLeft, mTop, mRight, mBottom);
12654 }
12655 } else {
12656 if (parent.mInvalidateRegion == null) {
12657 parent.mInvalidateRegion = new RectF();
12658 }
12659 final RectF region = parent.mInvalidateRegion;
12660 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12661 invalidationTransform);
12662
12663 // The child need to draw an animation, potentially offscreen, so
12664 // make sure we do not cancel invalidate requests
12665 parent.mPrivateFlags |= DRAW_ANIMATION;
12666
12667 final int left = mLeft + (int) region.left;
12668 final int top = mTop + (int) region.top;
12669 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12670 top + (int) (region.height() + .5f));
12671 }
12672 }
12673 return more;
12674 }
12675
Chet Haasea1cff502012-02-21 13:43:44 -080012676 /**
12677 * This method is called by getDisplayList() when a display list is created or re-rendered.
12678 * It sets or resets the current value of all properties on that display list (resetting is
12679 * necessary when a display list is being re-created, because we need to make sure that
12680 * previously-set transform values
12681 */
12682 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012683 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012684 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012685 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012686 if (mParent instanceof ViewGroup) {
12687 displayList.setClipChildren(
12688 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12689 }
Chet Haase9420abd2012-03-29 16:28:32 -070012690 float alpha = 1;
12691 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12692 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12693 ViewGroup parentVG = (ViewGroup) mParent;
12694 final boolean hasTransform =
12695 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12696 if (hasTransform) {
12697 Transformation transform = parentVG.mChildTransformation;
12698 final int transformType = parentVG.mChildTransformation.getTransformationType();
12699 if (transformType != Transformation.TYPE_IDENTITY) {
12700 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12701 alpha = transform.getAlpha();
12702 }
12703 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12704 displayList.setStaticMatrix(transform.getMatrix());
12705 }
12706 }
12707 }
Chet Haasea1cff502012-02-21 13:43:44 -080012708 }
12709 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012710 alpha *= mTransformationInfo.mAlpha;
12711 if (alpha < 1) {
12712 final int multipliedAlpha = (int) (255 * alpha);
12713 if (onSetAlpha(multipliedAlpha)) {
12714 alpha = 1;
12715 }
12716 }
12717 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012718 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12719 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12720 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12721 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012722 if (mTransformationInfo.mCamera == null) {
12723 mTransformationInfo.mCamera = new Camera();
12724 mTransformationInfo.matrix3D = new Matrix();
12725 }
12726 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012727 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12728 displayList.setPivotX(getPivotX());
12729 displayList.setPivotY(getPivotY());
12730 }
Chet Haase9420abd2012-03-29 16:28:32 -070012731 } else if (alpha < 1) {
12732 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012733 }
12734 }
12735 }
12736
Chet Haasebcca79a2012-02-14 08:45:14 -080012737 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012738 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12739 * This draw() method is an implementation detail and is not intended to be overridden or
12740 * to be called from anywhere else other than ViewGroup.drawChild().
12741 */
12742 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012743 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012744 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012745 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012746 final int flags = parent.mGroupFlags;
12747
Chet Haasea1cff502012-02-21 13:43:44 -080012748 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012749 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012750 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012751 }
12752
12753 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012754 boolean concatMatrix = false;
12755
12756 boolean scalingRequired = false;
12757 boolean caching;
12758 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12759
12760 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012761 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12762 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012763 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012764 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012765 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12766 } else {
12767 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12768 }
12769
Chet Haasebcca79a2012-02-14 08:45:14 -080012770 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012771 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012772 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012773 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012774 if (concatMatrix) {
12775 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
12776 }
Chet Haasebcca79a2012-02-14 08:45:14 -080012777 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070012778 } else {
12779 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
12780 mDisplayList != null) {
12781 // No longer animating: clear out old animation matrix
12782 mDisplayList.setAnimationMatrix(null);
12783 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
12784 }
12785 if (!useDisplayListProperties &&
12786 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12787 final boolean hasTransform =
12788 parent.getChildStaticTransformation(this, parent.mChildTransformation);
12789 if (hasTransform) {
12790 final int transformType = parent.mChildTransformation.getTransformationType();
12791 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12792 parent.mChildTransformation : null;
12793 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12794 }
Chet Haase64a48c12012-02-13 16:33:29 -080012795 }
12796 }
12797
12798 concatMatrix |= !childHasIdentityMatrix;
12799
12800 // Sets the flag as early as possible to allow draw() implementations
12801 // to call invalidate() successfully when doing animations
12802 mPrivateFlags |= DRAWN;
12803
Chet Haasebcca79a2012-02-14 08:45:14 -080012804 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012805 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12806 return more;
12807 }
12808
12809 if (hardwareAccelerated) {
12810 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12811 // retain the flag's value temporarily in the mRecreateDisplayList flag
12812 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12813 mPrivateFlags &= ~INVALIDATED;
12814 }
12815
12816 computeScroll();
12817
12818 final int sx = mScrollX;
12819 final int sy = mScrollY;
12820
12821 DisplayList displayList = null;
12822 Bitmap cache = null;
12823 boolean hasDisplayList = false;
12824 if (caching) {
12825 if (!hardwareAccelerated) {
12826 if (layerType != LAYER_TYPE_NONE) {
12827 layerType = LAYER_TYPE_SOFTWARE;
12828 buildDrawingCache(true);
12829 }
12830 cache = getDrawingCache(true);
12831 } else {
12832 switch (layerType) {
12833 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012834 if (useDisplayListProperties) {
12835 hasDisplayList = canHaveDisplayList();
12836 } else {
12837 buildDrawingCache(true);
12838 cache = getDrawingCache(true);
12839 }
Chet Haase64a48c12012-02-13 16:33:29 -080012840 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012841 case LAYER_TYPE_HARDWARE:
12842 if (useDisplayListProperties) {
12843 hasDisplayList = canHaveDisplayList();
12844 }
12845 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012846 case LAYER_TYPE_NONE:
12847 // Delay getting the display list until animation-driven alpha values are
12848 // set up and possibly passed on to the view
12849 hasDisplayList = canHaveDisplayList();
12850 break;
12851 }
12852 }
12853 }
Chet Haasea1cff502012-02-21 13:43:44 -080012854 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012855 if (useDisplayListProperties) {
12856 displayList = getDisplayList();
12857 if (!displayList.isValid()) {
12858 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12859 // to getDisplayList(), the display list will be marked invalid and we should not
12860 // try to use it again.
12861 displayList = null;
12862 hasDisplayList = false;
12863 useDisplayListProperties = false;
12864 }
12865 }
Chet Haase64a48c12012-02-13 16:33:29 -080012866
12867 final boolean hasNoCache = cache == null || hasDisplayList;
12868 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12869 layerType != LAYER_TYPE_HARDWARE;
12870
Chet Haasea1cff502012-02-21 13:43:44 -080012871 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012872 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012873 restoreTo = canvas.save();
12874 }
Chet Haase64a48c12012-02-13 16:33:29 -080012875 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012876 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012877 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012878 if (!useDisplayListProperties) {
12879 canvas.translate(mLeft, mTop);
12880 }
Chet Haase64a48c12012-02-13 16:33:29 -080012881 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012882 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012883 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012884 restoreTo = canvas.save();
12885 }
Chet Haase64a48c12012-02-13 16:33:29 -080012886 // mAttachInfo cannot be null, otherwise scalingRequired == false
12887 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12888 canvas.scale(scale, scale);
12889 }
12890 }
12891
Chet Haasea1cff502012-02-21 13:43:44 -080012892 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012893 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012894 if (transformToApply != null || !childHasIdentityMatrix) {
12895 int transX = 0;
12896 int transY = 0;
12897
12898 if (offsetForScroll) {
12899 transX = -sx;
12900 transY = -sy;
12901 }
12902
12903 if (transformToApply != null) {
12904 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012905 if (useDisplayListProperties) {
12906 displayList.setAnimationMatrix(transformToApply.getMatrix());
12907 } else {
12908 // Undo the scroll translation, apply the transformation matrix,
12909 // then redo the scroll translate to get the correct result.
12910 canvas.translate(-transX, -transY);
12911 canvas.concat(transformToApply.getMatrix());
12912 canvas.translate(transX, transY);
12913 }
Chet Haasea1cff502012-02-21 13:43:44 -080012914 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012915 }
12916
12917 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012918 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012919 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012920 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012921 }
12922 }
12923
Chet Haasea1cff502012-02-21 13:43:44 -080012924 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012925 canvas.translate(-transX, -transY);
12926 canvas.concat(getMatrix());
12927 canvas.translate(transX, transY);
12928 }
12929 }
12930
Chet Haase9420abd2012-03-29 16:28:32 -070012931 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012932 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012933 if (hasNoCache) {
12934 final int multipliedAlpha = (int) (255 * alpha);
12935 if (!onSetAlpha(multipliedAlpha)) {
12936 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012937 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012938 layerType != LAYER_TYPE_NONE) {
12939 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12940 }
Chet Haase9420abd2012-03-29 16:28:32 -070012941 if (useDisplayListProperties) {
12942 displayList.setAlpha(alpha * getAlpha());
12943 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012944 final int scrollX = hasDisplayList ? 0 : sx;
12945 final int scrollY = hasDisplayList ? 0 : sy;
12946 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12947 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012948 }
12949 } else {
12950 // Alpha is handled by the child directly, clobber the layer's alpha
12951 mPrivateFlags |= ALPHA_SET;
12952 }
12953 }
12954 }
12955 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12956 onSetAlpha(255);
12957 mPrivateFlags &= ~ALPHA_SET;
12958 }
12959
Chet Haasea1cff502012-02-21 13:43:44 -080012960 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12961 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012962 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012963 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012964 } else {
12965 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012966 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012967 } else {
12968 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12969 }
12970 }
12971 }
12972
Chet Haase9420abd2012-03-29 16:28:32 -070012973 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012974 displayList = getDisplayList();
12975 if (!displayList.isValid()) {
12976 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12977 // to getDisplayList(), the display list will be marked invalid and we should not
12978 // try to use it again.
12979 displayList = null;
12980 hasDisplayList = false;
12981 }
12982 }
12983
12984 if (hasNoCache) {
12985 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012986 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012987 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012988 if (layer != null && layer.isValid()) {
12989 mLayerPaint.setAlpha((int) (alpha * 255));
12990 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12991 layerRendered = true;
12992 } else {
12993 final int scrollX = hasDisplayList ? 0 : sx;
12994 final int scrollY = hasDisplayList ? 0 : sy;
12995 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012996 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012997 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12998 }
12999 }
13000
13001 if (!layerRendered) {
13002 if (!hasDisplayList) {
13003 // Fast path for layouts with no backgrounds
13004 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
13005 if (ViewDebug.TRACE_HIERARCHY) {
13006 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
13007 }
13008 mPrivateFlags &= ~DIRTY_MASK;
13009 dispatchDraw(canvas);
13010 } else {
13011 draw(canvas);
13012 }
13013 } else {
13014 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013015 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013016 }
13017 }
13018 } else if (cache != null) {
13019 mPrivateFlags &= ~DIRTY_MASK;
13020 Paint cachePaint;
13021
13022 if (layerType == LAYER_TYPE_NONE) {
13023 cachePaint = parent.mCachePaint;
13024 if (cachePaint == null) {
13025 cachePaint = new Paint();
13026 cachePaint.setDither(false);
13027 parent.mCachePaint = cachePaint;
13028 }
Chet Haase9420abd2012-03-29 16:28:32 -070013029 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013030 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013031 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13032 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013033 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013034 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013035 }
13036 } else {
13037 cachePaint = mLayerPaint;
13038 cachePaint.setAlpha((int) (alpha * 255));
13039 }
13040 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13041 }
13042
Chet Haasea1cff502012-02-21 13:43:44 -080013043 if (restoreTo >= 0) {
13044 canvas.restoreToCount(restoreTo);
13045 }
Chet Haase64a48c12012-02-13 16:33:29 -080013046
13047 if (a != null && !more) {
13048 if (!hardwareAccelerated && !a.getFillAfter()) {
13049 onSetAlpha(255);
13050 }
13051 parent.finishAnimatingView(this, a);
13052 }
13053
13054 if (more && hardwareAccelerated) {
13055 // invalidation is the trigger to recreate display lists, so if we're using
13056 // display lists to render, force an invalidate to allow the animation to
13057 // continue drawing another frame
13058 parent.invalidate(true);
13059 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13060 // alpha animations should cause the child to recreate its display list
13061 invalidate(true);
13062 }
13063 }
13064
13065 mRecreateDisplayList = false;
13066
13067 return more;
13068 }
13069
13070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013071 * Manually render this view (and all of its children) to the given Canvas.
13072 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013073 * called. When implementing a view, implement
13074 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13075 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013076 *
13077 * @param canvas The Canvas to which the View is rendered.
13078 */
13079 public void draw(Canvas canvas) {
13080 if (ViewDebug.TRACE_HIERARCHY) {
13081 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
13082 }
13083
Romain Guy5bcdff42009-05-14 21:27:18 -070013084 final int privateFlags = mPrivateFlags;
13085 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13086 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13087 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 /*
13090 * Draw traversal performs several drawing steps which must be executed
13091 * in the appropriate order:
13092 *
13093 * 1. Draw the background
13094 * 2. If necessary, save the canvas' layers to prepare for fading
13095 * 3. Draw view's content
13096 * 4. Draw children
13097 * 5. If necessary, draw the fading edges and restore layers
13098 * 6. Draw decorations (scrollbars for instance)
13099 */
13100
13101 // Step 1, draw the background, if needed
13102 int saveCount;
13103
Romain Guy24443ea2009-05-11 11:56:30 -070013104 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013105 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013106 if (background != null) {
13107 final int scrollX = mScrollX;
13108 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013109
Romain Guy24443ea2009-05-11 11:56:30 -070013110 if (mBackgroundSizeChanged) {
13111 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13112 mBackgroundSizeChanged = false;
13113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013114
Romain Guy24443ea2009-05-11 11:56:30 -070013115 if ((scrollX | scrollY) == 0) {
13116 background.draw(canvas);
13117 } else {
13118 canvas.translate(scrollX, scrollY);
13119 background.draw(canvas);
13120 canvas.translate(-scrollX, -scrollY);
13121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013122 }
13123 }
13124
13125 // skip step 2 & 5 if possible (common case)
13126 final int viewFlags = mViewFlags;
13127 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13128 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13129 if (!verticalEdges && !horizontalEdges) {
13130 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013131 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132
13133 // Step 4, draw the children
13134 dispatchDraw(canvas);
13135
13136 // Step 6, draw decorations (scrollbars)
13137 onDrawScrollBars(canvas);
13138
13139 // we're done...
13140 return;
13141 }
13142
13143 /*
13144 * Here we do the full fledged routine...
13145 * (this is an uncommon case where speed matters less,
13146 * this is why we repeat some of the tests that have been
13147 * done above)
13148 */
13149
13150 boolean drawTop = false;
13151 boolean drawBottom = false;
13152 boolean drawLeft = false;
13153 boolean drawRight = false;
13154
13155 float topFadeStrength = 0.0f;
13156 float bottomFadeStrength = 0.0f;
13157 float leftFadeStrength = 0.0f;
13158 float rightFadeStrength = 0.0f;
13159
13160 // Step 2, save the canvas' layers
13161 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013162
13163 final boolean offsetRequired = isPaddingOffsetRequired();
13164 if (offsetRequired) {
13165 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013166 }
13167
13168 int left = mScrollX + paddingLeft;
13169 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013170 int top = mScrollY + getFadeTop(offsetRequired);
13171 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013172
13173 if (offsetRequired) {
13174 right += getRightPaddingOffset();
13175 bottom += getBottomPaddingOffset();
13176 }
13177
13178 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013179 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013180 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013181
13182 // clip the fade length if top and bottom fades overlap
13183 // overlapping fades produce odd-looking artifacts
13184 if (verticalEdges && (top + length > bottom - length)) {
13185 length = (bottom - top) / 2;
13186 }
13187
13188 // also clip horizontal fades if necessary
13189 if (horizontalEdges && (left + length > right - length)) {
13190 length = (right - left) / 2;
13191 }
13192
13193 if (verticalEdges) {
13194 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013195 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013196 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013197 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013198 }
13199
13200 if (horizontalEdges) {
13201 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013202 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013203 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013204 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013205 }
13206
13207 saveCount = canvas.getSaveCount();
13208
13209 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013210 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13212
13213 if (drawTop) {
13214 canvas.saveLayer(left, top, right, top + length, null, flags);
13215 }
13216
13217 if (drawBottom) {
13218 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13219 }
13220
13221 if (drawLeft) {
13222 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13223 }
13224
13225 if (drawRight) {
13226 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13227 }
13228 } else {
13229 scrollabilityCache.setFadeColor(solidColor);
13230 }
13231
13232 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013233 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013234
13235 // Step 4, draw the children
13236 dispatchDraw(canvas);
13237
13238 // Step 5, draw the fade effect and restore layers
13239 final Paint p = scrollabilityCache.paint;
13240 final Matrix matrix = scrollabilityCache.matrix;
13241 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013242
13243 if (drawTop) {
13244 matrix.setScale(1, fadeHeight * topFadeStrength);
13245 matrix.postTranslate(left, top);
13246 fade.setLocalMatrix(matrix);
13247 canvas.drawRect(left, top, right, top + length, p);
13248 }
13249
13250 if (drawBottom) {
13251 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13252 matrix.postRotate(180);
13253 matrix.postTranslate(left, bottom);
13254 fade.setLocalMatrix(matrix);
13255 canvas.drawRect(left, bottom - length, right, bottom, p);
13256 }
13257
13258 if (drawLeft) {
13259 matrix.setScale(1, fadeHeight * leftFadeStrength);
13260 matrix.postRotate(-90);
13261 matrix.postTranslate(left, top);
13262 fade.setLocalMatrix(matrix);
13263 canvas.drawRect(left, top, left + length, bottom, p);
13264 }
13265
13266 if (drawRight) {
13267 matrix.setScale(1, fadeHeight * rightFadeStrength);
13268 matrix.postRotate(90);
13269 matrix.postTranslate(right, top);
13270 fade.setLocalMatrix(matrix);
13271 canvas.drawRect(right - length, top, right, bottom, p);
13272 }
13273
13274 canvas.restoreToCount(saveCount);
13275
13276 // Step 6, draw decorations (scrollbars)
13277 onDrawScrollBars(canvas);
13278 }
13279
13280 /**
13281 * Override this if your view is known to always be drawn on top of a solid color background,
13282 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13283 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13284 * should be set to 0xFF.
13285 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013286 * @see #setVerticalFadingEdgeEnabled(boolean)
13287 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013288 *
13289 * @return The known solid color background for this view, or 0 if the color may vary
13290 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013291 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013292 public int getSolidColor() {
13293 return 0;
13294 }
13295
13296 /**
13297 * Build a human readable string representation of the specified view flags.
13298 *
13299 * @param flags the view flags to convert to a string
13300 * @return a String representing the supplied flags
13301 */
13302 private static String printFlags(int flags) {
13303 String output = "";
13304 int numFlags = 0;
13305 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13306 output += "TAKES_FOCUS";
13307 numFlags++;
13308 }
13309
13310 switch (flags & VISIBILITY_MASK) {
13311 case INVISIBLE:
13312 if (numFlags > 0) {
13313 output += " ";
13314 }
13315 output += "INVISIBLE";
13316 // USELESS HERE numFlags++;
13317 break;
13318 case GONE:
13319 if (numFlags > 0) {
13320 output += " ";
13321 }
13322 output += "GONE";
13323 // USELESS HERE numFlags++;
13324 break;
13325 default:
13326 break;
13327 }
13328 return output;
13329 }
13330
13331 /**
13332 * Build a human readable string representation of the specified private
13333 * view flags.
13334 *
13335 * @param privateFlags the private view flags to convert to a string
13336 * @return a String representing the supplied flags
13337 */
13338 private static String printPrivateFlags(int privateFlags) {
13339 String output = "";
13340 int numFlags = 0;
13341
13342 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13343 output += "WANTS_FOCUS";
13344 numFlags++;
13345 }
13346
13347 if ((privateFlags & FOCUSED) == FOCUSED) {
13348 if (numFlags > 0) {
13349 output += " ";
13350 }
13351 output += "FOCUSED";
13352 numFlags++;
13353 }
13354
13355 if ((privateFlags & SELECTED) == SELECTED) {
13356 if (numFlags > 0) {
13357 output += " ";
13358 }
13359 output += "SELECTED";
13360 numFlags++;
13361 }
13362
13363 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13364 if (numFlags > 0) {
13365 output += " ";
13366 }
13367 output += "IS_ROOT_NAMESPACE";
13368 numFlags++;
13369 }
13370
13371 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13372 if (numFlags > 0) {
13373 output += " ";
13374 }
13375 output += "HAS_BOUNDS";
13376 numFlags++;
13377 }
13378
13379 if ((privateFlags & DRAWN) == DRAWN) {
13380 if (numFlags > 0) {
13381 output += " ";
13382 }
13383 output += "DRAWN";
13384 // USELESS HERE numFlags++;
13385 }
13386 return output;
13387 }
13388
13389 /**
13390 * <p>Indicates whether or not this view's layout will be requested during
13391 * the next hierarchy layout pass.</p>
13392 *
13393 * @return true if the layout will be forced during next layout pass
13394 */
13395 public boolean isLayoutRequested() {
13396 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13397 }
13398
13399 /**
13400 * Assign a size and position to a view and all of its
13401 * descendants
13402 *
13403 * <p>This is the second phase of the layout mechanism.
13404 * (The first is measuring). In this phase, each parent calls
13405 * layout on all of its children to position them.
13406 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013407 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 *
Chet Haase9c087442011-01-12 16:20:16 -080013409 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013410 * Derived classes with children should override
13411 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013412 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013413 *
13414 * @param l Left position, relative to parent
13415 * @param t Top position, relative to parent
13416 * @param r Right position, relative to parent
13417 * @param b Bottom position, relative to parent
13418 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013419 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013420 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013421 int oldL = mLeft;
13422 int oldT = mTop;
13423 int oldB = mBottom;
13424 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013425 boolean changed = setFrame(l, t, r, b);
13426 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13427 if (ViewDebug.TRACE_HIERARCHY) {
13428 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13429 }
13430
13431 onLayout(changed, l, t, r, b);
13432 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013433
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013434 ListenerInfo li = mListenerInfo;
13435 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013436 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013437 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013438 int numListeners = listenersCopy.size();
13439 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013440 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013441 }
13442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013443 }
13444 mPrivateFlags &= ~FORCE_LAYOUT;
13445 }
13446
13447 /**
13448 * Called from layout when this view should
13449 * assign a size and position to each of its children.
13450 *
13451 * Derived classes with children should override
13452 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013453 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454 * @param changed This is a new size or position for this view
13455 * @param left Left position, relative to parent
13456 * @param top Top position, relative to parent
13457 * @param right Right position, relative to parent
13458 * @param bottom Bottom position, relative to parent
13459 */
13460 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13461 }
13462
13463 /**
13464 * Assign a size and position to this view.
13465 *
13466 * This is called from layout.
13467 *
13468 * @param left Left position, relative to parent
13469 * @param top Top position, relative to parent
13470 * @param right Right position, relative to parent
13471 * @param bottom Bottom position, relative to parent
13472 * @return true if the new size and position are different than the
13473 * previous ones
13474 * {@hide}
13475 */
13476 protected boolean setFrame(int left, int top, int right, int bottom) {
13477 boolean changed = false;
13478
13479 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013480 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 + right + "," + bottom + ")");
13482 }
13483
13484 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13485 changed = true;
13486
13487 // Remember our drawn bit
13488 int drawn = mPrivateFlags & DRAWN;
13489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 int oldWidth = mRight - mLeft;
13491 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013492 int newWidth = right - left;
13493 int newHeight = bottom - top;
13494 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13495
13496 // Invalidate our old position
13497 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013498
13499 mLeft = left;
13500 mTop = top;
13501 mRight = right;
13502 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013503 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013504 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506
13507 mPrivateFlags |= HAS_BOUNDS;
13508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013509
Chet Haase75755e22011-07-18 17:48:25 -070013510 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013511 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13512 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013513 if (mTransformationInfo != null) {
13514 mTransformationInfo.mMatrixDirty = true;
13515 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013517 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13518 }
13519
13520 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13521 // If we are visible, force the DRAWN bit to on so that
13522 // this invalidate will go through (at least to our parent).
13523 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013524 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 // the DRAWN bit.
13526 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013527 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013528 // parent display list may need to be recreated based on a change in the bounds
13529 // of any child
13530 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013531 }
13532
13533 // Reset drawn bit to original value (invalidate turns it off)
13534 mPrivateFlags |= drawn;
13535
13536 mBackgroundSizeChanged = true;
13537 }
13538 return changed;
13539 }
13540
13541 /**
13542 * Finalize inflating a view from XML. This is called as the last phase
13543 * of inflation, after all child views have been added.
13544 *
13545 * <p>Even if the subclass overrides onFinishInflate, they should always be
13546 * sure to call the super method, so that we get called.
13547 */
13548 protected void onFinishInflate() {
13549 }
13550
13551 /**
13552 * Returns the resources associated with this view.
13553 *
13554 * @return Resources object.
13555 */
13556 public Resources getResources() {
13557 return mResources;
13558 }
13559
13560 /**
13561 * Invalidates the specified Drawable.
13562 *
13563 * @param drawable the drawable to invalidate
13564 */
13565 public void invalidateDrawable(Drawable drawable) {
13566 if (verifyDrawable(drawable)) {
13567 final Rect dirty = drawable.getBounds();
13568 final int scrollX = mScrollX;
13569 final int scrollY = mScrollY;
13570
13571 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13572 dirty.right + scrollX, dirty.bottom + scrollY);
13573 }
13574 }
13575
13576 /**
13577 * Schedules an action on a drawable to occur at a specified time.
13578 *
13579 * @param who the recipient of the action
13580 * @param what the action to run on the drawable
13581 * @param when the time at which the action must occur. Uses the
13582 * {@link SystemClock#uptimeMillis} timebase.
13583 */
13584 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013585 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013586 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013587 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013588 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13589 Choreographer.CALLBACK_ANIMATION, what, who,
13590 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013591 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013592 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013594 }
13595 }
13596
13597 /**
13598 * Cancels a scheduled action on a drawable.
13599 *
13600 * @param who the recipient of the action
13601 * @param what the action to cancel
13602 */
13603 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013604 if (verifyDrawable(who) && what != null) {
13605 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013606 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13607 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013608 } else {
13609 ViewRootImpl.getRunQueue().removeCallbacks(what);
13610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013611 }
13612 }
13613
13614 /**
13615 * Unschedule any events associated with the given Drawable. This can be
13616 * used when selecting a new Drawable into a view, so that the previous
13617 * one is completely unscheduled.
13618 *
13619 * @param who The Drawable to unschedule.
13620 *
13621 * @see #drawableStateChanged
13622 */
13623 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013624 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013625 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13626 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013627 }
13628 }
13629
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013630 /**
13631 * Return the layout direction of a given Drawable.
13632 *
13633 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013634 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013635 */
13636 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013637 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013638 }
13639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 /**
13641 * If your view subclass is displaying its own Drawable objects, it should
13642 * override this function and return true for any Drawable it is
13643 * displaying. This allows animations for those drawables to be
13644 * scheduled.
13645 *
13646 * <p>Be sure to call through to the super class when overriding this
13647 * function.
13648 *
13649 * @param who The Drawable to verify. Return true if it is one you are
13650 * displaying, else return the result of calling through to the
13651 * super class.
13652 *
13653 * @return boolean If true than the Drawable is being displayed in the
13654 * view; else false and it is not allowed to animate.
13655 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013656 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13657 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658 */
13659 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013660 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661 }
13662
13663 /**
13664 * This function is called whenever the state of the view changes in such
13665 * a way that it impacts the state of drawables being shown.
13666 *
13667 * <p>Be sure to call through to the superclass when overriding this
13668 * function.
13669 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013670 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 */
13672 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013673 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674 if (d != null && d.isStateful()) {
13675 d.setState(getDrawableState());
13676 }
13677 }
13678
13679 /**
13680 * Call this to force a view to update its drawable state. This will cause
13681 * drawableStateChanged to be called on this view. Views that are interested
13682 * in the new state should call getDrawableState.
13683 *
13684 * @see #drawableStateChanged
13685 * @see #getDrawableState
13686 */
13687 public void refreshDrawableState() {
13688 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13689 drawableStateChanged();
13690
13691 ViewParent parent = mParent;
13692 if (parent != null) {
13693 parent.childDrawableStateChanged(this);
13694 }
13695 }
13696
13697 /**
13698 * Return an array of resource IDs of the drawable states representing the
13699 * current state of the view.
13700 *
13701 * @return The current drawable state
13702 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013703 * @see Drawable#setState(int[])
13704 * @see #drawableStateChanged()
13705 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013706 */
13707 public final int[] getDrawableState() {
13708 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13709 return mDrawableState;
13710 } else {
13711 mDrawableState = onCreateDrawableState(0);
13712 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13713 return mDrawableState;
13714 }
13715 }
13716
13717 /**
13718 * Generate the new {@link android.graphics.drawable.Drawable} state for
13719 * this view. This is called by the view
13720 * system when the cached Drawable state is determined to be invalid. To
13721 * retrieve the current state, you should use {@link #getDrawableState}.
13722 *
13723 * @param extraSpace if non-zero, this is the number of extra entries you
13724 * would like in the returned array in which you can place your own
13725 * states.
13726 *
13727 * @return Returns an array holding the current {@link Drawable} state of
13728 * the view.
13729 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013730 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 */
13732 protected int[] onCreateDrawableState(int extraSpace) {
13733 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13734 mParent instanceof View) {
13735 return ((View) mParent).onCreateDrawableState(extraSpace);
13736 }
13737
13738 int[] drawableState;
13739
13740 int privateFlags = mPrivateFlags;
13741
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013742 int viewStateIndex = 0;
13743 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13744 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13745 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013746 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013747 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13748 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013749 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13750 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013751 // This is set if HW acceleration is requested, even if the current
13752 // process doesn't allow it. This is just to allow app preview
13753 // windows to better match their app.
13754 viewStateIndex |= VIEW_STATE_ACCELERATED;
13755 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013756 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757
Christopher Tate3d4bf172011-03-28 16:16:46 -070013758 final int privateFlags2 = mPrivateFlags2;
13759 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13760 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762 drawableState = VIEW_STATE_SETS[viewStateIndex];
13763
13764 //noinspection ConstantIfStatement
13765 if (false) {
13766 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13767 Log.i("View", toString()
13768 + " pressed=" + ((privateFlags & PRESSED) != 0)
13769 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13770 + " fo=" + hasFocus()
13771 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013772 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013773 + ": " + Arrays.toString(drawableState));
13774 }
13775
13776 if (extraSpace == 0) {
13777 return drawableState;
13778 }
13779
13780 final int[] fullState;
13781 if (drawableState != null) {
13782 fullState = new int[drawableState.length + extraSpace];
13783 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13784 } else {
13785 fullState = new int[extraSpace];
13786 }
13787
13788 return fullState;
13789 }
13790
13791 /**
13792 * Merge your own state values in <var>additionalState</var> into the base
13793 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013794 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013795 *
13796 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013797 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013798 * own additional state values.
13799 *
13800 * @param additionalState The additional state values you would like
13801 * added to <var>baseState</var>; this array is not modified.
13802 *
13803 * @return As a convenience, the <var>baseState</var> array you originally
13804 * passed into the function is returned.
13805 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013806 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013807 */
13808 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13809 final int N = baseState.length;
13810 int i = N - 1;
13811 while (i >= 0 && baseState[i] == 0) {
13812 i--;
13813 }
13814 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13815 return baseState;
13816 }
13817
13818 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013819 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13820 * on all Drawable objects associated with this view.
13821 */
13822 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013823 if (mBackground != null) {
13824 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013825 }
13826 }
13827
13828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013829 * Sets the background color for this view.
13830 * @param color the color of the background
13831 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013832 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013834 if (mBackground instanceof ColorDrawable) {
13835 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013836 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013837 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 }
13840
13841 /**
13842 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013843 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013844 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013845 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013846 * @attr ref android.R.styleable#View_background
13847 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013848 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013849 public void setBackgroundResource(int resid) {
13850 if (resid != 0 && resid == mBackgroundResource) {
13851 return;
13852 }
13853
13854 Drawable d= null;
13855 if (resid != 0) {
13856 d = mResources.getDrawable(resid);
13857 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013858 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859
13860 mBackgroundResource = resid;
13861 }
13862
13863 /**
13864 * Set the background to a given Drawable, or remove the background. If the
13865 * background has padding, this View's padding is set to the background's
13866 * padding. However, when a background is removed, this View's padding isn't
13867 * touched. If setting the padding is desired, please use
13868 * {@link #setPadding(int, int, int, int)}.
13869 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013870 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013871 * background
13872 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013873 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013874 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013875 setBackgroundDrawable(background);
13876 }
13877
13878 /**
13879 * @deprecated use {@link #setBackground(Drawable)} instead
13880 */
13881 @Deprecated
13882 public void setBackgroundDrawable(Drawable background) {
13883 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013884 return;
13885 }
13886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 boolean requestLayout = false;
13888
13889 mBackgroundResource = 0;
13890
13891 /*
13892 * Regardless of whether we're setting a new background or not, we want
13893 * to clear the previous drawable.
13894 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013895 if (mBackground != null) {
13896 mBackground.setCallback(null);
13897 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013898 }
13899
Philip Milne6c8ea062012-04-03 17:38:43 -070013900 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013901 Rect padding = sThreadLocal.get();
13902 if (padding == null) {
13903 padding = new Rect();
13904 sThreadLocal.set(padding);
13905 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013906 if (background.getPadding(padding)) {
13907 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013908 case LAYOUT_DIRECTION_RTL:
13909 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13910 break;
13911 case LAYOUT_DIRECTION_LTR:
13912 default:
13913 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013915 }
13916
13917 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13918 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013919 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13920 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 requestLayout = true;
13922 }
13923
Philip Milne6c8ea062012-04-03 17:38:43 -070013924 background.setCallback(this);
13925 if (background.isStateful()) {
13926 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013927 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013928 background.setVisible(getVisibility() == VISIBLE, false);
13929 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013930
13931 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13932 mPrivateFlags &= ~SKIP_DRAW;
13933 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13934 requestLayout = true;
13935 }
13936 } else {
13937 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013938 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939
13940 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13941 /*
13942 * This view ONLY drew the background before and we're removing
13943 * the background, so now it won't draw anything
13944 * (hence we SKIP_DRAW)
13945 */
13946 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13947 mPrivateFlags |= SKIP_DRAW;
13948 }
13949
13950 /*
13951 * When the background is set, we try to apply its padding to this
13952 * View. When the background is removed, we don't touch this View's
13953 * padding. This is noted in the Javadocs. Hence, we don't need to
13954 * requestLayout(), the invalidate() below is sufficient.
13955 */
13956
13957 // The old background's minimum size could have affected this
13958 // View's layout, so let's requestLayout
13959 requestLayout = true;
13960 }
13961
Romain Guy8f1344f52009-05-15 16:03:59 -070013962 computeOpaqueFlags();
13963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013964 if (requestLayout) {
13965 requestLayout();
13966 }
13967
13968 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013969 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013970 }
13971
13972 /**
13973 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013976 *
13977 * @see #setBackground(Drawable)
13978 *
13979 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980 */
13981 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013982 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 }
13984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013985 /**
13986 * Sets the padding. The view may add on the space required to display
13987 * the scrollbars, depending on the style and visibility of the scrollbars.
13988 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13989 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13990 * from the values set in this call.
13991 *
13992 * @attr ref android.R.styleable#View_padding
13993 * @attr ref android.R.styleable#View_paddingBottom
13994 * @attr ref android.R.styleable#View_paddingLeft
13995 * @attr ref android.R.styleable#View_paddingRight
13996 * @attr ref android.R.styleable#View_paddingTop
13997 * @param left the left padding in pixels
13998 * @param top the top padding in pixels
13999 * @param right the right padding in pixels
14000 * @param bottom the bottom padding in pixels
14001 */
14002 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014003 mUserPaddingStart = -1;
14004 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014005 mUserPaddingRelative = false;
14006
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014007 internalSetPadding(left, top, right, bottom);
14008 }
14009
14010 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014011 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 mUserPaddingRight = right;
14013 mUserPaddingBottom = bottom;
14014
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014015 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014016 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014017
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014018 // Common case is there are no scroll bars.
14019 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014020 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014021 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014022 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014023 switch (mVerticalScrollbarPosition) {
14024 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014025 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14026 left += offset;
14027 } else {
14028 right += offset;
14029 }
14030 break;
Adam Powell20232d02010-12-08 21:08:53 -080014031 case SCROLLBAR_POSITION_RIGHT:
14032 right += offset;
14033 break;
14034 case SCROLLBAR_POSITION_LEFT:
14035 left += offset;
14036 break;
14037 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014038 }
Adam Powell20232d02010-12-08 21:08:53 -080014039 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014040 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14041 ? 0 : getHorizontalScrollbarHeight();
14042 }
14043 }
Romain Guy8506ab42009-06-11 17:35:47 -070014044
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014045 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014046 changed = true;
14047 mPaddingLeft = left;
14048 }
14049 if (mPaddingTop != top) {
14050 changed = true;
14051 mPaddingTop = top;
14052 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014053 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014054 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014055 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014057 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014059 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060 }
14061
14062 if (changed) {
14063 requestLayout();
14064 }
14065 }
14066
14067 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014068 * Sets the relative padding. The view may add on the space required to display
14069 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014070 * from the values set in this call.
14071 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014072 * @param start the start padding in pixels
14073 * @param top the top padding in pixels
14074 * @param end the end padding in pixels
14075 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014076 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014077 */
14078 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014079 mUserPaddingStart = start;
14080 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014081 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014082
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014083 switch(getResolvedLayoutDirection()) {
14084 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014085 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014086 break;
14087 case LAYOUT_DIRECTION_LTR:
14088 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014089 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014090 }
14091 }
14092
14093 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014094 * Returns the top padding of this view.
14095 *
14096 * @return the top padding in pixels
14097 */
14098 public int getPaddingTop() {
14099 return mPaddingTop;
14100 }
14101
14102 /**
14103 * Returns the bottom padding of this view. If there are inset and enabled
14104 * scrollbars, this value may include the space required to display the
14105 * scrollbars as well.
14106 *
14107 * @return the bottom padding in pixels
14108 */
14109 public int getPaddingBottom() {
14110 return mPaddingBottom;
14111 }
14112
14113 /**
14114 * Returns the left padding of this view. If there are inset and enabled
14115 * scrollbars, this value may include the space required to display the
14116 * scrollbars as well.
14117 *
14118 * @return the left padding in pixels
14119 */
14120 public int getPaddingLeft() {
14121 return mPaddingLeft;
14122 }
14123
14124 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014125 * Returns the start padding of this view depending on its resolved layout direction.
14126 * If there are inset and enabled scrollbars, this value may include the space
14127 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014128 *
14129 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014130 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014131 */
14132 public int getPaddingStart() {
14133 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14134 mPaddingRight : mPaddingLeft;
14135 }
14136
14137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014138 * Returns the right padding of this view. If there are inset and enabled
14139 * scrollbars, this value may include the space required to display the
14140 * scrollbars as well.
14141 *
14142 * @return the right padding in pixels
14143 */
14144 public int getPaddingRight() {
14145 return mPaddingRight;
14146 }
14147
14148 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014149 * Returns the end padding of this view depending on its resolved layout direction.
14150 * If there are inset and enabled scrollbars, this value may include the space
14151 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014152 *
14153 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014154 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014155 */
14156 public int getPaddingEnd() {
14157 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14158 mPaddingLeft : mPaddingRight;
14159 }
14160
14161 /**
14162 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014163 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014164 *
14165 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014166 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014167 */
14168 public boolean isPaddingRelative() {
14169 return mUserPaddingRelative;
14170 }
14171
14172 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014173 * @hide
14174 */
Philip Milne7a23b492012-04-24 22:12:36 -070014175 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014176 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014177 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014178 }
14179 return mLayoutInsets;
14180 }
14181
14182 /**
14183 * @hide
14184 */
14185 public void setLayoutInsets(Insets layoutInsets) {
14186 mLayoutInsets = layoutInsets;
14187 }
14188
14189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014190 * Changes the selection state of this view. A view can be selected or not.
14191 * Note that selection is not the same as focus. Views are typically
14192 * selected in the context of an AdapterView like ListView or GridView;
14193 * the selected view is the view that is highlighted.
14194 *
14195 * @param selected true if the view must be selected, false otherwise
14196 */
14197 public void setSelected(boolean selected) {
14198 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14199 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014200 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014201 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 refreshDrawableState();
14203 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014204 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14205 notifyAccessibilityStateChanged();
14206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014207 }
14208 }
14209
14210 /**
14211 * Dispatch setSelected to all of this View's children.
14212 *
14213 * @see #setSelected(boolean)
14214 *
14215 * @param selected The new selected state
14216 */
14217 protected void dispatchSetSelected(boolean selected) {
14218 }
14219
14220 /**
14221 * Indicates the selection state of this view.
14222 *
14223 * @return true if the view is selected, false otherwise
14224 */
14225 @ViewDebug.ExportedProperty
14226 public boolean isSelected() {
14227 return (mPrivateFlags & SELECTED) != 0;
14228 }
14229
14230 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014231 * Changes the activated state of this view. A view can be activated or not.
14232 * Note that activation is not the same as selection. Selection is
14233 * a transient property, representing the view (hierarchy) the user is
14234 * currently interacting with. Activation is a longer-term state that the
14235 * user can move views in and out of. For example, in a list view with
14236 * single or multiple selection enabled, the views in the current selection
14237 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14238 * here.) The activated state is propagated down to children of the view it
14239 * is set on.
14240 *
14241 * @param activated true if the view must be activated, false otherwise
14242 */
14243 public void setActivated(boolean activated) {
14244 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14245 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014246 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014247 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014248 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014249 }
14250 }
14251
14252 /**
14253 * Dispatch setActivated to all of this View's children.
14254 *
14255 * @see #setActivated(boolean)
14256 *
14257 * @param activated The new activated state
14258 */
14259 protected void dispatchSetActivated(boolean activated) {
14260 }
14261
14262 /**
14263 * Indicates the activation state of this view.
14264 *
14265 * @return true if the view is activated, false otherwise
14266 */
14267 @ViewDebug.ExportedProperty
14268 public boolean isActivated() {
14269 return (mPrivateFlags & ACTIVATED) != 0;
14270 }
14271
14272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014273 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14274 * observer can be used to get notifications when global events, like
14275 * layout, happen.
14276 *
14277 * The returned ViewTreeObserver observer is not guaranteed to remain
14278 * valid for the lifetime of this View. If the caller of this method keeps
14279 * a long-lived reference to ViewTreeObserver, it should always check for
14280 * the return value of {@link ViewTreeObserver#isAlive()}.
14281 *
14282 * @return The ViewTreeObserver for this view's hierarchy.
14283 */
14284 public ViewTreeObserver getViewTreeObserver() {
14285 if (mAttachInfo != null) {
14286 return mAttachInfo.mTreeObserver;
14287 }
14288 if (mFloatingTreeObserver == null) {
14289 mFloatingTreeObserver = new ViewTreeObserver();
14290 }
14291 return mFloatingTreeObserver;
14292 }
14293
14294 /**
14295 * <p>Finds the topmost view in the current view hierarchy.</p>
14296 *
14297 * @return the topmost view containing this view
14298 */
14299 public View getRootView() {
14300 if (mAttachInfo != null) {
14301 final View v = mAttachInfo.mRootView;
14302 if (v != null) {
14303 return v;
14304 }
14305 }
Romain Guy8506ab42009-06-11 17:35:47 -070014306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014307 View parent = this;
14308
14309 while (parent.mParent != null && parent.mParent instanceof View) {
14310 parent = (View) parent.mParent;
14311 }
14312
14313 return parent;
14314 }
14315
14316 /**
14317 * <p>Computes the coordinates of this view on the screen. The argument
14318 * must be an array of two integers. After the method returns, the array
14319 * contains the x and y location in that order.</p>
14320 *
14321 * @param location an array of two integers in which to hold the coordinates
14322 */
14323 public void getLocationOnScreen(int[] location) {
14324 getLocationInWindow(location);
14325
14326 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014327 if (info != null) {
14328 location[0] += info.mWindowLeft;
14329 location[1] += info.mWindowTop;
14330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014331 }
14332
14333 /**
14334 * <p>Computes the coordinates of this view in its window. The argument
14335 * must be an array of two integers. After the method returns, the array
14336 * contains the x and y location in that order.</p>
14337 *
14338 * @param location an array of two integers in which to hold the coordinates
14339 */
14340 public void getLocationInWindow(int[] location) {
14341 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014342 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 }
14344
Gilles Debunne6583ce52011-12-06 18:09:02 -080014345 if (mAttachInfo == null) {
14346 // When the view is not attached to a window, this method does not make sense
14347 location[0] = location[1] = 0;
14348 return;
14349 }
14350
Gilles Debunnecea45132011-11-24 02:19:27 +010014351 float[] position = mAttachInfo.mTmpTransformLocation;
14352 position[0] = position[1] = 0.0f;
14353
14354 if (!hasIdentityMatrix()) {
14355 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014357
Gilles Debunnecea45132011-11-24 02:19:27 +010014358 position[0] += mLeft;
14359 position[1] += mTop;
14360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014361 ViewParent viewParent = mParent;
14362 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014363 final View view = (View) viewParent;
14364
14365 position[0] -= view.mScrollX;
14366 position[1] -= view.mScrollY;
14367
14368 if (!view.hasIdentityMatrix()) {
14369 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014370 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014371
14372 position[0] += view.mLeft;
14373 position[1] += view.mTop;
14374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014375 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014376 }
Romain Guy8506ab42009-06-11 17:35:47 -070014377
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014378 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014380 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14381 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014382 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014383
14384 location[0] = (int) (position[0] + 0.5f);
14385 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014386 }
14387
14388 /**
14389 * {@hide}
14390 * @param id the id of the view to be found
14391 * @return the view of the specified id, null if cannot be found
14392 */
14393 protected View findViewTraversal(int id) {
14394 if (id == mID) {
14395 return this;
14396 }
14397 return null;
14398 }
14399
14400 /**
14401 * {@hide}
14402 * @param tag the tag of the view to be found
14403 * @return the view of specified tag, null if cannot be found
14404 */
14405 protected View findViewWithTagTraversal(Object tag) {
14406 if (tag != null && tag.equals(mTag)) {
14407 return this;
14408 }
14409 return null;
14410 }
14411
14412 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014413 * {@hide}
14414 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014415 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014416 * @return The first view that matches the predicate or null.
14417 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014418 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014419 if (predicate.apply(this)) {
14420 return this;
14421 }
14422 return null;
14423 }
14424
14425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014426 * Look for a child view with the given id. If this view has the given
14427 * id, return this view.
14428 *
14429 * @param id The id to search for.
14430 * @return The view that has the given id in the hierarchy or null
14431 */
14432 public final View findViewById(int id) {
14433 if (id < 0) {
14434 return null;
14435 }
14436 return findViewTraversal(id);
14437 }
14438
14439 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014440 * Finds a view by its unuque and stable accessibility id.
14441 *
14442 * @param accessibilityId The searched accessibility id.
14443 * @return The found view.
14444 */
14445 final View findViewByAccessibilityId(int accessibilityId) {
14446 if (accessibilityId < 0) {
14447 return null;
14448 }
14449 return findViewByAccessibilityIdTraversal(accessibilityId);
14450 }
14451
14452 /**
14453 * Performs the traversal to find a view by its unuque and stable accessibility id.
14454 *
14455 * <strong>Note:</strong>This method does not stop at the root namespace
14456 * boundary since the user can touch the screen at an arbitrary location
14457 * potentially crossing the root namespace bounday which will send an
14458 * accessibility event to accessibility services and they should be able
14459 * to obtain the event source. Also accessibility ids are guaranteed to be
14460 * unique in the window.
14461 *
14462 * @param accessibilityId The accessibility id.
14463 * @return The found view.
14464 */
14465 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14466 if (getAccessibilityViewId() == accessibilityId) {
14467 return this;
14468 }
14469 return null;
14470 }
14471
14472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014473 * Look for a child view with the given tag. If this view has the given
14474 * tag, return this view.
14475 *
14476 * @param tag The tag to search for, using "tag.equals(getTag())".
14477 * @return The View that has the given tag in the hierarchy or null
14478 */
14479 public final View findViewWithTag(Object tag) {
14480 if (tag == null) {
14481 return null;
14482 }
14483 return findViewWithTagTraversal(tag);
14484 }
14485
14486 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014487 * {@hide}
14488 * Look for a child view that matches the specified predicate.
14489 * If this view matches the predicate, return this view.
14490 *
14491 * @param predicate The predicate to evaluate.
14492 * @return The first view that matches the predicate or null.
14493 */
14494 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014495 return findViewByPredicateTraversal(predicate, null);
14496 }
14497
14498 /**
14499 * {@hide}
14500 * Look for a child view that matches the specified predicate,
14501 * starting with the specified view and its descendents and then
14502 * recusively searching the ancestors and siblings of that view
14503 * until this view is reached.
14504 *
14505 * This method is useful in cases where the predicate does not match
14506 * a single unique view (perhaps multiple views use the same id)
14507 * and we are trying to find the view that is "closest" in scope to the
14508 * starting view.
14509 *
14510 * @param start The view to start from.
14511 * @param predicate The predicate to evaluate.
14512 * @return The first view that matches the predicate or null.
14513 */
14514 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14515 View childToSkip = null;
14516 for (;;) {
14517 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14518 if (view != null || start == this) {
14519 return view;
14520 }
14521
14522 ViewParent parent = start.getParent();
14523 if (parent == null || !(parent instanceof View)) {
14524 return null;
14525 }
14526
14527 childToSkip = start;
14528 start = (View) parent;
14529 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014530 }
14531
14532 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 * Sets the identifier for this view. The identifier does not have to be
14534 * unique in this view's hierarchy. The identifier should be a positive
14535 * number.
14536 *
14537 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014538 * @see #getId()
14539 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540 *
14541 * @param id a number used to identify the view
14542 *
14543 * @attr ref android.R.styleable#View_id
14544 */
14545 public void setId(int id) {
14546 mID = id;
14547 }
14548
14549 /**
14550 * {@hide}
14551 *
14552 * @param isRoot true if the view belongs to the root namespace, false
14553 * otherwise
14554 */
14555 public void setIsRootNamespace(boolean isRoot) {
14556 if (isRoot) {
14557 mPrivateFlags |= IS_ROOT_NAMESPACE;
14558 } else {
14559 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14560 }
14561 }
14562
14563 /**
14564 * {@hide}
14565 *
14566 * @return true if the view belongs to the root namespace, false otherwise
14567 */
14568 public boolean isRootNamespace() {
14569 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14570 }
14571
14572 /**
14573 * Returns this view's identifier.
14574 *
14575 * @return a positive integer used to identify the view or {@link #NO_ID}
14576 * if the view has no ID
14577 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014578 * @see #setId(int)
14579 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014580 * @attr ref android.R.styleable#View_id
14581 */
14582 @ViewDebug.CapturedViewProperty
14583 public int getId() {
14584 return mID;
14585 }
14586
14587 /**
14588 * Returns this view's tag.
14589 *
14590 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014591 *
14592 * @see #setTag(Object)
14593 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014594 */
14595 @ViewDebug.ExportedProperty
14596 public Object getTag() {
14597 return mTag;
14598 }
14599
14600 /**
14601 * Sets the tag associated with this view. A tag can be used to mark
14602 * a view in its hierarchy and does not have to be unique within the
14603 * hierarchy. Tags can also be used to store data within a view without
14604 * resorting to another data structure.
14605 *
14606 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014607 *
14608 * @see #getTag()
14609 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014610 */
14611 public void setTag(final Object tag) {
14612 mTag = tag;
14613 }
14614
14615 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014616 * Returns the tag associated with this view and the specified key.
14617 *
14618 * @param key The key identifying the tag
14619 *
14620 * @return the Object stored in this view as a tag
14621 *
14622 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014623 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014624 */
14625 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014626 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014627 return null;
14628 }
14629
14630 /**
14631 * Sets a tag associated with this view and a key. A tag can be used
14632 * to mark a view in its hierarchy and does not have to be unique within
14633 * the hierarchy. Tags can also be used to store data within a view
14634 * without resorting to another data structure.
14635 *
14636 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014637 * application to ensure it is unique (see the <a
14638 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14639 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014640 * the Android framework or not associated with any package will cause
14641 * an {@link IllegalArgumentException} to be thrown.
14642 *
14643 * @param key The key identifying the tag
14644 * @param tag An Object to tag the view with
14645 *
14646 * @throws IllegalArgumentException If they specified key is not valid
14647 *
14648 * @see #setTag(Object)
14649 * @see #getTag(int)
14650 */
14651 public void setTag(int key, final Object tag) {
14652 // If the package id is 0x00 or 0x01, it's either an undefined package
14653 // or a framework id
14654 if ((key >>> 24) < 2) {
14655 throw new IllegalArgumentException("The key must be an application-specific "
14656 + "resource id.");
14657 }
14658
Adam Powell2b2f6d62011-09-23 11:15:39 -070014659 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014660 }
14661
14662 /**
14663 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14664 * framework id.
14665 *
14666 * @hide
14667 */
14668 public void setTagInternal(int key, Object tag) {
14669 if ((key >>> 24) != 0x1) {
14670 throw new IllegalArgumentException("The key must be a framework-specific "
14671 + "resource id.");
14672 }
14673
Adam Powell2b2f6d62011-09-23 11:15:39 -070014674 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014675 }
14676
Adam Powell2b2f6d62011-09-23 11:15:39 -070014677 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014678 if (mKeyedTags == null) {
14679 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014680 }
14681
Adam Powell7db82ac2011-09-22 19:44:04 -070014682 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014683 }
14684
14685 /**
Romain Guy13922e02009-05-12 17:56:14 -070014686 * @param consistency The type of consistency. See ViewDebug for more information.
14687 *
14688 * @hide
14689 */
14690 protected boolean dispatchConsistencyCheck(int consistency) {
14691 return onConsistencyCheck(consistency);
14692 }
14693
14694 /**
14695 * Method that subclasses should implement to check their consistency. The type of
14696 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014697 *
Romain Guy13922e02009-05-12 17:56:14 -070014698 * @param consistency The type of consistency. See ViewDebug for more information.
14699 *
14700 * @throws IllegalStateException if the view is in an inconsistent state.
14701 *
14702 * @hide
14703 */
14704 protected boolean onConsistencyCheck(int consistency) {
14705 boolean result = true;
14706
14707 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14708 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14709
14710 if (checkLayout) {
14711 if (getParent() == null) {
14712 result = false;
14713 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14714 "View " + this + " does not have a parent.");
14715 }
14716
14717 if (mAttachInfo == null) {
14718 result = false;
14719 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14720 "View " + this + " is not attached to a window.");
14721 }
14722 }
14723
14724 if (checkDrawing) {
14725 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14726 // from their draw() method
14727
14728 if ((mPrivateFlags & DRAWN) != DRAWN &&
14729 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14730 result = false;
14731 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14732 "View " + this + " was invalidated but its drawing cache is valid.");
14733 }
14734 }
14735
14736 return result;
14737 }
14738
14739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014740 * Prints information about this view in the log output, with the tag
14741 * {@link #VIEW_LOG_TAG}.
14742 *
14743 * @hide
14744 */
14745 public void debug() {
14746 debug(0);
14747 }
14748
14749 /**
14750 * Prints information about this view in the log output, with the tag
14751 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14752 * indentation defined by the <code>depth</code>.
14753 *
14754 * @param depth the indentation level
14755 *
14756 * @hide
14757 */
14758 protected void debug(int depth) {
14759 String output = debugIndent(depth - 1);
14760
14761 output += "+ " + this;
14762 int id = getId();
14763 if (id != -1) {
14764 output += " (id=" + id + ")";
14765 }
14766 Object tag = getTag();
14767 if (tag != null) {
14768 output += " (tag=" + tag + ")";
14769 }
14770 Log.d(VIEW_LOG_TAG, output);
14771
14772 if ((mPrivateFlags & FOCUSED) != 0) {
14773 output = debugIndent(depth) + " FOCUSED";
14774 Log.d(VIEW_LOG_TAG, output);
14775 }
14776
14777 output = debugIndent(depth);
14778 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14779 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14780 + "} ";
14781 Log.d(VIEW_LOG_TAG, output);
14782
14783 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14784 || mPaddingBottom != 0) {
14785 output = debugIndent(depth);
14786 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14787 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14788 Log.d(VIEW_LOG_TAG, output);
14789 }
14790
14791 output = debugIndent(depth);
14792 output += "mMeasureWidth=" + mMeasuredWidth +
14793 " mMeasureHeight=" + mMeasuredHeight;
14794 Log.d(VIEW_LOG_TAG, output);
14795
14796 output = debugIndent(depth);
14797 if (mLayoutParams == null) {
14798 output += "BAD! no layout params";
14799 } else {
14800 output = mLayoutParams.debug(output);
14801 }
14802 Log.d(VIEW_LOG_TAG, output);
14803
14804 output = debugIndent(depth);
14805 output += "flags={";
14806 output += View.printFlags(mViewFlags);
14807 output += "}";
14808 Log.d(VIEW_LOG_TAG, output);
14809
14810 output = debugIndent(depth);
14811 output += "privateFlags={";
14812 output += View.printPrivateFlags(mPrivateFlags);
14813 output += "}";
14814 Log.d(VIEW_LOG_TAG, output);
14815 }
14816
14817 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014818 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014819 *
14820 * @param depth the indentation level
14821 * @return a String containing (depth * 2 + 3) * 2 white spaces
14822 *
14823 * @hide
14824 */
14825 protected static String debugIndent(int depth) {
14826 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14827 for (int i = 0; i < (depth * 2) + 3; i++) {
14828 spaces.append(' ').append(' ');
14829 }
14830 return spaces.toString();
14831 }
14832
14833 /**
14834 * <p>Return the offset of the widget's text baseline from the widget's top
14835 * boundary. If this widget does not support baseline alignment, this
14836 * method returns -1. </p>
14837 *
14838 * @return the offset of the baseline within the widget's bounds or -1
14839 * if baseline alignment is not supported
14840 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014841 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014842 public int getBaseline() {
14843 return -1;
14844 }
14845
14846 /**
14847 * Call this when something has changed which has invalidated the
14848 * layout of this view. This will schedule a layout pass of the view
14849 * tree.
14850 */
14851 public void requestLayout() {
14852 if (ViewDebug.TRACE_HIERARCHY) {
14853 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14854 }
14855
14856 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014857 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014858
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014859 if (mLayoutParams != null) {
14860 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14861 }
14862
14863 if (mParent != null && !mParent.isLayoutRequested()) {
14864 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014865 }
14866 }
14867
14868 /**
14869 * Forces this view to be laid out during the next layout pass.
14870 * This method does not call requestLayout() or forceLayout()
14871 * on the parent.
14872 */
14873 public void forceLayout() {
14874 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014875 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014876 }
14877
14878 /**
14879 * <p>
14880 * This is called to find out how big a view should be. The parent
14881 * supplies constraint information in the width and height parameters.
14882 * </p>
14883 *
14884 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014885 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014886 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014887 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888 * </p>
14889 *
14890 *
14891 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14892 * parent
14893 * @param heightMeasureSpec Vertical space requirements as imposed by the
14894 * parent
14895 *
14896 * @see #onMeasure(int, int)
14897 */
14898 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14899 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14900 widthMeasureSpec != mOldWidthMeasureSpec ||
14901 heightMeasureSpec != mOldHeightMeasureSpec) {
14902
14903 // first clears the measured dimension flag
14904 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14905
14906 if (ViewDebug.TRACE_HIERARCHY) {
14907 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14908 }
14909
14910 // measure ourselves, this should set the measured dimension flag back
14911 onMeasure(widthMeasureSpec, heightMeasureSpec);
14912
14913 // flag not set, setMeasuredDimension() was not invoked, we raise
14914 // an exception to warn the developer
14915 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14916 throw new IllegalStateException("onMeasure() did not set the"
14917 + " measured dimension by calling"
14918 + " setMeasuredDimension()");
14919 }
14920
14921 mPrivateFlags |= LAYOUT_REQUIRED;
14922 }
14923
14924 mOldWidthMeasureSpec = widthMeasureSpec;
14925 mOldHeightMeasureSpec = heightMeasureSpec;
14926 }
14927
14928 /**
14929 * <p>
14930 * Measure the view and its content to determine the measured width and the
14931 * measured height. This method is invoked by {@link #measure(int, int)} and
14932 * should be overriden by subclasses to provide accurate and efficient
14933 * measurement of their contents.
14934 * </p>
14935 *
14936 * <p>
14937 * <strong>CONTRACT:</strong> When overriding this method, you
14938 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14939 * measured width and height of this view. Failure to do so will trigger an
14940 * <code>IllegalStateException</code>, thrown by
14941 * {@link #measure(int, int)}. Calling the superclass'
14942 * {@link #onMeasure(int, int)} is a valid use.
14943 * </p>
14944 *
14945 * <p>
14946 * The base class implementation of measure defaults to the background size,
14947 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14948 * override {@link #onMeasure(int, int)} to provide better measurements of
14949 * their content.
14950 * </p>
14951 *
14952 * <p>
14953 * If this method is overridden, it is the subclass's responsibility to make
14954 * sure the measured height and width are at least the view's minimum height
14955 * and width ({@link #getSuggestedMinimumHeight()} and
14956 * {@link #getSuggestedMinimumWidth()}).
14957 * </p>
14958 *
14959 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14960 * The requirements are encoded with
14961 * {@link android.view.View.MeasureSpec}.
14962 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14963 * The requirements are encoded with
14964 * {@link android.view.View.MeasureSpec}.
14965 *
14966 * @see #getMeasuredWidth()
14967 * @see #getMeasuredHeight()
14968 * @see #setMeasuredDimension(int, int)
14969 * @see #getSuggestedMinimumHeight()
14970 * @see #getSuggestedMinimumWidth()
14971 * @see android.view.View.MeasureSpec#getMode(int)
14972 * @see android.view.View.MeasureSpec#getSize(int)
14973 */
14974 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14975 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14976 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14977 }
14978
14979 /**
14980 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14981 * measured width and measured height. Failing to do so will trigger an
14982 * exception at measurement time.</p>
14983 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014984 * @param measuredWidth The measured width of this view. May be a complex
14985 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14986 * {@link #MEASURED_STATE_TOO_SMALL}.
14987 * @param measuredHeight The measured height of this view. May be a complex
14988 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14989 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014990 */
14991 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14992 mMeasuredWidth = measuredWidth;
14993 mMeasuredHeight = measuredHeight;
14994
14995 mPrivateFlags |= MEASURED_DIMENSION_SET;
14996 }
14997
14998 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014999 * Merge two states as returned by {@link #getMeasuredState()}.
15000 * @param curState The current state as returned from a view or the result
15001 * of combining multiple views.
15002 * @param newState The new view state to combine.
15003 * @return Returns a new integer reflecting the combination of the two
15004 * states.
15005 */
15006 public static int combineMeasuredStates(int curState, int newState) {
15007 return curState | newState;
15008 }
15009
15010 /**
15011 * Version of {@link #resolveSizeAndState(int, int, int)}
15012 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15013 */
15014 public static int resolveSize(int size, int measureSpec) {
15015 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15016 }
15017
15018 /**
15019 * Utility to reconcile a desired size and state, with constraints imposed
15020 * by a MeasureSpec. Will take the desired size, unless a different size
15021 * is imposed by the constraints. The returned value is a compound integer,
15022 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15023 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15024 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 *
15026 * @param size How big the view wants to be
15027 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015028 * @return Size information bit mask as defined by
15029 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015030 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015031 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015032 int result = size;
15033 int specMode = MeasureSpec.getMode(measureSpec);
15034 int specSize = MeasureSpec.getSize(measureSpec);
15035 switch (specMode) {
15036 case MeasureSpec.UNSPECIFIED:
15037 result = size;
15038 break;
15039 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015040 if (specSize < size) {
15041 result = specSize | MEASURED_STATE_TOO_SMALL;
15042 } else {
15043 result = size;
15044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015045 break;
15046 case MeasureSpec.EXACTLY:
15047 result = specSize;
15048 break;
15049 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015050 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015051 }
15052
15053 /**
15054 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015055 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015056 * by the MeasureSpec.
15057 *
15058 * @param size Default size for this view
15059 * @param measureSpec Constraints imposed by the parent
15060 * @return The size this view should be.
15061 */
15062 public static int getDefaultSize(int size, int measureSpec) {
15063 int result = size;
15064 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015065 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015066
15067 switch (specMode) {
15068 case MeasureSpec.UNSPECIFIED:
15069 result = size;
15070 break;
15071 case MeasureSpec.AT_MOST:
15072 case MeasureSpec.EXACTLY:
15073 result = specSize;
15074 break;
15075 }
15076 return result;
15077 }
15078
15079 /**
15080 * Returns the suggested minimum height that the view should use. This
15081 * returns the maximum of the view's minimum height
15082 * and the background's minimum height
15083 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15084 * <p>
15085 * When being used in {@link #onMeasure(int, int)}, the caller should still
15086 * ensure the returned height is within the requirements of the parent.
15087 *
15088 * @return The suggested minimum height of the view.
15089 */
15090 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015091 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015093 }
15094
15095 /**
15096 * Returns the suggested minimum width that the view should use. This
15097 * returns the maximum of the view's minimum width)
15098 * and the background's minimum width
15099 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15100 * <p>
15101 * When being used in {@link #onMeasure(int, int)}, the caller should still
15102 * ensure the returned width is within the requirements of the parent.
15103 *
15104 * @return The suggested minimum width of the view.
15105 */
15106 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015107 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015109
Philip Milne6c8ea062012-04-03 17:38:43 -070015110 /**
15111 * Returns the minimum height of the view.
15112 *
15113 * @return the minimum height the view will try to be.
15114 *
15115 * @see #setMinimumHeight(int)
15116 *
15117 * @attr ref android.R.styleable#View_minHeight
15118 */
15119 public int getMinimumHeight() {
15120 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015121 }
15122
15123 /**
15124 * Sets the minimum height of the view. It is not guaranteed the view will
15125 * be able to achieve this minimum height (for example, if its parent layout
15126 * constrains it with less available height).
15127 *
15128 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015129 *
15130 * @see #getMinimumHeight()
15131 *
15132 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015133 */
15134 public void setMinimumHeight(int minHeight) {
15135 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015136 requestLayout();
15137 }
15138
15139 /**
15140 * Returns the minimum width of the view.
15141 *
15142 * @return the minimum width the view will try to be.
15143 *
15144 * @see #setMinimumWidth(int)
15145 *
15146 * @attr ref android.R.styleable#View_minWidth
15147 */
15148 public int getMinimumWidth() {
15149 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015150 }
15151
15152 /**
15153 * Sets the minimum width of the view. It is not guaranteed the view will
15154 * be able to achieve this minimum width (for example, if its parent layout
15155 * constrains it with less available width).
15156 *
15157 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015158 *
15159 * @see #getMinimumWidth()
15160 *
15161 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015162 */
15163 public void setMinimumWidth(int minWidth) {
15164 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015165 requestLayout();
15166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015167 }
15168
15169 /**
15170 * Get the animation currently associated with this view.
15171 *
15172 * @return The animation that is currently playing or
15173 * scheduled to play for this view.
15174 */
15175 public Animation getAnimation() {
15176 return mCurrentAnimation;
15177 }
15178
15179 /**
15180 * Start the specified animation now.
15181 *
15182 * @param animation the animation to start now
15183 */
15184 public void startAnimation(Animation animation) {
15185 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15186 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015187 invalidateParentCaches();
15188 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015189 }
15190
15191 /**
15192 * Cancels any animations for this view.
15193 */
15194 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015195 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015196 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015198 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015199 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015200 }
15201
15202 /**
15203 * Sets the next animation to play for this view.
15204 * If you want the animation to play immediately, use
15205 * startAnimation. This method provides allows fine-grained
15206 * control over the start time and invalidation, but you
15207 * must make sure that 1) the animation has a start time set, and
15208 * 2) the view will be invalidated when the animation is supposed to
15209 * start.
15210 *
15211 * @param animation The next animation, or null.
15212 */
15213 public void setAnimation(Animation animation) {
15214 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015216 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015217 // If the screen is off assume the animation start time is now instead of
15218 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15219 // would cause the animation to start when the screen turns back on
15220 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15221 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15222 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015224 animation.reset();
15225 }
15226 }
15227
15228 /**
15229 * Invoked by a parent ViewGroup to notify the start of the animation
15230 * currently associated with this view. If you override this method,
15231 * always call super.onAnimationStart();
15232 *
15233 * @see #setAnimation(android.view.animation.Animation)
15234 * @see #getAnimation()
15235 */
15236 protected void onAnimationStart() {
15237 mPrivateFlags |= ANIMATION_STARTED;
15238 }
15239
15240 /**
15241 * Invoked by a parent ViewGroup to notify the end of the animation
15242 * currently associated with this view. If you override this method,
15243 * always call super.onAnimationEnd();
15244 *
15245 * @see #setAnimation(android.view.animation.Animation)
15246 * @see #getAnimation()
15247 */
15248 protected void onAnimationEnd() {
15249 mPrivateFlags &= ~ANIMATION_STARTED;
15250 }
15251
15252 /**
15253 * Invoked if there is a Transform that involves alpha. Subclass that can
15254 * draw themselves with the specified alpha should return true, and then
15255 * respect that alpha when their onDraw() is called. If this returns false
15256 * then the view may be redirected to draw into an offscreen buffer to
15257 * fulfill the request, which will look fine, but may be slower than if the
15258 * subclass handles it internally. The default implementation returns false.
15259 *
15260 * @param alpha The alpha (0..255) to apply to the view's drawing
15261 * @return true if the view can draw with the specified alpha.
15262 */
15263 protected boolean onSetAlpha(int alpha) {
15264 return false;
15265 }
15266
15267 /**
15268 * This is used by the RootView to perform an optimization when
15269 * the view hierarchy contains one or several SurfaceView.
15270 * SurfaceView is always considered transparent, but its children are not,
15271 * therefore all View objects remove themselves from the global transparent
15272 * region (passed as a parameter to this function).
15273 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015274 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015275 *
15276 * @return Returns true if the effective visibility of the view at this
15277 * point is opaque, regardless of the transparent region; returns false
15278 * if it is possible for underlying windows to be seen behind the view.
15279 *
15280 * {@hide}
15281 */
15282 public boolean gatherTransparentRegion(Region region) {
15283 final AttachInfo attachInfo = mAttachInfo;
15284 if (region != null && attachInfo != null) {
15285 final int pflags = mPrivateFlags;
15286 if ((pflags & SKIP_DRAW) == 0) {
15287 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15288 // remove it from the transparent region.
15289 final int[] location = attachInfo.mTransparentLocation;
15290 getLocationInWindow(location);
15291 region.op(location[0], location[1], location[0] + mRight - mLeft,
15292 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015293 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015294 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15295 // exists, so we remove the background drawable's non-transparent
15296 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015297 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015298 }
15299 }
15300 return true;
15301 }
15302
15303 /**
15304 * Play a sound effect for this view.
15305 *
15306 * <p>The framework will play sound effects for some built in actions, such as
15307 * clicking, but you may wish to play these effects in your widget,
15308 * for instance, for internal navigation.
15309 *
15310 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15311 * {@link #isSoundEffectsEnabled()} is true.
15312 *
15313 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15314 */
15315 public void playSoundEffect(int soundConstant) {
15316 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15317 return;
15318 }
15319 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15320 }
15321
15322 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015323 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015324 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015325 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015326 *
15327 * <p>The framework will provide haptic feedback for some built in actions,
15328 * such as long presses, but you may wish to provide feedback for your
15329 * own widget.
15330 *
15331 * <p>The feedback will only be performed if
15332 * {@link #isHapticFeedbackEnabled()} is true.
15333 *
15334 * @param feedbackConstant One of the constants defined in
15335 * {@link HapticFeedbackConstants}
15336 */
15337 public boolean performHapticFeedback(int feedbackConstant) {
15338 return performHapticFeedback(feedbackConstant, 0);
15339 }
15340
15341 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015342 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015343 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015344 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015345 *
15346 * @param feedbackConstant One of the constants defined in
15347 * {@link HapticFeedbackConstants}
15348 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15349 */
15350 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15351 if (mAttachInfo == null) {
15352 return false;
15353 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015354 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015355 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015356 && !isHapticFeedbackEnabled()) {
15357 return false;
15358 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015359 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15360 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015361 }
15362
15363 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015364 * Request that the visibility of the status bar or other screen/window
15365 * decorations be changed.
15366 *
15367 * <p>This method is used to put the over device UI into temporary modes
15368 * where the user's attention is focused more on the application content,
15369 * by dimming or hiding surrounding system affordances. This is typically
15370 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15371 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15372 * to be placed behind the action bar (and with these flags other system
15373 * affordances) so that smooth transitions between hiding and showing them
15374 * can be done.
15375 *
15376 * <p>Two representative examples of the use of system UI visibility is
15377 * implementing a content browsing application (like a magazine reader)
15378 * and a video playing application.
15379 *
15380 * <p>The first code shows a typical implementation of a View in a content
15381 * browsing application. In this implementation, the application goes
15382 * into a content-oriented mode by hiding the status bar and action bar,
15383 * and putting the navigation elements into lights out mode. The user can
15384 * then interact with content while in this mode. Such an application should
15385 * provide an easy way for the user to toggle out of the mode (such as to
15386 * check information in the status bar or access notifications). In the
15387 * implementation here, this is done simply by tapping on the content.
15388 *
15389 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15390 * content}
15391 *
15392 * <p>This second code sample shows a typical implementation of a View
15393 * in a video playing application. In this situation, while the video is
15394 * playing the application would like to go into a complete full-screen mode,
15395 * to use as much of the display as possible for the video. When in this state
15396 * the user can not interact with the application; the system intercepts
15397 * touching on the screen to pop the UI out of full screen mode.
15398 *
15399 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15400 * content}
15401 *
15402 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15403 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15404 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15405 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015406 */
15407 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015408 if (visibility != mSystemUiVisibility) {
15409 mSystemUiVisibility = visibility;
15410 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15411 mParent.recomputeViewAttributes(this);
15412 }
Joe Onorato664644d2011-01-23 17:53:23 -080015413 }
15414 }
15415
15416 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015417 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15418 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15419 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15420 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15421 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015422 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015423 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015424 return mSystemUiVisibility;
15425 }
15426
Scott Mainec6331b2011-05-24 16:55:56 -070015427 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015428 * Returns the current system UI visibility that is currently set for
15429 * the entire window. This is the combination of the
15430 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15431 * views in the window.
15432 */
15433 public int getWindowSystemUiVisibility() {
15434 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15435 }
15436
15437 /**
15438 * Override to find out when the window's requested system UI visibility
15439 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15440 * This is different from the callbacks recieved through
15441 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15442 * in that this is only telling you about the local request of the window,
15443 * not the actual values applied by the system.
15444 */
15445 public void onWindowSystemUiVisibilityChanged(int visible) {
15446 }
15447
15448 /**
15449 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15450 * the view hierarchy.
15451 */
15452 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15453 onWindowSystemUiVisibilityChanged(visible);
15454 }
15455
15456 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015457 * Set a listener to receive callbacks when the visibility of the system bar changes.
15458 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15459 */
Joe Onorato664644d2011-01-23 17:53:23 -080015460 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015461 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015462 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15463 mParent.recomputeViewAttributes(this);
15464 }
15465 }
15466
15467 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015468 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15469 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015470 */
15471 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015472 ListenerInfo li = mListenerInfo;
15473 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15474 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015475 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015476 }
15477 }
15478
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015479 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15480 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15481 if (val != mSystemUiVisibility) {
15482 setSystemUiVisibility(val);
15483 }
15484 }
15485
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015486 /** @hide */
15487 public void setDisabledSystemUiVisibility(int flags) {
15488 if (mAttachInfo != null) {
15489 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15490 mAttachInfo.mDisabledSystemUiVisibility = flags;
15491 if (mParent != null) {
15492 mParent.recomputeViewAttributes(this);
15493 }
15494 }
15495 }
15496 }
15497
Joe Onorato664644d2011-01-23 17:53:23 -080015498 /**
Joe Malin32736f02011-01-19 16:14:20 -080015499 * Creates an image that the system displays during the drag and drop
15500 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15501 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15502 * appearance as the given View. The default also positions the center of the drag shadow
15503 * directly under the touch point. If no View is provided (the constructor with no parameters
15504 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15505 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15506 * default is an invisible drag shadow.
15507 * <p>
15508 * You are not required to use the View you provide to the constructor as the basis of the
15509 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15510 * anything you want as the drag shadow.
15511 * </p>
15512 * <p>
15513 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15514 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15515 * size and position of the drag shadow. It uses this data to construct a
15516 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15517 * so that your application can draw the shadow image in the Canvas.
15518 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015519 *
15520 * <div class="special reference">
15521 * <h3>Developer Guides</h3>
15522 * <p>For a guide to implementing drag and drop features, read the
15523 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15524 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015525 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015526 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015527 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015528
15529 /**
Joe Malin32736f02011-01-19 16:14:20 -080015530 * Constructs a shadow image builder based on a View. By default, the resulting drag
15531 * shadow will have the same appearance and dimensions as the View, with the touch point
15532 * over the center of the View.
15533 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015534 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015535 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015536 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015537 }
15538
Christopher Tate17ed60c2011-01-18 12:50:26 -080015539 /**
15540 * Construct a shadow builder object with no associated View. This
15541 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15542 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15543 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015544 * reference to any View object. If they are not overridden, then the result is an
15545 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015546 */
15547 public DragShadowBuilder() {
15548 mView = new WeakReference<View>(null);
15549 }
15550
15551 /**
15552 * Returns the View object that had been passed to the
15553 * {@link #View.DragShadowBuilder(View)}
15554 * constructor. If that View parameter was {@code null} or if the
15555 * {@link #View.DragShadowBuilder()}
15556 * constructor was used to instantiate the builder object, this method will return
15557 * null.
15558 *
15559 * @return The View object associate with this builder object.
15560 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015561 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015562 final public View getView() {
15563 return mView.get();
15564 }
15565
Christopher Tate2c095f32010-10-04 14:13:40 -070015566 /**
Joe Malin32736f02011-01-19 16:14:20 -080015567 * Provides the metrics for the shadow image. These include the dimensions of
15568 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015569 * be centered under the touch location while dragging.
15570 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015571 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015572 * same as the dimensions of the View itself and centers the shadow under
15573 * the touch point.
15574 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015575 *
Joe Malin32736f02011-01-19 16:14:20 -080015576 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15577 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15578 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15579 * image.
15580 *
15581 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15582 * shadow image that should be underneath the touch point during the drag and drop
15583 * operation. Your application must set {@link android.graphics.Point#x} to the
15584 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015585 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015586 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015587 final View view = mView.get();
15588 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015589 shadowSize.set(view.getWidth(), view.getHeight());
15590 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015591 } else {
15592 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15593 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015594 }
15595
15596 /**
Joe Malin32736f02011-01-19 16:14:20 -080015597 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15598 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015599 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015600 *
Joe Malin32736f02011-01-19 16:14:20 -080015601 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015602 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015603 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015604 final View view = mView.get();
15605 if (view != null) {
15606 view.draw(canvas);
15607 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015608 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015609 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015610 }
15611 }
15612
15613 /**
Joe Malin32736f02011-01-19 16:14:20 -080015614 * Starts a drag and drop operation. When your application calls this method, it passes a
15615 * {@link android.view.View.DragShadowBuilder} object to the system. The
15616 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15617 * to get metrics for the drag shadow, and then calls the object's
15618 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15619 * <p>
15620 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15621 * drag events to all the View objects in your application that are currently visible. It does
15622 * this either by calling the View object's drag listener (an implementation of
15623 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15624 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15625 * Both are passed a {@link android.view.DragEvent} object that has a
15626 * {@link android.view.DragEvent#getAction()} value of
15627 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15628 * </p>
15629 * <p>
15630 * Your application can invoke startDrag() on any attached View object. The View object does not
15631 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15632 * be related to the View the user selected for dragging.
15633 * </p>
15634 * @param data A {@link android.content.ClipData} object pointing to the data to be
15635 * transferred by the drag and drop operation.
15636 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15637 * drag shadow.
15638 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15639 * drop operation. This Object is put into every DragEvent object sent by the system during the
15640 * current drag.
15641 * <p>
15642 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15643 * to the target Views. For example, it can contain flags that differentiate between a
15644 * a copy operation and a move operation.
15645 * </p>
15646 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15647 * so the parameter should be set to 0.
15648 * @return {@code true} if the method completes successfully, or
15649 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15650 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015651 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015652 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015653 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015654 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015655 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015656 }
15657 boolean okay = false;
15658
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015659 Point shadowSize = new Point();
15660 Point shadowTouchPoint = new Point();
15661 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015662
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015663 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15664 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15665 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015666 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015667
Chris Tatea32dcf72010-10-14 12:13:50 -070015668 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015669 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15670 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015671 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015672 Surface surface = new Surface();
15673 try {
15674 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015675 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015676 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015677 + " surface=" + surface);
15678 if (token != null) {
15679 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015680 try {
Chris Tate6b391282010-10-14 15:48:59 -070015681 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015682 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015683 } finally {
15684 surface.unlockCanvasAndPost(canvas);
15685 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015686
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015687 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015688
15689 // Cache the local state object for delivery with DragEvents
15690 root.setLocalDragState(myLocalState);
15691
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015692 // repurpose 'shadowSize' for the last touch point
15693 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015694
Christopher Tatea53146c2010-09-07 11:57:52 -070015695 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015696 shadowSize.x, shadowSize.y,
15697 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015698 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015699
15700 // Off and running! Release our local surface instance; the drag
15701 // shadow surface is now managed by the system process.
15702 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015703 }
15704 } catch (Exception e) {
15705 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15706 surface.destroy();
15707 }
15708
15709 return okay;
15710 }
15711
Christopher Tatea53146c2010-09-07 11:57:52 -070015712 /**
Joe Malin32736f02011-01-19 16:14:20 -080015713 * Handles drag events sent by the system following a call to
15714 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15715 *<p>
15716 * When the system calls this method, it passes a
15717 * {@link android.view.DragEvent} object. A call to
15718 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15719 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15720 * operation.
15721 * @param event The {@link android.view.DragEvent} sent by the system.
15722 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15723 * in DragEvent, indicating the type of drag event represented by this object.
15724 * @return {@code true} if the method was successful, otherwise {@code false}.
15725 * <p>
15726 * The method should return {@code true} in response to an action type of
15727 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15728 * operation.
15729 * </p>
15730 * <p>
15731 * The method should also return {@code true} in response to an action type of
15732 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15733 * {@code false} if it didn't.
15734 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015735 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015736 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015737 return false;
15738 }
15739
15740 /**
Joe Malin32736f02011-01-19 16:14:20 -080015741 * Detects if this View is enabled and has a drag event listener.
15742 * If both are true, then it calls the drag event listener with the
15743 * {@link android.view.DragEvent} it received. If the drag event listener returns
15744 * {@code true}, then dispatchDragEvent() returns {@code true}.
15745 * <p>
15746 * For all other cases, the method calls the
15747 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15748 * method and returns its result.
15749 * </p>
15750 * <p>
15751 * This ensures that a drag event is always consumed, even if the View does not have a drag
15752 * event listener. However, if the View has a listener and the listener returns true, then
15753 * onDragEvent() is not called.
15754 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015755 */
15756 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015757 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015758 ListenerInfo li = mListenerInfo;
15759 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15760 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015761 return true;
15762 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015763 return onDragEvent(event);
15764 }
15765
Christopher Tate3d4bf172011-03-28 16:16:46 -070015766 boolean canAcceptDrag() {
15767 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15768 }
15769
Christopher Tatea53146c2010-09-07 11:57:52 -070015770 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015771 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15772 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015773 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015774 */
15775 public void onCloseSystemDialogs(String reason) {
15776 }
Joe Malin32736f02011-01-19 16:14:20 -080015777
Dianne Hackbornffa42482009-09-23 22:20:11 -070015778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015779 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015780 * update a Region being computed for
15781 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015782 * that any non-transparent parts of the Drawable are removed from the
15783 * given transparent region.
15784 *
15785 * @param dr The Drawable whose transparency is to be applied to the region.
15786 * @param region A Region holding the current transparency information,
15787 * where any parts of the region that are set are considered to be
15788 * transparent. On return, this region will be modified to have the
15789 * transparency information reduced by the corresponding parts of the
15790 * Drawable that are not transparent.
15791 * {@hide}
15792 */
15793 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15794 if (DBG) {
15795 Log.i("View", "Getting transparent region for: " + this);
15796 }
15797 final Region r = dr.getTransparentRegion();
15798 final Rect db = dr.getBounds();
15799 final AttachInfo attachInfo = mAttachInfo;
15800 if (r != null && attachInfo != null) {
15801 final int w = getRight()-getLeft();
15802 final int h = getBottom()-getTop();
15803 if (db.left > 0) {
15804 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15805 r.op(0, 0, db.left, h, Region.Op.UNION);
15806 }
15807 if (db.right < w) {
15808 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15809 r.op(db.right, 0, w, h, Region.Op.UNION);
15810 }
15811 if (db.top > 0) {
15812 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15813 r.op(0, 0, w, db.top, Region.Op.UNION);
15814 }
15815 if (db.bottom < h) {
15816 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15817 r.op(0, db.bottom, w, h, Region.Op.UNION);
15818 }
15819 final int[] location = attachInfo.mTransparentLocation;
15820 getLocationInWindow(location);
15821 r.translate(location[0], location[1]);
15822 region.op(r, Region.Op.INTERSECT);
15823 } else {
15824 region.op(db, Region.Op.DIFFERENCE);
15825 }
15826 }
15827
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015828 private void checkForLongClick(int delayOffset) {
15829 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15830 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015831
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015832 if (mPendingCheckForLongPress == null) {
15833 mPendingCheckForLongPress = new CheckForLongPress();
15834 }
15835 mPendingCheckForLongPress.rememberWindowAttachCount();
15836 postDelayed(mPendingCheckForLongPress,
15837 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015839 }
15840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015841 /**
15842 * Inflate a view from an XML resource. This convenience method wraps the {@link
15843 * LayoutInflater} class, which provides a full range of options for view inflation.
15844 *
15845 * @param context The Context object for your activity or application.
15846 * @param resource The resource ID to inflate
15847 * @param root A view group that will be the parent. Used to properly inflate the
15848 * layout_* parameters.
15849 * @see LayoutInflater
15850 */
15851 public static View inflate(Context context, int resource, ViewGroup root) {
15852 LayoutInflater factory = LayoutInflater.from(context);
15853 return factory.inflate(resource, root);
15854 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015856 /**
Adam Powell637d3372010-08-25 14:37:03 -070015857 * Scroll the view with standard behavior for scrolling beyond the normal
15858 * content boundaries. Views that call this method should override
15859 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15860 * results of an over-scroll operation.
15861 *
15862 * Views can use this method to handle any touch or fling-based scrolling.
15863 *
15864 * @param deltaX Change in X in pixels
15865 * @param deltaY Change in Y in pixels
15866 * @param scrollX Current X scroll value in pixels before applying deltaX
15867 * @param scrollY Current Y scroll value in pixels before applying deltaY
15868 * @param scrollRangeX Maximum content scroll range along the X axis
15869 * @param scrollRangeY Maximum content scroll range along the Y axis
15870 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15871 * along the X axis.
15872 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15873 * along the Y axis.
15874 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15875 * @return true if scrolling was clamped to an over-scroll boundary along either
15876 * axis, false otherwise.
15877 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015878 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015879 protected boolean overScrollBy(int deltaX, int deltaY,
15880 int scrollX, int scrollY,
15881 int scrollRangeX, int scrollRangeY,
15882 int maxOverScrollX, int maxOverScrollY,
15883 boolean isTouchEvent) {
15884 final int overScrollMode = mOverScrollMode;
15885 final boolean canScrollHorizontal =
15886 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15887 final boolean canScrollVertical =
15888 computeVerticalScrollRange() > computeVerticalScrollExtent();
15889 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15890 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15891 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15892 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15893
15894 int newScrollX = scrollX + deltaX;
15895 if (!overScrollHorizontal) {
15896 maxOverScrollX = 0;
15897 }
15898
15899 int newScrollY = scrollY + deltaY;
15900 if (!overScrollVertical) {
15901 maxOverScrollY = 0;
15902 }
15903
15904 // Clamp values if at the limits and record
15905 final int left = -maxOverScrollX;
15906 final int right = maxOverScrollX + scrollRangeX;
15907 final int top = -maxOverScrollY;
15908 final int bottom = maxOverScrollY + scrollRangeY;
15909
15910 boolean clampedX = false;
15911 if (newScrollX > right) {
15912 newScrollX = right;
15913 clampedX = true;
15914 } else if (newScrollX < left) {
15915 newScrollX = left;
15916 clampedX = true;
15917 }
15918
15919 boolean clampedY = false;
15920 if (newScrollY > bottom) {
15921 newScrollY = bottom;
15922 clampedY = true;
15923 } else if (newScrollY < top) {
15924 newScrollY = top;
15925 clampedY = true;
15926 }
15927
15928 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15929
15930 return clampedX || clampedY;
15931 }
15932
15933 /**
15934 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15935 * respond to the results of an over-scroll operation.
15936 *
15937 * @param scrollX New X scroll value in pixels
15938 * @param scrollY New Y scroll value in pixels
15939 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15940 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15941 */
15942 protected void onOverScrolled(int scrollX, int scrollY,
15943 boolean clampedX, boolean clampedY) {
15944 // Intentionally empty.
15945 }
15946
15947 /**
15948 * Returns the over-scroll mode for this view. The result will be
15949 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15950 * (allow over-scrolling only if the view content is larger than the container),
15951 * or {@link #OVER_SCROLL_NEVER}.
15952 *
15953 * @return This view's over-scroll mode.
15954 */
15955 public int getOverScrollMode() {
15956 return mOverScrollMode;
15957 }
15958
15959 /**
15960 * Set the over-scroll mode for this view. Valid over-scroll modes are
15961 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15962 * (allow over-scrolling only if the view content is larger than the container),
15963 * or {@link #OVER_SCROLL_NEVER}.
15964 *
15965 * Setting the over-scroll mode of a view will have an effect only if the
15966 * view is capable of scrolling.
15967 *
15968 * @param overScrollMode The new over-scroll mode for this view.
15969 */
15970 public void setOverScrollMode(int overScrollMode) {
15971 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15972 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15973 overScrollMode != OVER_SCROLL_NEVER) {
15974 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15975 }
15976 mOverScrollMode = overScrollMode;
15977 }
15978
15979 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015980 * Gets a scale factor that determines the distance the view should scroll
15981 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15982 * @return The vertical scroll scale factor.
15983 * @hide
15984 */
15985 protected float getVerticalScrollFactor() {
15986 if (mVerticalScrollFactor == 0) {
15987 TypedValue outValue = new TypedValue();
15988 if (!mContext.getTheme().resolveAttribute(
15989 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15990 throw new IllegalStateException(
15991 "Expected theme to define listPreferredItemHeight.");
15992 }
15993 mVerticalScrollFactor = outValue.getDimension(
15994 mContext.getResources().getDisplayMetrics());
15995 }
15996 return mVerticalScrollFactor;
15997 }
15998
15999 /**
16000 * Gets a scale factor that determines the distance the view should scroll
16001 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16002 * @return The horizontal scroll scale factor.
16003 * @hide
16004 */
16005 protected float getHorizontalScrollFactor() {
16006 // TODO: Should use something else.
16007 return getVerticalScrollFactor();
16008 }
16009
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016010 /**
16011 * Return the value specifying the text direction or policy that was set with
16012 * {@link #setTextDirection(int)}.
16013 *
16014 * @return the defined text direction. It can be one of:
16015 *
16016 * {@link #TEXT_DIRECTION_INHERIT},
16017 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16018 * {@link #TEXT_DIRECTION_ANY_RTL},
16019 * {@link #TEXT_DIRECTION_LTR},
16020 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016021 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016022 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016023 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016024 @ViewDebug.ExportedProperty(category = "text", mapping = {
16025 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16026 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16027 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16028 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16029 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16030 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16031 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016032 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016033 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016034 }
16035
16036 /**
16037 * Set the text direction.
16038 *
16039 * @param textDirection the direction to set. Should be one of:
16040 *
16041 * {@link #TEXT_DIRECTION_INHERIT},
16042 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16043 * {@link #TEXT_DIRECTION_ANY_RTL},
16044 * {@link #TEXT_DIRECTION_LTR},
16045 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016046 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016047 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016048 */
16049 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016050 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016051 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016052 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016053 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016054 // Set the new text direction
16055 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016056 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016057 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016058 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016059 }
16060 }
16061
16062 /**
16063 * Return the resolved text direction.
16064 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016065 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16066 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16067 * up the parent chain of the view. if there is no parent, then it will return the default
16068 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16069 *
16070 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016071 *
Doug Feltcb3791202011-07-07 11:57:48 -070016072 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16073 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016074 * {@link #TEXT_DIRECTION_LTR},
16075 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016076 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016077 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016078 */
16079 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016080 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016081 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016082 resolveTextDirection();
16083 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016084 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016085 }
16086
16087 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016088 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16089 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016090 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016091 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016092 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016093 // Reset any previous text direction resolution
16094 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16095
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016096 if (hasRtlSupport()) {
16097 // Set resolved text direction flag depending on text direction flag
16098 final int textDirection = getTextDirection();
16099 switch(textDirection) {
16100 case TEXT_DIRECTION_INHERIT:
16101 if (canResolveTextDirection()) {
16102 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016103
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016104 // Set current resolved direction to the same value as the parent's one
16105 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16106 switch (parentResolvedDirection) {
16107 case TEXT_DIRECTION_FIRST_STRONG:
16108 case TEXT_DIRECTION_ANY_RTL:
16109 case TEXT_DIRECTION_LTR:
16110 case TEXT_DIRECTION_RTL:
16111 case TEXT_DIRECTION_LOCALE:
16112 mPrivateFlags2 |=
16113 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16114 break;
16115 default:
16116 // Default resolved direction is "first strong" heuristic
16117 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16118 }
16119 } else {
16120 // We cannot do the resolution if there is no parent, so use the default one
16121 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016122 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016123 break;
16124 case TEXT_DIRECTION_FIRST_STRONG:
16125 case TEXT_DIRECTION_ANY_RTL:
16126 case TEXT_DIRECTION_LTR:
16127 case TEXT_DIRECTION_RTL:
16128 case TEXT_DIRECTION_LOCALE:
16129 // Resolved direction is the same as text direction
16130 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16131 break;
16132 default:
16133 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016134 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016135 }
16136 } else {
16137 // Default resolved direction is "first strong" heuristic
16138 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016139 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016140
16141 // Set to resolved
16142 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016143 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016144 }
16145
16146 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016147 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016148 * resolution should override this method.
16149 *
16150 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016151 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016152 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016153 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016154 }
16155
16156 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016157 * Check if text direction resolution can be done.
16158 *
16159 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016160 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016161 */
16162 public boolean canResolveTextDirection() {
16163 switch (getTextDirection()) {
16164 case TEXT_DIRECTION_INHERIT:
16165 return (mParent != null) && (mParent instanceof ViewGroup);
16166 default:
16167 return true;
16168 }
16169 }
16170
16171 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016172 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016173 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016174 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016175 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016176 */
16177 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016178 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016179 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016180 }
16181
16182 /**
16183 * Called when text direction is reset. Subclasses that care about text direction reset should
16184 * override this method and do a reset of the text direction of their children. The default
16185 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016186 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016187 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016188 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016189 }
16190
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016191 /**
16192 * Return the value specifying the text alignment or policy that was set with
16193 * {@link #setTextAlignment(int)}.
16194 *
16195 * @return the defined text alignment. It can be one of:
16196 *
16197 * {@link #TEXT_ALIGNMENT_INHERIT},
16198 * {@link #TEXT_ALIGNMENT_GRAVITY},
16199 * {@link #TEXT_ALIGNMENT_CENTER},
16200 * {@link #TEXT_ALIGNMENT_TEXT_START},
16201 * {@link #TEXT_ALIGNMENT_TEXT_END},
16202 * {@link #TEXT_ALIGNMENT_VIEW_START},
16203 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016204 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016205 */
16206 @ViewDebug.ExportedProperty(category = "text", mapping = {
16207 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16208 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16209 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16210 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16211 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16212 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16213 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16214 })
16215 public int getTextAlignment() {
16216 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16217 }
16218
16219 /**
16220 * Set the text alignment.
16221 *
16222 * @param textAlignment The text alignment to set. Should be one of
16223 *
16224 * {@link #TEXT_ALIGNMENT_INHERIT},
16225 * {@link #TEXT_ALIGNMENT_GRAVITY},
16226 * {@link #TEXT_ALIGNMENT_CENTER},
16227 * {@link #TEXT_ALIGNMENT_TEXT_START},
16228 * {@link #TEXT_ALIGNMENT_TEXT_END},
16229 * {@link #TEXT_ALIGNMENT_VIEW_START},
16230 * {@link #TEXT_ALIGNMENT_VIEW_END}
16231 *
16232 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016233 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016234 */
16235 public void setTextAlignment(int textAlignment) {
16236 if (textAlignment != getTextAlignment()) {
16237 // Reset the current and resolved text alignment
16238 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16239 resetResolvedTextAlignment();
16240 // Set the new text alignment
16241 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16242 // Refresh
16243 requestLayout();
16244 invalidate(true);
16245 }
16246 }
16247
16248 /**
16249 * Return the resolved text alignment.
16250 *
16251 * The resolved text alignment. This needs resolution if the value is
16252 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16253 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16254 *
16255 * @return the resolved text alignment. Returns one of:
16256 *
16257 * {@link #TEXT_ALIGNMENT_GRAVITY},
16258 * {@link #TEXT_ALIGNMENT_CENTER},
16259 * {@link #TEXT_ALIGNMENT_TEXT_START},
16260 * {@link #TEXT_ALIGNMENT_TEXT_END},
16261 * {@link #TEXT_ALIGNMENT_VIEW_START},
16262 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016263 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016264 */
16265 @ViewDebug.ExportedProperty(category = "text", mapping = {
16266 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16267 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16268 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16269 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16270 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16271 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16272 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16273 })
16274 public int getResolvedTextAlignment() {
16275 // If text alignment is not resolved, then resolve it
16276 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16277 resolveTextAlignment();
16278 }
16279 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16280 }
16281
16282 /**
16283 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16284 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016285 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016286 */
16287 public void resolveTextAlignment() {
16288 // Reset any previous text alignment resolution
16289 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16290
16291 if (hasRtlSupport()) {
16292 // Set resolved text alignment flag depending on text alignment flag
16293 final int textAlignment = getTextAlignment();
16294 switch (textAlignment) {
16295 case TEXT_ALIGNMENT_INHERIT:
16296 // Check if we can resolve the text alignment
16297 if (canResolveLayoutDirection() && mParent instanceof View) {
16298 View view = (View) mParent;
16299
16300 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16301 switch (parentResolvedTextAlignment) {
16302 case TEXT_ALIGNMENT_GRAVITY:
16303 case TEXT_ALIGNMENT_TEXT_START:
16304 case TEXT_ALIGNMENT_TEXT_END:
16305 case TEXT_ALIGNMENT_CENTER:
16306 case TEXT_ALIGNMENT_VIEW_START:
16307 case TEXT_ALIGNMENT_VIEW_END:
16308 // Resolved text alignment is the same as the parent resolved
16309 // text alignment
16310 mPrivateFlags2 |=
16311 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16312 break;
16313 default:
16314 // Use default resolved text alignment
16315 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16316 }
16317 }
16318 else {
16319 // We cannot do the resolution if there is no parent so use the default
16320 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16321 }
16322 break;
16323 case TEXT_ALIGNMENT_GRAVITY:
16324 case TEXT_ALIGNMENT_TEXT_START:
16325 case TEXT_ALIGNMENT_TEXT_END:
16326 case TEXT_ALIGNMENT_CENTER:
16327 case TEXT_ALIGNMENT_VIEW_START:
16328 case TEXT_ALIGNMENT_VIEW_END:
16329 // Resolved text alignment is the same as text alignment
16330 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16331 break;
16332 default:
16333 // Use default resolved text alignment
16334 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16335 }
16336 } else {
16337 // Use default resolved text alignment
16338 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16339 }
16340
16341 // Set the resolved
16342 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16343 onResolvedTextAlignmentChanged();
16344 }
16345
16346 /**
16347 * Check if text alignment resolution can be done.
16348 *
16349 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016350 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016351 */
16352 public boolean canResolveTextAlignment() {
16353 switch (getTextAlignment()) {
16354 case TEXT_DIRECTION_INHERIT:
16355 return (mParent != null);
16356 default:
16357 return true;
16358 }
16359 }
16360
16361 /**
16362 * Called when text alignment has been resolved. Subclasses that care about text alignment
16363 * resolution should override this method.
16364 *
16365 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016366 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016367 */
16368 public void onResolvedTextAlignmentChanged() {
16369 }
16370
16371 /**
16372 * Reset resolved text alignment. Text alignment can be resolved with a call to
16373 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16374 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016375 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016376 */
16377 public void resetResolvedTextAlignment() {
16378 // Reset any previous text alignment resolution
16379 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16380 onResolvedTextAlignmentReset();
16381 }
16382
16383 /**
16384 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16385 * override this method and do a reset of the text alignment of their children. The default
16386 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016387 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016388 */
16389 public void onResolvedTextAlignmentReset() {
16390 }
16391
Chet Haaseb39f0512011-05-24 14:36:40 -070016392 //
16393 // Properties
16394 //
16395 /**
16396 * A Property wrapper around the <code>alpha</code> functionality handled by the
16397 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16398 */
Chet Haased47f1532011-12-16 11:18:52 -080016399 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016400 @Override
16401 public void setValue(View object, float value) {
16402 object.setAlpha(value);
16403 }
16404
16405 @Override
16406 public Float get(View object) {
16407 return object.getAlpha();
16408 }
16409 };
16410
16411 /**
16412 * A Property wrapper around the <code>translationX</code> functionality handled by the
16413 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16414 */
Chet Haased47f1532011-12-16 11:18:52 -080016415 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016416 @Override
16417 public void setValue(View object, float value) {
16418 object.setTranslationX(value);
16419 }
16420
16421 @Override
16422 public Float get(View object) {
16423 return object.getTranslationX();
16424 }
16425 };
16426
16427 /**
16428 * A Property wrapper around the <code>translationY</code> functionality handled by the
16429 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16430 */
Chet Haased47f1532011-12-16 11:18:52 -080016431 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016432 @Override
16433 public void setValue(View object, float value) {
16434 object.setTranslationY(value);
16435 }
16436
16437 @Override
16438 public Float get(View object) {
16439 return object.getTranslationY();
16440 }
16441 };
16442
16443 /**
16444 * A Property wrapper around the <code>x</code> functionality handled by the
16445 * {@link View#setX(float)} and {@link View#getX()} methods.
16446 */
Chet Haased47f1532011-12-16 11:18:52 -080016447 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016448 @Override
16449 public void setValue(View object, float value) {
16450 object.setX(value);
16451 }
16452
16453 @Override
16454 public Float get(View object) {
16455 return object.getX();
16456 }
16457 };
16458
16459 /**
16460 * A Property wrapper around the <code>y</code> functionality handled by the
16461 * {@link View#setY(float)} and {@link View#getY()} methods.
16462 */
Chet Haased47f1532011-12-16 11:18:52 -080016463 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016464 @Override
16465 public void setValue(View object, float value) {
16466 object.setY(value);
16467 }
16468
16469 @Override
16470 public Float get(View object) {
16471 return object.getY();
16472 }
16473 };
16474
16475 /**
16476 * A Property wrapper around the <code>rotation</code> functionality handled by the
16477 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16478 */
Chet Haased47f1532011-12-16 11:18:52 -080016479 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016480 @Override
16481 public void setValue(View object, float value) {
16482 object.setRotation(value);
16483 }
16484
16485 @Override
16486 public Float get(View object) {
16487 return object.getRotation();
16488 }
16489 };
16490
16491 /**
16492 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16493 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16494 */
Chet Haased47f1532011-12-16 11:18:52 -080016495 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016496 @Override
16497 public void setValue(View object, float value) {
16498 object.setRotationX(value);
16499 }
16500
16501 @Override
16502 public Float get(View object) {
16503 return object.getRotationX();
16504 }
16505 };
16506
16507 /**
16508 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16509 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16510 */
Chet Haased47f1532011-12-16 11:18:52 -080016511 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016512 @Override
16513 public void setValue(View object, float value) {
16514 object.setRotationY(value);
16515 }
16516
16517 @Override
16518 public Float get(View object) {
16519 return object.getRotationY();
16520 }
16521 };
16522
16523 /**
16524 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16525 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16526 */
Chet Haased47f1532011-12-16 11:18:52 -080016527 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016528 @Override
16529 public void setValue(View object, float value) {
16530 object.setScaleX(value);
16531 }
16532
16533 @Override
16534 public Float get(View object) {
16535 return object.getScaleX();
16536 }
16537 };
16538
16539 /**
16540 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16541 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16542 */
Chet Haased47f1532011-12-16 11:18:52 -080016543 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016544 @Override
16545 public void setValue(View object, float value) {
16546 object.setScaleY(value);
16547 }
16548
16549 @Override
16550 public Float get(View object) {
16551 return object.getScaleY();
16552 }
16553 };
16554
Jeff Brown33bbfd22011-02-24 20:55:35 -080016555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016556 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16557 * Each MeasureSpec represents a requirement for either the width or the height.
16558 * A MeasureSpec is comprised of a size and a mode. There are three possible
16559 * modes:
16560 * <dl>
16561 * <dt>UNSPECIFIED</dt>
16562 * <dd>
16563 * The parent has not imposed any constraint on the child. It can be whatever size
16564 * it wants.
16565 * </dd>
16566 *
16567 * <dt>EXACTLY</dt>
16568 * <dd>
16569 * The parent has determined an exact size for the child. The child is going to be
16570 * given those bounds regardless of how big it wants to be.
16571 * </dd>
16572 *
16573 * <dt>AT_MOST</dt>
16574 * <dd>
16575 * The child can be as large as it wants up to the specified size.
16576 * </dd>
16577 * </dl>
16578 *
16579 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16580 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16581 */
16582 public static class MeasureSpec {
16583 private static final int MODE_SHIFT = 30;
16584 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16585
16586 /**
16587 * Measure specification mode: The parent has not imposed any constraint
16588 * on the child. It can be whatever size it wants.
16589 */
16590 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16591
16592 /**
16593 * Measure specification mode: The parent has determined an exact size
16594 * for the child. The child is going to be given those bounds regardless
16595 * of how big it wants to be.
16596 */
16597 public static final int EXACTLY = 1 << MODE_SHIFT;
16598
16599 /**
16600 * Measure specification mode: The child can be as large as it wants up
16601 * to the specified size.
16602 */
16603 public static final int AT_MOST = 2 << MODE_SHIFT;
16604
16605 /**
16606 * Creates a measure specification based on the supplied size and mode.
16607 *
16608 * The mode must always be one of the following:
16609 * <ul>
16610 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16611 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16612 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16613 * </ul>
16614 *
16615 * @param size the size of the measure specification
16616 * @param mode the mode of the measure specification
16617 * @return the measure specification based on size and mode
16618 */
16619 public static int makeMeasureSpec(int size, int mode) {
16620 return size + mode;
16621 }
16622
16623 /**
16624 * Extracts the mode from the supplied measure specification.
16625 *
16626 * @param measureSpec the measure specification to extract the mode from
16627 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16628 * {@link android.view.View.MeasureSpec#AT_MOST} or
16629 * {@link android.view.View.MeasureSpec#EXACTLY}
16630 */
16631 public static int getMode(int measureSpec) {
16632 return (measureSpec & MODE_MASK);
16633 }
16634
16635 /**
16636 * Extracts the size from the supplied measure specification.
16637 *
16638 * @param measureSpec the measure specification to extract the size from
16639 * @return the size in pixels defined in the supplied measure specification
16640 */
16641 public static int getSize(int measureSpec) {
16642 return (measureSpec & ~MODE_MASK);
16643 }
16644
16645 /**
16646 * Returns a String representation of the specified measure
16647 * specification.
16648 *
16649 * @param measureSpec the measure specification to convert to a String
16650 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16651 */
16652 public static String toString(int measureSpec) {
16653 int mode = getMode(measureSpec);
16654 int size = getSize(measureSpec);
16655
16656 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16657
16658 if (mode == UNSPECIFIED)
16659 sb.append("UNSPECIFIED ");
16660 else if (mode == EXACTLY)
16661 sb.append("EXACTLY ");
16662 else if (mode == AT_MOST)
16663 sb.append("AT_MOST ");
16664 else
16665 sb.append(mode).append(" ");
16666
16667 sb.append(size);
16668 return sb.toString();
16669 }
16670 }
16671
16672 class CheckForLongPress implements Runnable {
16673
16674 private int mOriginalWindowAttachCount;
16675
16676 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016677 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016678 && mOriginalWindowAttachCount == mWindowAttachCount) {
16679 if (performLongClick()) {
16680 mHasPerformedLongPress = true;
16681 }
16682 }
16683 }
16684
16685 public void rememberWindowAttachCount() {
16686 mOriginalWindowAttachCount = mWindowAttachCount;
16687 }
16688 }
Joe Malin32736f02011-01-19 16:14:20 -080016689
Adam Powelle14579b2009-12-16 18:39:52 -080016690 private final class CheckForTap implements Runnable {
16691 public void run() {
16692 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016693 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016694 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016695 }
16696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016697
Adam Powella35d7682010-03-12 14:48:13 -080016698 private final class PerformClick implements Runnable {
16699 public void run() {
16700 performClick();
16701 }
16702 }
16703
Dianne Hackborn63042d62011-01-26 18:56:29 -080016704 /** @hide */
16705 public void hackTurnOffWindowResizeAnim(boolean off) {
16706 mAttachInfo.mTurnOffWindowResizeAnim = off;
16707 }
Joe Malin32736f02011-01-19 16:14:20 -080016708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016709 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016710 * This method returns a ViewPropertyAnimator object, which can be used to animate
16711 * specific properties on this View.
16712 *
16713 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16714 */
16715 public ViewPropertyAnimator animate() {
16716 if (mAnimator == null) {
16717 mAnimator = new ViewPropertyAnimator(this);
16718 }
16719 return mAnimator;
16720 }
16721
16722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016723 * Interface definition for a callback to be invoked when a key event is
16724 * dispatched to this view. The callback will be invoked before the key
16725 * event is given to the view.
16726 */
16727 public interface OnKeyListener {
16728 /**
16729 * Called when a key is dispatched to a view. This allows listeners to
16730 * get a chance to respond before the target view.
16731 *
16732 * @param v The view the key has been dispatched to.
16733 * @param keyCode The code for the physical key that was pressed
16734 * @param event The KeyEvent object containing full information about
16735 * the event.
16736 * @return True if the listener has consumed the event, false otherwise.
16737 */
16738 boolean onKey(View v, int keyCode, KeyEvent event);
16739 }
16740
16741 /**
16742 * Interface definition for a callback to be invoked when a touch event is
16743 * dispatched to this view. The callback will be invoked before the touch
16744 * event is given to the view.
16745 */
16746 public interface OnTouchListener {
16747 /**
16748 * Called when a touch event is dispatched to a view. This allows listeners to
16749 * get a chance to respond before the target view.
16750 *
16751 * @param v The view the touch event has been dispatched to.
16752 * @param event The MotionEvent object containing full information about
16753 * the event.
16754 * @return True if the listener has consumed the event, false otherwise.
16755 */
16756 boolean onTouch(View v, MotionEvent event);
16757 }
16758
16759 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016760 * Interface definition for a callback to be invoked when a hover event is
16761 * dispatched to this view. The callback will be invoked before the hover
16762 * event is given to the view.
16763 */
16764 public interface OnHoverListener {
16765 /**
16766 * Called when a hover event is dispatched to a view. This allows listeners to
16767 * get a chance to respond before the target view.
16768 *
16769 * @param v The view the hover event has been dispatched to.
16770 * @param event The MotionEvent object containing full information about
16771 * the event.
16772 * @return True if the listener has consumed the event, false otherwise.
16773 */
16774 boolean onHover(View v, MotionEvent event);
16775 }
16776
16777 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016778 * Interface definition for a callback to be invoked when a generic motion event is
16779 * dispatched to this view. The callback will be invoked before the generic motion
16780 * event is given to the view.
16781 */
16782 public interface OnGenericMotionListener {
16783 /**
16784 * Called when a generic motion event is dispatched to a view. This allows listeners to
16785 * get a chance to respond before the target view.
16786 *
16787 * @param v The view the generic motion event has been dispatched to.
16788 * @param event The MotionEvent object containing full information about
16789 * the event.
16790 * @return True if the listener has consumed the event, false otherwise.
16791 */
16792 boolean onGenericMotion(View v, MotionEvent event);
16793 }
16794
16795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016796 * Interface definition for a callback to be invoked when a view has been clicked and held.
16797 */
16798 public interface OnLongClickListener {
16799 /**
16800 * Called when a view has been clicked and held.
16801 *
16802 * @param v The view that was clicked and held.
16803 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016804 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016805 */
16806 boolean onLongClick(View v);
16807 }
16808
16809 /**
Chris Tate32affef2010-10-18 15:29:21 -070016810 * Interface definition for a callback to be invoked when a drag is being dispatched
16811 * to this view. The callback will be invoked before the hosting view's own
16812 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16813 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016814 *
16815 * <div class="special reference">
16816 * <h3>Developer Guides</h3>
16817 * <p>For a guide to implementing drag and drop features, read the
16818 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16819 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016820 */
16821 public interface OnDragListener {
16822 /**
16823 * Called when a drag event is dispatched to a view. This allows listeners
16824 * to get a chance to override base View behavior.
16825 *
Joe Malin32736f02011-01-19 16:14:20 -080016826 * @param v The View that received the drag event.
16827 * @param event The {@link android.view.DragEvent} object for the drag event.
16828 * @return {@code true} if the drag event was handled successfully, or {@code false}
16829 * if the drag event was not handled. Note that {@code false} will trigger the View
16830 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016831 */
16832 boolean onDrag(View v, DragEvent event);
16833 }
16834
16835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016836 * Interface definition for a callback to be invoked when the focus state of
16837 * a view changed.
16838 */
16839 public interface OnFocusChangeListener {
16840 /**
16841 * Called when the focus state of a view has changed.
16842 *
16843 * @param v The view whose state has changed.
16844 * @param hasFocus The new focus state of v.
16845 */
16846 void onFocusChange(View v, boolean hasFocus);
16847 }
16848
16849 /**
16850 * Interface definition for a callback to be invoked when a view is clicked.
16851 */
16852 public interface OnClickListener {
16853 /**
16854 * Called when a view has been clicked.
16855 *
16856 * @param v The view that was clicked.
16857 */
16858 void onClick(View v);
16859 }
16860
16861 /**
16862 * Interface definition for a callback to be invoked when the context menu
16863 * for this view is being built.
16864 */
16865 public interface OnCreateContextMenuListener {
16866 /**
16867 * Called when the context menu for this view is being built. It is not
16868 * safe to hold onto the menu after this method returns.
16869 *
16870 * @param menu The context menu that is being built
16871 * @param v The view for which the context menu is being built
16872 * @param menuInfo Extra information about the item for which the
16873 * context menu should be shown. This information will vary
16874 * depending on the class of v.
16875 */
16876 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16877 }
16878
Joe Onorato664644d2011-01-23 17:53:23 -080016879 /**
16880 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016881 * visibility. This reports <strong>global</strong> changes to the system UI
16882 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016883 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016884 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016885 */
16886 public interface OnSystemUiVisibilityChangeListener {
16887 /**
16888 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016889 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016890 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016891 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016892 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16893 * <strong>global</strong> state of the UI visibility flags, not what your
16894 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016895 */
16896 public void onSystemUiVisibilityChange(int visibility);
16897 }
16898
Adam Powell4afd62b2011-02-18 15:02:18 -080016899 /**
16900 * Interface definition for a callback to be invoked when this view is attached
16901 * or detached from its window.
16902 */
16903 public interface OnAttachStateChangeListener {
16904 /**
16905 * Called when the view is attached to a window.
16906 * @param v The view that was attached
16907 */
16908 public void onViewAttachedToWindow(View v);
16909 /**
16910 * Called when the view is detached from a window.
16911 * @param v The view that was detached
16912 */
16913 public void onViewDetachedFromWindow(View v);
16914 }
16915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016916 private final class UnsetPressedState implements Runnable {
16917 public void run() {
16918 setPressed(false);
16919 }
16920 }
16921
16922 /**
16923 * Base class for derived classes that want to save and restore their own
16924 * state in {@link android.view.View#onSaveInstanceState()}.
16925 */
16926 public static class BaseSavedState extends AbsSavedState {
16927 /**
16928 * Constructor used when reading from a parcel. Reads the state of the superclass.
16929 *
16930 * @param source
16931 */
16932 public BaseSavedState(Parcel source) {
16933 super(source);
16934 }
16935
16936 /**
16937 * Constructor called by derived classes when creating their SavedState objects
16938 *
16939 * @param superState The state of the superclass of this view
16940 */
16941 public BaseSavedState(Parcelable superState) {
16942 super(superState);
16943 }
16944
16945 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16946 new Parcelable.Creator<BaseSavedState>() {
16947 public BaseSavedState createFromParcel(Parcel in) {
16948 return new BaseSavedState(in);
16949 }
16950
16951 public BaseSavedState[] newArray(int size) {
16952 return new BaseSavedState[size];
16953 }
16954 };
16955 }
16956
16957 /**
16958 * A set of information given to a view when it is attached to its parent
16959 * window.
16960 */
16961 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016962 interface Callbacks {
16963 void playSoundEffect(int effectId);
16964 boolean performHapticFeedback(int effectId, boolean always);
16965 }
16966
16967 /**
16968 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16969 * to a Handler. This class contains the target (View) to invalidate and
16970 * the coordinates of the dirty rectangle.
16971 *
16972 * For performance purposes, this class also implements a pool of up to
16973 * POOL_LIMIT objects that get reused. This reduces memory allocations
16974 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016975 */
Romain Guyd928d682009-03-31 17:52:16 -070016976 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016977 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016978 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16979 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016980 public InvalidateInfo newInstance() {
16981 return new InvalidateInfo();
16982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016983
Romain Guyd928d682009-03-31 17:52:16 -070016984 public void onAcquired(InvalidateInfo element) {
16985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016986
Romain Guyd928d682009-03-31 17:52:16 -070016987 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016988 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016989 }
16990 }, POOL_LIMIT)
16991 );
16992
16993 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016994 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016995
16996 View target;
16997
16998 int left;
16999 int top;
17000 int right;
17001 int bottom;
17002
Romain Guyd928d682009-03-31 17:52:16 -070017003 public void setNextPoolable(InvalidateInfo element) {
17004 mNext = element;
17005 }
17006
17007 public InvalidateInfo getNextPoolable() {
17008 return mNext;
17009 }
17010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017011 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017012 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017013 }
17014
17015 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017016 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017017 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017018
17019 public boolean isPooled() {
17020 return mIsPooled;
17021 }
17022
17023 public void setPooled(boolean isPooled) {
17024 mIsPooled = isPooled;
17025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026 }
17027
17028 final IWindowSession mSession;
17029
17030 final IWindow mWindow;
17031
17032 final IBinder mWindowToken;
17033
17034 final Callbacks mRootCallbacks;
17035
Romain Guy59a12ca2011-06-09 17:48:21 -070017036 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017038 /**
17039 * The top view of the hierarchy.
17040 */
17041 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017043 IBinder mPanelParentWindowToken;
17044 Surface mSurface;
17045
Romain Guyb051e892010-09-28 19:09:36 -070017046 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017047 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017048 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017049
Romain Guy7e4e5612012-03-05 14:37:29 -080017050 boolean mScreenOn;
17051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017052 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017053 * Scale factor used by the compatibility mode
17054 */
17055 float mApplicationScale;
17056
17057 /**
17058 * Indicates whether the application is in compatibility mode
17059 */
17060 boolean mScalingRequired;
17061
17062 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017063 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017064 */
17065 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017066
Dianne Hackborn63042d62011-01-26 18:56:29 -080017067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017068 * Left position of this view's window
17069 */
17070 int mWindowLeft;
17071
17072 /**
17073 * Top position of this view's window
17074 */
17075 int mWindowTop;
17076
17077 /**
Adam Powell26153a32010-11-08 15:22:27 -080017078 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017079 */
Adam Powell26153a32010-11-08 15:22:27 -080017080 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017081
17082 /**
Dianne Hackborn3556c9a2012-05-04 16:31:25 -070017083 * Describes the parts of the window that are currently completely
17084 * obscured by system UI elements.
17085 */
17086 final Rect mSystemInsets = new Rect();
17087
17088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017089 * For windows that are full-screen but using insets to layout inside
17090 * of the screen decorations, these are the current insets for the
17091 * content of the window.
17092 */
17093 final Rect mContentInsets = new Rect();
17094
17095 /**
17096 * For windows that are full-screen but using insets to layout inside
17097 * of the screen decorations, these are the current insets for the
17098 * actual visible parts of the window.
17099 */
17100 final Rect mVisibleInsets = new Rect();
17101
17102 /**
17103 * The internal insets given by this window. This value is
17104 * supplied by the client (through
17105 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17106 * be given to the window manager when changed to be used in laying
17107 * out windows behind it.
17108 */
17109 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17110 = new ViewTreeObserver.InternalInsetsInfo();
17111
17112 /**
17113 * All views in the window's hierarchy that serve as scroll containers,
17114 * used to determine if the window can be resized or must be panned
17115 * to adjust for a soft input area.
17116 */
17117 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17118
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017119 final KeyEvent.DispatcherState mKeyDispatchState
17120 = new KeyEvent.DispatcherState();
17121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017122 /**
17123 * Indicates whether the view's window currently has the focus.
17124 */
17125 boolean mHasWindowFocus;
17126
17127 /**
17128 * The current visibility of the window.
17129 */
17130 int mWindowVisibility;
17131
17132 /**
17133 * Indicates the time at which drawing started to occur.
17134 */
17135 long mDrawingTime;
17136
17137 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017138 * Indicates whether or not ignoring the DIRTY_MASK flags.
17139 */
17140 boolean mIgnoreDirtyState;
17141
17142 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017143 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17144 * to avoid clearing that flag prematurely.
17145 */
17146 boolean mSetIgnoreDirtyState = false;
17147
17148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017149 * Indicates whether the view's window is currently in touch mode.
17150 */
17151 boolean mInTouchMode;
17152
17153 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017154 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017155 * the next time it performs a traversal
17156 */
17157 boolean mRecomputeGlobalAttributes;
17158
17159 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017160 * Always report new attributes at next traversal.
17161 */
17162 boolean mForceReportNewAttributes;
17163
17164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017165 * Set during a traveral if any views want to keep the screen on.
17166 */
17167 boolean mKeepScreenOn;
17168
17169 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017170 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17171 */
17172 int mSystemUiVisibility;
17173
17174 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017175 * Hack to force certain system UI visibility flags to be cleared.
17176 */
17177 int mDisabledSystemUiVisibility;
17178
17179 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017180 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17181 * attached.
17182 */
17183 boolean mHasSystemUiListeners;
17184
17185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017186 * Set if the visibility of any views has changed.
17187 */
17188 boolean mViewVisibilityChanged;
17189
17190 /**
17191 * Set to true if a view has been scrolled.
17192 */
17193 boolean mViewScrollChanged;
17194
17195 /**
17196 * Global to the view hierarchy used as a temporary for dealing with
17197 * x/y points in the transparent region computations.
17198 */
17199 final int[] mTransparentLocation = new int[2];
17200
17201 /**
17202 * Global to the view hierarchy used as a temporary for dealing with
17203 * x/y points in the ViewGroup.invalidateChild implementation.
17204 */
17205 final int[] mInvalidateChildLocation = new int[2];
17206
Chet Haasec3aa3612010-06-17 08:50:37 -070017207
17208 /**
17209 * Global to the view hierarchy used as a temporary for dealing with
17210 * x/y location when view is transformed.
17211 */
17212 final float[] mTmpTransformLocation = new float[2];
17213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017214 /**
17215 * The view tree observer used to dispatch global events like
17216 * layout, pre-draw, touch mode change, etc.
17217 */
17218 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17219
17220 /**
17221 * A Canvas used by the view hierarchy to perform bitmap caching.
17222 */
17223 Canvas mCanvas;
17224
17225 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017226 * The view root impl.
17227 */
17228 final ViewRootImpl mViewRootImpl;
17229
17230 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017231 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017232 * handler can be used to pump events in the UI events queue.
17233 */
17234 final Handler mHandler;
17235
17236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017237 * Temporary for use in computing invalidate rectangles while
17238 * calling up the hierarchy.
17239 */
17240 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017241
17242 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017243 * Temporary for use in computing hit areas with transformed views
17244 */
17245 final RectF mTmpTransformRect = new RectF();
17246
17247 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017248 * Temporary list for use in collecting focusable descendents of a view.
17249 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017250 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017252 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017253 * The id of the window for accessibility purposes.
17254 */
17255 int mAccessibilityWindowId = View.NO_ID;
17256
17257 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017258 * Whether to ingore not exposed for accessibility Views when
17259 * reporting the view tree to accessibility services.
17260 */
17261 boolean mIncludeNotImportantViews;
17262
17263 /**
17264 * The drawable for highlighting accessibility focus.
17265 */
17266 Drawable mAccessibilityFocusDrawable;
17267
17268 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017269 * Show where the margins, bounds and layout bounds are for each view.
17270 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017271 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017272
17273 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017274 * Point used to compute visible regions.
17275 */
17276 final Point mPoint = new Point();
17277
17278 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017279 * Creates a new set of attachment information with the specified
17280 * events handler and thread.
17281 *
17282 * @param handler the events handler the view must use
17283 */
17284 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017285 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017286 mSession = session;
17287 mWindow = window;
17288 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017289 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017290 mHandler = handler;
17291 mRootCallbacks = effectPlayer;
17292 }
17293 }
17294
17295 /**
17296 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17297 * is supported. This avoids keeping too many unused fields in most
17298 * instances of View.</p>
17299 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017300 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017301
Mike Cleronf116bf82009-09-27 19:14:12 -070017302 /**
17303 * Scrollbars are not visible
17304 */
17305 public static final int OFF = 0;
17306
17307 /**
17308 * Scrollbars are visible
17309 */
17310 public static final int ON = 1;
17311
17312 /**
17313 * Scrollbars are fading away
17314 */
17315 public static final int FADING = 2;
17316
17317 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017319 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017320 public int scrollBarDefaultDelayBeforeFade;
17321 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017322
17323 public int scrollBarSize;
17324 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017325 public float[] interpolatorValues;
17326 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017327
17328 public final Paint paint;
17329 public final Matrix matrix;
17330 public Shader shader;
17331
Mike Cleronf116bf82009-09-27 19:14:12 -070017332 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17333
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017334 private static final float[] OPAQUE = { 255 };
17335 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017336
Mike Cleronf116bf82009-09-27 19:14:12 -070017337 /**
17338 * When fading should start. This time moves into the future every time
17339 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17340 */
17341 public long fadeStartTime;
17342
17343
17344 /**
17345 * The current state of the scrollbars: ON, OFF, or FADING
17346 */
17347 public int state = OFF;
17348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017349 private int mLastColor;
17350
Mike Cleronf116bf82009-09-27 19:14:12 -070017351 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17353 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017354 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17355 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356
17357 paint = new Paint();
17358 matrix = new Matrix();
17359 // use use a height of 1, and then wack the matrix each time we
17360 // actually use it.
17361 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017363 paint.setShader(shader);
17364 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017365 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017366 }
Romain Guy8506ab42009-06-11 17:35:47 -070017367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017368 public void setFadeColor(int color) {
17369 if (color != 0 && color != mLastColor) {
17370 mLastColor = color;
17371 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017372
Romain Guye55e1a72009-08-27 10:42:26 -070017373 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17374 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017376 paint.setShader(shader);
17377 // Restore the default transfer mode (src_over)
17378 paint.setXfermode(null);
17379 }
17380 }
Joe Malin32736f02011-01-19 16:14:20 -080017381
Mike Cleronf116bf82009-09-27 19:14:12 -070017382 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017383 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017384 if (now >= fadeStartTime) {
17385
17386 // the animation fades the scrollbars out by changing
17387 // the opacity (alpha) from fully opaque to fully
17388 // transparent
17389 int nextFrame = (int) now;
17390 int framesCount = 0;
17391
17392 Interpolator interpolator = scrollBarInterpolator;
17393
17394 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017395 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017396
17397 // End transparent
17398 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017399 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017400
17401 state = FADING;
17402
17403 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017404 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017405 }
17406 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017407 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017408
Svetoslav Ganova0156172011-06-26 17:55:44 -070017409 /**
17410 * Resuable callback for sending
17411 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17412 */
17413 private class SendViewScrolledAccessibilityEvent implements Runnable {
17414 public volatile boolean mIsPending;
17415
17416 public void run() {
17417 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17418 mIsPending = false;
17419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017420 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017421
17422 /**
17423 * <p>
17424 * This class represents a delegate that can be registered in a {@link View}
17425 * to enhance accessibility support via composition rather via inheritance.
17426 * It is specifically targeted to widget developers that extend basic View
17427 * classes i.e. classes in package android.view, that would like their
17428 * applications to be backwards compatible.
17429 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017430 * <div class="special reference">
17431 * <h3>Developer Guides</h3>
17432 * <p>For more information about making applications accessible, read the
17433 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17434 * developer guide.</p>
17435 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017436 * <p>
17437 * A scenario in which a developer would like to use an accessibility delegate
17438 * is overriding a method introduced in a later API version then the minimal API
17439 * version supported by the application. For example, the method
17440 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17441 * in API version 4 when the accessibility APIs were first introduced. If a
17442 * developer would like his application to run on API version 4 devices (assuming
17443 * all other APIs used by the application are version 4 or lower) and take advantage
17444 * of this method, instead of overriding the method which would break the application's
17445 * backwards compatibility, he can override the corresponding method in this
17446 * delegate and register the delegate in the target View if the API version of
17447 * the system is high enough i.e. the API version is same or higher to the API
17448 * version that introduced
17449 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17450 * </p>
17451 * <p>
17452 * Here is an example implementation:
17453 * </p>
17454 * <code><pre><p>
17455 * if (Build.VERSION.SDK_INT >= 14) {
17456 * // If the API version is equal of higher than the version in
17457 * // which onInitializeAccessibilityNodeInfo was introduced we
17458 * // register a delegate with a customized implementation.
17459 * View view = findViewById(R.id.view_id);
17460 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17461 * public void onInitializeAccessibilityNodeInfo(View host,
17462 * AccessibilityNodeInfo info) {
17463 * // Let the default implementation populate the info.
17464 * super.onInitializeAccessibilityNodeInfo(host, info);
17465 * // Set some other information.
17466 * info.setEnabled(host.isEnabled());
17467 * }
17468 * });
17469 * }
17470 * </code></pre></p>
17471 * <p>
17472 * This delegate contains methods that correspond to the accessibility methods
17473 * in View. If a delegate has been specified the implementation in View hands
17474 * off handling to the corresponding method in this delegate. The default
17475 * implementation the delegate methods behaves exactly as the corresponding
17476 * method in View for the case of no accessibility delegate been set. Hence,
17477 * to customize the behavior of a View method, clients can override only the
17478 * corresponding delegate method without altering the behavior of the rest
17479 * accessibility related methods of the host view.
17480 * </p>
17481 */
17482 public static class AccessibilityDelegate {
17483
17484 /**
17485 * Sends an accessibility event of the given type. If accessibility is not
17486 * enabled this method has no effect.
17487 * <p>
17488 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17489 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17490 * been set.
17491 * </p>
17492 *
17493 * @param host The View hosting the delegate.
17494 * @param eventType The type of the event to send.
17495 *
17496 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17497 */
17498 public void sendAccessibilityEvent(View host, int eventType) {
17499 host.sendAccessibilityEventInternal(eventType);
17500 }
17501
17502 /**
alanv8eeefef2012-05-07 16:57:53 -070017503 * Performs the specified accessibility action on the view. For
17504 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17505 * <p>
17506 * The default implementation behaves as
17507 * {@link View#performAccessibilityAction(int, Bundle)
17508 * View#performAccessibilityAction(int, Bundle)} for the case of
17509 * no accessibility delegate been set.
17510 * </p>
17511 *
17512 * @param action The action to perform.
17513 * @return Whether the action was performed.
17514 *
17515 * @see View#performAccessibilityAction(int, Bundle)
17516 * View#performAccessibilityAction(int, Bundle)
17517 */
17518 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17519 return host.performAccessibilityActionInternal(action, args);
17520 }
17521
17522 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017523 * Sends an accessibility event. This method behaves exactly as
17524 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17525 * empty {@link AccessibilityEvent} and does not perform a check whether
17526 * accessibility is enabled.
17527 * <p>
17528 * The default implementation behaves as
17529 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17530 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17531 * the case of no accessibility delegate been set.
17532 * </p>
17533 *
17534 * @param host The View hosting the delegate.
17535 * @param event The event to send.
17536 *
17537 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17538 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17539 */
17540 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17541 host.sendAccessibilityEventUncheckedInternal(event);
17542 }
17543
17544 /**
17545 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17546 * to its children for adding their text content to the event.
17547 * <p>
17548 * The default implementation behaves as
17549 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17550 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17551 * the case of no accessibility delegate been set.
17552 * </p>
17553 *
17554 * @param host The View hosting the delegate.
17555 * @param event The event.
17556 * @return True if the event population was completed.
17557 *
17558 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17559 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17560 */
17561 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17562 return host.dispatchPopulateAccessibilityEventInternal(event);
17563 }
17564
17565 /**
17566 * Gives a chance to the host View to populate the accessibility event with its
17567 * text content.
17568 * <p>
17569 * The default implementation behaves as
17570 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17571 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17572 * the case of no accessibility delegate been set.
17573 * </p>
17574 *
17575 * @param host The View hosting the delegate.
17576 * @param event The accessibility event which to populate.
17577 *
17578 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17579 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17580 */
17581 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17582 host.onPopulateAccessibilityEventInternal(event);
17583 }
17584
17585 /**
17586 * Initializes an {@link AccessibilityEvent} with information about the
17587 * the host View which is the event source.
17588 * <p>
17589 * The default implementation behaves as
17590 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17591 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17592 * the case of no accessibility delegate been set.
17593 * </p>
17594 *
17595 * @param host The View hosting the delegate.
17596 * @param event The event to initialize.
17597 *
17598 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17599 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17600 */
17601 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17602 host.onInitializeAccessibilityEventInternal(event);
17603 }
17604
17605 /**
17606 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17607 * <p>
17608 * The default implementation behaves as
17609 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17610 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17611 * the case of no accessibility delegate been set.
17612 * </p>
17613 *
17614 * @param host The View hosting the delegate.
17615 * @param info The instance to initialize.
17616 *
17617 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17618 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17619 */
17620 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17621 host.onInitializeAccessibilityNodeInfoInternal(info);
17622 }
17623
17624 /**
17625 * Called when a child of the host View has requested sending an
17626 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17627 * to augment the event.
17628 * <p>
17629 * The default implementation behaves as
17630 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17631 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17632 * the case of no accessibility delegate been set.
17633 * </p>
17634 *
17635 * @param host The View hosting the delegate.
17636 * @param child The child which requests sending the event.
17637 * @param event The event to be sent.
17638 * @return True if the event should be sent
17639 *
17640 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17641 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17642 */
17643 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17644 AccessibilityEvent event) {
17645 return host.onRequestSendAccessibilityEventInternal(child, event);
17646 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017647
17648 /**
17649 * Gets the provider for managing a virtual view hierarchy rooted at this View
17650 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17651 * that explore the window content.
17652 * <p>
17653 * The default implementation behaves as
17654 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17655 * the case of no accessibility delegate been set.
17656 * </p>
17657 *
17658 * @return The provider.
17659 *
17660 * @see AccessibilityNodeProvider
17661 */
17662 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17663 return null;
17664 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017666}