blob: 83301936b453bf41e441b549203e5c83925e49cb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900207 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
348 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700429 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700542 * <a name="Properties"></a>
543 * <h3>Properties</h3>
544 * <p>
545 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
546 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
547 * available both in the {@link Property} form as well as in similarly-named setter/getter
548 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
549 * be used to set persistent state associated with these rendering-related properties on the view.
550 * The properties and methods can also be used in conjunction with
551 * {@link android.animation.Animator Animator}-based animations, described more in the
552 * <a href="#Animation">Animation</a> section.
553 * </p>
554 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * <a name="Animation"></a>
556 * <h3>Animation</h3>
557 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700558 * Starting with Android 3.0, the preferred way of animating views is to use the
559 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
560 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
561 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
562 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
563 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
564 * makes animating these View properties particularly easy and efficient.
565 * </p>
566 * <p>
567 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 * You can attach an {@link Animation} object to a view using
569 * {@link #setAnimation(Animation)} or
570 * {@link #startAnimation(Animation)}. The animation can alter the scale,
571 * rotation, translation and alpha of a view over time. If the animation is
572 * attached to a view that has children, the animation will affect the entire
573 * subtree rooted by that node. When an animation is started, the framework will
574 * take care of redrawing the appropriate views until the animation completes.
575 * </p>
576 *
Jeff Brown85a31762010-09-01 17:01:00 -0700577 * <a name="Security"></a>
578 * <h3>Security</h3>
579 * <p>
580 * Sometimes it is essential that an application be able to verify that an action
581 * is being performed with the full knowledge and consent of the user, such as
582 * granting a permission request, making a purchase or clicking on an advertisement.
583 * Unfortunately, a malicious application could try to spoof the user into
584 * performing these actions, unaware, by concealing the intended purpose of the view.
585 * As a remedy, the framework offers a touch filtering mechanism that can be used to
586 * improve the security of views that provide access to sensitive functionality.
587 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700588 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800589 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700590 * will discard touches that are received whenever the view's window is obscured by
591 * another visible window. As a result, the view will not receive touches whenever a
592 * toast, dialog or other window appears above the view's window.
593 * </p><p>
594 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700595 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
596 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700597 * </p>
598 *
Romain Guy171c5922011-01-06 10:04:23 -0800599 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_background
601 * @attr ref android.R.styleable#View_clickable
602 * @attr ref android.R.styleable#View_contentDescription
603 * @attr ref android.R.styleable#View_drawingCacheQuality
604 * @attr ref android.R.styleable#View_duplicateParentState
605 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700606 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700607 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700609 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_isScrollContainer
612 * @attr ref android.R.styleable#View_focusable
613 * @attr ref android.R.styleable#View_focusableInTouchMode
614 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
615 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800616 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_longClickable
618 * @attr ref android.R.styleable#View_minHeight
619 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_nextFocusDown
621 * @attr ref android.R.styleable#View_nextFocusLeft
622 * @attr ref android.R.styleable#View_nextFocusRight
623 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_onClick
625 * @attr ref android.R.styleable#View_padding
626 * @attr ref android.R.styleable#View_paddingBottom
627 * @attr ref android.R.styleable#View_paddingLeft
628 * @attr ref android.R.styleable#View_paddingRight
629 * @attr ref android.R.styleable#View_paddingTop
630 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_rotation
632 * @attr ref android.R.styleable#View_rotationX
633 * @attr ref android.R.styleable#View_rotationY
634 * @attr ref android.R.styleable#View_scaleX
635 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 * @attr ref android.R.styleable#View_scrollX
637 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700638 * @attr ref android.R.styleable#View_scrollbarSize
639 * @attr ref android.R.styleable#View_scrollbarStyle
640 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700641 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
642 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
644 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 * @attr ref android.R.styleable#View_scrollbarThumbVertical
646 * @attr ref android.R.styleable#View_scrollbarTrackVertical
647 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
648 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_soundEffectsEnabled
650 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800651 * @attr ref android.R.styleable#View_transformPivotX
652 * @attr ref android.R.styleable#View_transformPivotY
653 * @attr ref android.R.styleable#View_translationX
654 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700655 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 *
657 * @see android.view.ViewGroup
658 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700659public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
660 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 private static final boolean DBG = false;
662
663 /**
664 * The logging tag used by this class with android.util.Log.
665 */
666 protected static final String VIEW_LOG_TAG = "View";
667
668 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700669 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700670 *
671 * @hide
672 */
673 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * Used to mark a View that has no ID.
677 */
678 public static final int NO_ID = -1;
679
680 /**
681 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
682 * calling setFlags.
683 */
684 private static final int NOT_FOCUSABLE = 0x00000000;
685
686 /**
687 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
688 * setFlags.
689 */
690 private static final int FOCUSABLE = 0x00000001;
691
692 /**
693 * Mask for use with setFlags indicating bits used for focus.
694 */
695 private static final int FOCUSABLE_MASK = 0x00000001;
696
697 /**
698 * This view will adjust its padding to fit sytem windows (e.g. status bar)
699 */
700 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
701
702 /**
Scott Main812634c22011-07-27 13:22:35 -0700703 * This view is visible.
704 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
705 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 */
707 public static final int VISIBLE = 0x00000000;
708
709 /**
710 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700711 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
712 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 */
714 public static final int INVISIBLE = 0x00000004;
715
716 /**
717 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700718 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
719 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 */
721 public static final int GONE = 0x00000008;
722
723 /**
724 * Mask for use with setFlags indicating bits used for visibility.
725 * {@hide}
726 */
727 static final int VISIBILITY_MASK = 0x0000000C;
728
729 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
730
731 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700732 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 * Use with ENABLED_MASK when calling setFlags.
734 * {@hide}
735 */
736 static final int ENABLED = 0x00000000;
737
738 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700739 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Use with ENABLED_MASK when calling setFlags.
741 * {@hide}
742 */
743 static final int DISABLED = 0x00000020;
744
745 /**
746 * Mask for use with setFlags indicating bits used for indicating whether
747 * this view is enabled
748 * {@hide}
749 */
750 static final int ENABLED_MASK = 0x00000020;
751
752 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700753 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
754 * called and further optimizations will be performed. It is okay to have
755 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 * {@hide}
757 */
758 static final int WILL_NOT_DRAW = 0x00000080;
759
760 /**
761 * Mask for use with setFlags indicating bits used for indicating whether
762 * this view is will draw
763 * {@hide}
764 */
765 static final int DRAW_MASK = 0x00000080;
766
767 /**
768 * <p>This view doesn't show scrollbars.</p>
769 * {@hide}
770 */
771 static final int SCROLLBARS_NONE = 0x00000000;
772
773 /**
774 * <p>This view shows horizontal scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
778
779 /**
780 * <p>This view shows vertical scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_VERTICAL = 0x00000200;
784
785 /**
786 * <p>Mask for use with setFlags indicating bits used for indicating which
787 * scrollbars are enabled.</p>
788 * {@hide}
789 */
790 static final int SCROLLBARS_MASK = 0x00000300;
791
Jeff Brown85a31762010-09-01 17:01:00 -0700792 /**
793 * Indicates that the view should filter touches when its window is obscured.
794 * Refer to the class comments for more information about this security feature.
795 * {@hide}
796 */
797 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
798
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700799 /**
800 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
801 * that they are optional and should be skipped if the window has
802 * requested system UI flags that ignore those insets for layout.
803 */
804 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * <p>This view doesn't show fading edges.</p>
808 * {@hide}
809 */
810 static final int FADING_EDGE_NONE = 0x00000000;
811
812 /**
813 * <p>This view shows horizontal fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
817
818 /**
819 * <p>This view shows vertical fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_VERTICAL = 0x00002000;
823
824 /**
825 * <p>Mask for use with setFlags indicating bits used for indicating which
826 * fading edges are enabled.</p>
827 * {@hide}
828 */
829 static final int FADING_EDGE_MASK = 0x00003000;
830
831 /**
832 * <p>Indicates this view can be clicked. When clickable, a View reacts
833 * to clicks by notifying the OnClickListener.<p>
834 * {@hide}
835 */
836 static final int CLICKABLE = 0x00004000;
837
838 /**
839 * <p>Indicates this view is caching its drawing into a bitmap.</p>
840 * {@hide}
841 */
842 static final int DRAWING_CACHE_ENABLED = 0x00008000;
843
844 /**
845 * <p>Indicates that no icicle should be saved for this view.<p>
846 * {@hide}
847 */
848 static final int SAVE_DISABLED = 0x000010000;
849
850 /**
851 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
852 * property.</p>
853 * {@hide}
854 */
855 static final int SAVE_DISABLED_MASK = 0x000010000;
856
857 /**
858 * <p>Indicates that no drawing cache should ever be created for this view.<p>
859 * {@hide}
860 */
861 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
862
863 /**
864 * <p>Indicates this view can take / keep focus when int touch mode.</p>
865 * {@hide}
866 */
867 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
868
869 /**
870 * <p>Enables low quality mode for the drawing cache.</p>
871 */
872 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
873
874 /**
875 * <p>Enables high quality mode for the drawing cache.</p>
876 */
877 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
878
879 /**
880 * <p>Enables automatic quality mode for the drawing cache.</p>
881 */
882 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
883
884 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
885 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
886 };
887
888 /**
889 * <p>Mask for use with setFlags indicating bits used for the cache
890 * quality property.</p>
891 * {@hide}
892 */
893 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
894
895 /**
896 * <p>
897 * Indicates this view can be long clicked. When long clickable, a View
898 * reacts to long clicks by notifying the OnLongClickListener or showing a
899 * context menu.
900 * </p>
901 * {@hide}
902 */
903 static final int LONG_CLICKABLE = 0x00200000;
904
905 /**
906 * <p>Indicates that this view gets its drawable states from its direct parent
907 * and ignores its original internal states.</p>
908 *
909 * @hide
910 */
911 static final int DUPLICATE_PARENT_STATE = 0x00400000;
912
913 /**
914 * The scrollbar style to display the scrollbars inside the content area,
915 * without increasing the padding. The scrollbars will be overlaid with
916 * translucency on the view's content.
917 */
918 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
919
920 /**
921 * The scrollbar style to display the scrollbars inside the padded area,
922 * increasing the padding of the view. The scrollbars will not overlap the
923 * content area of the view.
924 */
925 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
926
927 /**
928 * The scrollbar style to display the scrollbars at the edge of the view,
929 * without increasing the padding. The scrollbars will be overlaid with
930 * translucency.
931 */
932 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
933
934 /**
935 * The scrollbar style to display the scrollbars at the edge of the view,
936 * increasing the padding of the view. The scrollbars will only overlap the
937 * background, if any.
938 */
939 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
940
941 /**
942 * Mask to check if the scrollbar style is overlay or inset.
943 * {@hide}
944 */
945 static final int SCROLLBARS_INSET_MASK = 0x01000000;
946
947 /**
948 * Mask to check if the scrollbar style is inside or outside.
949 * {@hide}
950 */
951 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
952
953 /**
954 * Mask for scrollbar style.
955 * {@hide}
956 */
957 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
958
959 /**
960 * View flag indicating that the screen should remain on while the
961 * window containing this view is visible to the user. This effectively
962 * takes care of automatically setting the WindowManager's
963 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
964 */
965 public static final int KEEP_SCREEN_ON = 0x04000000;
966
967 /**
968 * View flag indicating whether this view should have sound effects enabled
969 * for events such as clicking and touching.
970 */
971 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
972
973 /**
974 * View flag indicating whether this view should have haptic feedback
975 * enabled for events such as long presses.
976 */
977 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
978
979 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700980 * <p>Indicates that the view hierarchy should stop saving state when
981 * it reaches this view. If state saving is initiated immediately at
982 * the view, it will be allowed.
983 * {@hide}
984 */
985 static final int PARENT_SAVE_DISABLED = 0x20000000;
986
987 /**
988 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
992
993 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700994 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
995 * should add all focusable Views regardless if they are focusable in touch mode.
996 */
997 public static final int FOCUSABLES_ALL = 0x00000000;
998
999 /**
1000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add only Views focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1004
1005 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only accessibility focusable Views.
1008 *
1009 * @hide
1010 */
1011 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1012
1013 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001014 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 * item.
1016 */
1017 public static final int FOCUS_BACKWARD = 0x00000001;
1018
1019 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001020 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 * item.
1022 */
1023 public static final int FOCUS_FORWARD = 0x00000002;
1024
1025 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001026 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 */
1028 public static final int FOCUS_LEFT = 0x00000011;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_UP = 0x00000021;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_RIGHT = 0x00000042;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_DOWN = 0x00000082;
1044
Svetoslav Ganov42138042012-03-20 11:51:39 -07001045 // Accessibility focus directions.
1046
1047 /**
1048 * The accessibility focus which is the current user position when
1049 * interacting with the accessibility framework.
1050 */
1051 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1052
1053 /**
1054 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1055 */
1056 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1057
1058 /**
1059 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1060 */
1061 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1062
1063 /**
1064 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1065 */
1066 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1067
1068 /**
1069 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1070 */
1071 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1072
1073 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001074 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001075 */
1076 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1077
1078 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001079 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001080 */
1081 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1082
1083 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001084 * Bits of {@link #getMeasuredWidthAndState()} and
1085 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1086 */
1087 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1088
1089 /**
1090 * Bits of {@link #getMeasuredWidthAndState()} and
1091 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1092 */
1093 public static final int MEASURED_STATE_MASK = 0xff000000;
1094
1095 /**
1096 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1097 * for functions that combine both width and height into a single int,
1098 * such as {@link #getMeasuredState()} and the childState argument of
1099 * {@link #resolveSizeAndState(int, int, int)}.
1100 */
1101 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1102
1103 /**
1104 * Bit of {@link #getMeasuredWidthAndState()} and
1105 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1106 * is smaller that the space the view would like to have.
1107 */
1108 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Base View state sets
1112 */
1113 // Singles
1114 /**
1115 * Indicates the view has no states set. States are used with
1116 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1117 * view depending on its state.
1118 *
1119 * @see android.graphics.drawable.Drawable
1120 * @see #getDrawableState()
1121 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001122 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 /**
1124 * Indicates the view is enabled. States are used with
1125 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1126 * view depending on its state.
1127 *
1128 * @see android.graphics.drawable.Drawable
1129 * @see #getDrawableState()
1130 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001131 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 /**
1133 * Indicates the view is focused. States are used with
1134 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1135 * view depending on its state.
1136 *
1137 * @see android.graphics.drawable.Drawable
1138 * @see #getDrawableState()
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is selected. States are used with
1143 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1144 * view depending on its state.
1145 *
1146 * @see android.graphics.drawable.Drawable
1147 * @see #getDrawableState()
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is pressed. States are used with
1152 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1153 * view depending on its state.
1154 *
1155 * @see android.graphics.drawable.Drawable
1156 * @see #getDrawableState()
1157 * @hide
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view's window has focus. States are used with
1162 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1163 * view depending on its state.
1164 *
1165 * @see android.graphics.drawable.Drawable
1166 * @see #getDrawableState()
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // Doubles
1170 /**
1171 * Indicates the view is enabled and has the focus.
1172 *
1173 * @see #ENABLED_STATE_SET
1174 * @see #FOCUSED_STATE_SET
1175 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001176 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 /**
1178 * Indicates the view is enabled and selected.
1179 *
1180 * @see #ENABLED_STATE_SET
1181 * @see #SELECTED_STATE_SET
1182 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001183 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 /**
1185 * Indicates the view is enabled and that its window has focus.
1186 *
1187 * @see #ENABLED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is focused and selected.
1193 *
1194 * @see #FOCUSED_STATE_SET
1195 * @see #SELECTED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view has the focus and that its window has the focus.
1200 *
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is selected and that its window has the focus.
1207 *
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 // Triples
1213 /**
1214 * Indicates the view is enabled, focused and selected.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is enabled, focused and its window has the focus.
1223 *
1224 * @see #ENABLED_STATE_SET
1225 * @see #FOCUSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is enabled, selected and its window has the focus.
1231 *
1232 * @see #ENABLED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is focused, selected and its window has the focus.
1239 *
1240 * @see #FOCUSED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 * @see #WINDOW_FOCUSED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is enabled, focused, selected and its window
1247 * has the focus.
1248 *
1249 * @see #ENABLED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed and its window has the focus.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #SELECTED_STATE_SET
1274 * @see #WINDOW_FOCUSED_STATE_SET
1275 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001276 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 /**
1278 * Indicates the view is pressed and focused.
1279 *
1280 * @see #PRESSED_STATE_SET
1281 * @see #FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, focused and its window has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, focused and selected.
1294 *
1295 * @see #PRESSED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 * @see #FOCUSED_STATE_SET
1298 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001299 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 /**
1301 * Indicates the view is pressed, focused, selected and its window has the focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #FOCUSED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed and enabled.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled and its window has the focus.
1318 *
1319 * @see #PRESSED_STATE_SET
1320 * @see #ENABLED_STATE_SET
1321 * @see #WINDOW_FOCUSED_STATE_SET
1322 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 /**
1325 * Indicates the view is pressed, enabled and selected.
1326 *
1327 * @see #PRESSED_STATE_SET
1328 * @see #ENABLED_STATE_SET
1329 * @see #SELECTED_STATE_SET
1330 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 /**
1333 * Indicates the view is pressed, enabled, selected and its window has the
1334 * focus.
1335 *
1336 * @see #PRESSED_STATE_SET
1337 * @see #ENABLED_STATE_SET
1338 * @see #SELECTED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and focused.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, focused and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #FOCUSED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled, focused and selected.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #SELECTED_STATE_SET
1366 * @see #FOCUSED_STATE_SET
1367 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 /**
1370 * Indicates the view is pressed, enabled, focused, selected and its window
1371 * has the focus.
1372 *
1373 * @see #PRESSED_STATE_SET
1374 * @see #ENABLED_STATE_SET
1375 * @see #SELECTED_STATE_SET
1376 * @see #FOCUSED_STATE_SET
1377 * @see #WINDOW_FOCUSED_STATE_SET
1378 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 /**
1382 * The order here is very important to {@link #getDrawableState()}
1383 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 private static final int[][] VIEW_STATE_SETS;
1385
Romain Guyb051e892010-09-28 19:09:36 -07001386 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1387 static final int VIEW_STATE_SELECTED = 1 << 1;
1388 static final int VIEW_STATE_FOCUSED = 1 << 2;
1389 static final int VIEW_STATE_ENABLED = 1 << 3;
1390 static final int VIEW_STATE_PRESSED = 1 << 4;
1391 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001392 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001393 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001394 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1395 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001396
1397 static final int[] VIEW_STATE_IDS = new int[] {
1398 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1399 R.attr.state_selected, VIEW_STATE_SELECTED,
1400 R.attr.state_focused, VIEW_STATE_FOCUSED,
1401 R.attr.state_enabled, VIEW_STATE_ENABLED,
1402 R.attr.state_pressed, VIEW_STATE_PRESSED,
1403 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001404 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001405 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001406 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001407 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 };
1409
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001410 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001411 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1412 throw new IllegalStateException(
1413 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1414 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001415 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001416 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001417 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001418 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001420 orderedIds[i * 2] = viewState;
1421 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001422 }
1423 }
1424 }
Romain Guyb051e892010-09-28 19:09:36 -07001425 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1426 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1427 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 int numBits = Integer.bitCount(i);
1429 int[] set = new int[numBits];
1430 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001431 for (int j = 0; j < orderedIds.length; j += 2) {
1432 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 set[pos++] = orderedIds[j];
1434 }
1435 }
1436 VIEW_STATE_SETS[i] = set;
1437 }
1438
1439 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1440 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1441 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1442 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1444 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1445 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1447 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1449 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1451 | VIEW_STATE_FOCUSED];
1452 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1453 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1455 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1457 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_ENABLED];
1460 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED];
1468 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1471
1472 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1473 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1475 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1477 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_PRESSED];
1480 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1487 | VIEW_STATE_PRESSED];
1488 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1490 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1491 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1498 | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1501 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1504 | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1510 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1513 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1514 | VIEW_STATE_PRESSED];
1515 }
1516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001518 * Accessibility event types that are dispatched for text population.
1519 */
1520 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1521 AccessibilityEvent.TYPE_VIEW_CLICKED
1522 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1523 | AccessibilityEvent.TYPE_VIEW_SELECTED
1524 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1525 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1526 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001527 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001528 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001529 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001530 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1531 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001532
1533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 * Temporary Rect currently for use in setBackground(). This will probably
1535 * be extended in the future to hold our own class with more than just
1536 * a Rect. :)
1537 */
1538 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001539
1540 /**
1541 * Map used to store views' tags.
1542 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001543 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001546 * The next available accessiiblity id.
1547 */
1548 private static int sNextAccessibilityViewId;
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * The animation currently associated with this view.
1552 * @hide
1553 */
1554 protected Animation mCurrentAnimation = null;
1555
1556 /**
1557 * Width as measured during measure pass.
1558 * {@hide}
1559 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001560 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001561 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
1563 /**
1564 * Height as measured during measure pass.
1565 * {@hide}
1566 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001567 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001568 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001571 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1572 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1573 * its display list. This flag, used only when hw accelerated, allows us to clear the
1574 * flag while retaining this information until it's needed (at getDisplayList() time and
1575 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1576 *
1577 * {@hide}
1578 */
1579 boolean mRecreateDisplayList = false;
1580
1581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 * The view's identifier.
1583 * {@hide}
1584 *
1585 * @see #setId(int)
1586 * @see #getId()
1587 */
1588 @ViewDebug.ExportedProperty(resolveId = true)
1589 int mID = NO_ID;
1590
1591 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001592 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001593 */
1594 int mAccessibilityViewId = NO_ID;
1595
1596 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001597 * @hide
1598 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001599 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001600
1601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 * The view's tag.
1603 * {@hide}
1604 *
1605 * @see #setTag(Object)
1606 * @see #getTag()
1607 */
1608 protected Object mTag;
1609
1610 // for mPrivateFlags:
1611 /** {@hide} */
1612 static final int WANTS_FOCUS = 0x00000001;
1613 /** {@hide} */
1614 static final int FOCUSED = 0x00000002;
1615 /** {@hide} */
1616 static final int SELECTED = 0x00000004;
1617 /** {@hide} */
1618 static final int IS_ROOT_NAMESPACE = 0x00000008;
1619 /** {@hide} */
1620 static final int HAS_BOUNDS = 0x00000010;
1621 /** {@hide} */
1622 static final int DRAWN = 0x00000020;
1623 /**
1624 * When this flag is set, this view is running an animation on behalf of its
1625 * children and should therefore not cancel invalidate requests, even if they
1626 * lie outside of this view's bounds.
1627 *
1628 * {@hide}
1629 */
1630 static final int DRAW_ANIMATION = 0x00000040;
1631 /** {@hide} */
1632 static final int SKIP_DRAW = 0x00000080;
1633 /** {@hide} */
1634 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1635 /** {@hide} */
1636 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1637 /** {@hide} */
1638 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1639 /** {@hide} */
1640 static final int MEASURED_DIMENSION_SET = 0x00000800;
1641 /** {@hide} */
1642 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001643 /** {@hide} */
1644 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
1646 private static final int PRESSED = 0x00004000;
1647
1648 /** {@hide} */
1649 static final int DRAWING_CACHE_VALID = 0x00008000;
1650 /**
1651 * Flag used to indicate that this view should be drawn once more (and only once
1652 * more) after its animation has completed.
1653 * {@hide}
1654 */
1655 static final int ANIMATION_STARTED = 0x00010000;
1656
1657 private static final int SAVE_STATE_CALLED = 0x00020000;
1658
1659 /**
1660 * Indicates that the View returned true when onSetAlpha() was called and that
1661 * the alpha must be restored.
1662 * {@hide}
1663 */
1664 static final int ALPHA_SET = 0x00040000;
1665
1666 /**
1667 * Set by {@link #setScrollContainer(boolean)}.
1668 */
1669 static final int SCROLL_CONTAINER = 0x00080000;
1670
1671 /**
1672 * Set by {@link #setScrollContainer(boolean)}.
1673 */
1674 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1675
1676 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001677 * View flag indicating whether this view was invalidated (fully or partially.)
1678 *
1679 * @hide
1680 */
1681 static final int DIRTY = 0x00200000;
1682
1683 /**
1684 * View flag indicating whether this view was invalidated by an opaque
1685 * invalidate request.
1686 *
1687 * @hide
1688 */
1689 static final int DIRTY_OPAQUE = 0x00400000;
1690
1691 /**
1692 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1693 *
1694 * @hide
1695 */
1696 static final int DIRTY_MASK = 0x00600000;
1697
1698 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001699 * Indicates whether the background is opaque.
1700 *
1701 * @hide
1702 */
1703 static final int OPAQUE_BACKGROUND = 0x00800000;
1704
1705 /**
1706 * Indicates whether the scrollbars are opaque.
1707 *
1708 * @hide
1709 */
1710 static final int OPAQUE_SCROLLBARS = 0x01000000;
1711
1712 /**
1713 * Indicates whether the view is opaque.
1714 *
1715 * @hide
1716 */
1717 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001718
Adam Powelle14579b2009-12-16 18:39:52 -08001719 /**
1720 * Indicates a prepressed state;
1721 * the short time between ACTION_DOWN and recognizing
1722 * a 'real' press. Prepressed is used to recognize quick taps
1723 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001724 *
Adam Powelle14579b2009-12-16 18:39:52 -08001725 * @hide
1726 */
1727 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001728
Adam Powellc9fbaab2010-02-16 17:16:19 -08001729 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001730 * Indicates whether the view is temporarily detached.
1731 *
1732 * @hide
1733 */
1734 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001735
Adam Powell8568c3a2010-04-19 14:26:11 -07001736 /**
1737 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001738 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001739 * @hide
1740 */
1741 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001742
1743 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001744 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1745 * @hide
1746 */
1747 private static final int HOVERED = 0x10000000;
1748
1749 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001750 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1751 * for transform operations
1752 *
1753 * @hide
1754 */
Adam Powellf37df072010-09-17 16:22:49 -07001755 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001756
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001757 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001758 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001759
Chet Haasefd2b0022010-08-06 13:08:56 -07001760 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 * Indicates that this view was specifically invalidated, not just dirtied because some
1762 * child view was invalidated. The flag is used to determine when we need to recreate
1763 * a view's display list (as opposed to just returning a reference to its existing
1764 * display list).
1765 *
1766 * @hide
1767 */
1768 static final int INVALIDATED = 0x80000000;
1769
Christopher Tate3d4bf172011-03-28 16:16:46 -07001770 /* Masks for mPrivateFlags2 */
1771
1772 /**
1773 * Indicates that this view has reported that it can accept the current drag's content.
1774 * Cleared when the drag operation concludes.
1775 * @hide
1776 */
1777 static final int DRAG_CAN_ACCEPT = 0x00000001;
1778
1779 /**
1780 * Indicates that this view is currently directly under the drag location in a
1781 * drag-and-drop operation involving content that it can accept. Cleared when
1782 * the drag exits the view, or when the drag operation concludes.
1783 * @hide
1784 */
1785 static final int DRAG_HOVERED = 0x00000002;
1786
Cibu Johny86666632010-02-22 13:01:02 -08001787 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001788 * Horizontal layout direction of this view is from Left to Right.
1789 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001790 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001791 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001792 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001793
1794 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795 * Horizontal layout direction of this view is from Right to Left.
1796 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001797 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001799 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001800
1801 /**
1802 * Horizontal layout direction of this view is inherited from its parent.
1803 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001804 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001806 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001807
1808 /**
1809 * Horizontal layout direction of this view is from deduced from the default language
1810 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001811 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001817 * @hide
1818 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001819 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1820
1821 /**
1822 * Mask for use with private flags indicating bits used for horizontal layout direction.
1823 * @hide
1824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001825 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1829 * right-to-left direction.
1830 * @hide
1831 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001832 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833
1834 /**
1835 * Indicates whether the view horizontal layout direction has been resolved.
1836 * @hide
1837 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001838 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1842 * @hide
1843 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001844 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /*
1847 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1848 * flag value.
1849 * @hide
1850 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001851 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1852 LAYOUT_DIRECTION_LTR,
1853 LAYOUT_DIRECTION_RTL,
1854 LAYOUT_DIRECTION_INHERIT,
1855 LAYOUT_DIRECTION_LOCALE
1856 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001857
1858 /**
1859 * Default horizontal layout direction.
1860 * @hide
1861 */
1862 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001863
Adam Powell539ee872012-02-03 19:00:49 -08001864 /**
1865 * Indicates that the view is tracking some sort of transient state
1866 * that the app should not need to be aware of, but that the framework
1867 * should take special care to preserve.
1868 *
1869 * @hide
1870 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001872
1873
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001874 /**
1875 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001876 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 */
1878 public static final int TEXT_DIRECTION_INHERIT = 0;
1879
1880 /**
1881 * Text direction is using "first strong algorithm". The first strong directional character
1882 * determines the paragraph direction. If there is no strong directional character, the
1883 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001884 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001885 */
1886 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1887
1888 /**
1889 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1890 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1891 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001892 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001893 */
1894 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1895
1896 /**
1897 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001898 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001899 */
1900 public static final int TEXT_DIRECTION_LTR = 3;
1901
1902 /**
1903 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001904 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001910 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001916 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917 */
1918 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1919
1920 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001921 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1922 * @hide
1923 */
1924 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1925
1926 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001927 * Mask for use with private flags indicating bits used for text direction.
1928 * @hide
1929 */
1930 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
1937 private static final int[] TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
1950 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
1956 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
1962 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1963
1964 /**
1965 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1966 * @hide
1967 */
1968 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1969 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001971 /*
1972 * Default text alignment. The text alignment of this View is inherited from its parent.
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001983 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001991 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001992 */
1993 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1994
1995 /**
1996 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1997 *
1998 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001999 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002000 */
2001 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2002
2003 /**
2004 * Center the paragraph, e.g. ALIGN_CENTER.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_CENTER = 4;
2010
2011 /**
2012 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2019
2020 /**
2021 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2022 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2023 *
2024 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002025 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002031 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002032 */
2033 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2034
2035 /**
2036 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2037 * @hide
2038 */
2039 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2040
2041 /**
2042 * Mask for use with private flags indicating bits used for text alignment.
2043 * @hide
2044 */
2045 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2046
2047 /**
2048 * Array of text direction flags for mapping attribute "textAlignment" to correct
2049 * flag value.
2050 * @hide
2051 */
2052 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2053 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2059 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2060 };
2061
2062 /**
2063 * Indicates whether the view text alignment has been resolved.
2064 * @hide
2065 */
2066 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2067
2068 /**
2069 * Bit shift to get the resolved text alignment.
2070 * @hide
2071 */
2072 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2073
2074 /**
2075 * Mask for use with private flags indicating bits used for text alignment.
2076 * @hide
2077 */
2078 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2079
2080 /**
2081 * Indicates whether if the view text alignment has been resolved to gravity
2082 */
2083 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2084 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2085
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 // Accessiblity constants for mPrivateFlags2
2087
2088 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002089 * Shift for the bits in {@link #mPrivateFlags2} related to the
2090 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002091 */
2092 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2093
2094 /**
2095 * Automatically determine whether a view is important for accessibility.
2096 */
2097 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2098
2099 /**
2100 * The view is important for accessibility.
2101 */
2102 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2103
2104 /**
2105 * The view is not important for accessibility.
2106 */
2107 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2108
2109 /**
2110 * The default whether the view is important for accessiblity.
2111 */
2112 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2113
2114 /**
2115 * Mask for obtainig the bits which specify how to determine
2116 * whether a view is important for accessibility.
2117 */
2118 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2119 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2120 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2121
2122 /**
2123 * Flag indicating whether a view has accessibility focus.
2124 */
2125 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2126
2127 /**
2128 * Flag indicating whether a view state for accessibility has changed.
2129 */
2130 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002131
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002132 /**
2133 * Flag indicating that view has an animation set on it. This is used to track whether an
2134 * animation is cleared between successive frames, in order to tell the associated
2135 * DisplayList to clear its animation matrix.
2136 */
2137 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x10000000;
2138
Chet Haase1a3ab172012-05-11 08:41:20 -07002139 /**
2140 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2141 * is used to check whether later changes to the view's transform should invalidate the
2142 * view to force the quickReject test to run again.
2143 */
2144 static final int VIEW_QUICK_REJECTED = 0x20000000;
2145
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002146 // Accessiblity constants for mPrivateFlags2
2147
2148 /**
2149 * Shift for the bits in {@link #mPrivateFlags2} related to the
2150 * "accessibilityFocusable" attribute.
2151 */
2152 static final int ACCESSIBILITY_FOCUSABLE_SHIFT = 30;
2153
2154 /**
2155 * The system determines whether the view can take accessibility focus - default (recommended).
2156 * <p>
2157 * Such a view is consideted by the focus search if it is:
2158 * <ul>
2159 * <li>
2160 * Important for accessibility and actionable (clickable, long clickable, focusable)
2161 * </li>
2162 * <li>
2163 * Important for accessibility, not actionable (clickable, long clickable, focusable),
2164 * and does not have an actionable predecessor.
2165 * </li>
2166 * </ul>
2167 * An accessibility srvice can request putting accessibility focus on such a view.
2168 * </p>
2169 *
2170 * @hide
2171 */
2172 public static final int ACCESSIBILITY_FOCUSABLE_AUTO = 0x00000000;
2173
2174 /**
2175 * The view can take accessibility focus.
2176 * <p>
2177 * A view that can take accessibility focus is always considered during focus
2178 * search and an accessibility service can request putting accessibility focus
2179 * on it.
2180 * </p>
2181 *
2182 * @hide
2183 */
2184 public static final int ACCESSIBILITY_FOCUSABLE_YES = 0x00000001;
2185
2186 /**
2187 * The view can not take accessibility focus.
2188 * <p>
2189 * A view that can not take accessibility focus is never considered during focus
2190 * search and an accessibility service can not request putting accessibility focus
2191 * on it.
2192 * </p>
2193 *
2194 * @hide
2195 */
2196 public static final int ACCESSIBILITY_FOCUSABLE_NO = 0x00000002;
2197
2198 /**
2199 * The default whether the view is accessiblity focusable.
2200 */
2201 static final int ACCESSIBILITY_FOCUSABLE_DEFAULT = ACCESSIBILITY_FOCUSABLE_AUTO;
2202
2203 /**
2204 * Mask for obtainig the bits which specifies how to determine
2205 * whether a view is accessibility focusable.
2206 */
2207 static final int ACCESSIBILITY_FOCUSABLE_MASK = (ACCESSIBILITY_FOCUSABLE_AUTO
2208 | ACCESSIBILITY_FOCUSABLE_YES | ACCESSIBILITY_FOCUSABLE_NO)
2209 << ACCESSIBILITY_FOCUSABLE_SHIFT;
2210
2211
Christopher Tate3d4bf172011-03-28 16:16:46 -07002212 /* End of masks for mPrivateFlags2 */
2213
2214 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2215
Chet Haasedaf98e92011-01-10 14:10:36 -08002216 /**
Adam Powell637d3372010-08-25 14:37:03 -07002217 * Always allow a user to over-scroll this view, provided it is a
2218 * view that can scroll.
2219 *
2220 * @see #getOverScrollMode()
2221 * @see #setOverScrollMode(int)
2222 */
2223 public static final int OVER_SCROLL_ALWAYS = 0;
2224
2225 /**
2226 * Allow a user to over-scroll this view only if the content is large
2227 * enough to meaningfully scroll, provided it is a view that can scroll.
2228 *
2229 * @see #getOverScrollMode()
2230 * @see #setOverScrollMode(int)
2231 */
2232 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2233
2234 /**
2235 * Never allow a user to over-scroll this view.
2236 *
2237 * @see #getOverScrollMode()
2238 * @see #setOverScrollMode(int)
2239 */
2240 public static final int OVER_SCROLL_NEVER = 2;
2241
2242 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002243 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2244 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002245 *
Joe Malin32736f02011-01-19 16:14:20 -08002246 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002247 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002248 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002249
2250 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002251 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2252 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002253 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002254 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002255 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002256 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002257 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002258 *
Joe Malin32736f02011-01-19 16:14:20 -08002259 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002260 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002261 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2262
2263 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002264 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2265 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002266 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002267 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002268 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2269 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2270 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002271 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002272 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2273 * window flags) for displaying content using every last pixel on the display.
2274 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002275 * <p>There is a limitation: because navigation controls are so important, the least user
2276 * interaction will cause them to reappear immediately. When this happens, both
2277 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2278 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002279 *
2280 * @see #setSystemUiVisibility(int)
2281 */
2282 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2283
2284 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002285 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2286 * into the normal fullscreen mode so that its content can take over the screen
2287 * while still allowing the user to interact with the application.
2288 *
2289 * <p>This has the same visual effect as
2290 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2291 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2292 * meaning that non-critical screen decorations (such as the status bar) will be
2293 * hidden while the user is in the View's window, focusing the experience on
2294 * that content. Unlike the window flag, if you are using ActionBar in
2295 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2296 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2297 * hide the action bar.
2298 *
2299 * <p>This approach to going fullscreen is best used over the window flag when
2300 * it is a transient state -- that is, the application does this at certain
2301 * points in its user interaction where it wants to allow the user to focus
2302 * on content, but not as a continuous state. For situations where the application
2303 * would like to simply stay full screen the entire time (such as a game that
2304 * wants to take over the screen), the
2305 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2306 * is usually a better approach. The state set here will be removed by the system
2307 * in various situations (such as the user moving to another application) like
2308 * the other system UI states.
2309 *
2310 * <p>When using this flag, the application should provide some easy facility
2311 * for the user to go out of it. A common example would be in an e-book
2312 * reader, where tapping on the screen brings back whatever screen and UI
2313 * decorations that had been hidden while the user was immersed in reading
2314 * the book.
2315 *
2316 * @see #setSystemUiVisibility(int)
2317 */
2318 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2319
2320 /**
2321 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2322 * flags, we would like a stable view of the content insets given to
2323 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2324 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002325 * as a continuous state. In the stock Android UI this is the space for
2326 * the system bar, nav bar, and status bar, but not more transient elements
2327 * such as an input method.
2328 *
2329 * The stable layout your UI sees is based on the system UI modes you can
2330 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2331 * then you will get a stable layout for changes of the
2332 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2333 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2334 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2335 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2336 * with a stable layout. (Note that you should avoid using
2337 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2338 *
2339 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2340 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2341 * then a hidden status bar will be considered a "stable" state for purposes
2342 * here. This allows your UI to continually hide the status bar, while still
2343 * using the system UI flags to hide the action bar while still retaining
2344 * a stable layout. Note that changing the window fullscreen flag will never
2345 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002346 *
2347 * <p>If you are using ActionBar in
2348 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2349 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2350 * insets it adds to those given to the application.
2351 */
2352 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2353
2354 /**
2355 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2356 * to be layed out as if it has requested
2357 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2358 * allows it to avoid artifacts when switching in and out of that mode, at
2359 * the expense that some of its user interface may be covered by screen
2360 * decorations when they are shown. You can perform layout of your inner
2361 * UI elements to account for the navagation system UI through the
2362 * {@link #fitSystemWindows(Rect)} method.
2363 */
2364 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2365
2366 /**
2367 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2368 * to be layed out as if it has requested
2369 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2370 * allows it to avoid artifacts when switching in and out of that mode, at
2371 * the expense that some of its user interface may be covered by screen
2372 * decorations when they are shown. You can perform layout of your inner
2373 * UI elements to account for non-fullscreen system UI through the
2374 * {@link #fitSystemWindows(Rect)} method.
2375 */
2376 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2377
2378 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002379 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2380 */
2381 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2382
2383 /**
2384 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2385 */
2386 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002387
2388 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002389 * @hide
2390 *
2391 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2392 * out of the public fields to keep the undefined bits out of the developer's way.
2393 *
2394 * Flag to make the status bar not expandable. Unless you also
2395 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2396 */
2397 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2398
2399 /**
2400 * @hide
2401 *
2402 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2403 * out of the public fields to keep the undefined bits out of the developer's way.
2404 *
2405 * Flag to hide notification icons and scrolling ticker text.
2406 */
2407 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2408
2409 /**
2410 * @hide
2411 *
2412 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2413 * out of the public fields to keep the undefined bits out of the developer's way.
2414 *
2415 * Flag to disable incoming notification alerts. This will not block
2416 * icons, but it will block sound, vibrating and other visual or aural notifications.
2417 */
2418 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2419
2420 /**
2421 * @hide
2422 *
2423 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2424 * out of the public fields to keep the undefined bits out of the developer's way.
2425 *
2426 * Flag to hide only the scrolling ticker. Note that
2427 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2428 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2429 */
2430 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2431
2432 /**
2433 * @hide
2434 *
2435 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2436 * out of the public fields to keep the undefined bits out of the developer's way.
2437 *
2438 * Flag to hide the center system info area.
2439 */
2440 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2441
2442 /**
2443 * @hide
2444 *
2445 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2446 * out of the public fields to keep the undefined bits out of the developer's way.
2447 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002448 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002449 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2450 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002451 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002452
2453 /**
2454 * @hide
2455 *
2456 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2457 * out of the public fields to keep the undefined bits out of the developer's way.
2458 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002459 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002460 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2461 */
2462 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2463
2464 /**
2465 * @hide
2466 *
2467 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2468 * out of the public fields to keep the undefined bits out of the developer's way.
2469 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002470 * Flag to hide only the clock. You might use this if your activity has
2471 * its own clock making the status bar's clock redundant.
2472 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002473 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2474
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002475 /**
2476 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002477 *
2478 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2479 * out of the public fields to keep the undefined bits out of the developer's way.
2480 *
2481 * Flag to hide only the recent apps button. Don't use this
2482 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2483 */
2484 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2485
2486 /**
2487 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002488 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002489 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002490
2491 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002492 * These are the system UI flags that can be cleared by events outside
2493 * of an application. Currently this is just the ability to tap on the
2494 * screen while hiding the navigation bar to have it return.
2495 * @hide
2496 */
2497 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002498 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2499 | SYSTEM_UI_FLAG_FULLSCREEN;
2500
2501 /**
2502 * Flags that can impact the layout in relation to system UI.
2503 */
2504 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2505 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2506 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002507
2508 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002509 * Find views that render the specified text.
2510 *
2511 * @see #findViewsWithText(ArrayList, CharSequence, int)
2512 */
2513 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2514
2515 /**
2516 * Find find views that contain the specified content description.
2517 *
2518 * @see #findViewsWithText(ArrayList, CharSequence, int)
2519 */
2520 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2521
2522 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002523 * Find views that contain {@link AccessibilityNodeProvider}. Such
2524 * a View is a root of virtual view hierarchy and may contain the searched
2525 * text. If this flag is set Views with providers are automatically
2526 * added and it is a responsibility of the client to call the APIs of
2527 * the provider to determine whether the virtual tree rooted at this View
2528 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2529 * represeting the virtual views with this text.
2530 *
2531 * @see #findViewsWithText(ArrayList, CharSequence, int)
2532 *
2533 * @hide
2534 */
2535 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2536
2537 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002538 * The undefined cursor position.
2539 */
2540 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2541
2542 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002543 * Indicates that the screen has changed state and is now off.
2544 *
2545 * @see #onScreenStateChanged(int)
2546 */
2547 public static final int SCREEN_STATE_OFF = 0x0;
2548
2549 /**
2550 * Indicates that the screen has changed state and is now on.
2551 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002552 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002553 */
2554 public static final int SCREEN_STATE_ON = 0x1;
2555
2556 /**
Adam Powell637d3372010-08-25 14:37:03 -07002557 * Controls the over-scroll mode for this view.
2558 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2559 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2560 * and {@link #OVER_SCROLL_NEVER}.
2561 */
2562 private int mOverScrollMode;
2563
2564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 * The parent this view is attached to.
2566 * {@hide}
2567 *
2568 * @see #getParent()
2569 */
2570 protected ViewParent mParent;
2571
2572 /**
2573 * {@hide}
2574 */
2575 AttachInfo mAttachInfo;
2576
2577 /**
2578 * {@hide}
2579 */
Romain Guy809a7f62009-05-14 15:44:42 -07002580 @ViewDebug.ExportedProperty(flagMapping = {
2581 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2582 name = "FORCE_LAYOUT"),
2583 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2584 name = "LAYOUT_REQUIRED"),
2585 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002586 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002587 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2588 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2589 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2590 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2591 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002593 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594
2595 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002596 * This view's request for the visibility of the status bar.
2597 * @hide
2598 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002599 @ViewDebug.ExportedProperty(flagMapping = {
2600 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2601 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2602 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2603 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2604 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2605 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2606 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2607 equals = SYSTEM_UI_FLAG_VISIBLE,
2608 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2609 })
Joe Onorato664644d2011-01-23 17:53:23 -08002610 int mSystemUiVisibility;
2611
2612 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002613 * Reference count for transient state.
2614 * @see #setHasTransientState(boolean)
2615 */
2616 int mTransientStateCount = 0;
2617
2618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 * Count of how many windows this view has been attached to.
2620 */
2621 int mWindowAttachCount;
2622
2623 /**
2624 * The layout parameters associated with this view and used by the parent
2625 * {@link android.view.ViewGroup} to determine how this view should be
2626 * laid out.
2627 * {@hide}
2628 */
2629 protected ViewGroup.LayoutParams mLayoutParams;
2630
2631 /**
2632 * The view flags hold various views states.
2633 * {@hide}
2634 */
2635 @ViewDebug.ExportedProperty
2636 int mViewFlags;
2637
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002638 static class TransformationInfo {
2639 /**
2640 * The transform matrix for the View. This transform is calculated internally
2641 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2642 * is used by default. Do *not* use this variable directly; instead call
2643 * getMatrix(), which will automatically recalculate the matrix if necessary
2644 * to get the correct matrix based on the latest rotation and scale properties.
2645 */
2646 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002647
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002648 /**
2649 * The transform matrix for the View. This transform is calculated internally
2650 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2651 * is used by default. Do *not* use this variable directly; instead call
2652 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2653 * to get the correct matrix based on the latest rotation and scale properties.
2654 */
2655 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002656
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002657 /**
2658 * An internal variable that tracks whether we need to recalculate the
2659 * transform matrix, based on whether the rotation or scaleX/Y properties
2660 * have changed since the matrix was last calculated.
2661 */
2662 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002663
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002664 /**
2665 * An internal variable that tracks whether we need to recalculate the
2666 * transform matrix, based on whether the rotation or scaleX/Y properties
2667 * have changed since the matrix was last calculated.
2668 */
2669 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002670
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002671 /**
2672 * A variable that tracks whether we need to recalculate the
2673 * transform matrix, based on whether the rotation or scaleX/Y properties
2674 * have changed since the matrix was last calculated. This variable
2675 * is only valid after a call to updateMatrix() or to a function that
2676 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2677 */
2678 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002679
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002680 /**
2681 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2682 */
2683 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002684
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002685 /**
2686 * This matrix is used when computing the matrix for 3D rotations.
2687 */
2688 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002689
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002690 /**
2691 * These prev values are used to recalculate a centered pivot point when necessary. The
2692 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2693 * set), so thes values are only used then as well.
2694 */
2695 private int mPrevWidth = -1;
2696 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002697
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002698 /**
2699 * The degrees rotation around the vertical axis through the pivot point.
2700 */
2701 @ViewDebug.ExportedProperty
2702 float mRotationY = 0f;
2703
2704 /**
2705 * The degrees rotation around the horizontal axis through the pivot point.
2706 */
2707 @ViewDebug.ExportedProperty
2708 float mRotationX = 0f;
2709
2710 /**
2711 * The degrees rotation around the pivot point.
2712 */
2713 @ViewDebug.ExportedProperty
2714 float mRotation = 0f;
2715
2716 /**
2717 * The amount of translation of the object away from its left property (post-layout).
2718 */
2719 @ViewDebug.ExportedProperty
2720 float mTranslationX = 0f;
2721
2722 /**
2723 * The amount of translation of the object away from its top property (post-layout).
2724 */
2725 @ViewDebug.ExportedProperty
2726 float mTranslationY = 0f;
2727
2728 /**
2729 * The amount of scale in the x direction around the pivot point. A
2730 * value of 1 means no scaling is applied.
2731 */
2732 @ViewDebug.ExportedProperty
2733 float mScaleX = 1f;
2734
2735 /**
2736 * The amount of scale in the y direction around the pivot point. A
2737 * value of 1 means no scaling is applied.
2738 */
2739 @ViewDebug.ExportedProperty
2740 float mScaleY = 1f;
2741
2742 /**
Chet Haasea33de552012-02-03 16:28:24 -08002743 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002744 */
2745 @ViewDebug.ExportedProperty
2746 float mPivotX = 0f;
2747
2748 /**
Chet Haasea33de552012-02-03 16:28:24 -08002749 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002750 */
2751 @ViewDebug.ExportedProperty
2752 float mPivotY = 0f;
2753
2754 /**
2755 * The opacity of the View. This is a value from 0 to 1, where 0 means
2756 * completely transparent and 1 means completely opaque.
2757 */
2758 @ViewDebug.ExportedProperty
2759 float mAlpha = 1f;
2760 }
2761
2762 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002763
Joe Malin32736f02011-01-19 16:14:20 -08002764 private boolean mLastIsOpaque;
2765
Chet Haasefd2b0022010-08-06 13:08:56 -07002766 /**
2767 * Convenience value to check for float values that are close enough to zero to be considered
2768 * zero.
2769 */
Romain Guy2542d192010-08-18 11:47:12 -07002770 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002771
2772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 * The distance in pixels from the left edge of this view's parent
2774 * to the left edge of this view.
2775 * {@hide}
2776 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002777 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 protected int mLeft;
2779 /**
2780 * The distance in pixels from the left edge of this view's parent
2781 * to the right edge of this view.
2782 * {@hide}
2783 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002784 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 protected int mRight;
2786 /**
2787 * The distance in pixels from the top edge of this view's parent
2788 * to the top edge of this view.
2789 * {@hide}
2790 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002791 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 protected int mTop;
2793 /**
2794 * The distance in pixels from the top edge of this view's parent
2795 * to the bottom edge of this view.
2796 * {@hide}
2797 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002798 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 protected int mBottom;
2800
2801 /**
2802 * The offset, in pixels, by which the content of this view is scrolled
2803 * horizontally.
2804 * {@hide}
2805 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002806 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 protected int mScrollX;
2808 /**
2809 * The offset, in pixels, by which the content of this view is scrolled
2810 * vertically.
2811 * {@hide}
2812 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002813 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 protected int mScrollY;
2815
2816 /**
2817 * The left padding in pixels, that is the distance in pixels between the
2818 * left edge of this view and the left edge of its content.
2819 * {@hide}
2820 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002821 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 protected int mPaddingLeft;
2823 /**
2824 * The right padding in pixels, that is the distance in pixels between the
2825 * right edge of this view and the right edge of its content.
2826 * {@hide}
2827 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002828 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 protected int mPaddingRight;
2830 /**
2831 * The top padding in pixels, that is the distance in pixels between the
2832 * top edge of this view and the top edge of its content.
2833 * {@hide}
2834 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002835 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 protected int mPaddingTop;
2837 /**
2838 * The bottom padding in pixels, that is the distance in pixels between the
2839 * bottom edge of this view and the bottom edge of its content.
2840 * {@hide}
2841 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002842 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 protected int mPaddingBottom;
2844
2845 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002846 * The layout insets in pixels, that is the distance in pixels between the
2847 * visible edges of this view its bounds.
2848 */
2849 private Insets mLayoutInsets;
2850
2851 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002852 * Briefly describes the view and is primarily used for accessibility support.
2853 */
2854 private CharSequence mContentDescription;
2855
2856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002858 *
2859 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002861 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002862 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863
2864 /**
2865 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002866 *
2867 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002869 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002870 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002872 /**
Adam Powell20232d02010-12-08 21:08:53 -08002873 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002874 *
2875 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002876 */
2877 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002878 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002879
2880 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002881 * Cache if the user padding is relative.
2882 *
2883 */
2884 @ViewDebug.ExportedProperty(category = "padding")
2885 boolean mUserPaddingRelative;
2886
2887 /**
2888 * Cache the paddingStart set by the user to append to the scrollbar's size.
2889 *
2890 */
2891 @ViewDebug.ExportedProperty(category = "padding")
2892 int mUserPaddingStart;
2893
2894 /**
2895 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2896 *
2897 */
2898 @ViewDebug.ExportedProperty(category = "padding")
2899 int mUserPaddingEnd;
2900
2901 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002902 * @hide
2903 */
2904 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2905 /**
2906 * @hide
2907 */
2908 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
Philip Milne6c8ea062012-04-03 17:38:43 -07002910 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911
2912 private int mBackgroundResource;
2913 private boolean mBackgroundSizeChanged;
2914
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002915 static class ListenerInfo {
2916 /**
2917 * Listener used to dispatch focus change events.
2918 * This field should be made private, so it is hidden from the SDK.
2919 * {@hide}
2920 */
2921 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002923 /**
2924 * Listeners for layout change events.
2925 */
2926 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002927
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002928 /**
2929 * Listeners for attach events.
2930 */
2931 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002932
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002933 /**
2934 * Listener used to dispatch click events.
2935 * This field should be made private, so it is hidden from the SDK.
2936 * {@hide}
2937 */
2938 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002940 /**
2941 * Listener used to dispatch long click events.
2942 * This field should be made private, so it is hidden from the SDK.
2943 * {@hide}
2944 */
2945 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002947 /**
2948 * Listener used to build the context menu.
2949 * This field should be made private, so it is hidden from the SDK.
2950 * {@hide}
2951 */
2952 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002954 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002956 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002958 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002961
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002962 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002963
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002964 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2965 }
2966
2967 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 /**
2970 * The application environment this view lives in.
2971 * This field should be made private, so it is hidden from the SDK.
2972 * {@hide}
2973 */
2974 protected Context mContext;
2975
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002976 private final Resources mResources;
2977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 private ScrollabilityCache mScrollCache;
2979
2980 private int[] mDrawableState = null;
2981
Romain Guy0211a0a2011-02-14 16:34:59 -08002982 /**
2983 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002984 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002985 * @hide
2986 */
2987 public boolean mCachingFailed;
2988
Romain Guy02890fd2010-08-06 17:58:44 -07002989 private Bitmap mDrawingCache;
2990 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002991 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002992 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993
2994 /**
2995 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2996 * the user may specify which view to go to next.
2997 */
2998 private int mNextFocusLeftId = View.NO_ID;
2999
3000 /**
3001 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3002 * the user may specify which view to go to next.
3003 */
3004 private int mNextFocusRightId = View.NO_ID;
3005
3006 /**
3007 * When this view has focus and the next focus is {@link #FOCUS_UP},
3008 * the user may specify which view to go to next.
3009 */
3010 private int mNextFocusUpId = View.NO_ID;
3011
3012 /**
3013 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3014 * the user may specify which view to go to next.
3015 */
3016 private int mNextFocusDownId = View.NO_ID;
3017
Jeff Brown4e6319b2010-12-13 10:36:51 -08003018 /**
3019 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3020 * the user may specify which view to go to next.
3021 */
3022 int mNextFocusForwardId = View.NO_ID;
3023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003025 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003026 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003027 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 private UnsetPressedState mUnsetPressedState;
3030
3031 /**
3032 * Whether the long press's action has been invoked. The tap's action is invoked on the
3033 * up event while a long press is invoked as soon as the long press duration is reached, so
3034 * a long press could be performed before the tap is checked, in which case the tap's action
3035 * should not be invoked.
3036 */
3037 private boolean mHasPerformedLongPress;
3038
3039 /**
3040 * The minimum height of the view. We'll try our best to have the height
3041 * of this view to at least this amount.
3042 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003043 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 private int mMinHeight;
3045
3046 /**
3047 * The minimum width of the view. We'll try our best to have the width
3048 * of this view to at least this amount.
3049 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003050 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 private int mMinWidth;
3052
3053 /**
3054 * The delegate to handle touch events that are physically in this view
3055 * but should be handled by another view.
3056 */
3057 private TouchDelegate mTouchDelegate = null;
3058
3059 /**
3060 * Solid color to use as a background when creating the drawing cache. Enables
3061 * the cache to use 16 bit bitmaps instead of 32 bit.
3062 */
3063 private int mDrawingCacheBackgroundColor = 0;
3064
3065 /**
3066 * Special tree observer used when mAttachInfo is null.
3067 */
3068 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003069
Adam Powelle14579b2009-12-16 18:39:52 -08003070 /**
3071 * Cache the touch slop from the context that created the view.
3072 */
3073 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003076 * Object that handles automatic animation of view properties.
3077 */
3078 private ViewPropertyAnimator mAnimator = null;
3079
3080 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003081 * Flag indicating that a drag can cross window boundaries. When
3082 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3083 * with this flag set, all visible applications will be able to participate
3084 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003085 *
3086 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003087 */
3088 public static final int DRAG_FLAG_GLOBAL = 1;
3089
3090 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003091 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3092 */
3093 private float mVerticalScrollFactor;
3094
3095 /**
Adam Powell20232d02010-12-08 21:08:53 -08003096 * Position of the vertical scroll bar.
3097 */
3098 private int mVerticalScrollbarPosition;
3099
3100 /**
3101 * Position the scroll bar at the default position as determined by the system.
3102 */
3103 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3104
3105 /**
3106 * Position the scroll bar along the left edge.
3107 */
3108 public static final int SCROLLBAR_POSITION_LEFT = 1;
3109
3110 /**
3111 * Position the scroll bar along the right edge.
3112 */
3113 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3114
3115 /**
Romain Guy171c5922011-01-06 10:04:23 -08003116 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003117 *
3118 * @see #getLayerType()
3119 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003120 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003121 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003122 */
3123 public static final int LAYER_TYPE_NONE = 0;
3124
3125 /**
3126 * <p>Indicates that the view has a software layer. A software layer is backed
3127 * by a bitmap and causes the view to be rendered using Android's software
3128 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003129 *
Romain Guy171c5922011-01-06 10:04:23 -08003130 * <p>Software layers have various usages:</p>
3131 * <p>When the application is not using hardware acceleration, a software layer
3132 * is useful to apply a specific color filter and/or blending mode and/or
3133 * translucency to a view and all its children.</p>
3134 * <p>When the application is using hardware acceleration, a software layer
3135 * is useful to render drawing primitives not supported by the hardware
3136 * accelerated pipeline. It can also be used to cache a complex view tree
3137 * into a texture and reduce the complexity of drawing operations. For instance,
3138 * when animating a complex view tree with a translation, a software layer can
3139 * be used to render the view tree only once.</p>
3140 * <p>Software layers should be avoided when the affected view tree updates
3141 * often. Every update will require to re-render the software layer, which can
3142 * potentially be slow (particularly when hardware acceleration is turned on
3143 * since the layer will have to be uploaded into a hardware texture after every
3144 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003145 *
3146 * @see #getLayerType()
3147 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003148 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003149 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003150 */
3151 public static final int LAYER_TYPE_SOFTWARE = 1;
3152
3153 /**
3154 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3155 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3156 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3157 * rendering pipeline, but only if hardware acceleration is turned on for the
3158 * view hierarchy. When hardware acceleration is turned off, hardware layers
3159 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003160 *
Romain Guy171c5922011-01-06 10:04:23 -08003161 * <p>A hardware layer is useful to apply a specific color filter and/or
3162 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003163 * <p>A hardware layer can be used to cache a complex view tree into a
3164 * texture and reduce the complexity of drawing operations. For instance,
3165 * when animating a complex view tree with a translation, a hardware layer can
3166 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003167 * <p>A hardware layer can also be used to increase the rendering quality when
3168 * rotation transformations are applied on a view. It can also be used to
3169 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003170 *
3171 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003172 * @see #setLayerType(int, android.graphics.Paint)
3173 * @see #LAYER_TYPE_NONE
3174 * @see #LAYER_TYPE_SOFTWARE
3175 */
3176 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003177
Romain Guy3aaff3a2011-01-12 14:18:47 -08003178 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3179 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3180 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3181 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3182 })
Romain Guy171c5922011-01-06 10:04:23 -08003183 int mLayerType = LAYER_TYPE_NONE;
3184 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003185 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003186
3187 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003188 * Set to true when the view is sending hover accessibility events because it
3189 * is the innermost hovered view.
3190 */
3191 private boolean mSendingHoverAccessibilityEvents;
3192
3193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 * Simple constructor to use when creating a view from code.
3195 *
3196 * @param context The Context the view is running in, through which it can
3197 * access the current theme, resources, etc.
3198 */
3199 public View(Context context) {
3200 mContext = context;
3201 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003202 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003203 // Set layout and text direction defaults
3204 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003205 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003206 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07003207 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT) |
3208 (ACCESSIBILITY_FOCUSABLE_DEFAULT << ACCESSIBILITY_FOCUSABLE_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003209 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003210 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003211 mUserPaddingStart = -1;
3212 mUserPaddingEnd = -1;
3213 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 }
3215
3216 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003217 * Delegate for injecting accessiblity functionality.
3218 */
3219 AccessibilityDelegate mAccessibilityDelegate;
3220
3221 /**
3222 * Consistency verifier for debugging purposes.
3223 * @hide
3224 */
3225 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3226 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3227 new InputEventConsistencyVerifier(this, 0) : null;
3228
3229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 * Constructor that is called when inflating a view from XML. This is called
3231 * when a view is being constructed from an XML file, supplying attributes
3232 * that were specified in the XML file. This version uses a default style of
3233 * 0, so the only attribute values applied are those in the Context's Theme
3234 * and the given AttributeSet.
3235 *
3236 * <p>
3237 * The method onFinishInflate() will be called after all children have been
3238 * added.
3239 *
3240 * @param context The Context the view is running in, through which it can
3241 * access the current theme, resources, etc.
3242 * @param attrs The attributes of the XML tag that is inflating the view.
3243 * @see #View(Context, AttributeSet, int)
3244 */
3245 public View(Context context, AttributeSet attrs) {
3246 this(context, attrs, 0);
3247 }
3248
3249 /**
3250 * Perform inflation from XML and apply a class-specific base style. This
3251 * constructor of View allows subclasses to use their own base style when
3252 * they are inflating. For example, a Button class's constructor would call
3253 * this version of the super class constructor and supply
3254 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3255 * the theme's button style to modify all of the base view attributes (in
3256 * particular its background) as well as the Button class's attributes.
3257 *
3258 * @param context The Context the view is running in, through which it can
3259 * access the current theme, resources, etc.
3260 * @param attrs The attributes of the XML tag that is inflating the view.
3261 * @param defStyle The default style to apply to this view. If 0, no style
3262 * will be applied (beyond what is included in the theme). This may
3263 * either be an attribute resource, whose value will be retrieved
3264 * from the current theme, or an explicit style resource.
3265 * @see #View(Context, AttributeSet)
3266 */
3267 public View(Context context, AttributeSet attrs, int defStyle) {
3268 this(context);
3269
3270 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3271 defStyle, 0);
3272
3273 Drawable background = null;
3274
3275 int leftPadding = -1;
3276 int topPadding = -1;
3277 int rightPadding = -1;
3278 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003279 int startPadding = -1;
3280 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281
3282 int padding = -1;
3283
3284 int viewFlagValues = 0;
3285 int viewFlagMasks = 0;
3286
3287 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 int x = 0;
3290 int y = 0;
3291
Chet Haase73066682010-11-29 15:55:32 -08003292 float tx = 0;
3293 float ty = 0;
3294 float rotation = 0;
3295 float rotationX = 0;
3296 float rotationY = 0;
3297 float sx = 1f;
3298 float sy = 1f;
3299 boolean transformSet = false;
3300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3302
Adam Powell637d3372010-08-25 14:37:03 -07003303 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 final int N = a.getIndexCount();
3305 for (int i = 0; i < N; i++) {
3306 int attr = a.getIndex(i);
3307 switch (attr) {
3308 case com.android.internal.R.styleable.View_background:
3309 background = a.getDrawable(attr);
3310 break;
3311 case com.android.internal.R.styleable.View_padding:
3312 padding = a.getDimensionPixelSize(attr, -1);
3313 break;
3314 case com.android.internal.R.styleable.View_paddingLeft:
3315 leftPadding = a.getDimensionPixelSize(attr, -1);
3316 break;
3317 case com.android.internal.R.styleable.View_paddingTop:
3318 topPadding = a.getDimensionPixelSize(attr, -1);
3319 break;
3320 case com.android.internal.R.styleable.View_paddingRight:
3321 rightPadding = a.getDimensionPixelSize(attr, -1);
3322 break;
3323 case com.android.internal.R.styleable.View_paddingBottom:
3324 bottomPadding = a.getDimensionPixelSize(attr, -1);
3325 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003326 case com.android.internal.R.styleable.View_paddingStart:
3327 startPadding = a.getDimensionPixelSize(attr, -1);
3328 break;
3329 case com.android.internal.R.styleable.View_paddingEnd:
3330 endPadding = a.getDimensionPixelSize(attr, -1);
3331 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 case com.android.internal.R.styleable.View_scrollX:
3333 x = a.getDimensionPixelOffset(attr, 0);
3334 break;
3335 case com.android.internal.R.styleable.View_scrollY:
3336 y = a.getDimensionPixelOffset(attr, 0);
3337 break;
Chet Haase73066682010-11-29 15:55:32 -08003338 case com.android.internal.R.styleable.View_alpha:
3339 setAlpha(a.getFloat(attr, 1f));
3340 break;
3341 case com.android.internal.R.styleable.View_transformPivotX:
3342 setPivotX(a.getDimensionPixelOffset(attr, 0));
3343 break;
3344 case com.android.internal.R.styleable.View_transformPivotY:
3345 setPivotY(a.getDimensionPixelOffset(attr, 0));
3346 break;
3347 case com.android.internal.R.styleable.View_translationX:
3348 tx = a.getDimensionPixelOffset(attr, 0);
3349 transformSet = true;
3350 break;
3351 case com.android.internal.R.styleable.View_translationY:
3352 ty = a.getDimensionPixelOffset(attr, 0);
3353 transformSet = true;
3354 break;
3355 case com.android.internal.R.styleable.View_rotation:
3356 rotation = a.getFloat(attr, 0);
3357 transformSet = true;
3358 break;
3359 case com.android.internal.R.styleable.View_rotationX:
3360 rotationX = a.getFloat(attr, 0);
3361 transformSet = true;
3362 break;
3363 case com.android.internal.R.styleable.View_rotationY:
3364 rotationY = a.getFloat(attr, 0);
3365 transformSet = true;
3366 break;
3367 case com.android.internal.R.styleable.View_scaleX:
3368 sx = a.getFloat(attr, 1f);
3369 transformSet = true;
3370 break;
3371 case com.android.internal.R.styleable.View_scaleY:
3372 sy = a.getFloat(attr, 1f);
3373 transformSet = true;
3374 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 case com.android.internal.R.styleable.View_id:
3376 mID = a.getResourceId(attr, NO_ID);
3377 break;
3378 case com.android.internal.R.styleable.View_tag:
3379 mTag = a.getText(attr);
3380 break;
3381 case com.android.internal.R.styleable.View_fitsSystemWindows:
3382 if (a.getBoolean(attr, false)) {
3383 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3384 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3385 }
3386 break;
3387 case com.android.internal.R.styleable.View_focusable:
3388 if (a.getBoolean(attr, false)) {
3389 viewFlagValues |= FOCUSABLE;
3390 viewFlagMasks |= FOCUSABLE_MASK;
3391 }
3392 break;
3393 case com.android.internal.R.styleable.View_focusableInTouchMode:
3394 if (a.getBoolean(attr, false)) {
3395 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3396 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3397 }
3398 break;
3399 case com.android.internal.R.styleable.View_clickable:
3400 if (a.getBoolean(attr, false)) {
3401 viewFlagValues |= CLICKABLE;
3402 viewFlagMasks |= CLICKABLE;
3403 }
3404 break;
3405 case com.android.internal.R.styleable.View_longClickable:
3406 if (a.getBoolean(attr, false)) {
3407 viewFlagValues |= LONG_CLICKABLE;
3408 viewFlagMasks |= LONG_CLICKABLE;
3409 }
3410 break;
3411 case com.android.internal.R.styleable.View_saveEnabled:
3412 if (!a.getBoolean(attr, true)) {
3413 viewFlagValues |= SAVE_DISABLED;
3414 viewFlagMasks |= SAVE_DISABLED_MASK;
3415 }
3416 break;
3417 case com.android.internal.R.styleable.View_duplicateParentState:
3418 if (a.getBoolean(attr, false)) {
3419 viewFlagValues |= DUPLICATE_PARENT_STATE;
3420 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3421 }
3422 break;
3423 case com.android.internal.R.styleable.View_visibility:
3424 final int visibility = a.getInt(attr, 0);
3425 if (visibility != 0) {
3426 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3427 viewFlagMasks |= VISIBILITY_MASK;
3428 }
3429 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003430 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003431 // Clear any layout direction flags (included resolved bits) already set
3432 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3433 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003434 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003435 final int value = (layoutDirection != -1) ?
3436 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3437 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003438 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 case com.android.internal.R.styleable.View_drawingCacheQuality:
3440 final int cacheQuality = a.getInt(attr, 0);
3441 if (cacheQuality != 0) {
3442 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3443 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3444 }
3445 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003446 case com.android.internal.R.styleable.View_contentDescription:
3447 mContentDescription = a.getString(attr);
3448 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3450 if (!a.getBoolean(attr, true)) {
3451 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3452 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3453 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003454 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003455 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3456 if (!a.getBoolean(attr, true)) {
3457 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3458 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3459 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003460 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 case R.styleable.View_scrollbars:
3462 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3463 if (scrollbars != SCROLLBARS_NONE) {
3464 viewFlagValues |= scrollbars;
3465 viewFlagMasks |= SCROLLBARS_MASK;
3466 initializeScrollbars(a);
3467 }
3468 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003469 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003471 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3472 // Ignore the attribute starting with ICS
3473 break;
3474 }
3475 // With builds < ICS, fall through and apply fading edges
3476 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3478 if (fadingEdge != FADING_EDGE_NONE) {
3479 viewFlagValues |= fadingEdge;
3480 viewFlagMasks |= FADING_EDGE_MASK;
3481 initializeFadingEdge(a);
3482 }
3483 break;
3484 case R.styleable.View_scrollbarStyle:
3485 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3486 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3487 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3488 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3489 }
3490 break;
3491 case R.styleable.View_isScrollContainer:
3492 setScrollContainer = true;
3493 if (a.getBoolean(attr, false)) {
3494 setScrollContainer(true);
3495 }
3496 break;
3497 case com.android.internal.R.styleable.View_keepScreenOn:
3498 if (a.getBoolean(attr, false)) {
3499 viewFlagValues |= KEEP_SCREEN_ON;
3500 viewFlagMasks |= KEEP_SCREEN_ON;
3501 }
3502 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003503 case R.styleable.View_filterTouchesWhenObscured:
3504 if (a.getBoolean(attr, false)) {
3505 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3506 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3507 }
3508 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 case R.styleable.View_nextFocusLeft:
3510 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3511 break;
3512 case R.styleable.View_nextFocusRight:
3513 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3514 break;
3515 case R.styleable.View_nextFocusUp:
3516 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3517 break;
3518 case R.styleable.View_nextFocusDown:
3519 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3520 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003521 case R.styleable.View_nextFocusForward:
3522 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3523 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 case R.styleable.View_minWidth:
3525 mMinWidth = a.getDimensionPixelSize(attr, 0);
3526 break;
3527 case R.styleable.View_minHeight:
3528 mMinHeight = a.getDimensionPixelSize(attr, 0);
3529 break;
Romain Guy9a817362009-05-01 10:57:14 -07003530 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003531 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003532 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003533 + "be used within a restricted context");
3534 }
3535
Romain Guy9a817362009-05-01 10:57:14 -07003536 final String handlerName = a.getString(attr);
3537 if (handlerName != null) {
3538 setOnClickListener(new OnClickListener() {
3539 private Method mHandler;
3540
3541 public void onClick(View v) {
3542 if (mHandler == null) {
3543 try {
3544 mHandler = getContext().getClass().getMethod(handlerName,
3545 View.class);
3546 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003547 int id = getId();
3548 String idText = id == NO_ID ? "" : " with id '"
3549 + getContext().getResources().getResourceEntryName(
3550 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003551 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003552 handlerName + "(View) in the activity "
3553 + getContext().getClass() + " for onClick handler"
3554 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003555 }
3556 }
3557
3558 try {
3559 mHandler.invoke(getContext(), View.this);
3560 } catch (IllegalAccessException e) {
3561 throw new IllegalStateException("Could not execute non "
3562 + "public method of the activity", e);
3563 } catch (InvocationTargetException e) {
3564 throw new IllegalStateException("Could not execute "
3565 + "method of the activity", e);
3566 }
3567 }
3568 });
3569 }
3570 break;
Adam Powell637d3372010-08-25 14:37:03 -07003571 case R.styleable.View_overScrollMode:
3572 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3573 break;
Adam Powell20232d02010-12-08 21:08:53 -08003574 case R.styleable.View_verticalScrollbarPosition:
3575 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3576 break;
Romain Guy171c5922011-01-06 10:04:23 -08003577 case R.styleable.View_layerType:
3578 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3579 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003580 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003581 // Clear any text direction flag already set
3582 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3583 // Set the text direction flags depending on the value of the attribute
3584 final int textDirection = a.getInt(attr, -1);
3585 if (textDirection != -1) {
3586 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3587 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003588 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003589 case R.styleable.View_textAlignment:
3590 // Clear any text alignment flag already set
3591 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3592 // Set the text alignment flag depending on the value of the attribute
3593 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3594 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3595 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003596 case R.styleable.View_importantForAccessibility:
3597 setImportantForAccessibility(a.getInt(attr,
3598 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 }
3600 }
3601
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003602 a.recycle();
3603
Adam Powell637d3372010-08-25 14:37:03 -07003604 setOverScrollMode(overScrollMode);
3605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003607 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 }
3609
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003610 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3611 // layout direction). Those cached values will be used later during padding resolution.
3612 mUserPaddingStart = startPadding;
3613 mUserPaddingEnd = endPadding;
3614
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003615 updateUserPaddingRelative();
3616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 if (padding >= 0) {
3618 leftPadding = padding;
3619 topPadding = padding;
3620 rightPadding = padding;
3621 bottomPadding = padding;
3622 }
3623
3624 // If the user specified the padding (either with android:padding or
3625 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3626 // use the default padding or the padding from the background drawable
3627 // (stored at this point in mPadding*)
3628 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3629 topPadding >= 0 ? topPadding : mPaddingTop,
3630 rightPadding >= 0 ? rightPadding : mPaddingRight,
3631 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3632
3633 if (viewFlagMasks != 0) {
3634 setFlags(viewFlagValues, viewFlagMasks);
3635 }
3636
3637 // Needs to be called after mViewFlags is set
3638 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3639 recomputePadding();
3640 }
3641
3642 if (x != 0 || y != 0) {
3643 scrollTo(x, y);
3644 }
3645
Chet Haase73066682010-11-29 15:55:32 -08003646 if (transformSet) {
3647 setTranslationX(tx);
3648 setTranslationY(ty);
3649 setRotation(rotation);
3650 setRotationX(rotationX);
3651 setRotationY(rotationY);
3652 setScaleX(sx);
3653 setScaleY(sy);
3654 }
3655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3657 setScrollContainer(true);
3658 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003659
3660 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
3662
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003663 private void updateUserPaddingRelative() {
3664 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3665 }
3666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 /**
3668 * Non-public constructor for use in testing
3669 */
3670 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003671 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
3673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 /**
3675 * <p>
3676 * Initializes the fading edges from a given set of styled attributes. This
3677 * method should be called by subclasses that need fading edges and when an
3678 * instance of these subclasses is created programmatically rather than
3679 * being inflated from XML. This method is automatically called when the XML
3680 * is inflated.
3681 * </p>
3682 *
3683 * @param a the styled attributes set to initialize the fading edges from
3684 */
3685 protected void initializeFadingEdge(TypedArray a) {
3686 initScrollCache();
3687
3688 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3689 R.styleable.View_fadingEdgeLength,
3690 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3691 }
3692
3693 /**
3694 * Returns the size of the vertical faded edges used to indicate that more
3695 * content in this view is visible.
3696 *
3697 * @return The size in pixels of the vertical faded edge or 0 if vertical
3698 * faded edges are not enabled for this view.
3699 * @attr ref android.R.styleable#View_fadingEdgeLength
3700 */
3701 public int getVerticalFadingEdgeLength() {
3702 if (isVerticalFadingEdgeEnabled()) {
3703 ScrollabilityCache cache = mScrollCache;
3704 if (cache != null) {
3705 return cache.fadingEdgeLength;
3706 }
3707 }
3708 return 0;
3709 }
3710
3711 /**
3712 * Set the size of the faded edge used to indicate that more content in this
3713 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003714 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3715 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3716 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 *
3718 * @param length The size in pixels of the faded edge used to indicate that more
3719 * content in this view is visible.
3720 */
3721 public void setFadingEdgeLength(int length) {
3722 initScrollCache();
3723 mScrollCache.fadingEdgeLength = length;
3724 }
3725
3726 /**
3727 * Returns the size of the horizontal faded edges used to indicate that more
3728 * content in this view is visible.
3729 *
3730 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3731 * faded edges are not enabled for this view.
3732 * @attr ref android.R.styleable#View_fadingEdgeLength
3733 */
3734 public int getHorizontalFadingEdgeLength() {
3735 if (isHorizontalFadingEdgeEnabled()) {
3736 ScrollabilityCache cache = mScrollCache;
3737 if (cache != null) {
3738 return cache.fadingEdgeLength;
3739 }
3740 }
3741 return 0;
3742 }
3743
3744 /**
3745 * Returns the width of the vertical scrollbar.
3746 *
3747 * @return The width in pixels of the vertical scrollbar or 0 if there
3748 * is no vertical scrollbar.
3749 */
3750 public int getVerticalScrollbarWidth() {
3751 ScrollabilityCache cache = mScrollCache;
3752 if (cache != null) {
3753 ScrollBarDrawable scrollBar = cache.scrollBar;
3754 if (scrollBar != null) {
3755 int size = scrollBar.getSize(true);
3756 if (size <= 0) {
3757 size = cache.scrollBarSize;
3758 }
3759 return size;
3760 }
3761 return 0;
3762 }
3763 return 0;
3764 }
3765
3766 /**
3767 * Returns the height of the horizontal scrollbar.
3768 *
3769 * @return The height in pixels of the horizontal scrollbar or 0 if
3770 * there is no horizontal scrollbar.
3771 */
3772 protected int getHorizontalScrollbarHeight() {
3773 ScrollabilityCache cache = mScrollCache;
3774 if (cache != null) {
3775 ScrollBarDrawable scrollBar = cache.scrollBar;
3776 if (scrollBar != null) {
3777 int size = scrollBar.getSize(false);
3778 if (size <= 0) {
3779 size = cache.scrollBarSize;
3780 }
3781 return size;
3782 }
3783 return 0;
3784 }
3785 return 0;
3786 }
3787
3788 /**
3789 * <p>
3790 * Initializes the scrollbars from a given set of styled attributes. This
3791 * method should be called by subclasses that need scrollbars and when an
3792 * instance of these subclasses is created programmatically rather than
3793 * being inflated from XML. This method is automatically called when the XML
3794 * is inflated.
3795 * </p>
3796 *
3797 * @param a the styled attributes set to initialize the scrollbars from
3798 */
3799 protected void initializeScrollbars(TypedArray a) {
3800 initScrollCache();
3801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003803
Mike Cleronf116bf82009-09-27 19:14:12 -07003804 if (scrollabilityCache.scrollBar == null) {
3805 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3806 }
Joe Malin32736f02011-01-19 16:14:20 -08003807
Romain Guy8bda2482010-03-02 11:42:11 -08003808 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809
Mike Cleronf116bf82009-09-27 19:14:12 -07003810 if (!fadeScrollbars) {
3811 scrollabilityCache.state = ScrollabilityCache.ON;
3812 }
3813 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003814
3815
Mike Cleronf116bf82009-09-27 19:14:12 -07003816 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3817 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3818 .getScrollBarFadeDuration());
3819 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3820 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3821 ViewConfiguration.getScrollDefaultDelay());
3822
Joe Malin32736f02011-01-19 16:14:20 -08003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3825 com.android.internal.R.styleable.View_scrollbarSize,
3826 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3827
3828 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3829 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3830
3831 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3832 if (thumb != null) {
3833 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3834 }
3835
3836 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3837 false);
3838 if (alwaysDraw) {
3839 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3840 }
3841
3842 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3843 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3844
3845 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3846 if (thumb != null) {
3847 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3848 }
3849
3850 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3851 false);
3852 if (alwaysDraw) {
3853 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3854 }
3855
3856 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003857 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 }
3859
3860 /**
3861 * <p>
3862 * Initalizes the scrollability cache if necessary.
3863 * </p>
3864 */
3865 private void initScrollCache() {
3866 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003867 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 }
3869 }
3870
Philip Milne6c8ea062012-04-03 17:38:43 -07003871 private ScrollabilityCache getScrollCache() {
3872 initScrollCache();
3873 return mScrollCache;
3874 }
3875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 /**
Adam Powell20232d02010-12-08 21:08:53 -08003877 * Set the position of the vertical scroll bar. Should be one of
3878 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3879 * {@link #SCROLLBAR_POSITION_RIGHT}.
3880 *
3881 * @param position Where the vertical scroll bar should be positioned.
3882 */
3883 public void setVerticalScrollbarPosition(int position) {
3884 if (mVerticalScrollbarPosition != position) {
3885 mVerticalScrollbarPosition = position;
3886 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003887 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003888 }
3889 }
3890
3891 /**
3892 * @return The position where the vertical scroll bar will show, if applicable.
3893 * @see #setVerticalScrollbarPosition(int)
3894 */
3895 public int getVerticalScrollbarPosition() {
3896 return mVerticalScrollbarPosition;
3897 }
3898
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003899 ListenerInfo getListenerInfo() {
3900 if (mListenerInfo != null) {
3901 return mListenerInfo;
3902 }
3903 mListenerInfo = new ListenerInfo();
3904 return mListenerInfo;
3905 }
3906
Adam Powell20232d02010-12-08 21:08:53 -08003907 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 * Register a callback to be invoked when focus of this view changed.
3909 *
3910 * @param l The callback that will run.
3911 */
3912 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003913 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 }
3915
3916 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003917 * Add a listener that will be called when the bounds of the view change due to
3918 * layout processing.
3919 *
3920 * @param listener The listener that will be called when layout bounds change.
3921 */
3922 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003923 ListenerInfo li = getListenerInfo();
3924 if (li.mOnLayoutChangeListeners == null) {
3925 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003926 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003927 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3928 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003929 }
Chet Haase21cd1382010-09-01 17:42:29 -07003930 }
3931
3932 /**
3933 * Remove a listener for layout changes.
3934 *
3935 * @param listener The listener for layout bounds change.
3936 */
3937 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003938 ListenerInfo li = mListenerInfo;
3939 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003940 return;
3941 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003942 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003943 }
3944
3945 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003946 * Add a listener for attach state changes.
3947 *
3948 * This listener will be called whenever this view is attached or detached
3949 * from a window. Remove the listener using
3950 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3951 *
3952 * @param listener Listener to attach
3953 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3954 */
3955 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003956 ListenerInfo li = getListenerInfo();
3957 if (li.mOnAttachStateChangeListeners == null) {
3958 li.mOnAttachStateChangeListeners
3959 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003960 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003961 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003962 }
3963
3964 /**
3965 * Remove a listener for attach state changes. The listener will receive no further
3966 * notification of window attach/detach events.
3967 *
3968 * @param listener Listener to remove
3969 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3970 */
3971 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003972 ListenerInfo li = mListenerInfo;
3973 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003974 return;
3975 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003976 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003977 }
3978
3979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 * Returns the focus-change callback registered for this view.
3981 *
3982 * @return The callback, or null if one is not registered.
3983 */
3984 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 }
3988
3989 /**
3990 * Register a callback to be invoked when this view is clicked. If this view is not
3991 * clickable, it becomes clickable.
3992 *
3993 * @param l The callback that will run
3994 *
3995 * @see #setClickable(boolean)
3996 */
3997 public void setOnClickListener(OnClickListener l) {
3998 if (!isClickable()) {
3999 setClickable(true);
4000 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004001 getListenerInfo().mOnClickListener = l;
4002 }
4003
4004 /**
4005 * Return whether this view has an attached OnClickListener. Returns
4006 * true if there is a listener, false if there is none.
4007 */
4008 public boolean hasOnClickListeners() {
4009 ListenerInfo li = mListenerInfo;
4010 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 }
4012
4013 /**
4014 * Register a callback to be invoked when this view is clicked and held. If this view is not
4015 * long clickable, it becomes long clickable.
4016 *
4017 * @param l The callback that will run
4018 *
4019 * @see #setLongClickable(boolean)
4020 */
4021 public void setOnLongClickListener(OnLongClickListener l) {
4022 if (!isLongClickable()) {
4023 setLongClickable(true);
4024 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004025 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 }
4027
4028 /**
4029 * Register a callback to be invoked when the context menu for this view is
4030 * being built. If this view is not long clickable, it becomes long clickable.
4031 *
4032 * @param l The callback that will run
4033 *
4034 */
4035 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4036 if (!isLongClickable()) {
4037 setLongClickable(true);
4038 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004039 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004043 * Call this view's OnClickListener, if it is defined. Performs all normal
4044 * actions associated with clicking: reporting accessibility event, playing
4045 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 *
4047 * @return True there was an assigned OnClickListener that was called, false
4048 * otherwise is returned.
4049 */
4050 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004051 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4052
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004053 ListenerInfo li = mListenerInfo;
4054 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004056 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 return true;
4058 }
4059
4060 return false;
4061 }
4062
4063 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4065 * this only calls the listener, and does not do any associated clicking
4066 * actions like reporting an accessibility event.
4067 *
4068 * @return True there was an assigned OnClickListener that was called, false
4069 * otherwise is returned.
4070 */
4071 public boolean callOnClick() {
4072 ListenerInfo li = mListenerInfo;
4073 if (li != null && li.mOnClickListener != null) {
4074 li.mOnClickListener.onClick(this);
4075 return true;
4076 }
4077 return false;
4078 }
4079
4080 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004081 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4082 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004084 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 */
4086 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004087 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004090 ListenerInfo li = mListenerInfo;
4091 if (li != null && li.mOnLongClickListener != null) {
4092 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 }
4094 if (!handled) {
4095 handled = showContextMenu();
4096 }
4097 if (handled) {
4098 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4099 }
4100 return handled;
4101 }
4102
4103 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004104 * Performs button-related actions during a touch down event.
4105 *
4106 * @param event The event.
4107 * @return True if the down was consumed.
4108 *
4109 * @hide
4110 */
4111 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4112 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4113 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4114 return true;
4115 }
4116 }
4117 return false;
4118 }
4119
4120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 * Bring up the context menu for this view.
4122 *
4123 * @return Whether a context menu was displayed.
4124 */
4125 public boolean showContextMenu() {
4126 return getParent().showContextMenuForChild(this);
4127 }
4128
4129 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004130 * Bring up the context menu for this view, referring to the item under the specified point.
4131 *
4132 * @param x The referenced x coordinate.
4133 * @param y The referenced y coordinate.
4134 * @param metaState The keyboard modifiers that were pressed.
4135 * @return Whether a context menu was displayed.
4136 *
4137 * @hide
4138 */
4139 public boolean showContextMenu(float x, float y, int metaState) {
4140 return showContextMenu();
4141 }
4142
4143 /**
Adam Powell6e346362010-07-23 10:18:23 -07004144 * Start an action mode.
4145 *
4146 * @param callback Callback that will control the lifecycle of the action mode
4147 * @return The new action mode if it is started, null otherwise
4148 *
4149 * @see ActionMode
4150 */
4151 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004152 ViewParent parent = getParent();
4153 if (parent == null) return null;
4154 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004155 }
4156
4157 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004158 * Register a callback to be invoked when a hardware key is pressed in this view.
4159 * Key presses in software input methods will generally not trigger the methods of
4160 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 * @param l the key listener to attach to this view
4162 */
4163 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004164 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166
4167 /**
4168 * Register a callback to be invoked when a touch event is sent to this view.
4169 * @param l the touch listener to attach to this view
4170 */
4171 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004172 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174
4175 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004176 * Register a callback to be invoked when a generic motion event is sent to this view.
4177 * @param l the generic motion listener to attach to this view
4178 */
4179 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004180 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004181 }
4182
4183 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004184 * Register a callback to be invoked when a hover event is sent to this view.
4185 * @param l the hover listener to attach to this view
4186 */
4187 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004188 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004189 }
4190
4191 /**
Joe Malin32736f02011-01-19 16:14:20 -08004192 * Register a drag event listener callback object for this View. The parameter is
4193 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4194 * View, the system calls the
4195 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4196 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004197 */
4198 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004199 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004200 }
4201
4202 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004203 * Give this view focus. This will cause
4204 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 *
4206 * Note: this does not check whether this {@link View} should get focus, it just
4207 * gives it focus no matter what. It should only be called internally by framework
4208 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4209 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004210 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4211 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 * focus moved when requestFocus() is called. It may not always
4213 * apply, in which case use the default View.FOCUS_DOWN.
4214 * @param previouslyFocusedRect The rectangle of the view that had focus
4215 * prior in this View's coordinate system.
4216 */
4217 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4218 if (DBG) {
4219 System.out.println(this + " requestFocus()");
4220 }
4221
4222 if ((mPrivateFlags & FOCUSED) == 0) {
4223 mPrivateFlags |= FOCUSED;
4224
4225 if (mParent != null) {
4226 mParent.requestChildFocus(this, this);
4227 }
4228
4229 onFocusChanged(true, direction, previouslyFocusedRect);
4230 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004231
4232 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4233 notifyAccessibilityStateChanged();
4234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 }
4236 }
4237
4238 /**
4239 * Request that a rectangle of this view be visible on the screen,
4240 * scrolling if necessary just enough.
4241 *
4242 * <p>A View should call this if it maintains some notion of which part
4243 * of its content is interesting. For example, a text editing view
4244 * should call this when its cursor moves.
4245 *
4246 * @param rectangle The rectangle.
4247 * @return Whether any parent scrolled.
4248 */
4249 public boolean requestRectangleOnScreen(Rect rectangle) {
4250 return requestRectangleOnScreen(rectangle, false);
4251 }
4252
4253 /**
4254 * Request that a rectangle of this view be visible on the screen,
4255 * scrolling if necessary just enough.
4256 *
4257 * <p>A View should call this if it maintains some notion of which part
4258 * of its content is interesting. For example, a text editing view
4259 * should call this when its cursor moves.
4260 *
4261 * <p>When <code>immediate</code> is set to true, scrolling will not be
4262 * animated.
4263 *
4264 * @param rectangle The rectangle.
4265 * @param immediate True to forbid animated scrolling, false otherwise
4266 * @return Whether any parent scrolled.
4267 */
4268 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4269 View child = this;
4270 ViewParent parent = mParent;
4271 boolean scrolled = false;
4272 while (parent != null) {
4273 scrolled |= parent.requestChildRectangleOnScreen(child,
4274 rectangle, immediate);
4275
4276 // offset rect so next call has the rectangle in the
4277 // coordinate system of its direct child.
4278 rectangle.offset(child.getLeft(), child.getTop());
4279 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4280
4281 if (!(parent instanceof View)) {
4282 break;
4283 }
Romain Guy8506ab42009-06-11 17:35:47 -07004284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 child = (View) parent;
4286 parent = child.getParent();
4287 }
4288 return scrolled;
4289 }
4290
4291 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004292 * Called when this view wants to give up focus. If focus is cleared
4293 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4294 * <p>
4295 * <strong>Note:</strong> When a View clears focus the framework is trying
4296 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004297 * View is the first from the top that can take focus, then all callbacks
4298 * related to clearing focus will be invoked after wich the framework will
4299 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004300 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 */
4302 public void clearFocus() {
4303 if (DBG) {
4304 System.out.println(this + " clearFocus()");
4305 }
4306
4307 if ((mPrivateFlags & FOCUSED) != 0) {
4308 mPrivateFlags &= ~FOCUSED;
4309
4310 if (mParent != null) {
4311 mParent.clearChildFocus(this);
4312 }
4313
4314 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004317
4318 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004319
4320 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4321 notifyAccessibilityStateChanged();
4322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 }
4324 }
4325
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004326 void ensureInputFocusOnFirstFocusable() {
4327 View root = getRootView();
4328 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004329 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 }
4331 }
4332
4333 /**
4334 * Called internally by the view system when a new view is getting focus.
4335 * This is what clears the old focus.
4336 */
4337 void unFocus() {
4338 if (DBG) {
4339 System.out.println(this + " unFocus()");
4340 }
4341
4342 if ((mPrivateFlags & FOCUSED) != 0) {
4343 mPrivateFlags &= ~FOCUSED;
4344
4345 onFocusChanged(false, 0, null);
4346 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004347
4348 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4349 notifyAccessibilityStateChanged();
4350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 }
4352 }
4353
4354 /**
4355 * Returns true if this view has focus iteself, or is the ancestor of the
4356 * view that has focus.
4357 *
4358 * @return True if this view has or contains focus, false otherwise.
4359 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004360 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 public boolean hasFocus() {
4362 return (mPrivateFlags & FOCUSED) != 0;
4363 }
4364
4365 /**
4366 * Returns true if this view is focusable or if it contains a reachable View
4367 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4368 * is a View whose parents do not block descendants focus.
4369 *
4370 * Only {@link #VISIBLE} views are considered focusable.
4371 *
4372 * @return True if the view is focusable or if the view contains a focusable
4373 * View, false otherwise.
4374 *
4375 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4376 */
4377 public boolean hasFocusable() {
4378 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4379 }
4380
4381 /**
4382 * Called by the view system when the focus state of this view changes.
4383 * When the focus change event is caused by directional navigation, direction
4384 * and previouslyFocusedRect provide insight into where the focus is coming from.
4385 * When overriding, be sure to call up through to the super class so that
4386 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 * @param gainFocus True if the View has focus; false otherwise.
4389 * @param direction The direction focus has moved when requestFocus()
4390 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004391 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4392 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4393 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4395 * system, of the previously focused view. If applicable, this will be
4396 * passed in as finer grained information about where the focus is coming
4397 * from (in addition to direction). Will be <code>null</code> otherwise.
4398 */
4399 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004400 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004401 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4402 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004403 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004404 }
4405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 InputMethodManager imm = InputMethodManager.peekInstance();
4407 if (!gainFocus) {
4408 if (isPressed()) {
4409 setPressed(false);
4410 }
4411 if (imm != null && mAttachInfo != null
4412 && mAttachInfo.mHasWindowFocus) {
4413 imm.focusOut(this);
4414 }
Romain Guya2431d02009-04-30 16:30:00 -07004415 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 } else if (imm != null && mAttachInfo != null
4417 && mAttachInfo.mHasWindowFocus) {
4418 imm.focusIn(this);
4419 }
Romain Guy8506ab42009-06-11 17:35:47 -07004420
Romain Guy0fd89bf2011-01-26 15:41:30 -08004421 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004422 ListenerInfo li = mListenerInfo;
4423 if (li != null && li.mOnFocusChangeListener != null) {
4424 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 }
Joe Malin32736f02011-01-19 16:14:20 -08004426
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004427 if (mAttachInfo != null) {
4428 mAttachInfo.mKeyDispatchState.reset(this);
4429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 }
4431
4432 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004433 * Sends an accessibility event of the given type. If accessiiblity is
4434 * not enabled this method has no effect. The default implementation calls
4435 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4436 * to populate information about the event source (this View), then calls
4437 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4438 * populate the text content of the event source including its descendants,
4439 * and last calls
4440 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4441 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004442 * <p>
4443 * If an {@link AccessibilityDelegate} has been specified via calling
4444 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4445 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4446 * responsible for handling this call.
4447 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004448 *
Scott Mainb303d832011-10-12 16:45:18 -07004449 * @param eventType The type of the event to send, as defined by several types from
4450 * {@link android.view.accessibility.AccessibilityEvent}, such as
4451 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4452 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004453 *
4454 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4455 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4456 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004457 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004458 */
4459 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004460 if (mAccessibilityDelegate != null) {
4461 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4462 } else {
4463 sendAccessibilityEventInternal(eventType);
4464 }
4465 }
4466
4467 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004468 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4469 * {@link AccessibilityEvent} to make an announcement which is related to some
4470 * sort of a context change for which none of the events representing UI transitions
4471 * is a good fit. For example, announcing a new page in a book. If accessibility
4472 * is not enabled this method does nothing.
4473 *
4474 * @param text The announcement text.
4475 */
4476 public void announceForAccessibility(CharSequence text) {
4477 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4478 AccessibilityEvent event = AccessibilityEvent.obtain(
4479 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4480 event.getText().add(text);
4481 sendAccessibilityEventUnchecked(event);
4482 }
4483 }
4484
4485 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004486 * @see #sendAccessibilityEvent(int)
4487 *
4488 * Note: Called from the default {@link AccessibilityDelegate}.
4489 */
4490 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004491 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4492 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4493 }
4494 }
4495
4496 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004497 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4498 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004499 * perform a check whether accessibility is enabled.
4500 * <p>
4501 * If an {@link AccessibilityDelegate} has been specified via calling
4502 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4503 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4504 * is responsible for handling this call.
4505 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004506 *
4507 * @param event The event to send.
4508 *
4509 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004510 */
4511 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004512 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004513 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004514 } else {
4515 sendAccessibilityEventUncheckedInternal(event);
4516 }
4517 }
4518
4519 /**
4520 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4521 *
4522 * Note: Called from the default {@link AccessibilityDelegate}.
4523 */
4524 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004525 if (!isShown()) {
4526 return;
4527 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004528 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004529 // Only a subset of accessibility events populates text content.
4530 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4531 dispatchPopulateAccessibilityEvent(event);
4532 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004533 // Intercept accessibility focus events fired by virtual nodes to keep
4534 // track of accessibility focus position in such nodes.
4535 final int eventType = event.getEventType();
4536 switch (eventType) {
4537 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4538 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4539 event.getSourceNodeId());
4540 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4541 ViewRootImpl viewRootImpl = getViewRootImpl();
4542 if (viewRootImpl != null) {
4543 viewRootImpl.setAccessibilityFocusedHost(this);
4544 }
4545 }
4546 } break;
4547 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4548 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4549 event.getSourceNodeId());
4550 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4551 ViewRootImpl viewRootImpl = getViewRootImpl();
4552 if (viewRootImpl != null) {
4553 viewRootImpl.setAccessibilityFocusedHost(null);
4554 }
4555 }
4556 } break;
4557 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004558 // In the beginning we called #isShown(), so we know that getParent() is not null.
4559 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004560 }
4561
4562 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004563 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4564 * to its children for adding their text content to the event. Note that the
4565 * event text is populated in a separate dispatch path since we add to the
4566 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004567 * A typical implementation will call
4568 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4569 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4570 * on each child. Override this method if custom population of the event text
4571 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004572 * <p>
4573 * If an {@link AccessibilityDelegate} has been specified via calling
4574 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4575 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4576 * is responsible for handling this call.
4577 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004578 * <p>
4579 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4580 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4581 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004582 *
4583 * @param event The event.
4584 *
4585 * @return True if the event population was completed.
4586 */
4587 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004588 if (mAccessibilityDelegate != null) {
4589 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4590 } else {
4591 return dispatchPopulateAccessibilityEventInternal(event);
4592 }
4593 }
4594
4595 /**
4596 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4597 *
4598 * Note: Called from the default {@link AccessibilityDelegate}.
4599 */
4600 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004601 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004602 return false;
4603 }
4604
4605 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004606 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004607 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004608 * text content. While this method is free to modify event
4609 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004610 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4611 * <p>
4612 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004613 * to the text added by the super implementation:
4614 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004615 * super.onPopulateAccessibilityEvent(event);
4616 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4617 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4618 * mCurrentDate.getTimeInMillis(), flags);
4619 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004620 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004621 * <p>
4622 * If an {@link AccessibilityDelegate} has been specified via calling
4623 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4624 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4625 * is responsible for handling this call.
4626 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004627 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4628 * information to the event, in case the default implementation has basic information to add.
4629 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004630 *
4631 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004632 *
4633 * @see #sendAccessibilityEvent(int)
4634 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004635 */
4636 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004637 if (mAccessibilityDelegate != null) {
4638 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4639 } else {
4640 onPopulateAccessibilityEventInternal(event);
4641 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004642 }
4643
4644 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004645 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4646 *
4647 * Note: Called from the default {@link AccessibilityDelegate}.
4648 */
4649 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4650
4651 }
4652
4653 /**
4654 * Initializes an {@link AccessibilityEvent} with information about
4655 * this View which is the event source. In other words, the source of
4656 * an accessibility event is the view whose state change triggered firing
4657 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004658 * <p>
4659 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004660 * to properties set by the super implementation:
4661 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4662 * super.onInitializeAccessibilityEvent(event);
4663 * event.setPassword(true);
4664 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004665 * <p>
4666 * If an {@link AccessibilityDelegate} has been specified via calling
4667 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4668 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4669 * is responsible for handling this call.
4670 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004671 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4672 * information to the event, in case the default implementation has basic information to add.
4673 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004674 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004675 *
4676 * @see #sendAccessibilityEvent(int)
4677 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4678 */
4679 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004680 if (mAccessibilityDelegate != null) {
4681 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4682 } else {
4683 onInitializeAccessibilityEventInternal(event);
4684 }
4685 }
4686
4687 /**
4688 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4689 *
4690 * Note: Called from the default {@link AccessibilityDelegate}.
4691 */
4692 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004693 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004694 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004695 event.setPackageName(getContext().getPackageName());
4696 event.setEnabled(isEnabled());
4697 event.setContentDescription(mContentDescription);
4698
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004699 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004700 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004701 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4702 FOCUSABLES_ALL);
4703 event.setItemCount(focusablesTempList.size());
4704 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4705 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004706 }
4707 }
4708
4709 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004710 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4711 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4712 * This method is responsible for obtaining an accessibility node info from a
4713 * pool of reusable instances and calling
4714 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4715 * initialize the former.
4716 * <p>
4717 * Note: The client is responsible for recycling the obtained instance by calling
4718 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4719 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004720 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004721 * @return A populated {@link AccessibilityNodeInfo}.
4722 *
4723 * @see AccessibilityNodeInfo
4724 */
4725 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004726 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4727 if (provider != null) {
4728 return provider.createAccessibilityNodeInfo(View.NO_ID);
4729 } else {
4730 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4731 onInitializeAccessibilityNodeInfo(info);
4732 return info;
4733 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004734 }
4735
4736 /**
4737 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4738 * The base implementation sets:
4739 * <ul>
4740 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004741 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4742 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004743 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4744 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4745 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4746 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4747 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4748 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4749 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4750 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4751 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4752 * </ul>
4753 * <p>
4754 * Subclasses should override this method, call the super implementation,
4755 * and set additional attributes.
4756 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004757 * <p>
4758 * If an {@link AccessibilityDelegate} has been specified via calling
4759 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4760 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4761 * is responsible for handling this call.
4762 * </p>
4763 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004764 * @param info The instance to initialize.
4765 */
4766 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004767 if (mAccessibilityDelegate != null) {
4768 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4769 } else {
4770 onInitializeAccessibilityNodeInfoInternal(info);
4771 }
4772 }
4773
4774 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004775 * Gets the location of this view in screen coordintates.
4776 *
4777 * @param outRect The output location
4778 */
4779 private void getBoundsOnScreen(Rect outRect) {
4780 if (mAttachInfo == null) {
4781 return;
4782 }
4783
4784 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004785 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004786
4787 if (!hasIdentityMatrix()) {
4788 getMatrix().mapRect(position);
4789 }
4790
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004791 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004792
4793 ViewParent parent = mParent;
4794 while (parent instanceof View) {
4795 View parentView = (View) parent;
4796
4797 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4798
4799 if (!parentView.hasIdentityMatrix()) {
4800 parentView.getMatrix().mapRect(position);
4801 }
4802
4803 position.offset(parentView.mLeft, parentView.mTop);
4804
4805 parent = parentView.mParent;
4806 }
4807
4808 if (parent instanceof ViewRootImpl) {
4809 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4810 position.offset(0, -viewRootImpl.mCurScrollY);
4811 }
4812
4813 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4814
4815 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4816 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4817 }
4818
4819 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004820 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4821 *
4822 * Note: Called from the default {@link AccessibilityDelegate}.
4823 */
4824 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004825 Rect bounds = mAttachInfo.mTmpInvalRect;
4826 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004827 info.setBoundsInParent(bounds);
4828
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004829 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004830 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004831
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004832 ViewParent parent = getParentForAccessibility();
4833 if (parent instanceof View) {
4834 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004835 }
4836
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004837 info.setVisibleToUser(isVisibleToUser());
4838
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004839 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004840 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004841 info.setContentDescription(getContentDescription());
4842
4843 info.setEnabled(isEnabled());
4844 info.setClickable(isClickable());
4845 info.setFocusable(isFocusable());
4846 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004847 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004848 info.setSelected(isSelected());
4849 info.setLongClickable(isLongClickable());
4850
4851 // TODO: These make sense only if we are in an AdapterView but all
4852 // views can be selected. Maybe from accessiiblity perspective
4853 // we should report as selectable view in an AdapterView.
4854 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4855 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4856
4857 if (isFocusable()) {
4858 if (isFocused()) {
4859 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4860 } else {
4861 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4862 }
4863 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004864
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004865 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004866 final int mode = getAccessibilityFocusable();
4867 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4868 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4869 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004870 } else {
4871 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4872 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004873
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004874 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004875 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4876 }
4877
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004878 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004879 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4880 }
4881
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004882 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004883 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4884 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4885 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004886 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4887 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004888 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004889 }
4890
4891 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004892 * Computes whether this view is visible to the user. Such a view is
4893 * attached, visible, all its predecessors are visible, it is not clipped
4894 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004895 *
4896 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004897 *
4898 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004899 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004900 protected boolean isVisibleToUser() {
4901 return isVisibleToUser(null);
4902 }
4903
4904 /**
4905 * Computes whether the given portion of this view is visible to the user. Such a view is
4906 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4907 * the specified portion is not clipped entirely by its predecessors.
4908 *
4909 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4910 * <code>null</code>, and the entire view will be tested in this case.
4911 * When <code>true</code> is returned by the function, the actual visible
4912 * region will be stored in this parameter; that is, if boundInView is fully
4913 * contained within the view, no modification will be made, otherwise regions
4914 * outside of the visible area of the view will be clipped.
4915 *
4916 * @return Whether the specified portion of the view is visible on the screen.
4917 *
4918 * @hide
4919 */
4920 protected boolean isVisibleToUser(Rect boundInView) {
4921 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4922 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004923 // The first two checks are made also made by isShown() which
4924 // however traverses the tree up to the parent to catch that.
4925 // Therefore, we do some fail fast check to minimize the up
4926 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004927 boolean isVisible = mAttachInfo != null
4928 && mAttachInfo.mWindowVisibility == View.VISIBLE
4929 && getAlpha() > 0
4930 && isShown()
4931 && getGlobalVisibleRect(visibleRect, offset);
4932 if (isVisible && boundInView != null) {
4933 visibleRect.offset(-offset.x, -offset.y);
4934 isVisible &= boundInView.intersect(visibleRect);
4935 }
4936 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004937 }
4938
4939 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004940 * Sets a delegate for implementing accessibility support via compositon as
4941 * opposed to inheritance. The delegate's primary use is for implementing
4942 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4943 *
4944 * @param delegate The delegate instance.
4945 *
4946 * @see AccessibilityDelegate
4947 */
4948 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4949 mAccessibilityDelegate = delegate;
4950 }
4951
4952 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004953 * Gets the provider for managing a virtual view hierarchy rooted at this View
4954 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4955 * that explore the window content.
4956 * <p>
4957 * If this method returns an instance, this instance is responsible for managing
4958 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4959 * View including the one representing the View itself. Similarly the returned
4960 * instance is responsible for performing accessibility actions on any virtual
4961 * view or the root view itself.
4962 * </p>
4963 * <p>
4964 * If an {@link AccessibilityDelegate} has been specified via calling
4965 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4966 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4967 * is responsible for handling this call.
4968 * </p>
4969 *
4970 * @return The provider.
4971 *
4972 * @see AccessibilityNodeProvider
4973 */
4974 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4975 if (mAccessibilityDelegate != null) {
4976 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4977 } else {
4978 return null;
4979 }
4980 }
4981
4982 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004983 * Gets the unique identifier of this view on the screen for accessibility purposes.
4984 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4985 *
4986 * @return The view accessibility id.
4987 *
4988 * @hide
4989 */
4990 public int getAccessibilityViewId() {
4991 if (mAccessibilityViewId == NO_ID) {
4992 mAccessibilityViewId = sNextAccessibilityViewId++;
4993 }
4994 return mAccessibilityViewId;
4995 }
4996
4997 /**
4998 * Gets the unique identifier of the window in which this View reseides.
4999 *
5000 * @return The window accessibility id.
5001 *
5002 * @hide
5003 */
5004 public int getAccessibilityWindowId() {
5005 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5006 }
5007
5008 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005009 * Gets the {@link View} description. It briefly describes the view and is
5010 * primarily used for accessibility support. Set this property to enable
5011 * better accessibility support for your application. This is especially
5012 * true for views that do not have textual representation (For example,
5013 * ImageButton).
5014 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005015 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005016 *
5017 * @attr ref android.R.styleable#View_contentDescription
5018 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005019 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005020 public CharSequence getContentDescription() {
5021 return mContentDescription;
5022 }
5023
5024 /**
5025 * Sets the {@link View} description. It briefly describes the view and is
5026 * primarily used for accessibility support. Set this property to enable
5027 * better accessibility support for your application. This is especially
5028 * true for views that do not have textual representation (For example,
5029 * ImageButton).
5030 *
5031 * @param contentDescription The content description.
5032 *
5033 * @attr ref android.R.styleable#View_contentDescription
5034 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005035 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005036 public void setContentDescription(CharSequence contentDescription) {
5037 mContentDescription = contentDescription;
5038 }
5039
5040 /**
Romain Guya2431d02009-04-30 16:30:00 -07005041 * Invoked whenever this view loses focus, either by losing window focus or by losing
5042 * focus within its window. This method can be used to clear any state tied to the
5043 * focus. For instance, if a button is held pressed with the trackball and the window
5044 * loses focus, this method can be used to cancel the press.
5045 *
5046 * Subclasses of View overriding this method should always call super.onFocusLost().
5047 *
5048 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005049 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005050 *
5051 * @hide pending API council approval
5052 */
5053 protected void onFocusLost() {
5054 resetPressedState();
5055 }
5056
5057 private void resetPressedState() {
5058 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5059 return;
5060 }
5061
5062 if (isPressed()) {
5063 setPressed(false);
5064
5065 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005066 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005067 }
5068 }
5069 }
5070
5071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 * Returns true if this view has focus
5073 *
5074 * @return True if this view has focus, false otherwise.
5075 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005076 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 public boolean isFocused() {
5078 return (mPrivateFlags & FOCUSED) != 0;
5079 }
5080
5081 /**
5082 * Find the view in the hierarchy rooted at this view that currently has
5083 * focus.
5084 *
5085 * @return The view that currently has focus, or null if no focused view can
5086 * be found.
5087 */
5088 public View findFocus() {
5089 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5090 }
5091
5092 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005093 * Indicates whether this view is one of the set of scrollable containers in
5094 * its window.
5095 *
5096 * @return whether this view is one of the set of scrollable containers in
5097 * its window
5098 *
5099 * @attr ref android.R.styleable#View_isScrollContainer
5100 */
5101 public boolean isScrollContainer() {
5102 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5103 }
5104
5105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 * Change whether this view is one of the set of scrollable containers in
5107 * its window. This will be used to determine whether the window can
5108 * resize or must pan when a soft input area is open -- scrollable
5109 * containers allow the window to use resize mode since the container
5110 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005111 *
5112 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 */
5114 public void setScrollContainer(boolean isScrollContainer) {
5115 if (isScrollContainer) {
5116 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5117 mAttachInfo.mScrollContainers.add(this);
5118 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5119 }
5120 mPrivateFlags |= SCROLL_CONTAINER;
5121 } else {
5122 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5123 mAttachInfo.mScrollContainers.remove(this);
5124 }
5125 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5126 }
5127 }
5128
5129 /**
5130 * Returns the quality of the drawing cache.
5131 *
5132 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5133 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5134 *
5135 * @see #setDrawingCacheQuality(int)
5136 * @see #setDrawingCacheEnabled(boolean)
5137 * @see #isDrawingCacheEnabled()
5138 *
5139 * @attr ref android.R.styleable#View_drawingCacheQuality
5140 */
5141 public int getDrawingCacheQuality() {
5142 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5143 }
5144
5145 /**
5146 * Set the drawing cache quality of this view. This value is used only when the
5147 * drawing cache is enabled
5148 *
5149 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5150 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5151 *
5152 * @see #getDrawingCacheQuality()
5153 * @see #setDrawingCacheEnabled(boolean)
5154 * @see #isDrawingCacheEnabled()
5155 *
5156 * @attr ref android.R.styleable#View_drawingCacheQuality
5157 */
5158 public void setDrawingCacheQuality(int quality) {
5159 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5160 }
5161
5162 /**
5163 * Returns whether the screen should remain on, corresponding to the current
5164 * value of {@link #KEEP_SCREEN_ON}.
5165 *
5166 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5167 *
5168 * @see #setKeepScreenOn(boolean)
5169 *
5170 * @attr ref android.R.styleable#View_keepScreenOn
5171 */
5172 public boolean getKeepScreenOn() {
5173 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5174 }
5175
5176 /**
5177 * Controls whether the screen should remain on, modifying the
5178 * value of {@link #KEEP_SCREEN_ON}.
5179 *
5180 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5181 *
5182 * @see #getKeepScreenOn()
5183 *
5184 * @attr ref android.R.styleable#View_keepScreenOn
5185 */
5186 public void setKeepScreenOn(boolean keepScreenOn) {
5187 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5188 }
5189
5190 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005191 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5192 * @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 -08005193 *
5194 * @attr ref android.R.styleable#View_nextFocusLeft
5195 */
5196 public int getNextFocusLeftId() {
5197 return mNextFocusLeftId;
5198 }
5199
5200 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005201 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5202 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5203 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 *
5205 * @attr ref android.R.styleable#View_nextFocusLeft
5206 */
5207 public void setNextFocusLeftId(int nextFocusLeftId) {
5208 mNextFocusLeftId = nextFocusLeftId;
5209 }
5210
5211 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005212 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5213 * @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 -08005214 *
5215 * @attr ref android.R.styleable#View_nextFocusRight
5216 */
5217 public int getNextFocusRightId() {
5218 return mNextFocusRightId;
5219 }
5220
5221 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005222 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5223 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5224 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005225 *
5226 * @attr ref android.R.styleable#View_nextFocusRight
5227 */
5228 public void setNextFocusRightId(int nextFocusRightId) {
5229 mNextFocusRightId = nextFocusRightId;
5230 }
5231
5232 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005233 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5234 * @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 -08005235 *
5236 * @attr ref android.R.styleable#View_nextFocusUp
5237 */
5238 public int getNextFocusUpId() {
5239 return mNextFocusUpId;
5240 }
5241
5242 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005243 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5244 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5245 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 *
5247 * @attr ref android.R.styleable#View_nextFocusUp
5248 */
5249 public void setNextFocusUpId(int nextFocusUpId) {
5250 mNextFocusUpId = nextFocusUpId;
5251 }
5252
5253 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005254 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5255 * @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 -08005256 *
5257 * @attr ref android.R.styleable#View_nextFocusDown
5258 */
5259 public int getNextFocusDownId() {
5260 return mNextFocusDownId;
5261 }
5262
5263 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005264 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5265 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5266 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 *
5268 * @attr ref android.R.styleable#View_nextFocusDown
5269 */
5270 public void setNextFocusDownId(int nextFocusDownId) {
5271 mNextFocusDownId = nextFocusDownId;
5272 }
5273
5274 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005275 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5276 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5277 *
5278 * @attr ref android.R.styleable#View_nextFocusForward
5279 */
5280 public int getNextFocusForwardId() {
5281 return mNextFocusForwardId;
5282 }
5283
5284 /**
5285 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5286 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5287 * decide automatically.
5288 *
5289 * @attr ref android.R.styleable#View_nextFocusForward
5290 */
5291 public void setNextFocusForwardId(int nextFocusForwardId) {
5292 mNextFocusForwardId = nextFocusForwardId;
5293 }
5294
5295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 * Returns the visibility of this view and all of its ancestors
5297 *
5298 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5299 */
5300 public boolean isShown() {
5301 View current = this;
5302 //noinspection ConstantConditions
5303 do {
5304 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5305 return false;
5306 }
5307 ViewParent parent = current.mParent;
5308 if (parent == null) {
5309 return false; // We are not attached to the view root
5310 }
5311 if (!(parent instanceof View)) {
5312 return true;
5313 }
5314 current = (View) parent;
5315 } while (current != null);
5316
5317 return false;
5318 }
5319
5320 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005321 * Called by the view hierarchy when the content insets for a window have
5322 * changed, to allow it to adjust its content to fit within those windows.
5323 * The content insets tell you the space that the status bar, input method,
5324 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005326 * <p>You do not normally need to deal with this function, since the default
5327 * window decoration given to applications takes care of applying it to the
5328 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5329 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5330 * and your content can be placed under those system elements. You can then
5331 * use this method within your view hierarchy if you have parts of your UI
5332 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005334 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005335 * inset's to the view's padding, consuming that content (modifying the
5336 * insets to be 0), and returning true. This behavior is off by default, but can
5337 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5338 *
5339 * <p>This function's traversal down the hierarchy is depth-first. The same content
5340 * insets object is propagated down the hierarchy, so any changes made to it will
5341 * be seen by all following views (including potentially ones above in
5342 * the hierarchy since this is a depth-first traversal). The first view
5343 * that returns true will abort the entire traversal.
5344 *
5345 * <p>The default implementation works well for a situation where it is
5346 * used with a container that covers the entire window, allowing it to
5347 * apply the appropriate insets to its content on all edges. If you need
5348 * a more complicated layout (such as two different views fitting system
5349 * windows, one on the top of the window, and one on the bottom),
5350 * you can override the method and handle the insets however you would like.
5351 * Note that the insets provided by the framework are always relative to the
5352 * far edges of the window, not accounting for the location of the called view
5353 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005354 * where the layout will place the view, as it is done before layout happens.)
5355 *
5356 * <p>Note: unlike many View methods, there is no dispatch phase to this
5357 * call. If you are overriding it in a ViewGroup and want to allow the
5358 * call to continue to your children, you must be sure to call the super
5359 * implementation.
5360 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005361 * <p>Here is a sample layout that makes use of fitting system windows
5362 * to have controls for a video view placed inside of the window decorations
5363 * that it hides and shows. This can be used with code like the second
5364 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5365 *
5366 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5367 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005368 * @param insets Current content insets of the window. Prior to
5369 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5370 * the insets or else you and Android will be unhappy.
5371 *
5372 * @return Return true if this view applied the insets and it should not
5373 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005374 * @see #getFitsSystemWindows()
5375 * @see #setFitsSystemWindows()
5376 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 */
5378 protected boolean fitSystemWindows(Rect insets) {
5379 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005380 mUserPaddingStart = -1;
5381 mUserPaddingEnd = -1;
5382 mUserPaddingRelative = false;
5383 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5384 || mAttachInfo == null
5385 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5386 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5387 return true;
5388 } else {
5389 internalSetPadding(0, 0, 0, 0);
5390 return false;
5391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005392 }
5393 return false;
5394 }
5395
5396 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005397 * Sets whether or not this view should account for system screen decorations
5398 * such as the status bar and inset its content; that is, controlling whether
5399 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5400 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005401 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005402 * <p>Note that if you are providing your own implementation of
5403 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5404 * flag to true -- your implementation will be overriding the default
5405 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005406 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005407 * @param fitSystemWindows If true, then the default implementation of
5408 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005409 *
5410 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005411 * @see #getFitsSystemWindows()
5412 * @see #fitSystemWindows(Rect)
5413 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005414 */
5415 public void setFitsSystemWindows(boolean fitSystemWindows) {
5416 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5417 }
5418
5419 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005420 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005421 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5422 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005423 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005424 * @return Returns true if the default implementation of
5425 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005426 *
5427 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005428 * @see #setFitsSystemWindows()
5429 * @see #fitSystemWindows(Rect)
5430 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005431 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005432 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005433 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5434 }
5435
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005436 /** @hide */
5437 public boolean fitsSystemWindows() {
5438 return getFitsSystemWindows();
5439 }
5440
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005441 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005442 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5443 */
5444 public void requestFitSystemWindows() {
5445 if (mParent != null) {
5446 mParent.requestFitSystemWindows();
5447 }
5448 }
5449
5450 /**
5451 * For use by PhoneWindow to make its own system window fitting optional.
5452 * @hide
5453 */
5454 public void makeOptionalFitsSystemWindows() {
5455 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5456 }
5457
5458 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 * Returns the visibility status for this view.
5460 *
5461 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5462 * @attr ref android.R.styleable#View_visibility
5463 */
5464 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005465 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5466 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5467 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 })
5469 public int getVisibility() {
5470 return mViewFlags & VISIBILITY_MASK;
5471 }
5472
5473 /**
5474 * Set the enabled state of this view.
5475 *
5476 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5477 * @attr ref android.R.styleable#View_visibility
5478 */
5479 @RemotableViewMethod
5480 public void setVisibility(int visibility) {
5481 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005482 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
5484
5485 /**
5486 * Returns the enabled status for this view. The interpretation of the
5487 * enabled state varies by subclass.
5488 *
5489 * @return True if this view is enabled, false otherwise.
5490 */
5491 @ViewDebug.ExportedProperty
5492 public boolean isEnabled() {
5493 return (mViewFlags & ENABLED_MASK) == ENABLED;
5494 }
5495
5496 /**
5497 * Set the enabled state of this view. The interpretation of the enabled
5498 * state varies by subclass.
5499 *
5500 * @param enabled True if this view is enabled, false otherwise.
5501 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005502 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005504 if (enabled == isEnabled()) return;
5505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5507
5508 /*
5509 * The View most likely has to change its appearance, so refresh
5510 * the drawable state.
5511 */
5512 refreshDrawableState();
5513
5514 // Invalidate too, since the default behavior for views is to be
5515 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005516 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 }
5518
5519 /**
5520 * Set whether this view can receive the focus.
5521 *
5522 * Setting this to false will also ensure that this view is not focusable
5523 * in touch mode.
5524 *
5525 * @param focusable If true, this view can receive the focus.
5526 *
5527 * @see #setFocusableInTouchMode(boolean)
5528 * @attr ref android.R.styleable#View_focusable
5529 */
5530 public void setFocusable(boolean focusable) {
5531 if (!focusable) {
5532 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5533 }
5534 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5535 }
5536
5537 /**
5538 * Set whether this view can receive focus while in touch mode.
5539 *
5540 * Setting this to true will also ensure that this view is focusable.
5541 *
5542 * @param focusableInTouchMode If true, this view can receive the focus while
5543 * in touch mode.
5544 *
5545 * @see #setFocusable(boolean)
5546 * @attr ref android.R.styleable#View_focusableInTouchMode
5547 */
5548 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5549 // Focusable in touch mode should always be set before the focusable flag
5550 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5551 // which, in touch mode, will not successfully request focus on this view
5552 // because the focusable in touch mode flag is not set
5553 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5554 if (focusableInTouchMode) {
5555 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5556 }
5557 }
5558
5559 /**
5560 * Set whether this view should have sound effects enabled for events such as
5561 * clicking and touching.
5562 *
5563 * <p>You may wish to disable sound effects for a view if you already play sounds,
5564 * for instance, a dial key that plays dtmf tones.
5565 *
5566 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5567 * @see #isSoundEffectsEnabled()
5568 * @see #playSoundEffect(int)
5569 * @attr ref android.R.styleable#View_soundEffectsEnabled
5570 */
5571 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5572 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5573 }
5574
5575 /**
5576 * @return whether this view should have sound effects enabled for events such as
5577 * clicking and touching.
5578 *
5579 * @see #setSoundEffectsEnabled(boolean)
5580 * @see #playSoundEffect(int)
5581 * @attr ref android.R.styleable#View_soundEffectsEnabled
5582 */
5583 @ViewDebug.ExportedProperty
5584 public boolean isSoundEffectsEnabled() {
5585 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5586 }
5587
5588 /**
5589 * Set whether this view should have haptic feedback for events such as
5590 * long presses.
5591 *
5592 * <p>You may wish to disable haptic feedback if your view already controls
5593 * its own haptic feedback.
5594 *
5595 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5596 * @see #isHapticFeedbackEnabled()
5597 * @see #performHapticFeedback(int)
5598 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5599 */
5600 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5601 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5602 }
5603
5604 /**
5605 * @return whether this view should have haptic feedback enabled for events
5606 * long presses.
5607 *
5608 * @see #setHapticFeedbackEnabled(boolean)
5609 * @see #performHapticFeedback(int)
5610 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5611 */
5612 @ViewDebug.ExportedProperty
5613 public boolean isHapticFeedbackEnabled() {
5614 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5615 }
5616
5617 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005618 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005619 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005620 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5621 * {@link #LAYOUT_DIRECTION_RTL},
5622 * {@link #LAYOUT_DIRECTION_INHERIT} or
5623 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005624 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005625 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005626 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005627 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005628 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005629 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5630 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5631 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5632 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005633 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005634 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005635 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005636 }
5637
5638 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005639 * Set the layout direction for this view. This will propagate a reset of layout direction
5640 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005641 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005642 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5643 * {@link #LAYOUT_DIRECTION_RTL},
5644 * {@link #LAYOUT_DIRECTION_INHERIT} or
5645 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005646 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005647 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005648 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005649 */
5650 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005651 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005652 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005653 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005654 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005655 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005656 // Set the new layout direction (filtered) and ask for a layout pass
5657 mPrivateFlags2 |=
5658 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5659 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005660 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005661 }
5662
5663 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005664 * Returns the resolved layout direction for this view.
5665 *
5666 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005667 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005668 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005669 */
5670 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005671 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5672 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005673 })
5674 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005675 // The layout diretion will be resolved only if needed
5676 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5677 resolveLayoutDirection();
5678 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005679 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005680 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5681 }
5682
5683 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005684 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5685 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005686 *
5687 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005688 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005689 */
5690 @ViewDebug.ExportedProperty(category = "layout")
5691 public boolean isLayoutRtl() {
5692 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5693 }
5694
5695 /**
Adam Powell539ee872012-02-03 19:00:49 -08005696 * Indicates whether the view is currently tracking transient state that the
5697 * app should not need to concern itself with saving and restoring, but that
5698 * the framework should take special note to preserve when possible.
5699 *
Adam Powell785c4472012-05-02 21:25:39 -07005700 * <p>A view with transient state cannot be trivially rebound from an external
5701 * data source, such as an adapter binding item views in a list. This may be
5702 * because the view is performing an animation, tracking user selection
5703 * of content, or similar.</p>
5704 *
Adam Powell539ee872012-02-03 19:00:49 -08005705 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005706 */
5707 @ViewDebug.ExportedProperty(category = "layout")
5708 public boolean hasTransientState() {
5709 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5710 }
5711
5712 /**
5713 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005714 * framework should attempt to preserve when possible. This flag is reference counted,
5715 * so every call to setHasTransientState(true) should be paired with a later call
5716 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005717 *
Adam Powell785c4472012-05-02 21:25:39 -07005718 * <p>A view with transient state cannot be trivially rebound from an external
5719 * data source, such as an adapter binding item views in a list. This may be
5720 * because the view is performing an animation, tracking user selection
5721 * of content, or similar.</p>
5722 *
Adam Powell539ee872012-02-03 19:00:49 -08005723 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005724 */
5725 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005726 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5727 mTransientStateCount - 1;
5728 if (mTransientStateCount < 0) {
5729 mTransientStateCount = 0;
5730 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5731 "unmatched pair of setHasTransientState calls");
5732 }
5733 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005734 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005735 // update flag if we've just incremented up from 0 or decremented down to 0
5736 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5737 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5738 if (mParent != null) {
5739 try {
5740 mParent.childHasTransientStateChanged(this, hasTransientState);
5741 } catch (AbstractMethodError e) {
5742 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5743 " does not fully implement ViewParent", e);
5744 }
Adam Powell539ee872012-02-03 19:00:49 -08005745 }
5746 }
5747 }
5748
5749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 * If this view doesn't do any drawing on its own, set this flag to
5751 * allow further optimizations. By default, this flag is not set on
5752 * View, but could be set on some View subclasses such as ViewGroup.
5753 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005754 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5755 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 *
5757 * @param willNotDraw whether or not this View draw on its own
5758 */
5759 public void setWillNotDraw(boolean willNotDraw) {
5760 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5761 }
5762
5763 /**
5764 * Returns whether or not this View draws on its own.
5765 *
5766 * @return true if this view has nothing to draw, false otherwise
5767 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005768 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 public boolean willNotDraw() {
5770 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5771 }
5772
5773 /**
5774 * When a View's drawing cache is enabled, drawing is redirected to an
5775 * offscreen bitmap. Some views, like an ImageView, must be able to
5776 * bypass this mechanism if they already draw a single bitmap, to avoid
5777 * unnecessary usage of the memory.
5778 *
5779 * @param willNotCacheDrawing true if this view does not cache its
5780 * drawing, false otherwise
5781 */
5782 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5783 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5784 }
5785
5786 /**
5787 * Returns whether or not this View can cache its drawing or not.
5788 *
5789 * @return true if this view does not cache its drawing, false otherwise
5790 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005791 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 public boolean willNotCacheDrawing() {
5793 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5794 }
5795
5796 /**
5797 * Indicates whether this view reacts to click events or not.
5798 *
5799 * @return true if the view is clickable, false otherwise
5800 *
5801 * @see #setClickable(boolean)
5802 * @attr ref android.R.styleable#View_clickable
5803 */
5804 @ViewDebug.ExportedProperty
5805 public boolean isClickable() {
5806 return (mViewFlags & CLICKABLE) == CLICKABLE;
5807 }
5808
5809 /**
5810 * Enables or disables click events for this view. When a view
5811 * is clickable it will change its state to "pressed" on every click.
5812 * Subclasses should set the view clickable to visually react to
5813 * user's clicks.
5814 *
5815 * @param clickable true to make the view clickable, false otherwise
5816 *
5817 * @see #isClickable()
5818 * @attr ref android.R.styleable#View_clickable
5819 */
5820 public void setClickable(boolean clickable) {
5821 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5822 }
5823
5824 /**
5825 * Indicates whether this view reacts to long click events or not.
5826 *
5827 * @return true if the view is long clickable, false otherwise
5828 *
5829 * @see #setLongClickable(boolean)
5830 * @attr ref android.R.styleable#View_longClickable
5831 */
5832 public boolean isLongClickable() {
5833 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5834 }
5835
5836 /**
5837 * Enables or disables long click events for this view. When a view is long
5838 * clickable it reacts to the user holding down the button for a longer
5839 * duration than a tap. This event can either launch the listener or a
5840 * context menu.
5841 *
5842 * @param longClickable true to make the view long clickable, false otherwise
5843 * @see #isLongClickable()
5844 * @attr ref android.R.styleable#View_longClickable
5845 */
5846 public void setLongClickable(boolean longClickable) {
5847 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5848 }
5849
5850 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005851 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 *
5853 * @see #isClickable()
5854 * @see #setClickable(boolean)
5855 *
5856 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5857 * the View's internal state from a previously set "pressed" state.
5858 */
5859 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005860 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 if (pressed) {
5863 mPrivateFlags |= PRESSED;
5864 } else {
5865 mPrivateFlags &= ~PRESSED;
5866 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005867
5868 if (needsRefresh) {
5869 refreshDrawableState();
5870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 dispatchSetPressed(pressed);
5872 }
5873
5874 /**
5875 * Dispatch setPressed to all of this View's children.
5876 *
5877 * @see #setPressed(boolean)
5878 *
5879 * @param pressed The new pressed state
5880 */
5881 protected void dispatchSetPressed(boolean pressed) {
5882 }
5883
5884 /**
5885 * Indicates whether the view is currently in pressed state. Unless
5886 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5887 * the pressed state.
5888 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005889 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005890 * @see #isClickable()
5891 * @see #setClickable(boolean)
5892 *
5893 * @return true if the view is currently pressed, false otherwise
5894 */
5895 public boolean isPressed() {
5896 return (mPrivateFlags & PRESSED) == PRESSED;
5897 }
5898
5899 /**
5900 * Indicates whether this view will save its state (that is,
5901 * whether its {@link #onSaveInstanceState} method will be called).
5902 *
5903 * @return Returns true if the view state saving is enabled, else false.
5904 *
5905 * @see #setSaveEnabled(boolean)
5906 * @attr ref android.R.styleable#View_saveEnabled
5907 */
5908 public boolean isSaveEnabled() {
5909 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5910 }
5911
5912 /**
5913 * Controls whether the saving of this view's state is
5914 * enabled (that is, whether its {@link #onSaveInstanceState} method
5915 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005916 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 * for its state to be saved. This flag can only disable the
5918 * saving of this view; any child views may still have their state saved.
5919 *
5920 * @param enabled Set to false to <em>disable</em> state saving, or true
5921 * (the default) to allow it.
5922 *
5923 * @see #isSaveEnabled()
5924 * @see #setId(int)
5925 * @see #onSaveInstanceState()
5926 * @attr ref android.R.styleable#View_saveEnabled
5927 */
5928 public void setSaveEnabled(boolean enabled) {
5929 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5930 }
5931
Jeff Brown85a31762010-09-01 17:01:00 -07005932 /**
5933 * Gets whether the framework should discard touches when the view's
5934 * window is obscured by another visible window.
5935 * Refer to the {@link View} security documentation for more details.
5936 *
5937 * @return True if touch filtering is enabled.
5938 *
5939 * @see #setFilterTouchesWhenObscured(boolean)
5940 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5941 */
5942 @ViewDebug.ExportedProperty
5943 public boolean getFilterTouchesWhenObscured() {
5944 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5945 }
5946
5947 /**
5948 * Sets whether the framework should discard touches when the view's
5949 * window is obscured by another visible window.
5950 * Refer to the {@link View} security documentation for more details.
5951 *
5952 * @param enabled True if touch filtering should be enabled.
5953 *
5954 * @see #getFilterTouchesWhenObscured
5955 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5956 */
5957 public void setFilterTouchesWhenObscured(boolean enabled) {
5958 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5959 FILTER_TOUCHES_WHEN_OBSCURED);
5960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961
5962 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005963 * Indicates whether the entire hierarchy under this view will save its
5964 * state when a state saving traversal occurs from its parent. The default
5965 * is true; if false, these views will not be saved unless
5966 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5967 *
5968 * @return Returns true if the view state saving from parent is enabled, else false.
5969 *
5970 * @see #setSaveFromParentEnabled(boolean)
5971 */
5972 public boolean isSaveFromParentEnabled() {
5973 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5974 }
5975
5976 /**
5977 * Controls whether the entire hierarchy under this view will save its
5978 * state when a state saving traversal occurs from its parent. The default
5979 * is true; if false, these views will not be saved unless
5980 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5981 *
5982 * @param enabled Set to false to <em>disable</em> state saving, or true
5983 * (the default) to allow it.
5984 *
5985 * @see #isSaveFromParentEnabled()
5986 * @see #setId(int)
5987 * @see #onSaveInstanceState()
5988 */
5989 public void setSaveFromParentEnabled(boolean enabled) {
5990 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5991 }
5992
5993
5994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 * Returns whether this View is able to take focus.
5996 *
5997 * @return True if this view can take focus, or false otherwise.
5998 * @attr ref android.R.styleable#View_focusable
5999 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006000 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 public final boolean isFocusable() {
6002 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6003 }
6004
6005 /**
6006 * When a view is focusable, it may not want to take focus when in touch mode.
6007 * For example, a button would like focus when the user is navigating via a D-pad
6008 * so that the user can click on it, but once the user starts touching the screen,
6009 * the button shouldn't take focus
6010 * @return Whether the view is focusable in touch mode.
6011 * @attr ref android.R.styleable#View_focusableInTouchMode
6012 */
6013 @ViewDebug.ExportedProperty
6014 public final boolean isFocusableInTouchMode() {
6015 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6016 }
6017
6018 /**
6019 * Find the nearest view in the specified direction that can take focus.
6020 * This does not actually give focus to that view.
6021 *
6022 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6023 *
6024 * @return The nearest focusable in the specified direction, or null if none
6025 * can be found.
6026 */
6027 public View focusSearch(int direction) {
6028 if (mParent != null) {
6029 return mParent.focusSearch(this, direction);
6030 } else {
6031 return null;
6032 }
6033 }
6034
6035 /**
6036 * This method is the last chance for the focused view and its ancestors to
6037 * respond to an arrow key. This is called when the focused view did not
6038 * consume the key internally, nor could the view system find a new view in
6039 * the requested direction to give focus to.
6040 *
6041 * @param focused The currently focused view.
6042 * @param direction The direction focus wants to move. One of FOCUS_UP,
6043 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6044 * @return True if the this view consumed this unhandled move.
6045 */
6046 public boolean dispatchUnhandledMove(View focused, int direction) {
6047 return false;
6048 }
6049
6050 /**
6051 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006052 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006054 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6055 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 * @return The user specified next view, or null if there is none.
6057 */
6058 View findUserSetNextFocus(View root, int direction) {
6059 switch (direction) {
6060 case FOCUS_LEFT:
6061 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006062 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 case FOCUS_RIGHT:
6064 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006065 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 case FOCUS_UP:
6067 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006068 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 case FOCUS_DOWN:
6070 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006071 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006072 case FOCUS_FORWARD:
6073 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006074 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006075 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006076 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006077 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006078 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006079 @Override
6080 public boolean apply(View t) {
6081 return t.mNextFocusForwardId == id;
6082 }
6083 });
6084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086 return null;
6087 }
6088
Jeff Brown4dfbec22011-08-15 14:55:37 -07006089 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6090 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6091 @Override
6092 public boolean apply(View t) {
6093 return t.mID == childViewId;
6094 }
6095 });
6096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 if (result == null) {
6098 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6099 + "by user for id " + childViewId);
6100 }
6101 return result;
6102 }
6103
6104 /**
6105 * Find and return all focusable views that are descendants of this view,
6106 * possibly including this view if it is focusable itself.
6107 *
6108 * @param direction The direction of the focus
6109 * @return A list of focusable views
6110 */
6111 public ArrayList<View> getFocusables(int direction) {
6112 ArrayList<View> result = new ArrayList<View>(24);
6113 addFocusables(result, direction);
6114 return result;
6115 }
6116
6117 /**
6118 * Add any focusable views that are descendants of this view (possibly
6119 * including this view if it is focusable itself) to views. If we are in touch mode,
6120 * only add views that are also focusable in touch mode.
6121 *
6122 * @param views Focusable views found so far
6123 * @param direction The direction of the focus
6124 */
6125 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006126 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128
svetoslavganov75986cf2009-05-14 22:28:01 -07006129 /**
6130 * Adds any focusable views that are descendants of this view (possibly
6131 * including this view if it is focusable itself) to views. This method
6132 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006133 * only views focusable in touch mode if we are in touch mode or
6134 * only views that can take accessibility focus if accessibility is enabeld
6135 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006136 *
6137 * @param views Focusable views found so far or null if all we are interested is
6138 * the number of focusables.
6139 * @param direction The direction of the focus.
6140 * @param focusableMode The type of focusables to be added.
6141 *
6142 * @see #FOCUSABLES_ALL
6143 * @see #FOCUSABLES_TOUCH_MODE
6144 */
6145 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006147 return;
6148 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006149 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006150 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006151 views.add(this);
6152 return;
6153 }
6154 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006155 if (!isFocusable()) {
6156 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006157 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006158 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6159 && isInTouchMode() && !isFocusableInTouchMode()) {
6160 return;
6161 }
6162 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 }
6164
6165 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006166 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006167 * The search is performed by either the text that the View renders or the content
6168 * description that describes the view for accessibility purposes and the view does
6169 * not render or both. Clients can specify how the search is to be performed via
6170 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6171 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006172 *
6173 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006174 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006175 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006176 * @see #FIND_VIEWS_WITH_TEXT
6177 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6178 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006179 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006180 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006181 if (getAccessibilityNodeProvider() != null) {
6182 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6183 outViews.add(this);
6184 }
6185 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006186 && (searched != null && searched.length() > 0)
6187 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006188 String searchedLowerCase = searched.toString().toLowerCase();
6189 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6190 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6191 outViews.add(this);
6192 }
6193 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006194 }
6195
6196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006197 * Find and return all touchable views that are descendants of this view,
6198 * possibly including this view if it is touchable itself.
6199 *
6200 * @return A list of touchable views
6201 */
6202 public ArrayList<View> getTouchables() {
6203 ArrayList<View> result = new ArrayList<View>();
6204 addTouchables(result);
6205 return result;
6206 }
6207
6208 /**
6209 * Add any touchable views that are descendants of this view (possibly
6210 * including this view if it is touchable itself) to views.
6211 *
6212 * @param views Touchable views found so far
6213 */
6214 public void addTouchables(ArrayList<View> views) {
6215 final int viewFlags = mViewFlags;
6216
6217 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6218 && (viewFlags & ENABLED_MASK) == ENABLED) {
6219 views.add(this);
6220 }
6221 }
6222
6223 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006224 * Returns whether this View is accessibility focused.
6225 *
6226 * @return True if this View is accessibility focused.
6227 */
6228 boolean isAccessibilityFocused() {
6229 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6230 }
6231
6232 /**
6233 * Call this to try to give accessibility focus to this view.
6234 *
6235 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6236 * returns false or the view is no visible or the view already has accessibility
6237 * focus.
6238 *
6239 * See also {@link #focusSearch(int)}, which is what you call to say that you
6240 * have focus, and you want your parent to look for the next one.
6241 *
6242 * @return Whether this view actually took accessibility focus.
6243 *
6244 * @hide
6245 */
6246 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006247 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6248 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006249 return false;
6250 }
6251 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6252 return false;
6253 }
6254 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6255 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6256 ViewRootImpl viewRootImpl = getViewRootImpl();
6257 if (viewRootImpl != null) {
6258 viewRootImpl.setAccessibilityFocusedHost(this);
6259 }
6260 invalidate();
6261 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6262 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006263 return true;
6264 }
6265 return false;
6266 }
6267
6268 /**
6269 * Call this to try to clear accessibility focus of this view.
6270 *
6271 * See also {@link #focusSearch(int)}, which is what you call to say that you
6272 * have focus, and you want your parent to look for the next one.
6273 *
6274 * @hide
6275 */
6276 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006277 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6278 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006279 invalidate();
6280 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6281 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006282 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006283 // Clear the global reference of accessibility focus if this
6284 // view or any of its descendants had accessibility focus.
6285 ViewRootImpl viewRootImpl = getViewRootImpl();
6286 if (viewRootImpl != null) {
6287 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6288 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6289 viewRootImpl.setAccessibilityFocusedHost(null);
6290 }
6291 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006292 }
6293
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006294 private void requestAccessibilityFocusFromHover() {
6295 if (includeForAccessibility() && isActionableForAccessibility()) {
6296 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006297 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006298 } else {
6299 if (mParent != null) {
6300 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6301 if (nextFocus != null) {
6302 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006303 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006304 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006305 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006306 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006307 }
6308
Svetoslav Ganova90e4512012-06-01 19:02:32 -07006309 private boolean canTakeAccessibilityFocusFromHover() {
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006310 if (includeForAccessibility() && isActionableForAccessibility()) {
6311 return true;
6312 }
6313 if (mParent != null) {
6314 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006315 }
6316 return false;
6317 }
6318
6319 /**
6320 * Clears accessibility focus without calling any callback methods
6321 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6322 * is used for clearing accessibility focus when giving this focus to
6323 * another view.
6324 */
6325 void clearAccessibilityFocusNoCallbacks() {
6326 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6327 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6328 invalidate();
6329 }
6330 }
6331
6332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 * Call this to try to give focus to a specific view or to one of its
6334 * descendants.
6335 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006336 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6337 * false), or if it is focusable and it is not focusable in touch mode
6338 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006339 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006340 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 * have focus, and you want your parent to look for the next one.
6342 *
6343 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6344 * {@link #FOCUS_DOWN} and <code>null</code>.
6345 *
6346 * @return Whether this view or one of its descendants actually took focus.
6347 */
6348 public final boolean requestFocus() {
6349 return requestFocus(View.FOCUS_DOWN);
6350 }
6351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 /**
6353 * Call this to try to give focus to a specific view or to one of its
6354 * descendants and give it a hint about what direction focus is heading.
6355 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006356 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6357 * false), or if it is focusable and it is not focusable in touch mode
6358 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006360 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 * have focus, and you want your parent to look for the next one.
6362 *
6363 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6364 * <code>null</code> set for the previously focused rectangle.
6365 *
6366 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6367 * @return Whether this view or one of its descendants actually took focus.
6368 */
6369 public final boolean requestFocus(int direction) {
6370 return requestFocus(direction, null);
6371 }
6372
6373 /**
6374 * Call this to try to give focus to a specific view or to one of its descendants
6375 * and give it hints about the direction and a specific rectangle that the focus
6376 * is coming from. The rectangle can help give larger views a finer grained hint
6377 * about where focus is coming from, and therefore, where to show selection, or
6378 * forward focus change internally.
6379 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006380 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6381 * false), or if it is focusable and it is not focusable in touch mode
6382 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 *
6384 * A View will not take focus if it is not visible.
6385 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006386 * A View will not take focus if one of its parents has
6387 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6388 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006390 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 * have focus, and you want your parent to look for the next one.
6392 *
6393 * You may wish to override this method if your custom {@link View} has an internal
6394 * {@link View} that it wishes to forward the request to.
6395 *
6396 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6397 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6398 * to give a finer grained hint about where focus is coming from. May be null
6399 * if there is no hint.
6400 * @return Whether this view or one of its descendants actually took focus.
6401 */
6402 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006403 return requestFocusNoSearch(direction, previouslyFocusedRect);
6404 }
6405
6406 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 // need to be focusable
6408 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6409 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6410 return false;
6411 }
6412
6413 // need to be focusable in touch mode if in touch mode
6414 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006415 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6416 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 }
6418
6419 // need to not have any parents blocking us
6420 if (hasAncestorThatBlocksDescendantFocus()) {
6421 return false;
6422 }
6423
6424 handleFocusGainInternal(direction, previouslyFocusedRect);
6425 return true;
6426 }
6427
6428 /**
6429 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6430 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6431 * touch mode to request focus when they are touched.
6432 *
6433 * @return Whether this view or one of its descendants actually took focus.
6434 *
6435 * @see #isInTouchMode()
6436 *
6437 */
6438 public final boolean requestFocusFromTouch() {
6439 // Leave touch mode if we need to
6440 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006441 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006442 if (viewRoot != null) {
6443 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 }
6445 }
6446 return requestFocus(View.FOCUS_DOWN);
6447 }
6448
6449 /**
6450 * @return Whether any ancestor of this view blocks descendant focus.
6451 */
6452 private boolean hasAncestorThatBlocksDescendantFocus() {
6453 ViewParent ancestor = mParent;
6454 while (ancestor instanceof ViewGroup) {
6455 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6456 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6457 return true;
6458 } else {
6459 ancestor = vgAncestor.getParent();
6460 }
6461 }
6462 return false;
6463 }
6464
6465 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006466 * Gets the mode for determining whether this View is important for accessibility
6467 * which is if it fires accessibility events and if it is reported to
6468 * accessibility services that query the screen.
6469 *
6470 * @return The mode for determining whether a View is important for accessibility.
6471 *
6472 * @attr ref android.R.styleable#View_importantForAccessibility
6473 *
6474 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6475 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6476 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6477 */
6478 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006479 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6480 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6481 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006482 })
6483 public int getImportantForAccessibility() {
6484 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6485 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6486 }
6487
6488 /**
6489 * Sets how to determine whether this view is important for accessibility
6490 * which is if it fires accessibility events and if it is reported to
6491 * accessibility services that query the screen.
6492 *
6493 * @param mode How to determine whether this view is important for accessibility.
6494 *
6495 * @attr ref android.R.styleable#View_importantForAccessibility
6496 *
6497 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6498 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6499 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6500 */
6501 public void setImportantForAccessibility(int mode) {
6502 if (mode != getImportantForAccessibility()) {
6503 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6504 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6505 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6506 notifyAccessibilityStateChanged();
6507 }
6508 }
6509
6510 /**
6511 * Gets whether this view should be exposed for accessibility.
6512 *
6513 * @return Whether the view is exposed for accessibility.
6514 *
6515 * @hide
6516 */
6517 public boolean isImportantForAccessibility() {
6518 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6519 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6520 switch (mode) {
6521 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6522 return true;
6523 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6524 return false;
6525 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6526 return isActionableForAccessibility() || hasListenersForAccessibility();
6527 default:
6528 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6529 + mode);
6530 }
6531 }
6532
6533 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006534 * Gets the mode for determining whether this View can take accessibility focus.
6535 *
6536 * @return The mode for determining whether a View can take accessibility focus.
6537 *
6538 * @attr ref android.R.styleable#View_accessibilityFocusable
6539 *
6540 * @see #ACCESSIBILITY_FOCUSABLE_YES
6541 * @see #ACCESSIBILITY_FOCUSABLE_NO
6542 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6543 *
6544 * @hide
6545 */
6546 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6547 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6548 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6549 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6550 })
6551 public int getAccessibilityFocusable() {
6552 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6553 }
6554
6555 /**
6556 * Sets how to determine whether this view can take accessibility focus.
6557 *
6558 * @param mode How to determine whether this view can take accessibility focus.
6559 *
6560 * @attr ref android.R.styleable#View_accessibilityFocusable
6561 *
6562 * @see #ACCESSIBILITY_FOCUSABLE_YES
6563 * @see #ACCESSIBILITY_FOCUSABLE_NO
6564 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6565 *
6566 * @hide
6567 */
6568 public void setAccessibilityFocusable(int mode) {
6569 if (mode != getAccessibilityFocusable()) {
6570 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6571 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6572 & ACCESSIBILITY_FOCUSABLE_MASK;
6573 notifyAccessibilityStateChanged();
6574 }
6575 }
6576
6577 /**
6578 * Gets whether this view can take accessibility focus.
6579 *
6580 * @return Whether the view can take accessibility focus.
6581 *
6582 * @hide
6583 */
6584 public boolean isAccessibilityFocusable() {
6585 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6586 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6587 switch (mode) {
6588 case ACCESSIBILITY_FOCUSABLE_YES:
6589 return true;
6590 case ACCESSIBILITY_FOCUSABLE_NO:
6591 return false;
6592 case ACCESSIBILITY_FOCUSABLE_AUTO:
6593 return canTakeAccessibilityFocusFromHover()
6594 || getAccessibilityNodeProvider() != null;
6595 default:
6596 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6597 }
6598 }
6599
6600 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006601 * Gets the parent for accessibility purposes. Note that the parent for
6602 * accessibility is not necessary the immediate parent. It is the first
6603 * predecessor that is important for accessibility.
6604 *
6605 * @return The parent for accessibility purposes.
6606 */
6607 public ViewParent getParentForAccessibility() {
6608 if (mParent instanceof View) {
6609 View parentView = (View) mParent;
6610 if (parentView.includeForAccessibility()) {
6611 return mParent;
6612 } else {
6613 return mParent.getParentForAccessibility();
6614 }
6615 }
6616 return null;
6617 }
6618
6619 /**
6620 * Adds the children of a given View for accessibility. Since some Views are
6621 * not important for accessibility the children for accessibility are not
6622 * necessarily direct children of the riew, rather they are the first level of
6623 * descendants important for accessibility.
6624 *
6625 * @param children The list of children for accessibility.
6626 */
6627 public void addChildrenForAccessibility(ArrayList<View> children) {
6628 if (includeForAccessibility()) {
6629 children.add(this);
6630 }
6631 }
6632
6633 /**
6634 * Whether to regard this view for accessibility. A view is regarded for
6635 * accessibility if it is important for accessibility or the querying
6636 * accessibility service has explicitly requested that view not
6637 * important for accessibility are regarded.
6638 *
6639 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006640 *
6641 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006642 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006643 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006644 if (mAttachInfo != null) {
6645 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006646 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006647 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006648 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006649 }
6650 return false;
6651 }
6652
6653 /**
6654 * Returns whether the View is considered actionable from
6655 * accessibility perspective. Such view are important for
6656 * accessiiblity.
6657 *
6658 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006659 *
6660 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006661 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006662 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006663 return (isClickable() || isLongClickable() || isFocusable());
6664 }
6665
6666 /**
6667 * Returns whether the View has registered callbacks wich makes it
6668 * important for accessiiblity.
6669 *
6670 * @return True if the view is actionable for accessibility.
6671 */
6672 private boolean hasListenersForAccessibility() {
6673 ListenerInfo info = getListenerInfo();
6674 return mTouchDelegate != null || info.mOnKeyListener != null
6675 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6676 || info.mOnHoverListener != null || info.mOnDragListener != null;
6677 }
6678
6679 /**
6680 * Notifies accessibility services that some view's important for
6681 * accessibility state has changed. Note that such notifications
6682 * are made at most once every
6683 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6684 * to avoid unnecessary load to the system. Also once a view has
6685 * made a notifucation this method is a NOP until the notification has
6686 * been sent to clients.
6687 *
6688 * @hide
6689 *
6690 * TODO: Makse sure this method is called for any view state change
6691 * that is interesting for accessilility purposes.
6692 */
6693 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006694 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6695 return;
6696 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006697 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6698 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6699 if (mParent != null) {
6700 mParent.childAccessibilityStateChanged(this);
6701 }
6702 }
6703 }
6704
6705 /**
6706 * Reset the state indicating the this view has requested clients
6707 * interested in its accessiblity state to be notified.
6708 *
6709 * @hide
6710 */
6711 public void resetAccessibilityStateChanged() {
6712 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6713 }
6714
6715 /**
6716 * Performs the specified accessibility action on the view. For
6717 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006718 * <p>
6719 * If an {@link AccessibilityDelegate} has been specified via calling
6720 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6721 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6722 * is responsible for handling this call.
6723 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006724 *
6725 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006726 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006727 * @return Whether the action was performed.
6728 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006729 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006730 if (mAccessibilityDelegate != null) {
6731 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6732 } else {
6733 return performAccessibilityActionInternal(action, arguments);
6734 }
6735 }
6736
6737 /**
6738 * @see #performAccessibilityAction(int, Bundle)
6739 *
6740 * Note: Called from the default {@link AccessibilityDelegate}.
6741 */
6742 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006743 switch (action) {
6744 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006745 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006746 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006747 }
6748 } break;
6749 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6750 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006751 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006752 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006753 } break;
6754 case AccessibilityNodeInfo.ACTION_FOCUS: {
6755 if (!hasFocus()) {
6756 // Get out of touch mode since accessibility
6757 // wants to move focus around.
6758 getViewRootImpl().ensureTouchMode(false);
6759 return requestFocus();
6760 }
6761 } break;
6762 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6763 if (hasFocus()) {
6764 clearFocus();
6765 return !isFocused();
6766 }
6767 } break;
6768 case AccessibilityNodeInfo.ACTION_SELECT: {
6769 if (!isSelected()) {
6770 setSelected(true);
6771 return isSelected();
6772 }
6773 } break;
6774 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6775 if (isSelected()) {
6776 setSelected(false);
6777 return !isSelected();
6778 }
6779 } break;
6780 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006781 final int mode = getAccessibilityFocusable();
6782 if (!isAccessibilityFocused()
6783 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6784 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006785 return requestAccessibilityFocus();
6786 }
6787 } break;
6788 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6789 if (isAccessibilityFocused()) {
6790 clearAccessibilityFocus();
6791 return true;
6792 }
6793 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006794 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6795 if (arguments != null) {
6796 final int granularity = arguments.getInt(
6797 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6798 return nextAtGranularity(granularity);
6799 }
6800 } break;
6801 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6802 if (arguments != null) {
6803 final int granularity = arguments.getInt(
6804 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6805 return previousAtGranularity(granularity);
6806 }
6807 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006808 }
6809 return false;
6810 }
6811
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006812 private boolean nextAtGranularity(int granularity) {
6813 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006814 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006815 return false;
6816 }
6817 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6818 if (iterator == null) {
6819 return false;
6820 }
6821 final int current = getAccessibilityCursorPosition();
6822 final int[] range = iterator.following(current);
6823 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006824 return false;
6825 }
6826 final int start = range[0];
6827 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006828 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006829 sendViewTextTraversedAtGranularityEvent(
6830 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6831 granularity, start, end);
6832 return true;
6833 }
6834
6835 private boolean previousAtGranularity(int granularity) {
6836 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006837 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006838 return false;
6839 }
6840 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6841 if (iterator == null) {
6842 return false;
6843 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006844 int current = getAccessibilityCursorPosition();
6845 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6846 current = text.length();
6847 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6848 // When traversing by character we always put the cursor after the character
6849 // to ease edit and have to compensate before asking the for previous segment.
6850 current--;
6851 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006852 final int[] range = iterator.preceding(current);
6853 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006854 return false;
6855 }
6856 final int start = range[0];
6857 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006858 // Always put the cursor after the character to ease edit.
6859 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6860 setAccessibilityCursorPosition(end);
6861 } else {
6862 setAccessibilityCursorPosition(start);
6863 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006864 sendViewTextTraversedAtGranularityEvent(
6865 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6866 granularity, start, end);
6867 return true;
6868 }
6869
6870 /**
6871 * Gets the text reported for accessibility purposes.
6872 *
6873 * @return The accessibility text.
6874 *
6875 * @hide
6876 */
6877 public CharSequence getIterableTextForAccessibility() {
6878 return mContentDescription;
6879 }
6880
6881 /**
6882 * @hide
6883 */
6884 public int getAccessibilityCursorPosition() {
6885 return mAccessibilityCursorPosition;
6886 }
6887
6888 /**
6889 * @hide
6890 */
6891 public void setAccessibilityCursorPosition(int position) {
6892 mAccessibilityCursorPosition = position;
6893 }
6894
6895 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6896 int fromIndex, int toIndex) {
6897 if (mParent == null) {
6898 return;
6899 }
6900 AccessibilityEvent event = AccessibilityEvent.obtain(
6901 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6902 onInitializeAccessibilityEvent(event);
6903 onPopulateAccessibilityEvent(event);
6904 event.setFromIndex(fromIndex);
6905 event.setToIndex(toIndex);
6906 event.setAction(action);
6907 event.setMovementGranularity(granularity);
6908 mParent.requestSendAccessibilityEvent(this, event);
6909 }
6910
6911 /**
6912 * @hide
6913 */
6914 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6915 switch (granularity) {
6916 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6917 CharSequence text = getIterableTextForAccessibility();
6918 if (text != null && text.length() > 0) {
6919 CharacterTextSegmentIterator iterator =
6920 CharacterTextSegmentIterator.getInstance(mContext);
6921 iterator.initialize(text.toString());
6922 return iterator;
6923 }
6924 } break;
6925 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6926 CharSequence text = getIterableTextForAccessibility();
6927 if (text != null && text.length() > 0) {
6928 WordTextSegmentIterator iterator =
6929 WordTextSegmentIterator.getInstance(mContext);
6930 iterator.initialize(text.toString());
6931 return iterator;
6932 }
6933 } break;
6934 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6935 CharSequence text = getIterableTextForAccessibility();
6936 if (text != null && text.length() > 0) {
6937 ParagraphTextSegmentIterator iterator =
6938 ParagraphTextSegmentIterator.getInstance();
6939 iterator.initialize(text.toString());
6940 return iterator;
6941 }
6942 } break;
6943 }
6944 return null;
6945 }
6946
Svetoslav Ganov42138042012-03-20 11:51:39 -07006947 /**
Romain Guya440b002010-02-24 15:57:54 -08006948 * @hide
6949 */
6950 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006951 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006952 clearDisplayList();
6953
Romain Guya440b002010-02-24 15:57:54 -08006954 onStartTemporaryDetach();
6955 }
6956
6957 /**
6958 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6960 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006961 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 */
6963 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006964 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006965 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006966 }
6967
6968 /**
6969 * @hide
6970 */
6971 public void dispatchFinishTemporaryDetach() {
6972 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 }
Romain Guy8506ab42009-06-11 17:35:47 -07006974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 /**
6976 * Called after {@link #onStartTemporaryDetach} when the container is done
6977 * changing the view.
6978 */
6979 public void onFinishTemporaryDetach() {
6980 }
Romain Guy8506ab42009-06-11 17:35:47 -07006981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006983 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6984 * for this view's window. Returns null if the view is not currently attached
6985 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006986 * just use the standard high-level event callbacks like
6987 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006988 */
6989 public KeyEvent.DispatcherState getKeyDispatcherState() {
6990 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6991 }
Joe Malin32736f02011-01-19 16:14:20 -08006992
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 * Dispatch a key event before it is processed by any input method
6995 * associated with the view hierarchy. This can be used to intercept
6996 * key events in special situations before the IME consumes them; a
6997 * typical example would be handling the BACK key to update the application's
6998 * UI instead of allowing the IME to see it and close itself.
6999 *
7000 * @param event The key event to be dispatched.
7001 * @return True if the event was handled, false otherwise.
7002 */
7003 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7004 return onKeyPreIme(event.getKeyCode(), event);
7005 }
7006
7007 /**
7008 * Dispatch a key event to the next view on the focus path. This path runs
7009 * from the top of the view tree down to the currently focused view. If this
7010 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7011 * the next node down the focus path. This method also fires any key
7012 * listeners.
7013 *
7014 * @param event The key event to be dispatched.
7015 * @return True if the event was handled, false otherwise.
7016 */
7017 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007018 if (mInputEventConsistencyVerifier != null) {
7019 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007021
Jeff Brown21bc5c92011-02-28 18:27:14 -08007022 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007023 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007024 ListenerInfo li = mListenerInfo;
7025 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7026 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 return true;
7028 }
7029
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007030 if (event.dispatch(this, mAttachInfo != null
7031 ? mAttachInfo.mKeyDispatchState : null, this)) {
7032 return true;
7033 }
7034
7035 if (mInputEventConsistencyVerifier != null) {
7036 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7037 }
7038 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 }
7040
7041 /**
7042 * Dispatches a key shortcut event.
7043 *
7044 * @param event The key event to be dispatched.
7045 * @return True if the event was handled by the view, false otherwise.
7046 */
7047 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7048 return onKeyShortcut(event.getKeyCode(), event);
7049 }
7050
7051 /**
7052 * Pass the touch screen motion event down to the target view, or this
7053 * view if it is the target.
7054 *
7055 * @param event The motion event to be dispatched.
7056 * @return True if the event was handled by the view, false otherwise.
7057 */
7058 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007059 if (mInputEventConsistencyVerifier != null) {
7060 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7061 }
7062
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007063 if (onFilterTouchEventForSecurity(event)) {
7064 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007065 ListenerInfo li = mListenerInfo;
7066 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7067 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007068 return true;
7069 }
7070
7071 if (onTouchEvent(event)) {
7072 return true;
7073 }
Jeff Brown85a31762010-09-01 17:01:00 -07007074 }
7075
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007076 if (mInputEventConsistencyVerifier != null) {
7077 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007079 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 }
7081
7082 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007083 * Filter the touch event to apply security policies.
7084 *
7085 * @param event The motion event to be filtered.
7086 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007087 *
Jeff Brown85a31762010-09-01 17:01:00 -07007088 * @see #getFilterTouchesWhenObscured
7089 */
7090 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007091 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007092 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7093 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7094 // Window is obscured, drop this touch.
7095 return false;
7096 }
7097 return true;
7098 }
7099
7100 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 * Pass a trackball motion event down to the focused view.
7102 *
7103 * @param event The motion event to be dispatched.
7104 * @return True if the event was handled by the view, false otherwise.
7105 */
7106 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007107 if (mInputEventConsistencyVerifier != null) {
7108 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7109 }
7110
Romain Guy02ccac62011-06-24 13:20:23 -07007111 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 }
7113
7114 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007115 * Dispatch a generic motion event.
7116 * <p>
7117 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7118 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007119 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007120 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007121 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007122 *
7123 * @param event The motion event to be dispatched.
7124 * @return True if the event was handled by the view, false otherwise.
7125 */
7126 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007127 if (mInputEventConsistencyVerifier != null) {
7128 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7129 }
7130
Jeff Browna032cc02011-03-07 16:56:21 -08007131 final int source = event.getSource();
7132 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7133 final int action = event.getAction();
7134 if (action == MotionEvent.ACTION_HOVER_ENTER
7135 || action == MotionEvent.ACTION_HOVER_MOVE
7136 || action == MotionEvent.ACTION_HOVER_EXIT) {
7137 if (dispatchHoverEvent(event)) {
7138 return true;
7139 }
7140 } else if (dispatchGenericPointerEvent(event)) {
7141 return true;
7142 }
7143 } else if (dispatchGenericFocusedEvent(event)) {
7144 return true;
7145 }
7146
Jeff Brown10b62902011-06-20 16:40:37 -07007147 if (dispatchGenericMotionEventInternal(event)) {
7148 return true;
7149 }
7150
7151 if (mInputEventConsistencyVerifier != null) {
7152 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7153 }
7154 return false;
7155 }
7156
7157 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007158 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007159 ListenerInfo li = mListenerInfo;
7160 if (li != null && li.mOnGenericMotionListener != null
7161 && (mViewFlags & ENABLED_MASK) == ENABLED
7162 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007163 return true;
7164 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007165
7166 if (onGenericMotionEvent(event)) {
7167 return true;
7168 }
7169
7170 if (mInputEventConsistencyVerifier != null) {
7171 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7172 }
7173 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007174 }
7175
7176 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007177 * Dispatch a hover event.
7178 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007179 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007180 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007181 * </p>
7182 *
7183 * @param event The motion event to be dispatched.
7184 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007185 */
7186 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007187 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007188 ListenerInfo li = mListenerInfo;
7189 if (li != null && li.mOnHoverListener != null
7190 && (mViewFlags & ENABLED_MASK) == ENABLED
7191 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007192 return true;
7193 }
7194
Jeff Browna032cc02011-03-07 16:56:21 -08007195 return onHoverEvent(event);
7196 }
7197
7198 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007199 * Returns true if the view has a child to which it has recently sent
7200 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7201 * it does not have a hovered child, then it must be the innermost hovered view.
7202 * @hide
7203 */
7204 protected boolean hasHoveredChild() {
7205 return false;
7206 }
7207
7208 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007209 * Dispatch a generic motion event to the view under the first pointer.
7210 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007211 * Do not call this method directly.
7212 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007213 * </p>
7214 *
7215 * @param event The motion event to be dispatched.
7216 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007217 */
7218 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7219 return false;
7220 }
7221
7222 /**
7223 * Dispatch a generic motion event to the currently focused view.
7224 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007225 * Do not call this method directly.
7226 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007227 * </p>
7228 *
7229 * @param event The motion event to be dispatched.
7230 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007231 */
7232 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7233 return false;
7234 }
7235
7236 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007237 * Dispatch a pointer event.
7238 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007239 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7240 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7241 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007242 * and should not be expected to handle other pointing device features.
7243 * </p>
7244 *
7245 * @param event The motion event to be dispatched.
7246 * @return True if the event was handled by the view, false otherwise.
7247 * @hide
7248 */
7249 public final boolean dispatchPointerEvent(MotionEvent event) {
7250 if (event.isTouchEvent()) {
7251 return dispatchTouchEvent(event);
7252 } else {
7253 return dispatchGenericMotionEvent(event);
7254 }
7255 }
7256
7257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 * Called when the window containing this view gains or loses window focus.
7259 * ViewGroups should override to route to their children.
7260 *
7261 * @param hasFocus True if the window containing this view now has focus,
7262 * false otherwise.
7263 */
7264 public void dispatchWindowFocusChanged(boolean hasFocus) {
7265 onWindowFocusChanged(hasFocus);
7266 }
7267
7268 /**
7269 * Called when the window containing this view gains or loses focus. Note
7270 * that this is separate from view focus: to receive key events, both
7271 * your view and its window must have focus. If a window is displayed
7272 * on top of yours that takes input focus, then your own window will lose
7273 * focus but the view focus will remain unchanged.
7274 *
7275 * @param hasWindowFocus True if the window containing this view now has
7276 * focus, false otherwise.
7277 */
7278 public void onWindowFocusChanged(boolean hasWindowFocus) {
7279 InputMethodManager imm = InputMethodManager.peekInstance();
7280 if (!hasWindowFocus) {
7281 if (isPressed()) {
7282 setPressed(false);
7283 }
7284 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7285 imm.focusOut(this);
7286 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007287 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007288 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007289 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7291 imm.focusIn(this);
7292 }
7293 refreshDrawableState();
7294 }
7295
7296 /**
7297 * Returns true if this view is in a window that currently has window focus.
7298 * Note that this is not the same as the view itself having focus.
7299 *
7300 * @return True if this view is in a window that currently has window focus.
7301 */
7302 public boolean hasWindowFocus() {
7303 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7304 }
7305
7306 /**
Adam Powell326d8082009-12-09 15:10:07 -08007307 * Dispatch a view visibility change down the view hierarchy.
7308 * ViewGroups should override to route to their children.
7309 * @param changedView The view whose visibility changed. Could be 'this' or
7310 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007311 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7312 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007313 */
7314 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7315 onVisibilityChanged(changedView, visibility);
7316 }
7317
7318 /**
7319 * Called when the visibility of the view or an ancestor of the view is changed.
7320 * @param changedView The view whose visibility changed. Could be 'this' or
7321 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007322 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7323 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007324 */
7325 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007326 if (visibility == VISIBLE) {
7327 if (mAttachInfo != null) {
7328 initialAwakenScrollBars();
7329 } else {
7330 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7331 }
7332 }
Adam Powell326d8082009-12-09 15:10:07 -08007333 }
7334
7335 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007336 * Dispatch a hint about whether this view is displayed. For instance, when
7337 * a View moves out of the screen, it might receives a display hint indicating
7338 * the view is not displayed. Applications should not <em>rely</em> on this hint
7339 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007340 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007341 * @param hint A hint about whether or not this view is displayed:
7342 * {@link #VISIBLE} or {@link #INVISIBLE}.
7343 */
7344 public void dispatchDisplayHint(int hint) {
7345 onDisplayHint(hint);
7346 }
7347
7348 /**
7349 * Gives this view a hint about whether is displayed or not. For instance, when
7350 * a View moves out of the screen, it might receives a display hint indicating
7351 * the view is not displayed. Applications should not <em>rely</em> on this hint
7352 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007353 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007354 * @param hint A hint about whether or not this view is displayed:
7355 * {@link #VISIBLE} or {@link #INVISIBLE}.
7356 */
7357 protected void onDisplayHint(int hint) {
7358 }
7359
7360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 * Dispatch a window visibility change down the view hierarchy.
7362 * ViewGroups should override to route to their children.
7363 *
7364 * @param visibility The new visibility of the window.
7365 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007366 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 */
7368 public void dispatchWindowVisibilityChanged(int visibility) {
7369 onWindowVisibilityChanged(visibility);
7370 }
7371
7372 /**
7373 * Called when the window containing has change its visibility
7374 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7375 * that this tells you whether or not your window is being made visible
7376 * to the window manager; this does <em>not</em> tell you whether or not
7377 * your window is obscured by other windows on the screen, even if it
7378 * is itself visible.
7379 *
7380 * @param visibility The new visibility of the window.
7381 */
7382 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007383 if (visibility == VISIBLE) {
7384 initialAwakenScrollBars();
7385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 }
7387
7388 /**
7389 * Returns the current visibility of the window this view is attached to
7390 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7391 *
7392 * @return Returns the current visibility of the view's window.
7393 */
7394 public int getWindowVisibility() {
7395 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7396 }
7397
7398 /**
7399 * Retrieve the overall visible display size in which the window this view is
7400 * attached to has been positioned in. This takes into account screen
7401 * decorations above the window, for both cases where the window itself
7402 * is being position inside of them or the window is being placed under
7403 * then and covered insets are used for the window to position its content
7404 * inside. In effect, this tells you the available area where content can
7405 * be placed and remain visible to users.
7406 *
7407 * <p>This function requires an IPC back to the window manager to retrieve
7408 * the requested information, so should not be used in performance critical
7409 * code like drawing.
7410 *
7411 * @param outRect Filled in with the visible display frame. If the view
7412 * is not attached to a window, this is simply the raw display size.
7413 */
7414 public void getWindowVisibleDisplayFrame(Rect outRect) {
7415 if (mAttachInfo != null) {
7416 try {
7417 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7418 } catch (RemoteException e) {
7419 return;
7420 }
7421 // XXX This is really broken, and probably all needs to be done
7422 // in the window manager, and we need to know more about whether
7423 // we want the area behind or in front of the IME.
7424 final Rect insets = mAttachInfo.mVisibleInsets;
7425 outRect.left += insets.left;
7426 outRect.top += insets.top;
7427 outRect.right -= insets.right;
7428 outRect.bottom -= insets.bottom;
7429 return;
7430 }
7431 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007432 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 }
7434
7435 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007436 * Dispatch a notification about a resource configuration change down
7437 * the view hierarchy.
7438 * ViewGroups should override to route to their children.
7439 *
7440 * @param newConfig The new resource configuration.
7441 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007442 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007443 */
7444 public void dispatchConfigurationChanged(Configuration newConfig) {
7445 onConfigurationChanged(newConfig);
7446 }
7447
7448 /**
7449 * Called when the current configuration of the resources being used
7450 * by the application have changed. You can use this to decide when
7451 * to reload resources that can changed based on orientation and other
7452 * configuration characterstics. You only need to use this if you are
7453 * not relying on the normal {@link android.app.Activity} mechanism of
7454 * recreating the activity instance upon a configuration change.
7455 *
7456 * @param newConfig The new resource configuration.
7457 */
7458 protected void onConfigurationChanged(Configuration newConfig) {
7459 }
7460
7461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 * Private function to aggregate all per-view attributes in to the view
7463 * root.
7464 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007465 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7466 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007467 }
7468
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007469 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7470 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007471 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007472 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007473 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007474 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007475 ListenerInfo li = mListenerInfo;
7476 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007477 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 }
7480 }
7481
7482 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007483 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007485 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7486 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 ai.mRecomputeGlobalAttributes = true;
7488 }
7489 }
7490 }
7491
7492 /**
7493 * Returns whether the device is currently in touch mode. Touch mode is entered
7494 * once the user begins interacting with the device by touch, and affects various
7495 * things like whether focus is always visible to the user.
7496 *
7497 * @return Whether the device is in touch mode.
7498 */
7499 @ViewDebug.ExportedProperty
7500 public boolean isInTouchMode() {
7501 if (mAttachInfo != null) {
7502 return mAttachInfo.mInTouchMode;
7503 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007504 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007505 }
7506 }
7507
7508 /**
7509 * Returns the context the view is running in, through which it can
7510 * access the current theme, resources, etc.
7511 *
7512 * @return The view's Context.
7513 */
7514 @ViewDebug.CapturedViewProperty
7515 public final Context getContext() {
7516 return mContext;
7517 }
7518
7519 /**
7520 * Handle a key event before it is processed by any input method
7521 * associated with the view hierarchy. This can be used to intercept
7522 * key events in special situations before the IME consumes them; a
7523 * typical example would be handling the BACK key to update the application's
7524 * UI instead of allowing the IME to see it and close itself.
7525 *
7526 * @param keyCode The value in event.getKeyCode().
7527 * @param event Description of the key event.
7528 * @return If you handled the event, return true. If you want to allow the
7529 * event to be handled by the next receiver, return false.
7530 */
7531 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7532 return false;
7533 }
7534
7535 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007536 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7537 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7539 * is released, if the view is enabled and clickable.
7540 *
Jean Chalard405bc512012-05-29 19:12:34 +09007541 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7542 * although some may elect to do so in some situations. Do not rely on this to
7543 * catch software key presses.
7544 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 * @param keyCode A key code that represents the button pressed, from
7546 * {@link android.view.KeyEvent}.
7547 * @param event The KeyEvent object that defines the button action.
7548 */
7549 public boolean onKeyDown(int keyCode, KeyEvent event) {
7550 boolean result = false;
7551
7552 switch (keyCode) {
7553 case KeyEvent.KEYCODE_DPAD_CENTER:
7554 case KeyEvent.KEYCODE_ENTER: {
7555 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7556 return true;
7557 }
7558 // Long clickable items don't necessarily have to be clickable
7559 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7560 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7561 (event.getRepeatCount() == 0)) {
7562 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007563 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 return true;
7565 }
7566 break;
7567 }
7568 }
7569 return result;
7570 }
7571
7572 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007573 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7574 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7575 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007576 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7577 * although some may elect to do so in some situations. Do not rely on this to
7578 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007579 */
7580 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7581 return false;
7582 }
7583
7584 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007585 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7586 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7588 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007589 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7590 * although some may elect to do so in some situations. Do not rely on this to
7591 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 *
7593 * @param keyCode A key code that represents the button pressed, from
7594 * {@link android.view.KeyEvent}.
7595 * @param event The KeyEvent object that defines the button action.
7596 */
7597 public boolean onKeyUp(int keyCode, KeyEvent event) {
7598 boolean result = false;
7599
7600 switch (keyCode) {
7601 case KeyEvent.KEYCODE_DPAD_CENTER:
7602 case KeyEvent.KEYCODE_ENTER: {
7603 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7604 return true;
7605 }
7606 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7607 setPressed(false);
7608
7609 if (!mHasPerformedLongPress) {
7610 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007611 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007612
7613 result = performClick();
7614 }
7615 }
7616 break;
7617 }
7618 }
7619 return result;
7620 }
7621
7622 /**
7623 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7624 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7625 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007626 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7627 * although some may elect to do so in some situations. Do not rely on this to
7628 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 *
7630 * @param keyCode A key code that represents the button pressed, from
7631 * {@link android.view.KeyEvent}.
7632 * @param repeatCount The number of times the action was made.
7633 * @param event The KeyEvent object that defines the button action.
7634 */
7635 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7636 return false;
7637 }
7638
7639 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007640 * Called on the focused view when a key shortcut event is not handled.
7641 * Override this method to implement local key shortcuts for the View.
7642 * Key shortcuts can also be implemented by setting the
7643 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644 *
7645 * @param keyCode The value in event.getKeyCode().
7646 * @param event Description of the key event.
7647 * @return If you handled the event, return true. If you want to allow the
7648 * event to be handled by the next receiver, return false.
7649 */
7650 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7651 return false;
7652 }
7653
7654 /**
7655 * Check whether the called view is a text editor, in which case it
7656 * would make sense to automatically display a soft input window for
7657 * it. Subclasses should override this if they implement
7658 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007659 * a call on that method would return a non-null InputConnection, and
7660 * they are really a first-class editor that the user would normally
7661 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007662 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007663 * <p>The default implementation always returns false. This does
7664 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7665 * will not be called or the user can not otherwise perform edits on your
7666 * view; it is just a hint to the system that this is not the primary
7667 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007668 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 * @return Returns true if this view is a text editor, else false.
7670 */
7671 public boolean onCheckIsTextEditor() {
7672 return false;
7673 }
Romain Guy8506ab42009-06-11 17:35:47 -07007674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007675 /**
7676 * Create a new InputConnection for an InputMethod to interact
7677 * with the view. The default implementation returns null, since it doesn't
7678 * support input methods. You can override this to implement such support.
7679 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007680 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 * <p>When implementing this, you probably also want to implement
7682 * {@link #onCheckIsTextEditor()} to indicate you will return a
7683 * non-null InputConnection.
7684 *
7685 * @param outAttrs Fill in with attribute information about the connection.
7686 */
7687 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7688 return null;
7689 }
7690
7691 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007692 * Called by the {@link android.view.inputmethod.InputMethodManager}
7693 * when a view who is not the current
7694 * input connection target is trying to make a call on the manager. The
7695 * default implementation returns false; you can override this to return
7696 * true for certain views if you are performing InputConnection proxying
7697 * to them.
7698 * @param view The View that is making the InputMethodManager call.
7699 * @return Return true to allow the call, false to reject.
7700 */
7701 public boolean checkInputConnectionProxy(View view) {
7702 return false;
7703 }
Romain Guy8506ab42009-06-11 17:35:47 -07007704
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 * Show the context menu for this view. It is not safe to hold on to the
7707 * menu after returning from this method.
7708 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007709 * You should normally not overload this method. Overload
7710 * {@link #onCreateContextMenu(ContextMenu)} or define an
7711 * {@link OnCreateContextMenuListener} to add items to the context menu.
7712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 * @param menu The context menu to populate
7714 */
7715 public void createContextMenu(ContextMenu menu) {
7716 ContextMenuInfo menuInfo = getContextMenuInfo();
7717
7718 // Sets the current menu info so all items added to menu will have
7719 // my extra info set.
7720 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7721
7722 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007723 ListenerInfo li = mListenerInfo;
7724 if (li != null && li.mOnCreateContextMenuListener != null) {
7725 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 }
7727
7728 // Clear the extra information so subsequent items that aren't mine don't
7729 // have my extra info.
7730 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7731
7732 if (mParent != null) {
7733 mParent.createContextMenu(menu);
7734 }
7735 }
7736
7737 /**
7738 * Views should implement this if they have extra information to associate
7739 * with the context menu. The return result is supplied as a parameter to
7740 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7741 * callback.
7742 *
7743 * @return Extra information about the item for which the context menu
7744 * should be shown. This information will vary across different
7745 * subclasses of View.
7746 */
7747 protected ContextMenuInfo getContextMenuInfo() {
7748 return null;
7749 }
7750
7751 /**
7752 * Views should implement this if the view itself is going to add items to
7753 * the context menu.
7754 *
7755 * @param menu the context menu to populate
7756 */
7757 protected void onCreateContextMenu(ContextMenu menu) {
7758 }
7759
7760 /**
7761 * Implement this method to handle trackball motion events. The
7762 * <em>relative</em> movement of the trackball since the last event
7763 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7764 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7765 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7766 * they will often be fractional values, representing the more fine-grained
7767 * movement information available from a trackball).
7768 *
7769 * @param event The motion event.
7770 * @return True if the event was handled, false otherwise.
7771 */
7772 public boolean onTrackballEvent(MotionEvent event) {
7773 return false;
7774 }
7775
7776 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007777 * Implement this method to handle generic motion events.
7778 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007779 * Generic motion events describe joystick movements, mouse hovers, track pad
7780 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007781 * {@link MotionEvent#getSource() source} of the motion event specifies
7782 * the class of input that was received. Implementations of this method
7783 * must examine the bits in the source before processing the event.
7784 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007785 * </p><p>
7786 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7787 * are delivered to the view under the pointer. All other generic motion events are
7788 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007789 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007790 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007791 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007792 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7793 * // process the joystick movement...
7794 * return true;
7795 * }
7796 * }
7797 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7798 * switch (event.getAction()) {
7799 * case MotionEvent.ACTION_HOVER_MOVE:
7800 * // process the mouse hover movement...
7801 * return true;
7802 * case MotionEvent.ACTION_SCROLL:
7803 * // process the scroll wheel movement...
7804 * return true;
7805 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007806 * }
7807 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007808 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007809 *
7810 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007811 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007812 */
7813 public boolean onGenericMotionEvent(MotionEvent event) {
7814 return false;
7815 }
7816
7817 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007818 * Implement this method to handle hover events.
7819 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007820 * This method is called whenever a pointer is hovering into, over, or out of the
7821 * bounds of a view and the view is not currently being touched.
7822 * Hover events are represented as pointer events with action
7823 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7824 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7825 * </p>
7826 * <ul>
7827 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7828 * when the pointer enters the bounds of the view.</li>
7829 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7830 * when the pointer has already entered the bounds of the view and has moved.</li>
7831 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7832 * when the pointer has exited the bounds of the view or when the pointer is
7833 * about to go down due to a button click, tap, or similar user action that
7834 * causes the view to be touched.</li>
7835 * </ul>
7836 * <p>
7837 * The view should implement this method to return true to indicate that it is
7838 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007839 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007840 * The default implementation calls {@link #setHovered} to update the hovered state
7841 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007842 * is enabled and is clickable. The default implementation also sends hover
7843 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007844 * </p>
7845 *
7846 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007847 * @return True if the view handled the hover event.
7848 *
7849 * @see #isHovered
7850 * @see #setHovered
7851 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007852 */
7853 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007854 // The root view may receive hover (or touch) events that are outside the bounds of
7855 // the window. This code ensures that we only send accessibility events for
7856 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007857 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007858 if (!mSendingHoverAccessibilityEvents) {
7859 if ((action == MotionEvent.ACTION_HOVER_ENTER
7860 || action == MotionEvent.ACTION_HOVER_MOVE)
7861 && !hasHoveredChild()
7862 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007863 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007864 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007865 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007866 }
7867 } else {
7868 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007869 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007870 && !pointInView(event.getX(), event.getY()))) {
7871 mSendingHoverAccessibilityEvents = false;
7872 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007873 // If the window does not have input focus we take away accessibility
7874 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007875 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007876 getViewRootImpl().setAccessibilityFocusedHost(null);
7877 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007878 }
Jeff Browna1b24182011-07-28 13:38:24 -07007879 }
7880
Jeff Brown87b7f802011-06-21 18:35:45 -07007881 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007882 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007883 case MotionEvent.ACTION_HOVER_ENTER:
7884 setHovered(true);
7885 break;
7886 case MotionEvent.ACTION_HOVER_EXIT:
7887 setHovered(false);
7888 break;
7889 }
Jeff Browna1b24182011-07-28 13:38:24 -07007890
7891 // Dispatch the event to onGenericMotionEvent before returning true.
7892 // This is to provide compatibility with existing applications that
7893 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7894 // break because of the new default handling for hoverable views
7895 // in onHoverEvent.
7896 // Note that onGenericMotionEvent will be called by default when
7897 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7898 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007899 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007900 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007901
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007902 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007903 }
7904
7905 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007906 * Returns true if the view should handle {@link #onHoverEvent}
7907 * by calling {@link #setHovered} to change its hovered state.
7908 *
7909 * @return True if the view is hoverable.
7910 */
7911 private boolean isHoverable() {
7912 final int viewFlags = mViewFlags;
7913 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7914 return false;
7915 }
7916
7917 return (viewFlags & CLICKABLE) == CLICKABLE
7918 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7919 }
7920
7921 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007922 * Returns true if the view is currently hovered.
7923 *
7924 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007925 *
7926 * @see #setHovered
7927 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007928 */
Jeff Brown10b62902011-06-20 16:40:37 -07007929 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007930 public boolean isHovered() {
7931 return (mPrivateFlags & HOVERED) != 0;
7932 }
7933
7934 /**
7935 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007936 * <p>
7937 * Calling this method also changes the drawable state of the view. This
7938 * enables the view to react to hover by using different drawable resources
7939 * to change its appearance.
7940 * </p><p>
7941 * The {@link #onHoverChanged} method is called when the hovered state changes.
7942 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007943 *
7944 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007945 *
7946 * @see #isHovered
7947 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007948 */
7949 public void setHovered(boolean hovered) {
7950 if (hovered) {
7951 if ((mPrivateFlags & HOVERED) == 0) {
7952 mPrivateFlags |= HOVERED;
7953 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007954 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007955 }
7956 } else {
7957 if ((mPrivateFlags & HOVERED) != 0) {
7958 mPrivateFlags &= ~HOVERED;
7959 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007960 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007961 }
7962 }
7963 }
7964
7965 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007966 * Implement this method to handle hover state changes.
7967 * <p>
7968 * This method is called whenever the hover state changes as a result of a
7969 * call to {@link #setHovered}.
7970 * </p>
7971 *
7972 * @param hovered The current hover state, as returned by {@link #isHovered}.
7973 *
7974 * @see #isHovered
7975 * @see #setHovered
7976 */
7977 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007978 }
7979
7980 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 * Implement this method to handle touch screen motion events.
7982 *
7983 * @param event The motion event.
7984 * @return True if the event was handled, false otherwise.
7985 */
7986 public boolean onTouchEvent(MotionEvent event) {
7987 final int viewFlags = mViewFlags;
7988
7989 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007990 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007991 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007993 // A disabled view that is clickable still consumes the touch
7994 // events, it just doesn't respond to them.
7995 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7996 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7997 }
7998
7999 if (mTouchDelegate != null) {
8000 if (mTouchDelegate.onTouchEvent(event)) {
8001 return true;
8002 }
8003 }
8004
8005 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8006 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8007 switch (event.getAction()) {
8008 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008009 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8010 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 // take focus if we don't have it already and we should in
8012 // touch mode.
8013 boolean focusTaken = false;
8014 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8015 focusTaken = requestFocus();
8016 }
8017
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008018 if (prepressed) {
8019 // The button is being released before we actually
8020 // showed it as pressed. Make it show the pressed
8021 // state now (before scheduling the click) to ensure
8022 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008023 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008024 }
Joe Malin32736f02011-01-19 16:14:20 -08008025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 if (!mHasPerformedLongPress) {
8027 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008028 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029
8030 // Only perform take click actions if we were in the pressed state
8031 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008032 // Use a Runnable and post this rather than calling
8033 // performClick directly. This lets other visual state
8034 // of the view update before click actions start.
8035 if (mPerformClick == null) {
8036 mPerformClick = new PerformClick();
8037 }
8038 if (!post(mPerformClick)) {
8039 performClick();
8040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 }
8042 }
8043
8044 if (mUnsetPressedState == null) {
8045 mUnsetPressedState = new UnsetPressedState();
8046 }
8047
Adam Powelle14579b2009-12-16 18:39:52 -08008048 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008049 postDelayed(mUnsetPressedState,
8050 ViewConfiguration.getPressedStateDuration());
8051 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 // If the post failed, unpress right now
8053 mUnsetPressedState.run();
8054 }
Adam Powelle14579b2009-12-16 18:39:52 -08008055 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008056 }
8057 break;
8058
8059 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008060 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008061
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008062 if (performButtonActionOnTouchDown(event)) {
8063 break;
8064 }
8065
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008066 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008067 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008068
8069 // For views inside a scrolling container, delay the pressed feedback for
8070 // a short period in case this is a scroll.
8071 if (isInScrollingContainer) {
8072 mPrivateFlags |= PREPRESSED;
8073 if (mPendingCheckForTap == null) {
8074 mPendingCheckForTap = new CheckForTap();
8075 }
8076 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8077 } else {
8078 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008079 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008080 checkForLongClick(0);
8081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 break;
8083
8084 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008085 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008086 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 break;
8088
8089 case MotionEvent.ACTION_MOVE:
8090 final int x = (int) event.getX();
8091 final int y = (int) event.getY();
8092
8093 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008094 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008096 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008098 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008099 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100
Adam Powell4d6f0662012-02-21 15:11:11 -08008101 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 }
8104 break;
8105 }
8106 return true;
8107 }
8108
8109 return false;
8110 }
8111
8112 /**
Adam Powell10298662011-08-14 18:26:30 -07008113 * @hide
8114 */
8115 public boolean isInScrollingContainer() {
8116 ViewParent p = getParent();
8117 while (p != null && p instanceof ViewGroup) {
8118 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8119 return true;
8120 }
8121 p = p.getParent();
8122 }
8123 return false;
8124 }
8125
8126 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008127 * Remove the longpress detection timer.
8128 */
8129 private void removeLongPressCallback() {
8130 if (mPendingCheckForLongPress != null) {
8131 removeCallbacks(mPendingCheckForLongPress);
8132 }
8133 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008134
8135 /**
8136 * Remove the pending click action
8137 */
8138 private void removePerformClickCallback() {
8139 if (mPerformClick != null) {
8140 removeCallbacks(mPerformClick);
8141 }
8142 }
8143
Adam Powelle14579b2009-12-16 18:39:52 -08008144 /**
Romain Guya440b002010-02-24 15:57:54 -08008145 * Remove the prepress detection timer.
8146 */
8147 private void removeUnsetPressCallback() {
8148 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8149 setPressed(false);
8150 removeCallbacks(mUnsetPressedState);
8151 }
8152 }
8153
8154 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008155 * Remove the tap detection timer.
8156 */
8157 private void removeTapCallback() {
8158 if (mPendingCheckForTap != null) {
8159 mPrivateFlags &= ~PREPRESSED;
8160 removeCallbacks(mPendingCheckForTap);
8161 }
8162 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008163
8164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 * Cancels a pending long press. Your subclass can use this if you
8166 * want the context menu to come up if the user presses and holds
8167 * at the same place, but you don't want it to come up if they press
8168 * and then move around enough to cause scrolling.
8169 */
8170 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008171 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008172
8173 /*
8174 * The prepressed state handled by the tap callback is a display
8175 * construct, but the tap callback will post a long press callback
8176 * less its own timeout. Remove it here.
8177 */
8178 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008182 * Remove the pending callback for sending a
8183 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8184 */
8185 private void removeSendViewScrolledAccessibilityEventCallback() {
8186 if (mSendViewScrolledAccessibilityEvent != null) {
8187 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008188 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008189 }
8190 }
8191
8192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008193 * Sets the TouchDelegate for this View.
8194 */
8195 public void setTouchDelegate(TouchDelegate delegate) {
8196 mTouchDelegate = delegate;
8197 }
8198
8199 /**
8200 * Gets the TouchDelegate for this View.
8201 */
8202 public TouchDelegate getTouchDelegate() {
8203 return mTouchDelegate;
8204 }
8205
8206 /**
8207 * Set flags controlling behavior of this view.
8208 *
8209 * @param flags Constant indicating the value which should be set
8210 * @param mask Constant indicating the bit range that should be changed
8211 */
8212 void setFlags(int flags, int mask) {
8213 int old = mViewFlags;
8214 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8215
8216 int changed = mViewFlags ^ old;
8217 if (changed == 0) {
8218 return;
8219 }
8220 int privateFlags = mPrivateFlags;
8221
8222 /* Check if the FOCUSABLE bit has changed */
8223 if (((changed & FOCUSABLE_MASK) != 0) &&
8224 ((privateFlags & HAS_BOUNDS) !=0)) {
8225 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8226 && ((privateFlags & FOCUSED) != 0)) {
8227 /* Give up focus if we are no longer focusable */
8228 clearFocus();
8229 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8230 && ((privateFlags & FOCUSED) == 0)) {
8231 /*
8232 * Tell the view system that we are now available to take focus
8233 * if no one else already has it.
8234 */
8235 if (mParent != null) mParent.focusableViewAvailable(this);
8236 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008237 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8238 notifyAccessibilityStateChanged();
8239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 }
8241
8242 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8243 if ((changed & VISIBILITY_MASK) != 0) {
8244 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008245 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008246 * it was not visible. Marking it drawn ensures that the invalidation will
8247 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008249 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008250 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251
8252 needGlobalAttributesUpdate(true);
8253
8254 // a view becoming visible is worth notifying the parent
8255 // about in case nothing has focus. even if this specific view
8256 // isn't focusable, it may contain something that is, so let
8257 // the root view try to give this focus if nothing else does.
8258 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8259 mParent.focusableViewAvailable(this);
8260 }
8261 }
8262 }
8263
8264 /* Check if the GONE bit has changed */
8265 if ((changed & GONE) != 0) {
8266 needGlobalAttributesUpdate(false);
8267 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268
Romain Guyecd80ee2009-12-03 17:13:02 -08008269 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8270 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008271 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008272 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008273 if (mParent instanceof View) {
8274 // GONE views noop invalidation, so invalidate the parent
8275 ((View) mParent).invalidate(true);
8276 }
8277 // Mark the view drawn to ensure that it gets invalidated properly the next
8278 // time it is visible and gets invalidated
8279 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 }
8281 if (mAttachInfo != null) {
8282 mAttachInfo.mViewVisibilityChanged = true;
8283 }
8284 }
8285
8286 /* Check if the VISIBLE bit has changed */
8287 if ((changed & INVISIBLE) != 0) {
8288 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008289 /*
8290 * If this view is becoming invisible, set the DRAWN flag so that
8291 * the next invalidate() will not be skipped.
8292 */
8293 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294
8295 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008296 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 if (getRootView() != this) {
8298 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008299 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 }
8301 }
8302 if (mAttachInfo != null) {
8303 mAttachInfo.mViewVisibilityChanged = true;
8304 }
8305 }
8306
Adam Powell326d8082009-12-09 15:10:07 -08008307 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008308 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008309 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8310 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008311 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008312 } else if (mParent != null) {
8313 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008314 }
Adam Powell326d8082009-12-09 15:10:07 -08008315 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8316 }
8317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8319 destroyDrawingCache();
8320 }
8321
8322 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8323 destroyDrawingCache();
8324 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008325 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008326 }
8327
8328 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8329 destroyDrawingCache();
8330 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8331 }
8332
8333 if ((changed & DRAW_MASK) != 0) {
8334 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008335 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 mPrivateFlags &= ~SKIP_DRAW;
8337 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8338 } else {
8339 mPrivateFlags |= SKIP_DRAW;
8340 }
8341 } else {
8342 mPrivateFlags &= ~SKIP_DRAW;
8343 }
8344 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008345 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 }
8347
8348 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008349 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 mParent.recomputeViewAttributes(this);
8351 }
8352 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008353
8354 if (AccessibilityManager.getInstance(mContext).isEnabled()
8355 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8356 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8357 notifyAccessibilityStateChanged();
8358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008359 }
8360
8361 /**
8362 * Change the view's z order in the tree, so it's on top of other sibling
8363 * views
8364 */
8365 public void bringToFront() {
8366 if (mParent != null) {
8367 mParent.bringChildToFront(this);
8368 }
8369 }
8370
8371 /**
8372 * This is called in response to an internal scroll in this view (i.e., the
8373 * view scrolled its own contents). This is typically as a result of
8374 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8375 * called.
8376 *
8377 * @param l Current horizontal scroll origin.
8378 * @param t Current vertical scroll origin.
8379 * @param oldl Previous horizontal scroll origin.
8380 * @param oldt Previous vertical scroll origin.
8381 */
8382 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008383 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8384 postSendViewScrolledAccessibilityEventCallback();
8385 }
8386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 mBackgroundSizeChanged = true;
8388
8389 final AttachInfo ai = mAttachInfo;
8390 if (ai != null) {
8391 ai.mViewScrollChanged = true;
8392 }
8393 }
8394
8395 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008396 * Interface definition for a callback to be invoked when the layout bounds of a view
8397 * changes due to layout processing.
8398 */
8399 public interface OnLayoutChangeListener {
8400 /**
8401 * Called when the focus state of a view has changed.
8402 *
8403 * @param v The view whose state has changed.
8404 * @param left The new value of the view's left property.
8405 * @param top The new value of the view's top property.
8406 * @param right The new value of the view's right property.
8407 * @param bottom The new value of the view's bottom property.
8408 * @param oldLeft The previous value of the view's left property.
8409 * @param oldTop The previous value of the view's top property.
8410 * @param oldRight The previous value of the view's right property.
8411 * @param oldBottom The previous value of the view's bottom property.
8412 */
8413 void onLayoutChange(View v, int left, int top, int right, int bottom,
8414 int oldLeft, int oldTop, int oldRight, int oldBottom);
8415 }
8416
8417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 * This is called during layout when the size of this view has changed. If
8419 * you were just added to the view hierarchy, you're called with the old
8420 * values of 0.
8421 *
8422 * @param w Current width of this view.
8423 * @param h Current height of this view.
8424 * @param oldw Old width of this view.
8425 * @param oldh Old height of this view.
8426 */
8427 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8428 }
8429
8430 /**
8431 * Called by draw to draw the child views. This may be overridden
8432 * by derived classes to gain control just before its children are drawn
8433 * (but after its own view has been drawn).
8434 * @param canvas the canvas on which to draw the view
8435 */
8436 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 }
8439
8440 /**
8441 * Gets the parent of this view. Note that the parent is a
8442 * ViewParent and not necessarily a View.
8443 *
8444 * @return Parent of this view.
8445 */
8446 public final ViewParent getParent() {
8447 return mParent;
8448 }
8449
8450 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008451 * Set the horizontal scrolled position of your view. This will cause a call to
8452 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8453 * invalidated.
8454 * @param value the x position to scroll to
8455 */
8456 public void setScrollX(int value) {
8457 scrollTo(value, mScrollY);
8458 }
8459
8460 /**
8461 * Set the vertical scrolled position of your view. This will cause a call to
8462 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8463 * invalidated.
8464 * @param value the y position to scroll to
8465 */
8466 public void setScrollY(int value) {
8467 scrollTo(mScrollX, value);
8468 }
8469
8470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 * Return the scrolled left position of this view. This is the left edge of
8472 * the displayed part of your view. You do not need to draw any pixels
8473 * farther left, since those are outside of the frame of your view on
8474 * screen.
8475 *
8476 * @return The left edge of the displayed part of your view, in pixels.
8477 */
8478 public final int getScrollX() {
8479 return mScrollX;
8480 }
8481
8482 /**
8483 * Return the scrolled top position of this view. This is the top edge of
8484 * the displayed part of your view. You do not need to draw any pixels above
8485 * it, since those are outside of the frame of your view on screen.
8486 *
8487 * @return The top edge of the displayed part of your view, in pixels.
8488 */
8489 public final int getScrollY() {
8490 return mScrollY;
8491 }
8492
8493 /**
8494 * Return the width of the your view.
8495 *
8496 * @return The width of your view, in pixels.
8497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008498 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 public final int getWidth() {
8500 return mRight - mLeft;
8501 }
8502
8503 /**
8504 * Return the height of your view.
8505 *
8506 * @return The height of your view, in pixels.
8507 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008508 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 public final int getHeight() {
8510 return mBottom - mTop;
8511 }
8512
8513 /**
8514 * Return the visible drawing bounds of your view. Fills in the output
8515 * rectangle with the values from getScrollX(), getScrollY(),
8516 * getWidth(), and getHeight().
8517 *
8518 * @param outRect The (scrolled) drawing bounds of the view.
8519 */
8520 public void getDrawingRect(Rect outRect) {
8521 outRect.left = mScrollX;
8522 outRect.top = mScrollY;
8523 outRect.right = mScrollX + (mRight - mLeft);
8524 outRect.bottom = mScrollY + (mBottom - mTop);
8525 }
8526
8527 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008528 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8529 * raw width component (that is the result is masked by
8530 * {@link #MEASURED_SIZE_MASK}).
8531 *
8532 * @return The raw measured width of this view.
8533 */
8534 public final int getMeasuredWidth() {
8535 return mMeasuredWidth & MEASURED_SIZE_MASK;
8536 }
8537
8538 /**
8539 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008540 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008541 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008542 * This should be used during measurement and layout calculations only. Use
8543 * {@link #getWidth()} to see how wide a view is after layout.
8544 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008545 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008547 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 return mMeasuredWidth;
8549 }
8550
8551 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008552 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8553 * raw width component (that is the result is masked by
8554 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008556 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 */
8558 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008559 return mMeasuredHeight & MEASURED_SIZE_MASK;
8560 }
8561
8562 /**
8563 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008564 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008565 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8566 * This should be used during measurement and layout calculations only. Use
8567 * {@link #getHeight()} to see how wide a view is after layout.
8568 *
8569 * @return The measured width of this view as a bit mask.
8570 */
8571 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 return mMeasuredHeight;
8573 }
8574
8575 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008576 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8577 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8578 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8579 * and the height component is at the shifted bits
8580 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8581 */
8582 public final int getMeasuredState() {
8583 return (mMeasuredWidth&MEASURED_STATE_MASK)
8584 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8585 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8586 }
8587
8588 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008589 * The transform matrix of this view, which is calculated based on the current
8590 * roation, scale, and pivot properties.
8591 *
8592 * @see #getRotation()
8593 * @see #getScaleX()
8594 * @see #getScaleY()
8595 * @see #getPivotX()
8596 * @see #getPivotY()
8597 * @return The current transform matrix for the view
8598 */
8599 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008600 if (mTransformationInfo != null) {
8601 updateMatrix();
8602 return mTransformationInfo.mMatrix;
8603 }
8604 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008605 }
8606
8607 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008608 * Utility function to determine if the value is far enough away from zero to be
8609 * considered non-zero.
8610 * @param value A floating point value to check for zero-ness
8611 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8612 */
8613 private static boolean nonzero(float value) {
8614 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8615 }
8616
8617 /**
Jeff Brown86671742010-09-30 20:00:15 -07008618 * Returns true if the transform matrix is the identity matrix.
8619 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008620 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008621 * @return True if the transform matrix is the identity matrix, false otherwise.
8622 */
Jeff Brown86671742010-09-30 20:00:15 -07008623 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008624 if (mTransformationInfo != null) {
8625 updateMatrix();
8626 return mTransformationInfo.mMatrixIsIdentity;
8627 }
8628 return true;
8629 }
8630
8631 void ensureTransformationInfo() {
8632 if (mTransformationInfo == null) {
8633 mTransformationInfo = new TransformationInfo();
8634 }
Jeff Brown86671742010-09-30 20:00:15 -07008635 }
8636
8637 /**
8638 * Recomputes the transform matrix if necessary.
8639 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008640 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008641 final TransformationInfo info = mTransformationInfo;
8642 if (info == null) {
8643 return;
8644 }
8645 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008646 // transform-related properties have changed since the last time someone
8647 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008648
8649 // Figure out if we need to update the pivot point
8650 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008651 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8652 info.mPrevWidth = mRight - mLeft;
8653 info.mPrevHeight = mBottom - mTop;
8654 info.mPivotX = info.mPrevWidth / 2f;
8655 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008656 }
8657 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008658 info.mMatrix.reset();
8659 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8660 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8661 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8662 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008663 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008664 if (info.mCamera == null) {
8665 info.mCamera = new Camera();
8666 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008667 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008668 info.mCamera.save();
8669 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8670 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8671 info.mCamera.getMatrix(info.matrix3D);
8672 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8673 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8674 info.mPivotY + info.mTranslationY);
8675 info.mMatrix.postConcat(info.matrix3D);
8676 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008677 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008678 info.mMatrixDirty = false;
8679 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8680 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008681 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008682 }
8683
8684 /**
8685 * Utility method to retrieve the inverse of the current mMatrix property.
8686 * We cache the matrix to avoid recalculating it when transform properties
8687 * have not changed.
8688 *
8689 * @return The inverse of the current matrix of this view.
8690 */
Jeff Brown86671742010-09-30 20:00:15 -07008691 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008692 final TransformationInfo info = mTransformationInfo;
8693 if (info != null) {
8694 updateMatrix();
8695 if (info.mInverseMatrixDirty) {
8696 if (info.mInverseMatrix == null) {
8697 info.mInverseMatrix = new Matrix();
8698 }
8699 info.mMatrix.invert(info.mInverseMatrix);
8700 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008701 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008702 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008703 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008705 }
8706
8707 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008708 * Gets the distance along the Z axis from the camera to this view.
8709 *
8710 * @see #setCameraDistance(float)
8711 *
8712 * @return The distance along the Z axis.
8713 */
8714 public float getCameraDistance() {
8715 ensureTransformationInfo();
8716 final float dpi = mResources.getDisplayMetrics().densityDpi;
8717 final TransformationInfo info = mTransformationInfo;
8718 if (info.mCamera == null) {
8719 info.mCamera = new Camera();
8720 info.matrix3D = new Matrix();
8721 }
8722 return -(info.mCamera.getLocationZ() * dpi);
8723 }
8724
8725 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008726 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8727 * views are drawn) from the camera to this view. The camera's distance
8728 * affects 3D transformations, for instance rotations around the X and Y
8729 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008730 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008731 * use a camera distance that's greater than the height (X axis rotation) or
8732 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008733 *
Romain Guya5364ee2011-02-24 14:46:04 -08008734 * <p>The distance of the camera from the view plane can have an affect on the
8735 * perspective distortion of the view when it is rotated around the x or y axis.
8736 * For example, a large distance will result in a large viewing angle, and there
8737 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008738 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008739 * also result in some drawing artifacts if the rotated view ends up partially
8740 * behind the camera (which is why the recommendation is to use a distance at
8741 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008742 *
Romain Guya5364ee2011-02-24 14:46:04 -08008743 * <p>The distance is expressed in "depth pixels." The default distance depends
8744 * on the screen density. For instance, on a medium density display, the
8745 * default distance is 1280. On a high density display, the default distance
8746 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008747 *
Romain Guya5364ee2011-02-24 14:46:04 -08008748 * <p>If you want to specify a distance that leads to visually consistent
8749 * results across various densities, use the following formula:</p>
8750 * <pre>
8751 * float scale = context.getResources().getDisplayMetrics().density;
8752 * view.setCameraDistance(distance * scale);
8753 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008754 *
Romain Guya5364ee2011-02-24 14:46:04 -08008755 * <p>The density scale factor of a high density display is 1.5,
8756 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008757 *
Romain Guya5364ee2011-02-24 14:46:04 -08008758 * @param distance The distance in "depth pixels", if negative the opposite
8759 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008760 *
8761 * @see #setRotationX(float)
8762 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008763 */
8764 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008765 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008766
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008767 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008768 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008769 final TransformationInfo info = mTransformationInfo;
8770 if (info.mCamera == null) {
8771 info.mCamera = new Camera();
8772 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008773 }
8774
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008775 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8776 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008777
Chet Haase9d1992d2012-03-13 11:03:25 -07008778 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008779 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008780 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008781 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008782 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8783 // View was rejected last time it was drawn by its parent; this may have changed
8784 invalidateParentIfNeeded();
8785 }
Romain Guya5364ee2011-02-24 14:46:04 -08008786 }
8787
8788 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008789 * The degrees that the view is rotated around the pivot point.
8790 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008791 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008792 * @see #getPivotX()
8793 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008794 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008795 * @return The degrees of rotation.
8796 */
Chet Haasea5531132012-02-02 13:41:44 -08008797 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008798 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008799 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008800 }
8801
8802 /**
Chet Haase897247b2010-09-09 14:54:47 -07008803 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8804 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008805 *
8806 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008807 *
8808 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008809 * @see #getPivotX()
8810 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008811 * @see #setRotationX(float)
8812 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008813 *
8814 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008815 */
8816 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008817 ensureTransformationInfo();
8818 final TransformationInfo info = mTransformationInfo;
8819 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008820 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008821 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008822 info.mRotation = rotation;
8823 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008824 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008825 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008826 mDisplayList.setRotation(rotation);
8827 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008828 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8829 // View was rejected last time it was drawn by its parent; this may have changed
8830 invalidateParentIfNeeded();
8831 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008832 }
8833 }
8834
8835 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008836 * The degrees that the view is rotated around the vertical axis through the pivot point.
8837 *
8838 * @see #getPivotX()
8839 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008840 * @see #setRotationY(float)
8841 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008842 * @return The degrees of Y rotation.
8843 */
Chet Haasea5531132012-02-02 13:41:44 -08008844 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008845 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008846 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008847 }
8848
8849 /**
Chet Haase897247b2010-09-09 14:54:47 -07008850 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8851 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8852 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008853 *
Romain Guya5364ee2011-02-24 14:46:04 -08008854 * When rotating large views, it is recommended to adjust the camera distance
8855 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008856 *
8857 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008858 *
8859 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008860 * @see #getPivotX()
8861 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008862 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008863 * @see #setRotationX(float)
8864 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008865 *
8866 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008867 */
8868 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008869 ensureTransformationInfo();
8870 final TransformationInfo info = mTransformationInfo;
8871 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008872 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008873 info.mRotationY = rotationY;
8874 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008875 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008876 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008877 mDisplayList.setRotationY(rotationY);
8878 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008879 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8880 // View was rejected last time it was drawn by its parent; this may have changed
8881 invalidateParentIfNeeded();
8882 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008883 }
8884 }
8885
8886 /**
8887 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8888 *
8889 * @see #getPivotX()
8890 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008891 * @see #setRotationX(float)
8892 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008893 * @return The degrees of X rotation.
8894 */
Chet Haasea5531132012-02-02 13:41:44 -08008895 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008896 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008898 }
8899
8900 /**
Chet Haase897247b2010-09-09 14:54:47 -07008901 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8902 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8903 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008904 *
Romain Guya5364ee2011-02-24 14:46:04 -08008905 * When rotating large views, it is recommended to adjust the camera distance
8906 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008907 *
8908 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008909 *
8910 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008911 * @see #getPivotX()
8912 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008913 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008914 * @see #setRotationY(float)
8915 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008916 *
8917 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008918 */
8919 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008920 ensureTransformationInfo();
8921 final TransformationInfo info = mTransformationInfo;
8922 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008923 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008924 info.mRotationX = rotationX;
8925 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008926 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008927 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008928 mDisplayList.setRotationX(rotationX);
8929 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008930 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8931 // View was rejected last time it was drawn by its parent; this may have changed
8932 invalidateParentIfNeeded();
8933 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008934 }
8935 }
8936
8937 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008938 * The amount that the view is scaled in x around the pivot point, as a proportion of
8939 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8940 *
Joe Onorato93162322010-09-16 15:42:01 -04008941 * <p>By default, this is 1.0f.
8942 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008943 * @see #getPivotX()
8944 * @see #getPivotY()
8945 * @return The scaling factor.
8946 */
Chet Haasea5531132012-02-02 13:41:44 -08008947 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008949 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 }
8951
8952 /**
8953 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8954 * the view's unscaled width. A value of 1 means that no scaling is applied.
8955 *
8956 * @param scaleX The scaling factor.
8957 * @see #getPivotX()
8958 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008959 *
8960 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008961 */
8962 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008963 ensureTransformationInfo();
8964 final TransformationInfo info = mTransformationInfo;
8965 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008966 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008967 info.mScaleX = scaleX;
8968 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008969 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008970 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008971 mDisplayList.setScaleX(scaleX);
8972 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008973 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8974 // View was rejected last time it was drawn by its parent; this may have changed
8975 invalidateParentIfNeeded();
8976 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008977 }
8978 }
8979
8980 /**
8981 * The amount that the view is scaled in y around the pivot point, as a proportion of
8982 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8983 *
Joe Onorato93162322010-09-16 15:42:01 -04008984 * <p>By default, this is 1.0f.
8985 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008986 * @see #getPivotX()
8987 * @see #getPivotY()
8988 * @return The scaling factor.
8989 */
Chet Haasea5531132012-02-02 13:41:44 -08008990 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008991 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008992 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008993 }
8994
8995 /**
8996 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8997 * the view's unscaled width. A value of 1 means that no scaling is applied.
8998 *
8999 * @param scaleY The scaling factor.
9000 * @see #getPivotX()
9001 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009002 *
9003 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009004 */
9005 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009006 ensureTransformationInfo();
9007 final TransformationInfo info = mTransformationInfo;
9008 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009009 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009010 info.mScaleY = scaleY;
9011 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009012 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009013 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009014 mDisplayList.setScaleY(scaleY);
9015 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009016 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9017 // View was rejected last time it was drawn by its parent; this may have changed
9018 invalidateParentIfNeeded();
9019 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009020 }
9021 }
9022
9023 /**
9024 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9025 * and {@link #setScaleX(float) scaled}.
9026 *
9027 * @see #getRotation()
9028 * @see #getScaleX()
9029 * @see #getScaleY()
9030 * @see #getPivotY()
9031 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009032 *
9033 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009034 */
Chet Haasea5531132012-02-02 13:41:44 -08009035 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009036 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009037 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009038 }
9039
9040 /**
9041 * Sets the x location of the point around which the view is
9042 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009043 * By default, the pivot point is centered on the object.
9044 * Setting this property disables this behavior and causes the view to use only the
9045 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 *
9047 * @param pivotX The x location of the pivot point.
9048 * @see #getRotation()
9049 * @see #getScaleX()
9050 * @see #getScaleY()
9051 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009052 *
9053 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009054 */
9055 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009056 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009057 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009058 final TransformationInfo info = mTransformationInfo;
9059 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009060 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009061 info.mPivotX = pivotX;
9062 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009063 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009064 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009065 mDisplayList.setPivotX(pivotX);
9066 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009067 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9068 // View was rejected last time it was drawn by its parent; this may have changed
9069 invalidateParentIfNeeded();
9070 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009071 }
9072 }
9073
9074 /**
9075 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9076 * and {@link #setScaleY(float) scaled}.
9077 *
9078 * @see #getRotation()
9079 * @see #getScaleX()
9080 * @see #getScaleY()
9081 * @see #getPivotY()
9082 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009083 *
9084 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009085 */
Chet Haasea5531132012-02-02 13:41:44 -08009086 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009087 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009088 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009089 }
9090
9091 /**
9092 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009093 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9094 * Setting this property disables this behavior and causes the view to use only the
9095 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009096 *
9097 * @param pivotY The y location of the pivot point.
9098 * @see #getRotation()
9099 * @see #getScaleX()
9100 * @see #getScaleY()
9101 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009102 *
9103 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009104 */
9105 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009106 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009107 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009108 final TransformationInfo info = mTransformationInfo;
9109 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009110 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009111 info.mPivotY = pivotY;
9112 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009113 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009114 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009115 mDisplayList.setPivotY(pivotY);
9116 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009117 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9118 // View was rejected last time it was drawn by its parent; this may have changed
9119 invalidateParentIfNeeded();
9120 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009121 }
9122 }
9123
9124 /**
9125 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9126 * completely transparent and 1 means the view is completely opaque.
9127 *
Joe Onorato93162322010-09-16 15:42:01 -04009128 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009129 * @return The opacity of the view.
9130 */
Chet Haasea5531132012-02-02 13:41:44 -08009131 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009132 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009133 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009134 }
9135
9136 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009137 * Returns whether this View has content which overlaps. This function, intended to be
9138 * overridden by specific View types, is an optimization when alpha is set on a view. If
9139 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9140 * and then composited it into place, which can be expensive. If the view has no overlapping
9141 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9142 * An example of overlapping rendering is a TextView with a background image, such as a
9143 * Button. An example of non-overlapping rendering is a TextView with no background, or
9144 * an ImageView with only the foreground image. The default implementation returns true;
9145 * subclasses should override if they have cases which can be optimized.
9146 *
9147 * @return true if the content in this view might overlap, false otherwise.
9148 */
9149 public boolean hasOverlappingRendering() {
9150 return true;
9151 }
9152
9153 /**
Romain Guy171c5922011-01-06 10:04:23 -08009154 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9155 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009156 *
Romain Guy171c5922011-01-06 10:04:23 -08009157 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9158 * responsible for applying the opacity itself. Otherwise, calling this method is
9159 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009160 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009161 *
Chet Haasea5531132012-02-02 13:41:44 -08009162 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9163 * performance implications. It is generally best to use the alpha property sparingly and
9164 * transiently, as in the case of fading animations.</p>
9165 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009166 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009167 *
Joe Malin32736f02011-01-19 16:14:20 -08009168 * @see #setLayerType(int, android.graphics.Paint)
9169 *
Chet Haase73066682010-11-29 15:55:32 -08009170 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009171 */
9172 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009173 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009174 if (mTransformationInfo.mAlpha != alpha) {
9175 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009176 if (onSetAlpha((int) (alpha * 255))) {
9177 mPrivateFlags |= ALPHA_SET;
9178 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009179 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009180 invalidate(true);
9181 } else {
9182 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009183 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009184 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009185 mDisplayList.setAlpha(alpha);
9186 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009187 }
Chet Haaseed032702010-10-01 14:05:54 -07009188 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009189 }
9190
9191 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009192 * Faster version of setAlpha() which performs the same steps except there are
9193 * no calls to invalidate(). The caller of this function should perform proper invalidation
9194 * on the parent and this object. The return value indicates whether the subclass handles
9195 * alpha (the return value for onSetAlpha()).
9196 *
9197 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009198 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9199 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009200 */
9201 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009202 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009203 if (mTransformationInfo.mAlpha != alpha) {
9204 mTransformationInfo.mAlpha = alpha;
9205 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9206 if (subclassHandlesAlpha) {
9207 mPrivateFlags |= ALPHA_SET;
9208 return true;
9209 } else {
9210 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009211 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009212 mDisplayList.setAlpha(alpha);
9213 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009214 }
Chet Haasea00f3862011-02-22 06:34:40 -08009215 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009216 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009217 }
9218
9219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 * Top position of this view relative to its parent.
9221 *
9222 * @return The top of this view, in pixels.
9223 */
9224 @ViewDebug.CapturedViewProperty
9225 public final int getTop() {
9226 return mTop;
9227 }
9228
9229 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009230 * Sets the top position of this view relative to its parent. This method is meant to be called
9231 * by the layout system and should not generally be called otherwise, because the property
9232 * may be changed at any time by the layout.
9233 *
9234 * @param top The top of this view, in pixels.
9235 */
9236 public final void setTop(int top) {
9237 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009238 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009239 final boolean matrixIsIdentity = mTransformationInfo == null
9240 || mTransformationInfo.mMatrixIsIdentity;
9241 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009242 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009243 int minTop;
9244 int yLoc;
9245 if (top < mTop) {
9246 minTop = top;
9247 yLoc = top - mTop;
9248 } else {
9249 minTop = mTop;
9250 yLoc = 0;
9251 }
Chet Haasee9140a72011-02-16 16:23:29 -08009252 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009253 }
9254 } else {
9255 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009256 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009257 }
9258
Chet Haaseed032702010-10-01 14:05:54 -07009259 int width = mRight - mLeft;
9260 int oldHeight = mBottom - mTop;
9261
Chet Haase21cd1382010-09-01 17:42:29 -07009262 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009263 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009264 mDisplayList.setTop(mTop);
9265 }
Chet Haase21cd1382010-09-01 17:42:29 -07009266
Chet Haaseed032702010-10-01 14:05:54 -07009267 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9268
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009269 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009270 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9271 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009272 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009273 }
Chet Haase21cd1382010-09-01 17:42:29 -07009274 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009275 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009276 }
Chet Haase55dbb652010-12-21 20:15:08 -08009277 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009278 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009279 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9280 // View was rejected last time it was drawn by its parent; this may have changed
9281 invalidateParentIfNeeded();
9282 }
Chet Haase21cd1382010-09-01 17:42:29 -07009283 }
9284 }
9285
9286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 * Bottom position of this view relative to its parent.
9288 *
9289 * @return The bottom of this view, in pixels.
9290 */
9291 @ViewDebug.CapturedViewProperty
9292 public final int getBottom() {
9293 return mBottom;
9294 }
9295
9296 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009297 * True if this view has changed since the last time being drawn.
9298 *
9299 * @return The dirty state of this view.
9300 */
9301 public boolean isDirty() {
9302 return (mPrivateFlags & DIRTY_MASK) != 0;
9303 }
9304
9305 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009306 * Sets the bottom position of this view relative to its parent. This method is meant to be
9307 * called by the layout system and should not generally be called otherwise, because the
9308 * property may be changed at any time by the layout.
9309 *
9310 * @param bottom The bottom of this view, in pixels.
9311 */
9312 public final void setBottom(int bottom) {
9313 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009314 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009315 final boolean matrixIsIdentity = mTransformationInfo == null
9316 || mTransformationInfo.mMatrixIsIdentity;
9317 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009318 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009319 int maxBottom;
9320 if (bottom < mBottom) {
9321 maxBottom = mBottom;
9322 } else {
9323 maxBottom = bottom;
9324 }
Chet Haasee9140a72011-02-16 16:23:29 -08009325 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009326 }
9327 } else {
9328 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009329 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009330 }
9331
Chet Haaseed032702010-10-01 14:05:54 -07009332 int width = mRight - mLeft;
9333 int oldHeight = mBottom - mTop;
9334
Chet Haase21cd1382010-09-01 17:42:29 -07009335 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009336 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009337 mDisplayList.setBottom(mBottom);
9338 }
Chet Haase21cd1382010-09-01 17:42:29 -07009339
Chet Haaseed032702010-10-01 14:05:54 -07009340 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9341
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009342 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009343 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9344 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009345 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009346 }
Chet Haase21cd1382010-09-01 17:42:29 -07009347 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009348 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009349 }
Chet Haase55dbb652010-12-21 20:15:08 -08009350 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009351 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009352 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9353 // View was rejected last time it was drawn by its parent; this may have changed
9354 invalidateParentIfNeeded();
9355 }
Chet Haase21cd1382010-09-01 17:42:29 -07009356 }
9357 }
9358
9359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 * Left position of this view relative to its parent.
9361 *
9362 * @return The left edge of this view, in pixels.
9363 */
9364 @ViewDebug.CapturedViewProperty
9365 public final int getLeft() {
9366 return mLeft;
9367 }
9368
9369 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009370 * Sets the left position of this view relative to its parent. This method is meant to be called
9371 * by the layout system and should not generally be called otherwise, because the property
9372 * may be changed at any time by the layout.
9373 *
9374 * @param left The bottom of this view, in pixels.
9375 */
9376 public final void setLeft(int left) {
9377 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009378 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009379 final boolean matrixIsIdentity = mTransformationInfo == null
9380 || mTransformationInfo.mMatrixIsIdentity;
9381 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009382 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009383 int minLeft;
9384 int xLoc;
9385 if (left < mLeft) {
9386 minLeft = left;
9387 xLoc = left - mLeft;
9388 } else {
9389 minLeft = mLeft;
9390 xLoc = 0;
9391 }
Chet Haasee9140a72011-02-16 16:23:29 -08009392 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009393 }
9394 } else {
9395 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009396 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009397 }
9398
Chet Haaseed032702010-10-01 14:05:54 -07009399 int oldWidth = mRight - mLeft;
9400 int height = mBottom - mTop;
9401
Chet Haase21cd1382010-09-01 17:42:29 -07009402 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009403 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009404 mDisplayList.setLeft(left);
9405 }
Chet Haase21cd1382010-09-01 17:42:29 -07009406
Chet Haaseed032702010-10-01 14:05:54 -07009407 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9408
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009409 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009410 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9411 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009412 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009413 }
Chet Haase21cd1382010-09-01 17:42:29 -07009414 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009415 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009416 }
Chet Haase55dbb652010-12-21 20:15:08 -08009417 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009418 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009419 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9420 // View was rejected last time it was drawn by its parent; this may have changed
9421 invalidateParentIfNeeded();
9422 }
Chet Haase21cd1382010-09-01 17:42:29 -07009423 }
9424 }
9425
9426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 * Right position of this view relative to its parent.
9428 *
9429 * @return The right edge of this view, in pixels.
9430 */
9431 @ViewDebug.CapturedViewProperty
9432 public final int getRight() {
9433 return mRight;
9434 }
9435
9436 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009437 * Sets the right position of this view relative to its parent. This method is meant to be called
9438 * by the layout system and should not generally be called otherwise, because the property
9439 * may be changed at any time by the layout.
9440 *
9441 * @param right The bottom of this view, in pixels.
9442 */
9443 public final void setRight(int right) {
9444 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009445 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009446 final boolean matrixIsIdentity = mTransformationInfo == null
9447 || mTransformationInfo.mMatrixIsIdentity;
9448 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009449 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009450 int maxRight;
9451 if (right < mRight) {
9452 maxRight = mRight;
9453 } else {
9454 maxRight = right;
9455 }
Chet Haasee9140a72011-02-16 16:23:29 -08009456 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009457 }
9458 } else {
9459 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009460 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009461 }
9462
Chet Haaseed032702010-10-01 14:05:54 -07009463 int oldWidth = mRight - mLeft;
9464 int height = mBottom - mTop;
9465
Chet Haase21cd1382010-09-01 17:42:29 -07009466 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009467 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009468 mDisplayList.setRight(mRight);
9469 }
Chet Haase21cd1382010-09-01 17:42:29 -07009470
Chet Haaseed032702010-10-01 14:05:54 -07009471 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9472
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009473 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009474 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9475 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009476 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009477 }
Chet Haase21cd1382010-09-01 17:42:29 -07009478 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009479 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009480 }
Chet Haase55dbb652010-12-21 20:15:08 -08009481 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009482 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009483 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9484 // View was rejected last time it was drawn by its parent; this may have changed
9485 invalidateParentIfNeeded();
9486 }
Chet Haase21cd1382010-09-01 17:42:29 -07009487 }
9488 }
9489
9490 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009491 * The visual x position of this view, in pixels. This is equivalent to the
9492 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009493 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009494 *
Chet Haasedf030d22010-07-30 17:22:38 -07009495 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009496 */
Chet Haasea5531132012-02-02 13:41:44 -08009497 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009498 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009499 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009500 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009501
Chet Haasedf030d22010-07-30 17:22:38 -07009502 /**
9503 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9504 * {@link #setTranslationX(float) translationX} property to be the difference between
9505 * the x value passed in and the current {@link #getLeft() left} property.
9506 *
9507 * @param x The visual x position of this view, in pixels.
9508 */
9509 public void setX(float x) {
9510 setTranslationX(x - mLeft);
9511 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009512
Chet Haasedf030d22010-07-30 17:22:38 -07009513 /**
9514 * The visual y position of this view, in pixels. This is equivalent to the
9515 * {@link #setTranslationY(float) translationY} property plus the current
9516 * {@link #getTop() top} property.
9517 *
9518 * @return The visual y position of this view, in pixels.
9519 */
Chet Haasea5531132012-02-02 13:41:44 -08009520 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009521 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009522 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009523 }
9524
9525 /**
9526 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9527 * {@link #setTranslationY(float) translationY} property to be the difference between
9528 * the y value passed in and the current {@link #getTop() top} property.
9529 *
9530 * @param y The visual y position of this view, in pixels.
9531 */
9532 public void setY(float y) {
9533 setTranslationY(y - mTop);
9534 }
9535
9536
9537 /**
9538 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9539 * This position is post-layout, in addition to wherever the object's
9540 * layout placed it.
9541 *
9542 * @return The horizontal position of this view relative to its left position, in pixels.
9543 */
Chet Haasea5531132012-02-02 13:41:44 -08009544 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009545 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009546 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009547 }
9548
9549 /**
9550 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9551 * This effectively positions the object post-layout, in addition to wherever the object's
9552 * layout placed it.
9553 *
9554 * @param translationX The horizontal position of this view relative to its left position,
9555 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009556 *
9557 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009558 */
9559 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009560 ensureTransformationInfo();
9561 final TransformationInfo info = mTransformationInfo;
9562 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009563 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009564 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009565 info.mTranslationX = translationX;
9566 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009567 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009568 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009569 mDisplayList.setTranslationX(translationX);
9570 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009571 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9572 // View was rejected last time it was drawn by its parent; this may have changed
9573 invalidateParentIfNeeded();
9574 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009575 }
9576 }
9577
9578 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009579 * The horizontal location of this view relative to its {@link #getTop() top} position.
9580 * This position is post-layout, in addition to wherever the object's
9581 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009582 *
Chet Haasedf030d22010-07-30 17:22:38 -07009583 * @return The vertical position of this view relative to its top position,
9584 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009585 */
Chet Haasea5531132012-02-02 13:41:44 -08009586 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009587 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009588 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009589 }
9590
9591 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009592 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9593 * This effectively positions the object post-layout, in addition to wherever the object's
9594 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009595 *
Chet Haasedf030d22010-07-30 17:22:38 -07009596 * @param translationY The vertical position of this view relative to its top position,
9597 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009598 *
9599 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009600 */
Chet Haasedf030d22010-07-30 17:22:38 -07009601 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009602 ensureTransformationInfo();
9603 final TransformationInfo info = mTransformationInfo;
9604 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009605 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009606 info.mTranslationY = translationY;
9607 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009608 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009609 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009610 mDisplayList.setTranslationY(translationY);
9611 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009612 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9613 // View was rejected last time it was drawn by its parent; this may have changed
9614 invalidateParentIfNeeded();
9615 }
Chet Haasedf030d22010-07-30 17:22:38 -07009616 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009617 }
9618
9619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620 * Hit rectangle in parent's coordinates
9621 *
9622 * @param outRect The hit rectangle of the view.
9623 */
9624 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009625 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009626 final TransformationInfo info = mTransformationInfo;
9627 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009628 outRect.set(mLeft, mTop, mRight, mBottom);
9629 } else {
9630 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009631 tmpRect.set(-info.mPivotX, -info.mPivotY,
9632 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9633 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009634 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9635 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009636 }
9637 }
9638
9639 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009640 * Determines whether the given point, in local coordinates is inside the view.
9641 */
9642 /*package*/ final boolean pointInView(float localX, float localY) {
9643 return localX >= 0 && localX < (mRight - mLeft)
9644 && localY >= 0 && localY < (mBottom - mTop);
9645 }
9646
9647 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009648 * Utility method to determine whether the given point, in local coordinates,
9649 * is inside the view, where the area of the view is expanded by the slop factor.
9650 * This method is called while processing touch-move events to determine if the event
9651 * is still within the view.
9652 */
9653 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009654 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009655 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009656 }
9657
9658 /**
9659 * When a view has focus and the user navigates away from it, the next view is searched for
9660 * starting from the rectangle filled in by this method.
9661 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009662 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9663 * of the view. However, if your view maintains some idea of internal selection,
9664 * such as a cursor, or a selected row or column, you should override this method and
9665 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009666 *
9667 * @param r The rectangle to fill in, in this view's coordinates.
9668 */
9669 public void getFocusedRect(Rect r) {
9670 getDrawingRect(r);
9671 }
9672
9673 /**
9674 * If some part of this view is not clipped by any of its parents, then
9675 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009676 * coordinates (without taking possible View rotations into account), offset
9677 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9678 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 *
9680 * @param r If true is returned, r holds the global coordinates of the
9681 * visible portion of this view.
9682 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9683 * between this view and its root. globalOffet may be null.
9684 * @return true if r is non-empty (i.e. part of the view is visible at the
9685 * root level.
9686 */
9687 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9688 int width = mRight - mLeft;
9689 int height = mBottom - mTop;
9690 if (width > 0 && height > 0) {
9691 r.set(0, 0, width, height);
9692 if (globalOffset != null) {
9693 globalOffset.set(-mScrollX, -mScrollY);
9694 }
9695 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9696 }
9697 return false;
9698 }
9699
9700 public final boolean getGlobalVisibleRect(Rect r) {
9701 return getGlobalVisibleRect(r, null);
9702 }
9703
9704 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009705 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 if (getGlobalVisibleRect(r, offset)) {
9707 r.offset(-offset.x, -offset.y); // make r local
9708 return true;
9709 }
9710 return false;
9711 }
9712
9713 /**
9714 * Offset this view's vertical location by the specified number of pixels.
9715 *
9716 * @param offset the number of pixels to offset the view by
9717 */
9718 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009719 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009720 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009721 final boolean matrixIsIdentity = mTransformationInfo == null
9722 || mTransformationInfo.mMatrixIsIdentity;
9723 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009724 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009725 invalidateViewProperty(false, false);
9726 } else {
9727 final ViewParent p = mParent;
9728 if (p != null && mAttachInfo != null) {
9729 final Rect r = mAttachInfo.mTmpInvalRect;
9730 int minTop;
9731 int maxBottom;
9732 int yLoc;
9733 if (offset < 0) {
9734 minTop = mTop + offset;
9735 maxBottom = mBottom;
9736 yLoc = offset;
9737 } else {
9738 minTop = mTop;
9739 maxBottom = mBottom + offset;
9740 yLoc = 0;
9741 }
9742 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9743 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009744 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009745 }
9746 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009747 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009748 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009749
Chet Haasec3aa3612010-06-17 08:50:37 -07009750 mTop += offset;
9751 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009752 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009753 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009754 invalidateViewProperty(false, false);
9755 } else {
9756 if (!matrixIsIdentity) {
9757 invalidateViewProperty(false, true);
9758 }
9759 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009760 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009762 }
9763
9764 /**
9765 * Offset this view's horizontal location by the specified amount of pixels.
9766 *
9767 * @param offset the numer of pixels to offset the view by
9768 */
9769 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009770 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009771 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009772 final boolean matrixIsIdentity = mTransformationInfo == null
9773 || mTransformationInfo.mMatrixIsIdentity;
9774 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009775 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009776 invalidateViewProperty(false, false);
9777 } else {
9778 final ViewParent p = mParent;
9779 if (p != null && mAttachInfo != null) {
9780 final Rect r = mAttachInfo.mTmpInvalRect;
9781 int minLeft;
9782 int maxRight;
9783 if (offset < 0) {
9784 minLeft = mLeft + offset;
9785 maxRight = mRight;
9786 } else {
9787 minLeft = mLeft;
9788 maxRight = mRight + offset;
9789 }
9790 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9791 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009792 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009793 }
9794 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009795 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009796 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009797
Chet Haasec3aa3612010-06-17 08:50:37 -07009798 mLeft += offset;
9799 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009800 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009801 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009802 invalidateViewProperty(false, false);
9803 } else {
9804 if (!matrixIsIdentity) {
9805 invalidateViewProperty(false, true);
9806 }
9807 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009808 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 }
9811
9812 /**
9813 * Get the LayoutParams associated with this view. All views should have
9814 * layout parameters. These supply parameters to the <i>parent</i> of this
9815 * view specifying how it should be arranged. There are many subclasses of
9816 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9817 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009818 *
9819 * This method may return null if this View is not attached to a parent
9820 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9821 * was not invoked successfully. When a View is attached to a parent
9822 * ViewGroup, this method must not return null.
9823 *
9824 * @return The LayoutParams associated with this view, or null if no
9825 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009827 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009828 public ViewGroup.LayoutParams getLayoutParams() {
9829 return mLayoutParams;
9830 }
9831
9832 /**
9833 * Set the layout parameters associated with this view. These supply
9834 * parameters to the <i>parent</i> of this view specifying how it should be
9835 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9836 * correspond to the different subclasses of ViewGroup that are responsible
9837 * for arranging their children.
9838 *
Romain Guy01c174b2011-02-22 11:51:06 -08009839 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 */
9841 public void setLayoutParams(ViewGroup.LayoutParams params) {
9842 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009843 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844 }
9845 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009846 if (mParent instanceof ViewGroup) {
9847 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 requestLayout();
9850 }
9851
9852 /**
9853 * Set the scrolled position of your view. This will cause a call to
9854 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9855 * invalidated.
9856 * @param x the x position to scroll to
9857 * @param y the y position to scroll to
9858 */
9859 public void scrollTo(int x, int y) {
9860 if (mScrollX != x || mScrollY != y) {
9861 int oldX = mScrollX;
9862 int oldY = mScrollY;
9863 mScrollX = x;
9864 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009865 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009867 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009868 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 }
9871 }
9872
9873 /**
9874 * Move the scrolled position of your view. This will cause a call to
9875 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9876 * invalidated.
9877 * @param x the amount of pixels to scroll by horizontally
9878 * @param y the amount of pixels to scroll by vertically
9879 */
9880 public void scrollBy(int x, int y) {
9881 scrollTo(mScrollX + x, mScrollY + y);
9882 }
9883
9884 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009885 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9886 * animation to fade the scrollbars out after a default delay. If a subclass
9887 * provides animated scrolling, the start delay should equal the duration
9888 * of the scrolling animation.</p>
9889 *
9890 * <p>The animation starts only if at least one of the scrollbars is
9891 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9892 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9893 * this method returns true, and false otherwise. If the animation is
9894 * started, this method calls {@link #invalidate()}; in that case the
9895 * caller should not call {@link #invalidate()}.</p>
9896 *
9897 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009898 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009899 *
9900 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9901 * and {@link #scrollTo(int, int)}.</p>
9902 *
9903 * @return true if the animation is played, false otherwise
9904 *
9905 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009906 * @see #scrollBy(int, int)
9907 * @see #scrollTo(int, int)
9908 * @see #isHorizontalScrollBarEnabled()
9909 * @see #isVerticalScrollBarEnabled()
9910 * @see #setHorizontalScrollBarEnabled(boolean)
9911 * @see #setVerticalScrollBarEnabled(boolean)
9912 */
9913 protected boolean awakenScrollBars() {
9914 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009915 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009916 }
9917
9918 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009919 * Trigger the scrollbars to draw.
9920 * This method differs from awakenScrollBars() only in its default duration.
9921 * initialAwakenScrollBars() will show the scroll bars for longer than
9922 * usual to give the user more of a chance to notice them.
9923 *
9924 * @return true if the animation is played, false otherwise.
9925 */
9926 private boolean initialAwakenScrollBars() {
9927 return mScrollCache != null &&
9928 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9929 }
9930
9931 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009932 * <p>
9933 * Trigger the scrollbars to draw. When invoked this method starts an
9934 * animation to fade the scrollbars out after a fixed delay. If a subclass
9935 * provides animated scrolling, the start delay should equal the duration of
9936 * the scrolling animation.
9937 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009938 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009939 * <p>
9940 * The animation starts only if at least one of the scrollbars is enabled,
9941 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9942 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9943 * this method returns true, and false otherwise. If the animation is
9944 * started, this method calls {@link #invalidate()}; in that case the caller
9945 * should not call {@link #invalidate()}.
9946 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009947 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009948 * <p>
9949 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009950 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009951 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009952 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009953 * @param startDelay the delay, in milliseconds, after which the animation
9954 * should start; when the delay is 0, the animation starts
9955 * immediately
9956 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009957 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009958 * @see #scrollBy(int, int)
9959 * @see #scrollTo(int, int)
9960 * @see #isHorizontalScrollBarEnabled()
9961 * @see #isVerticalScrollBarEnabled()
9962 * @see #setHorizontalScrollBarEnabled(boolean)
9963 * @see #setVerticalScrollBarEnabled(boolean)
9964 */
9965 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009966 return awakenScrollBars(startDelay, true);
9967 }
Joe Malin32736f02011-01-19 16:14:20 -08009968
Mike Cleron290947b2009-09-29 18:34:32 -07009969 /**
9970 * <p>
9971 * Trigger the scrollbars to draw. When invoked this method starts an
9972 * animation to fade the scrollbars out after a fixed delay. If a subclass
9973 * provides animated scrolling, the start delay should equal the duration of
9974 * the scrolling animation.
9975 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009976 *
Mike Cleron290947b2009-09-29 18:34:32 -07009977 * <p>
9978 * The animation starts only if at least one of the scrollbars is enabled,
9979 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9980 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9981 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009982 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009983 * is set to true; in that case the caller
9984 * should not call {@link #invalidate()}.
9985 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009986 *
Mike Cleron290947b2009-09-29 18:34:32 -07009987 * <p>
9988 * This method should be invoked everytime a subclass directly updates the
9989 * scroll parameters.
9990 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009991 *
Mike Cleron290947b2009-09-29 18:34:32 -07009992 * @param startDelay the delay, in milliseconds, after which the animation
9993 * should start; when the delay is 0, the animation starts
9994 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009995 *
Mike Cleron290947b2009-09-29 18:34:32 -07009996 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009997 *
Mike Cleron290947b2009-09-29 18:34:32 -07009998 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009999 *
Mike Cleron290947b2009-09-29 18:34:32 -070010000 * @see #scrollBy(int, int)
10001 * @see #scrollTo(int, int)
10002 * @see #isHorizontalScrollBarEnabled()
10003 * @see #isVerticalScrollBarEnabled()
10004 * @see #setHorizontalScrollBarEnabled(boolean)
10005 * @see #setVerticalScrollBarEnabled(boolean)
10006 */
10007 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010008 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010009
Mike Cleronf116bf82009-09-27 19:14:12 -070010010 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10011 return false;
10012 }
10013
10014 if (scrollCache.scrollBar == null) {
10015 scrollCache.scrollBar = new ScrollBarDrawable();
10016 }
10017
10018 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10019
Mike Cleron290947b2009-09-29 18:34:32 -070010020 if (invalidate) {
10021 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010022 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010023 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010024
10025 if (scrollCache.state == ScrollabilityCache.OFF) {
10026 // FIXME: this is copied from WindowManagerService.
10027 // We should get this value from the system when it
10028 // is possible to do so.
10029 final int KEY_REPEAT_FIRST_DELAY = 750;
10030 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10031 }
10032
10033 // Tell mScrollCache when we should start fading. This may
10034 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010035 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010036 scrollCache.fadeStartTime = fadeStartTime;
10037 scrollCache.state = ScrollabilityCache.ON;
10038
10039 // Schedule our fader to run, unscheduling any old ones first
10040 if (mAttachInfo != null) {
10041 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10042 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10043 }
10044
10045 return true;
10046 }
10047
10048 return false;
10049 }
10050
10051 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010052 * Do not invalidate views which are not visible and which are not running an animation. They
10053 * will not get drawn and they should not set dirty flags as if they will be drawn
10054 */
10055 private boolean skipInvalidate() {
10056 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10057 (!(mParent instanceof ViewGroup) ||
10058 !((ViewGroup) mParent).isViewTransitioning(this));
10059 }
10060 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010061 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010062 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10063 * in the future. This must be called from a UI thread. To call from a non-UI
10064 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 *
10066 * WARNING: This method is destructive to dirty.
10067 * @param dirty the rectangle representing the bounds of the dirty region
10068 */
10069 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010070 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010071 return;
10072 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010073 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010074 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10075 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010077 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010078 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 final ViewParent p = mParent;
10080 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010081 //noinspection PointlessBooleanExpression,ConstantConditions
10082 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10083 if (p != null && ai != null && ai.mHardwareAccelerated) {
10084 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010085 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010086 p.invalidateChild(this, null);
10087 return;
10088 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 if (p != null && ai != null) {
10091 final int scrollX = mScrollX;
10092 final int scrollY = mScrollY;
10093 final Rect r = ai.mTmpInvalRect;
10094 r.set(dirty.left - scrollX, dirty.top - scrollY,
10095 dirty.right - scrollX, dirty.bottom - scrollY);
10096 mParent.invalidateChild(this, r);
10097 }
10098 }
10099 }
10100
10101 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010102 * 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 -080010103 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010104 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10105 * will be called at some point in the future. This must be called from
10106 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 * @param l the left position of the dirty region
10108 * @param t the top position of the dirty region
10109 * @param r the right position of the dirty region
10110 * @param b the bottom position of the dirty region
10111 */
10112 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010113 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010114 return;
10115 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010116 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010117 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10118 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010119 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010120 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010121 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 final ViewParent p = mParent;
10123 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010124 //noinspection PointlessBooleanExpression,ConstantConditions
10125 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10126 if (p != null && ai != null && ai.mHardwareAccelerated) {
10127 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010128 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010129 p.invalidateChild(this, null);
10130 return;
10131 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 if (p != null && ai != null && l < r && t < b) {
10134 final int scrollX = mScrollX;
10135 final int scrollY = mScrollY;
10136 final Rect tmpr = ai.mTmpInvalRect;
10137 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10138 p.invalidateChild(this, tmpr);
10139 }
10140 }
10141 }
10142
10143 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010144 * Invalidate the whole view. If the view is visible,
10145 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10146 * the future. This must be called from a UI thread. To call from a non-UI thread,
10147 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 */
10149 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010150 invalidate(true);
10151 }
Joe Malin32736f02011-01-19 16:14:20 -080010152
Chet Haaseed032702010-10-01 14:05:54 -070010153 /**
10154 * This is where the invalidate() work actually happens. A full invalidate()
10155 * causes the drawing cache to be invalidated, but this function can be called with
10156 * invalidateCache set to false to skip that invalidation step for cases that do not
10157 * need it (for example, a component that remains at the same dimensions with the same
10158 * content).
10159 *
10160 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10161 * well. This is usually true for a full invalidate, but may be set to false if the
10162 * View's contents or dimensions have not changed.
10163 */
Romain Guy849d0a32011-02-01 17:20:48 -080010164 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010165 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010166 return;
10167 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010168 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010169 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010170 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10171 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010172 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010173 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010174 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010175 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010176 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010178 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010179 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010180 //noinspection PointlessBooleanExpression,ConstantConditions
10181 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10182 if (p != null && ai != null && ai.mHardwareAccelerated) {
10183 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010184 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010185 p.invalidateChild(this, null);
10186 return;
10187 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010188 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010190 if (p != null && ai != null) {
10191 final Rect r = ai.mTmpInvalRect;
10192 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10193 // Don't call invalidate -- we don't want to internally scroll
10194 // our own bounds
10195 p.invalidateChild(this, r);
10196 }
10197 }
10198 }
10199
10200 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010201 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10202 * set any flags or handle all of the cases handled by the default invalidation methods.
10203 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10204 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10205 * walk up the hierarchy, transforming the dirty rect as necessary.
10206 *
10207 * The method also handles normal invalidation logic if display list properties are not
10208 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10209 * backup approach, to handle these cases used in the various property-setting methods.
10210 *
10211 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10212 * are not being used in this view
10213 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10214 * list properties are not being used in this view
10215 */
10216 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010217 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010218 if (invalidateParent) {
10219 invalidateParentCaches();
10220 }
10221 if (forceRedraw) {
10222 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10223 }
10224 invalidate(false);
10225 } else {
10226 final AttachInfo ai = mAttachInfo;
10227 final ViewParent p = mParent;
10228 if (p != null && ai != null) {
10229 final Rect r = ai.mTmpInvalRect;
10230 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10231 if (mParent instanceof ViewGroup) {
10232 ((ViewGroup) mParent).invalidateChildFast(this, r);
10233 } else {
10234 mParent.invalidateChild(this, r);
10235 }
10236 }
10237 }
10238 }
10239
10240 /**
10241 * Utility method to transform a given Rect by the current matrix of this view.
10242 */
10243 void transformRect(final Rect rect) {
10244 if (!getMatrix().isIdentity()) {
10245 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10246 boundingRect.set(rect);
10247 getMatrix().mapRect(boundingRect);
10248 rect.set((int) (boundingRect.left - 0.5f),
10249 (int) (boundingRect.top - 0.5f),
10250 (int) (boundingRect.right + 0.5f),
10251 (int) (boundingRect.bottom + 0.5f));
10252 }
10253 }
10254
10255 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010256 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010257 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10258 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010259 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10260 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010261 *
10262 * @hide
10263 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010264 protected void invalidateParentCaches() {
10265 if (mParent instanceof View) {
10266 ((View) mParent).mPrivateFlags |= INVALIDATED;
10267 }
10268 }
Joe Malin32736f02011-01-19 16:14:20 -080010269
Romain Guy0fd89bf2011-01-26 15:41:30 -080010270 /**
10271 * Used to indicate that the parent of this view should be invalidated. This functionality
10272 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10273 * which is necessary when various parent-managed properties of the view change, such as
10274 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10275 * an invalidation event to the parent.
10276 *
10277 * @hide
10278 */
10279 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010280 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010281 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010282 }
10283 }
10284
10285 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010286 * Indicates whether this View is opaque. An opaque View guarantees that it will
10287 * draw all the pixels overlapping its bounds using a fully opaque color.
10288 *
10289 * Subclasses of View should override this method whenever possible to indicate
10290 * whether an instance is opaque. Opaque Views are treated in a special way by
10291 * the View hierarchy, possibly allowing it to perform optimizations during
10292 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010293 *
Romain Guy24443ea2009-05-11 11:56:30 -070010294 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010295 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010296 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010297 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010298 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010299 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10300 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010301 }
10302
Adam Powell20232d02010-12-08 21:08:53 -080010303 /**
10304 * @hide
10305 */
10306 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010307 // Opaque if:
10308 // - Has a background
10309 // - Background is opaque
10310 // - Doesn't have scrollbars or scrollbars are inside overlay
10311
Philip Milne6c8ea062012-04-03 17:38:43 -070010312 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010313 mPrivateFlags |= OPAQUE_BACKGROUND;
10314 } else {
10315 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10316 }
10317
10318 final int flags = mViewFlags;
10319 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10320 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10321 mPrivateFlags |= OPAQUE_SCROLLBARS;
10322 } else {
10323 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10324 }
10325 }
10326
10327 /**
10328 * @hide
10329 */
10330 protected boolean hasOpaqueScrollbars() {
10331 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010332 }
10333
10334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010335 * @return A handler associated with the thread running the View. This
10336 * handler can be used to pump events in the UI events queue.
10337 */
10338 public Handler getHandler() {
10339 if (mAttachInfo != null) {
10340 return mAttachInfo.mHandler;
10341 }
10342 return null;
10343 }
10344
10345 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010346 * Gets the view root associated with the View.
10347 * @return The view root, or null if none.
10348 * @hide
10349 */
10350 public ViewRootImpl getViewRootImpl() {
10351 if (mAttachInfo != null) {
10352 return mAttachInfo.mViewRootImpl;
10353 }
10354 return null;
10355 }
10356
10357 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010358 * <p>Causes the Runnable to be added to the message queue.
10359 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010360 *
Romain Guye63a4f32011-08-11 11:33:31 -070010361 * <p>This method can be invoked from outside of the UI thread
10362 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 *
10364 * @param action The Runnable that will be executed.
10365 *
10366 * @return Returns true if the Runnable was successfully placed in to the
10367 * message queue. Returns false on failure, usually because the
10368 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010369 *
10370 * @see #postDelayed
10371 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010372 */
10373 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010374 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010375 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010376 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010378 // Assume that post will succeed later
10379 ViewRootImpl.getRunQueue().post(action);
10380 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 }
10382
10383 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010384 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010386 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010387 *
Romain Guye63a4f32011-08-11 11:33:31 -070010388 * <p>This method can be invoked from outside of the UI thread
10389 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 *
10391 * @param action The Runnable that will be executed.
10392 * @param delayMillis The delay (in milliseconds) until the Runnable
10393 * will be executed.
10394 *
10395 * @return true if the Runnable was successfully placed in to the
10396 * message queue. Returns false on failure, usually because the
10397 * looper processing the message queue is exiting. Note that a
10398 * result of true does not mean the Runnable will be processed --
10399 * if the looper is quit before the delivery time of the message
10400 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010401 *
10402 * @see #post
10403 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 */
10405 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010406 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010407 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010408 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010409 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010410 // Assume that post will succeed later
10411 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10412 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010413 }
10414
10415 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010416 * <p>Causes the Runnable to execute on the next animation time step.
10417 * The runnable will be run on the user interface thread.</p>
10418 *
10419 * <p>This method can be invoked from outside of the UI thread
10420 * only when this View is attached to a window.</p>
10421 *
10422 * @param action The Runnable that will be executed.
10423 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010424 * @see #postOnAnimationDelayed
10425 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010426 */
10427 public void postOnAnimation(Runnable action) {
10428 final AttachInfo attachInfo = mAttachInfo;
10429 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010430 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10431 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010432 } else {
10433 // Assume that post will succeed later
10434 ViewRootImpl.getRunQueue().post(action);
10435 }
10436 }
10437
10438 /**
10439 * <p>Causes the Runnable to execute on the next animation time step,
10440 * after the specified amount of time elapses.
10441 * The runnable will be run on the user interface thread.</p>
10442 *
10443 * <p>This method can be invoked from outside of the UI thread
10444 * only when this View is attached to a window.</p>
10445 *
10446 * @param action The Runnable that will be executed.
10447 * @param delayMillis The delay (in milliseconds) until the Runnable
10448 * will be executed.
10449 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010450 * @see #postOnAnimation
10451 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010452 */
10453 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10454 final AttachInfo attachInfo = mAttachInfo;
10455 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010456 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10457 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010458 } else {
10459 // Assume that post will succeed later
10460 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10461 }
10462 }
10463
10464 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010465 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010466 *
Romain Guye63a4f32011-08-11 11:33:31 -070010467 * <p>This method can be invoked from outside of the UI thread
10468 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 *
10470 * @param action The Runnable to remove from the message handling queue
10471 *
10472 * @return true if this view could ask the Handler to remove the Runnable,
10473 * false otherwise. When the returned value is true, the Runnable
10474 * may or may not have been actually removed from the message queue
10475 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010476 *
10477 * @see #post
10478 * @see #postDelayed
10479 * @see #postOnAnimation
10480 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010481 */
10482 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010483 if (action != null) {
10484 final AttachInfo attachInfo = mAttachInfo;
10485 if (attachInfo != null) {
10486 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010487 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10488 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010489 } else {
10490 // Assume that post will succeed later
10491 ViewRootImpl.getRunQueue().removeCallbacks(action);
10492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 return true;
10495 }
10496
10497 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010498 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10499 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 *
Romain Guye63a4f32011-08-11 11:33:31 -070010501 * <p>This method can be invoked from outside of the UI thread
10502 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010503 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010504 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010505 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 */
10507 public void postInvalidate() {
10508 postInvalidateDelayed(0);
10509 }
10510
10511 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010512 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10513 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010514 *
Romain Guye63a4f32011-08-11 11:33:31 -070010515 * <p>This method can be invoked from outside of the UI thread
10516 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 *
10518 * @param left The left coordinate of the rectangle to invalidate.
10519 * @param top The top coordinate of the rectangle to invalidate.
10520 * @param right The right coordinate of the rectangle to invalidate.
10521 * @param bottom The bottom coordinate of the rectangle to invalidate.
10522 *
10523 * @see #invalidate(int, int, int, int)
10524 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010525 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 */
10527 public void postInvalidate(int left, int top, int right, int bottom) {
10528 postInvalidateDelayed(0, left, top, right, bottom);
10529 }
10530
10531 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010532 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10533 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010534 *
Romain Guye63a4f32011-08-11 11:33:31 -070010535 * <p>This method can be invoked from outside of the UI thread
10536 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 *
10538 * @param delayMilliseconds the duration in milliseconds to delay the
10539 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010540 *
10541 * @see #invalidate()
10542 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 */
10544 public void postInvalidateDelayed(long delayMilliseconds) {
10545 // We try only with the AttachInfo because there's no point in invalidating
10546 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010547 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010548 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010549 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 }
10551 }
10552
10553 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010554 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10555 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010556 *
Romain Guye63a4f32011-08-11 11:33:31 -070010557 * <p>This method can be invoked from outside of the UI thread
10558 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 *
10560 * @param delayMilliseconds the duration in milliseconds to delay the
10561 * invalidation by
10562 * @param left The left coordinate of the rectangle to invalidate.
10563 * @param top The top coordinate of the rectangle to invalidate.
10564 * @param right The right coordinate of the rectangle to invalidate.
10565 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010566 *
10567 * @see #invalidate(int, int, int, int)
10568 * @see #invalidate(Rect)
10569 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 */
10571 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10572 int right, int bottom) {
10573
10574 // We try only with the AttachInfo because there's no point in invalidating
10575 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010576 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010577 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010578 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10579 info.target = this;
10580 info.left = left;
10581 info.top = top;
10582 info.right = right;
10583 info.bottom = bottom;
10584
Jeff Browna175a5b2012-02-15 19:18:31 -080010585 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 }
10587 }
10588
10589 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010590 * <p>Cause an invalidate to happen on the next animation time step, typically the
10591 * next display frame.</p>
10592 *
10593 * <p>This method can be invoked from outside of the UI thread
10594 * only when this View is attached to a window.</p>
10595 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010596 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010597 */
10598 public void postInvalidateOnAnimation() {
10599 // We try only with the AttachInfo because there's no point in invalidating
10600 // if we are not attached to our window
10601 final AttachInfo attachInfo = mAttachInfo;
10602 if (attachInfo != null) {
10603 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10604 }
10605 }
10606
10607 /**
10608 * <p>Cause an invalidate of the specified area to happen on the next animation
10609 * time step, typically the next display frame.</p>
10610 *
10611 * <p>This method can be invoked from outside of the UI thread
10612 * only when this View is attached to a window.</p>
10613 *
10614 * @param left The left coordinate of the rectangle to invalidate.
10615 * @param top The top coordinate of the rectangle to invalidate.
10616 * @param right The right coordinate of the rectangle to invalidate.
10617 * @param bottom The bottom coordinate of the rectangle to invalidate.
10618 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010619 * @see #invalidate(int, int, int, int)
10620 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010621 */
10622 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10623 // We try only with the AttachInfo because there's no point in invalidating
10624 // if we are not attached to our window
10625 final AttachInfo attachInfo = mAttachInfo;
10626 if (attachInfo != null) {
10627 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10628 info.target = this;
10629 info.left = left;
10630 info.top = top;
10631 info.right = right;
10632 info.bottom = bottom;
10633
10634 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10635 }
10636 }
10637
10638 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010639 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10640 * This event is sent at most once every
10641 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10642 */
10643 private void postSendViewScrolledAccessibilityEventCallback() {
10644 if (mSendViewScrolledAccessibilityEvent == null) {
10645 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10646 }
10647 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10648 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10649 postDelayed(mSendViewScrolledAccessibilityEvent,
10650 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10651 }
10652 }
10653
10654 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 * Called by a parent to request that a child update its values for mScrollX
10656 * and mScrollY if necessary. This will typically be done if the child is
10657 * animating a scroll using a {@link android.widget.Scroller Scroller}
10658 * object.
10659 */
10660 public void computeScroll() {
10661 }
10662
10663 /**
10664 * <p>Indicate whether the horizontal edges are faded when the view is
10665 * scrolled horizontally.</p>
10666 *
10667 * @return true if the horizontal edges should are faded on scroll, false
10668 * otherwise
10669 *
10670 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010671 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010672 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010673 */
10674 public boolean isHorizontalFadingEdgeEnabled() {
10675 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10676 }
10677
10678 /**
10679 * <p>Define whether the horizontal edges should be faded when this view
10680 * is scrolled horizontally.</p>
10681 *
10682 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10683 * be faded when the view is scrolled
10684 * horizontally
10685 *
10686 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010687 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010688 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 */
10690 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10691 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10692 if (horizontalFadingEdgeEnabled) {
10693 initScrollCache();
10694 }
10695
10696 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10697 }
10698 }
10699
10700 /**
10701 * <p>Indicate whether the vertical edges are faded when the view is
10702 * scrolled horizontally.</p>
10703 *
10704 * @return true if the vertical edges should are faded on scroll, false
10705 * otherwise
10706 *
10707 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010708 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010709 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 */
10711 public boolean isVerticalFadingEdgeEnabled() {
10712 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10713 }
10714
10715 /**
10716 * <p>Define whether the vertical edges should be faded when this view
10717 * is scrolled vertically.</p>
10718 *
10719 * @param verticalFadingEdgeEnabled true if the vertical edges should
10720 * be faded when the view is scrolled
10721 * vertically
10722 *
10723 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010724 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010725 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 */
10727 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10728 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10729 if (verticalFadingEdgeEnabled) {
10730 initScrollCache();
10731 }
10732
10733 mViewFlags ^= FADING_EDGE_VERTICAL;
10734 }
10735 }
10736
10737 /**
10738 * Returns the strength, or intensity, of the top faded edge. The strength is
10739 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10740 * returns 0.0 or 1.0 but no value in between.
10741 *
10742 * Subclasses should override this method to provide a smoother fade transition
10743 * when scrolling occurs.
10744 *
10745 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10746 */
10747 protected float getTopFadingEdgeStrength() {
10748 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10749 }
10750
10751 /**
10752 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10753 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10754 * returns 0.0 or 1.0 but no value in between.
10755 *
10756 * Subclasses should override this method to provide a smoother fade transition
10757 * when scrolling occurs.
10758 *
10759 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10760 */
10761 protected float getBottomFadingEdgeStrength() {
10762 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10763 computeVerticalScrollRange() ? 1.0f : 0.0f;
10764 }
10765
10766 /**
10767 * Returns the strength, or intensity, of the left faded edge. The strength is
10768 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10769 * returns 0.0 or 1.0 but no value in between.
10770 *
10771 * Subclasses should override this method to provide a smoother fade transition
10772 * when scrolling occurs.
10773 *
10774 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10775 */
10776 protected float getLeftFadingEdgeStrength() {
10777 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10778 }
10779
10780 /**
10781 * Returns the strength, or intensity, of the right faded edge. The strength is
10782 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10783 * returns 0.0 or 1.0 but no value in between.
10784 *
10785 * Subclasses should override this method to provide a smoother fade transition
10786 * when scrolling occurs.
10787 *
10788 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10789 */
10790 protected float getRightFadingEdgeStrength() {
10791 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10792 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10793 }
10794
10795 /**
10796 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10797 * scrollbar is not drawn by default.</p>
10798 *
10799 * @return true if the horizontal scrollbar should be painted, false
10800 * otherwise
10801 *
10802 * @see #setHorizontalScrollBarEnabled(boolean)
10803 */
10804 public boolean isHorizontalScrollBarEnabled() {
10805 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10806 }
10807
10808 /**
10809 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10810 * scrollbar is not drawn by default.</p>
10811 *
10812 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10813 * be painted
10814 *
10815 * @see #isHorizontalScrollBarEnabled()
10816 */
10817 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10818 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10819 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010820 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010821 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010822 }
10823 }
10824
10825 /**
10826 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10827 * scrollbar is not drawn by default.</p>
10828 *
10829 * @return true if the vertical scrollbar should be painted, false
10830 * otherwise
10831 *
10832 * @see #setVerticalScrollBarEnabled(boolean)
10833 */
10834 public boolean isVerticalScrollBarEnabled() {
10835 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10836 }
10837
10838 /**
10839 * <p>Define whether the vertical scrollbar should be drawn or not. The
10840 * scrollbar is not drawn by default.</p>
10841 *
10842 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10843 * be painted
10844 *
10845 * @see #isVerticalScrollBarEnabled()
10846 */
10847 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10848 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10849 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010850 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010851 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 }
10853 }
10854
Adam Powell20232d02010-12-08 21:08:53 -080010855 /**
10856 * @hide
10857 */
10858 protected void recomputePadding() {
10859 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010860 }
Joe Malin32736f02011-01-19 16:14:20 -080010861
Mike Cleronfe81d382009-09-28 14:22:16 -070010862 /**
10863 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010864 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010865 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010866 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010867 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010868 */
10869 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10870 initScrollCache();
10871 final ScrollabilityCache scrollabilityCache = mScrollCache;
10872 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010873 if (fadeScrollbars) {
10874 scrollabilityCache.state = ScrollabilityCache.OFF;
10875 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010876 scrollabilityCache.state = ScrollabilityCache.ON;
10877 }
10878 }
Joe Malin32736f02011-01-19 16:14:20 -080010879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 /**
Joe Malin32736f02011-01-19 16:14:20 -080010881 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010882 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010883 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010884 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010885 *
10886 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010887 */
10888 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010889 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010890 }
Joe Malin32736f02011-01-19 16:14:20 -080010891
Mike Cleron52f0a642009-09-28 18:21:37 -070010892 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010893 *
10894 * Returns the delay before scrollbars fade.
10895 *
10896 * @return the delay before scrollbars fade
10897 *
10898 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10899 */
10900 public int getScrollBarDefaultDelayBeforeFade() {
10901 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10902 mScrollCache.scrollBarDefaultDelayBeforeFade;
10903 }
10904
10905 /**
10906 * Define the delay before scrollbars fade.
10907 *
10908 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10909 *
10910 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10911 */
10912 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10913 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10914 }
10915
10916 /**
10917 *
10918 * Returns the scrollbar fade duration.
10919 *
10920 * @return the scrollbar fade duration
10921 *
10922 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10923 */
10924 public int getScrollBarFadeDuration() {
10925 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10926 mScrollCache.scrollBarFadeDuration;
10927 }
10928
10929 /**
10930 * Define the scrollbar fade duration.
10931 *
10932 * @param scrollBarFadeDuration - the scrollbar fade duration
10933 *
10934 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10935 */
10936 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10937 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10938 }
10939
10940 /**
10941 *
10942 * Returns the scrollbar size.
10943 *
10944 * @return the scrollbar size
10945 *
10946 * @attr ref android.R.styleable#View_scrollbarSize
10947 */
10948 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010949 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010950 mScrollCache.scrollBarSize;
10951 }
10952
10953 /**
10954 * Define the scrollbar size.
10955 *
10956 * @param scrollBarSize - the scrollbar size
10957 *
10958 * @attr ref android.R.styleable#View_scrollbarSize
10959 */
10960 public void setScrollBarSize(int scrollBarSize) {
10961 getScrollCache().scrollBarSize = scrollBarSize;
10962 }
10963
10964 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10966 * inset. When inset, they add to the padding of the view. And the scrollbars
10967 * can be drawn inside the padding area or on the edge of the view. For example,
10968 * if a view has a background drawable and you want to draw the scrollbars
10969 * inside the padding specified by the drawable, you can use
10970 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10971 * appear at the edge of the view, ignoring the padding, then you can use
10972 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10973 * @param style the style of the scrollbars. Should be one of
10974 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10975 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10976 * @see #SCROLLBARS_INSIDE_OVERLAY
10977 * @see #SCROLLBARS_INSIDE_INSET
10978 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10979 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010980 *
10981 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010982 */
10983 public void setScrollBarStyle(int style) {
10984 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10985 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010986 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010987 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988 }
10989 }
10990
10991 /**
10992 * <p>Returns the current scrollbar style.</p>
10993 * @return the current scrollbar style
10994 * @see #SCROLLBARS_INSIDE_OVERLAY
10995 * @see #SCROLLBARS_INSIDE_INSET
10996 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10997 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010998 *
10999 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011000 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011001 @ViewDebug.ExportedProperty(mapping = {
11002 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11003 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11004 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11005 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11006 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 public int getScrollBarStyle() {
11008 return mViewFlags & SCROLLBARS_STYLE_MASK;
11009 }
11010
11011 /**
11012 * <p>Compute the horizontal range that the horizontal scrollbar
11013 * represents.</p>
11014 *
11015 * <p>The range is expressed in arbitrary units that must be the same as the
11016 * units used by {@link #computeHorizontalScrollExtent()} and
11017 * {@link #computeHorizontalScrollOffset()}.</p>
11018 *
11019 * <p>The default range is the drawing width of this view.</p>
11020 *
11021 * @return the total horizontal range represented by the horizontal
11022 * scrollbar
11023 *
11024 * @see #computeHorizontalScrollExtent()
11025 * @see #computeHorizontalScrollOffset()
11026 * @see android.widget.ScrollBarDrawable
11027 */
11028 protected int computeHorizontalScrollRange() {
11029 return getWidth();
11030 }
11031
11032 /**
11033 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11034 * within the horizontal range. This value is used to compute the position
11035 * of the thumb within the scrollbar's track.</p>
11036 *
11037 * <p>The range is expressed in arbitrary units that must be the same as the
11038 * units used by {@link #computeHorizontalScrollRange()} and
11039 * {@link #computeHorizontalScrollExtent()}.</p>
11040 *
11041 * <p>The default offset is the scroll offset of this view.</p>
11042 *
11043 * @return the horizontal offset of the scrollbar's thumb
11044 *
11045 * @see #computeHorizontalScrollRange()
11046 * @see #computeHorizontalScrollExtent()
11047 * @see android.widget.ScrollBarDrawable
11048 */
11049 protected int computeHorizontalScrollOffset() {
11050 return mScrollX;
11051 }
11052
11053 /**
11054 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11055 * within the horizontal range. This value is used to compute the length
11056 * of the thumb within the scrollbar's track.</p>
11057 *
11058 * <p>The range is expressed in arbitrary units that must be the same as the
11059 * units used by {@link #computeHorizontalScrollRange()} and
11060 * {@link #computeHorizontalScrollOffset()}.</p>
11061 *
11062 * <p>The default extent is the drawing width of this view.</p>
11063 *
11064 * @return the horizontal extent of the scrollbar's thumb
11065 *
11066 * @see #computeHorizontalScrollRange()
11067 * @see #computeHorizontalScrollOffset()
11068 * @see android.widget.ScrollBarDrawable
11069 */
11070 protected int computeHorizontalScrollExtent() {
11071 return getWidth();
11072 }
11073
11074 /**
11075 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11076 *
11077 * <p>The range is expressed in arbitrary units that must be the same as the
11078 * units used by {@link #computeVerticalScrollExtent()} and
11079 * {@link #computeVerticalScrollOffset()}.</p>
11080 *
11081 * @return the total vertical range represented by the vertical scrollbar
11082 *
11083 * <p>The default range is the drawing height of this view.</p>
11084 *
11085 * @see #computeVerticalScrollExtent()
11086 * @see #computeVerticalScrollOffset()
11087 * @see android.widget.ScrollBarDrawable
11088 */
11089 protected int computeVerticalScrollRange() {
11090 return getHeight();
11091 }
11092
11093 /**
11094 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11095 * within the horizontal range. This value is used to compute the position
11096 * of the thumb within the scrollbar's track.</p>
11097 *
11098 * <p>The range is expressed in arbitrary units that must be the same as the
11099 * units used by {@link #computeVerticalScrollRange()} and
11100 * {@link #computeVerticalScrollExtent()}.</p>
11101 *
11102 * <p>The default offset is the scroll offset of this view.</p>
11103 *
11104 * @return the vertical offset of the scrollbar's thumb
11105 *
11106 * @see #computeVerticalScrollRange()
11107 * @see #computeVerticalScrollExtent()
11108 * @see android.widget.ScrollBarDrawable
11109 */
11110 protected int computeVerticalScrollOffset() {
11111 return mScrollY;
11112 }
11113
11114 /**
11115 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11116 * within the vertical range. This value is used to compute the length
11117 * of the thumb within the scrollbar's track.</p>
11118 *
11119 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011120 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 * {@link #computeVerticalScrollOffset()}.</p>
11122 *
11123 * <p>The default extent is the drawing height of this view.</p>
11124 *
11125 * @return the vertical extent of the scrollbar's thumb
11126 *
11127 * @see #computeVerticalScrollRange()
11128 * @see #computeVerticalScrollOffset()
11129 * @see android.widget.ScrollBarDrawable
11130 */
11131 protected int computeVerticalScrollExtent() {
11132 return getHeight();
11133 }
11134
11135 /**
Adam Powell69159442011-06-13 17:53:06 -070011136 * Check if this view can be scrolled horizontally in a certain direction.
11137 *
11138 * @param direction Negative to check scrolling left, positive to check scrolling right.
11139 * @return true if this view can be scrolled in the specified direction, false otherwise.
11140 */
11141 public boolean canScrollHorizontally(int direction) {
11142 final int offset = computeHorizontalScrollOffset();
11143 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11144 if (range == 0) return false;
11145 if (direction < 0) {
11146 return offset > 0;
11147 } else {
11148 return offset < range - 1;
11149 }
11150 }
11151
11152 /**
11153 * Check if this view can be scrolled vertically in a certain direction.
11154 *
11155 * @param direction Negative to check scrolling up, positive to check scrolling down.
11156 * @return true if this view can be scrolled in the specified direction, false otherwise.
11157 */
11158 public boolean canScrollVertically(int direction) {
11159 final int offset = computeVerticalScrollOffset();
11160 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11161 if (range == 0) return false;
11162 if (direction < 0) {
11163 return offset > 0;
11164 } else {
11165 return offset < range - 1;
11166 }
11167 }
11168
11169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11171 * scrollbars are painted only if they have been awakened first.</p>
11172 *
11173 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011174 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011175 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011177 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 // scrollbars are drawn only when the animation is running
11179 final ScrollabilityCache cache = mScrollCache;
11180 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011181
Mike Cleronf116bf82009-09-27 19:14:12 -070011182 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011183
Mike Cleronf116bf82009-09-27 19:14:12 -070011184 if (state == ScrollabilityCache.OFF) {
11185 return;
11186 }
Joe Malin32736f02011-01-19 16:14:20 -080011187
Mike Cleronf116bf82009-09-27 19:14:12 -070011188 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011189
Mike Cleronf116bf82009-09-27 19:14:12 -070011190 if (state == ScrollabilityCache.FADING) {
11191 // We're fading -- get our fade interpolation
11192 if (cache.interpolatorValues == null) {
11193 cache.interpolatorValues = new float[1];
11194 }
Joe Malin32736f02011-01-19 16:14:20 -080011195
Mike Cleronf116bf82009-09-27 19:14:12 -070011196 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011197
Mike Cleronf116bf82009-09-27 19:14:12 -070011198 // Stops the animation if we're done
11199 if (cache.scrollBarInterpolator.timeToValues(values) ==
11200 Interpolator.Result.FREEZE_END) {
11201 cache.state = ScrollabilityCache.OFF;
11202 } else {
11203 cache.scrollBar.setAlpha(Math.round(values[0]));
11204 }
Joe Malin32736f02011-01-19 16:14:20 -080011205
11206 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011207 // drawing. We only want this when we're fading so that
11208 // we prevent excessive redraws
11209 invalidate = true;
11210 } else {
11211 // We're just on -- but we may have been fading before so
11212 // reset alpha
11213 cache.scrollBar.setAlpha(255);
11214 }
11215
Joe Malin32736f02011-01-19 16:14:20 -080011216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 final int viewFlags = mViewFlags;
11218
11219 final boolean drawHorizontalScrollBar =
11220 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11221 final boolean drawVerticalScrollBar =
11222 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11223 && !isVerticalScrollBarHidden();
11224
11225 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11226 final int width = mRight - mLeft;
11227 final int height = mBottom - mTop;
11228
11229 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230
Mike Reede8853fc2009-09-04 14:01:48 -040011231 final int scrollX = mScrollX;
11232 final int scrollY = mScrollY;
11233 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11234
Mike Cleronf116bf82009-09-27 19:14:12 -070011235 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011237 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011238 int size = scrollBar.getSize(false);
11239 if (size <= 0) {
11240 size = cache.scrollBarSize;
11241 }
11242
Mike Cleronf116bf82009-09-27 19:14:12 -070011243 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011244 computeHorizontalScrollOffset(),
11245 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011246 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011247 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011248 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011249 left = scrollX + (mPaddingLeft & inside);
11250 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11251 bottom = top + size;
11252 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11253 if (invalidate) {
11254 invalidate(left, top, right, bottom);
11255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 }
11257
11258 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011259 int size = scrollBar.getSize(true);
11260 if (size <= 0) {
11261 size = cache.scrollBarSize;
11262 }
11263
Mike Reede8853fc2009-09-04 14:01:48 -040011264 scrollBar.setParameters(computeVerticalScrollRange(),
11265 computeVerticalScrollOffset(),
11266 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011267 switch (mVerticalScrollbarPosition) {
11268 default:
11269 case SCROLLBAR_POSITION_DEFAULT:
11270 case SCROLLBAR_POSITION_RIGHT:
11271 left = scrollX + width - size - (mUserPaddingRight & inside);
11272 break;
11273 case SCROLLBAR_POSITION_LEFT:
11274 left = scrollX + (mUserPaddingLeft & inside);
11275 break;
11276 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011277 top = scrollY + (mPaddingTop & inside);
11278 right = left + size;
11279 bottom = scrollY + height - (mUserPaddingBottom & inside);
11280 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11281 if (invalidate) {
11282 invalidate(left, top, right, bottom);
11283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 }
11285 }
11286 }
11287 }
Romain Guy8506ab42009-06-11 17:35:47 -070011288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011289 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011290 * 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 -080011291 * FastScroller is visible.
11292 * @return whether to temporarily hide the vertical scrollbar
11293 * @hide
11294 */
11295 protected boolean isVerticalScrollBarHidden() {
11296 return false;
11297 }
11298
11299 /**
11300 * <p>Draw the horizontal scrollbar if
11301 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 * @param canvas the canvas on which to draw the scrollbar
11304 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 *
11306 * @see #isHorizontalScrollBarEnabled()
11307 * @see #computeHorizontalScrollRange()
11308 * @see #computeHorizontalScrollExtent()
11309 * @see #computeHorizontalScrollOffset()
11310 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011311 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011312 */
Romain Guy8fb95422010-08-17 18:38:51 -070011313 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11314 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011315 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011316 scrollBar.draw(canvas);
11317 }
Mike Reede8853fc2009-09-04 14:01:48 -040011318
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11321 * returns true.</p>
11322 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011323 * @param canvas the canvas on which to draw the scrollbar
11324 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 *
11326 * @see #isVerticalScrollBarEnabled()
11327 * @see #computeVerticalScrollRange()
11328 * @see #computeVerticalScrollExtent()
11329 * @see #computeVerticalScrollOffset()
11330 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011331 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332 */
Romain Guy8fb95422010-08-17 18:38:51 -070011333 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11334 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011335 scrollBar.setBounds(l, t, r, b);
11336 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 }
11338
11339 /**
11340 * Implement this to do your drawing.
11341 *
11342 * @param canvas the canvas on which the background will be drawn
11343 */
11344 protected void onDraw(Canvas canvas) {
11345 }
11346
11347 /*
11348 * Caller is responsible for calling requestLayout if necessary.
11349 * (This allows addViewInLayout to not request a new layout.)
11350 */
11351 void assignParent(ViewParent parent) {
11352 if (mParent == null) {
11353 mParent = parent;
11354 } else if (parent == null) {
11355 mParent = null;
11356 } else {
11357 throw new RuntimeException("view " + this + " being added, but"
11358 + " it already has a parent");
11359 }
11360 }
11361
11362 /**
11363 * This is called when the view is attached to a window. At this point it
11364 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011365 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11366 * however it may be called any time before the first onDraw -- including
11367 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011368 *
11369 * @see #onDetachedFromWindow()
11370 */
11371 protected void onAttachedToWindow() {
11372 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11373 mParent.requestTransparentRegion(this);
11374 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011375
Adam Powell8568c3a2010-04-19 14:26:11 -070011376 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11377 initialAwakenScrollBars();
11378 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11379 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011380
Chet Haasea9b61ac2010-12-20 07:40:25 -080011381 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011382
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011383 // Order is important here: LayoutDirection MUST be resolved before Padding
11384 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011385 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011386 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011387 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011388 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011389
Svetoslav Ganov42138042012-03-20 11:51:39 -070011390 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011391 if (isFocused()) {
11392 InputMethodManager imm = InputMethodManager.peekInstance();
11393 imm.focusIn(this);
11394 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011395
11396 if (mAttachInfo != null && mDisplayList != null) {
11397 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11398 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011399 }
Cibu Johny86666632010-02-22 13:01:02 -080011400
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011401 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011402 * @see #onScreenStateChanged(int)
11403 */
11404 void dispatchScreenStateChanged(int screenState) {
11405 onScreenStateChanged(screenState);
11406 }
11407
11408 /**
11409 * This method is called whenever the state of the screen this view is
11410 * attached to changes. A state change will usually occurs when the screen
11411 * turns on or off (whether it happens automatically or the user does it
11412 * manually.)
11413 *
11414 * @param screenState The new state of the screen. Can be either
11415 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11416 */
11417 public void onScreenStateChanged(int screenState) {
11418 }
11419
11420 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011421 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11422 */
11423 private boolean hasRtlSupport() {
11424 return mContext.getApplicationInfo().hasRtlSupport();
11425 }
11426
11427 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011428 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11429 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011430 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011431 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011432 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011433 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011434 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011435 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011436
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011437 if (hasRtlSupport()) {
11438 // Set resolved depending on layout direction
11439 switch (getLayoutDirection()) {
11440 case LAYOUT_DIRECTION_INHERIT:
11441 // If this is root view, no need to look at parent's layout dir.
11442 if (canResolveLayoutDirection()) {
11443 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011444
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011445 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11446 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11447 }
11448 } else {
11449 // Nothing to do, LTR by default
11450 }
11451 break;
11452 case LAYOUT_DIRECTION_RTL:
11453 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11454 break;
11455 case LAYOUT_DIRECTION_LOCALE:
11456 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011457 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11458 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011459 break;
11460 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011461 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011462 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011463 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011464
11465 // Set to resolved
11466 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011467 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011468 // Resolve padding
11469 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011470 }
11471
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011472 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011473 * Called when layout direction has been resolved.
11474 *
11475 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011476 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011477 */
11478 public void onResolvedLayoutDirectionChanged() {
11479 }
11480
11481 /**
11482 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011483 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011484 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011485 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011486 // If the user specified the absolute padding (either with android:padding or
11487 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11488 // use the default padding or the padding from the background drawable
11489 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011490 int resolvedLayoutDirection = getResolvedLayoutDirection();
11491 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011492 case LAYOUT_DIRECTION_RTL:
11493 // Start user padding override Right user padding. Otherwise, if Right user
11494 // padding is not defined, use the default Right padding. If Right user padding
11495 // is defined, just use it.
11496 if (mUserPaddingStart >= 0) {
11497 mUserPaddingRight = mUserPaddingStart;
11498 } else if (mUserPaddingRight < 0) {
11499 mUserPaddingRight = mPaddingRight;
11500 }
11501 if (mUserPaddingEnd >= 0) {
11502 mUserPaddingLeft = mUserPaddingEnd;
11503 } else if (mUserPaddingLeft < 0) {
11504 mUserPaddingLeft = mPaddingLeft;
11505 }
11506 break;
11507 case LAYOUT_DIRECTION_LTR:
11508 default:
11509 // Start user padding override Left user padding. Otherwise, if Left user
11510 // padding is not defined, use the default left padding. If Left user padding
11511 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011512 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011513 mUserPaddingLeft = mUserPaddingStart;
11514 } else if (mUserPaddingLeft < 0) {
11515 mUserPaddingLeft = mPaddingLeft;
11516 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011517 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011518 mUserPaddingRight = mUserPaddingEnd;
11519 } else if (mUserPaddingRight < 0) {
11520 mUserPaddingRight = mPaddingRight;
11521 }
11522 }
11523
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011524 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11525
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011526 if(isPaddingRelative()) {
11527 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11528 } else {
11529 recomputePadding();
11530 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011531 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011532 }
11533
11534 /**
11535 * Resolve padding depending on the layout direction. Subclasses that care about
11536 * padding resolution should override this method. The default implementation does
11537 * nothing.
11538 *
11539 * @param layoutDirection the direction of the layout
11540 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011541 * @see {@link #LAYOUT_DIRECTION_LTR}
11542 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011543 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011544 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011545 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011546 }
11547
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011548 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011549 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011550 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011551 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011552 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011553 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011554 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011555 switch (getLayoutDirection()) {
11556 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011557 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011558 default:
11559 return true;
11560 }
11561 }
11562
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011563 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011564 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11565 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011566 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011567 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011568 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011569 // Reset the current resolved bits
11570 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011571 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011572 // Reset also the text direction
11573 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011574 }
11575
11576 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011577 * Called during reset of resolved layout direction.
11578 *
11579 * Subclasses need to override this method to clear cached information that depends on the
11580 * resolved layout direction, or to inform child views that inherit their layout direction.
11581 *
11582 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011583 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011584 */
11585 public void onResolvedLayoutDirectionReset() {
11586 }
11587
11588 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011589 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011590 *
11591 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011592 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011593 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011594 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011595 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011596 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597 }
11598
11599 /**
11600 * This is called when the view is detached from a window. At this point it
11601 * no longer has a surface for drawing.
11602 *
11603 * @see #onAttachedToWindow()
11604 */
11605 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011606 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011607
Romain Guya440b002010-02-24 15:57:54 -080011608 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011609 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011610 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011611 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011613 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011614
Romain Guya998dff2012-03-23 18:58:36 -070011615 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011616
11617 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011618 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011619 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011620 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011621 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011622 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011623 // Should never happen
11624 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011625 }
11626
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011627 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011628
11629 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011630 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011631 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 }
11633
11634 /**
11635 * @return The number of times this view has been attached to a window
11636 */
11637 protected int getWindowAttachCount() {
11638 return mWindowAttachCount;
11639 }
11640
11641 /**
11642 * Retrieve a unique token identifying the window this view is attached to.
11643 * @return Return the window's token for use in
11644 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11645 */
11646 public IBinder getWindowToken() {
11647 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11648 }
11649
11650 /**
11651 * Retrieve a unique token identifying the top-level "real" window of
11652 * the window that this view is attached to. That is, this is like
11653 * {@link #getWindowToken}, except if the window this view in is a panel
11654 * window (attached to another containing window), then the token of
11655 * the containing window is returned instead.
11656 *
11657 * @return Returns the associated window token, either
11658 * {@link #getWindowToken()} or the containing window's token.
11659 */
11660 public IBinder getApplicationWindowToken() {
11661 AttachInfo ai = mAttachInfo;
11662 if (ai != null) {
11663 IBinder appWindowToken = ai.mPanelParentWindowToken;
11664 if (appWindowToken == null) {
11665 appWindowToken = ai.mWindowToken;
11666 }
11667 return appWindowToken;
11668 }
11669 return null;
11670 }
11671
11672 /**
11673 * Retrieve private session object this view hierarchy is using to
11674 * communicate with the window manager.
11675 * @return the session object to communicate with the window manager
11676 */
11677 /*package*/ IWindowSession getWindowSession() {
11678 return mAttachInfo != null ? mAttachInfo.mSession : null;
11679 }
11680
11681 /**
11682 * @param info the {@link android.view.View.AttachInfo} to associated with
11683 * this view
11684 */
11685 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11686 //System.out.println("Attached! " + this);
11687 mAttachInfo = info;
11688 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011689 // We will need to evaluate the drawable state at least once.
11690 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011691 if (mFloatingTreeObserver != null) {
11692 info.mTreeObserver.merge(mFloatingTreeObserver);
11693 mFloatingTreeObserver = null;
11694 }
11695 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11696 mAttachInfo.mScrollContainers.add(this);
11697 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11698 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011699 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011701
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011702 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011703 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011704 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011705 if (listeners != null && listeners.size() > 0) {
11706 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11707 // perform the dispatching. The iterator is a safe guard against listeners that
11708 // could mutate the list by calling the various add/remove methods. This prevents
11709 // the array from being modified while we iterate it.
11710 for (OnAttachStateChangeListener listener : listeners) {
11711 listener.onViewAttachedToWindow(this);
11712 }
11713 }
11714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011715 int vis = info.mWindowVisibility;
11716 if (vis != GONE) {
11717 onWindowVisibilityChanged(vis);
11718 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011719 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11720 // If nobody has evaluated the drawable state yet, then do it now.
11721 refreshDrawableState();
11722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011723 }
11724
11725 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011726 AttachInfo info = mAttachInfo;
11727 if (info != null) {
11728 int vis = info.mWindowVisibility;
11729 if (vis != GONE) {
11730 onWindowVisibilityChanged(GONE);
11731 }
11732 }
11733
11734 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011735
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011736 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011737 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011738 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011739 if (listeners != null && listeners.size() > 0) {
11740 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11741 // perform the dispatching. The iterator is a safe guard against listeners that
11742 // could mutate the list by calling the various add/remove methods. This prevents
11743 // the array from being modified while we iterate it.
11744 for (OnAttachStateChangeListener listener : listeners) {
11745 listener.onViewDetachedFromWindow(this);
11746 }
11747 }
11748
Romain Guy01d5edc2011-01-28 11:28:53 -080011749 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011750 mAttachInfo.mScrollContainers.remove(this);
11751 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11752 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011754 mAttachInfo = null;
11755 }
11756
11757 /**
11758 * Store this view hierarchy's frozen state into the given container.
11759 *
11760 * @param container The SparseArray in which to save the view's state.
11761 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011762 * @see #restoreHierarchyState(android.util.SparseArray)
11763 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11764 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 */
11766 public void saveHierarchyState(SparseArray<Parcelable> container) {
11767 dispatchSaveInstanceState(container);
11768 }
11769
11770 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011771 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11772 * this view and its children. May be overridden to modify how freezing happens to a
11773 * 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 -080011774 *
11775 * @param container The SparseArray in which to save the view's state.
11776 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011777 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11778 * @see #saveHierarchyState(android.util.SparseArray)
11779 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011780 */
11781 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11782 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11783 mPrivateFlags &= ~SAVE_STATE_CALLED;
11784 Parcelable state = onSaveInstanceState();
11785 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11786 throw new IllegalStateException(
11787 "Derived class did not call super.onSaveInstanceState()");
11788 }
11789 if (state != null) {
11790 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11791 // + ": " + state);
11792 container.put(mID, state);
11793 }
11794 }
11795 }
11796
11797 /**
11798 * Hook allowing a view to generate a representation of its internal state
11799 * that can later be used to create a new instance with that same state.
11800 * This state should only contain information that is not persistent or can
11801 * not be reconstructed later. For example, you will never store your
11802 * current position on screen because that will be computed again when a
11803 * new instance of the view is placed in its view hierarchy.
11804 * <p>
11805 * Some examples of things you may store here: the current cursor position
11806 * in a text view (but usually not the text itself since that is stored in a
11807 * content provider or other persistent storage), the currently selected
11808 * item in a list view.
11809 *
11810 * @return Returns a Parcelable object containing the view's current dynamic
11811 * state, or null if there is nothing interesting to save. The
11812 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011813 * @see #onRestoreInstanceState(android.os.Parcelable)
11814 * @see #saveHierarchyState(android.util.SparseArray)
11815 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011816 * @see #setSaveEnabled(boolean)
11817 */
11818 protected Parcelable onSaveInstanceState() {
11819 mPrivateFlags |= SAVE_STATE_CALLED;
11820 return BaseSavedState.EMPTY_STATE;
11821 }
11822
11823 /**
11824 * Restore this view hierarchy's frozen state from the given container.
11825 *
11826 * @param container The SparseArray which holds previously frozen states.
11827 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011828 * @see #saveHierarchyState(android.util.SparseArray)
11829 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11830 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011831 */
11832 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11833 dispatchRestoreInstanceState(container);
11834 }
11835
11836 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011837 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11838 * state for this view and its children. May be overridden to modify how restoring
11839 * happens to a view's children; for example, some views may want to not store state
11840 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 *
11842 * @param container The SparseArray which holds previously saved state.
11843 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011844 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11845 * @see #restoreHierarchyState(android.util.SparseArray)
11846 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 */
11848 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11849 if (mID != NO_ID) {
11850 Parcelable state = container.get(mID);
11851 if (state != null) {
11852 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11853 // + ": " + state);
11854 mPrivateFlags &= ~SAVE_STATE_CALLED;
11855 onRestoreInstanceState(state);
11856 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11857 throw new IllegalStateException(
11858 "Derived class did not call super.onRestoreInstanceState()");
11859 }
11860 }
11861 }
11862 }
11863
11864 /**
11865 * Hook allowing a view to re-apply a representation of its internal state that had previously
11866 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11867 * null state.
11868 *
11869 * @param state The frozen state that had previously been returned by
11870 * {@link #onSaveInstanceState}.
11871 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011872 * @see #onSaveInstanceState()
11873 * @see #restoreHierarchyState(android.util.SparseArray)
11874 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011875 */
11876 protected void onRestoreInstanceState(Parcelable state) {
11877 mPrivateFlags |= SAVE_STATE_CALLED;
11878 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011879 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11880 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011881 + "when two views of different type have the same id in the same hierarchy. "
11882 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011883 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 }
11885 }
11886
11887 /**
11888 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11889 *
11890 * @return the drawing start time in milliseconds
11891 */
11892 public long getDrawingTime() {
11893 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11894 }
11895
11896 /**
11897 * <p>Enables or disables the duplication of the parent's state into this view. When
11898 * duplication is enabled, this view gets its drawable state from its parent rather
11899 * than from its own internal properties.</p>
11900 *
11901 * <p>Note: in the current implementation, setting this property to true after the
11902 * view was added to a ViewGroup might have no effect at all. This property should
11903 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11904 *
11905 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11906 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011907 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011908 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11909 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 *
11911 * @param enabled True to enable duplication of the parent's drawable state, false
11912 * to disable it.
11913 *
11914 * @see #getDrawableState()
11915 * @see #isDuplicateParentStateEnabled()
11916 */
11917 public void setDuplicateParentStateEnabled(boolean enabled) {
11918 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11919 }
11920
11921 /**
11922 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11923 *
11924 * @return True if this view's drawable state is duplicated from the parent,
11925 * false otherwise
11926 *
11927 * @see #getDrawableState()
11928 * @see #setDuplicateParentStateEnabled(boolean)
11929 */
11930 public boolean isDuplicateParentStateEnabled() {
11931 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11932 }
11933
11934 /**
Romain Guy171c5922011-01-06 10:04:23 -080011935 * <p>Specifies the type of layer backing this view. The layer can be
11936 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11937 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011938 *
Romain Guy171c5922011-01-06 10:04:23 -080011939 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11940 * instance that controls how the layer is composed on screen. The following
11941 * properties of the paint are taken into account when composing the layer:</p>
11942 * <ul>
11943 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11944 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11945 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11946 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011947 *
Romain Guy171c5922011-01-06 10:04:23 -080011948 * <p>If this view has an alpha value set to < 1.0 by calling
11949 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11950 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11951 * equivalent to setting a hardware layer on this view and providing a paint with
11952 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011953 *
Romain Guy171c5922011-01-06 10:04:23 -080011954 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11955 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11956 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011957 *
Romain Guy171c5922011-01-06 10:04:23 -080011958 * @param layerType The ype of layer to use with this view, must be one of
11959 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11960 * {@link #LAYER_TYPE_HARDWARE}
11961 * @param paint The paint used to compose the layer. This argument is optional
11962 * and can be null. It is ignored when the layer type is
11963 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011964 *
11965 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011966 * @see #LAYER_TYPE_NONE
11967 * @see #LAYER_TYPE_SOFTWARE
11968 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011969 * @see #setAlpha(float)
11970 *
Romain Guy171c5922011-01-06 10:04:23 -080011971 * @attr ref android.R.styleable#View_layerType
11972 */
11973 public void setLayerType(int layerType, Paint paint) {
11974 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011975 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011976 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11977 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011978
Romain Guyd6cd5722011-01-17 14:42:41 -080011979 if (layerType == mLayerType) {
11980 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11981 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011982 invalidateParentCaches();
11983 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011984 }
11985 return;
11986 }
Romain Guy171c5922011-01-06 10:04:23 -080011987
11988 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011989 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011990 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011991 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011992 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011993 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011994 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011995 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011996 default:
11997 break;
Romain Guy171c5922011-01-06 10:04:23 -080011998 }
11999
12000 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012001 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12002 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12003 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012004
Romain Guy0fd89bf2011-01-26 15:41:30 -080012005 invalidateParentCaches();
12006 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012007 }
12008
12009 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012010 * Indicates whether this view has a static layer. A view with layer type
12011 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12012 * dynamic.
12013 */
12014 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012015 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012016 }
12017
12018 /**
Romain Guy171c5922011-01-06 10:04:23 -080012019 * Indicates what type of layer is currently associated with this view. By default
12020 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12021 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12022 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012023 *
Romain Guy171c5922011-01-06 10:04:23 -080012024 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12025 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012026 *
12027 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012028 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012029 * @see #LAYER_TYPE_NONE
12030 * @see #LAYER_TYPE_SOFTWARE
12031 * @see #LAYER_TYPE_HARDWARE
12032 */
12033 public int getLayerType() {
12034 return mLayerType;
12035 }
Joe Malin32736f02011-01-19 16:14:20 -080012036
Romain Guy6c319ca2011-01-11 14:29:25 -080012037 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012038 * Forces this view's layer to be created and this view to be rendered
12039 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12040 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012041 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012042 * This method can for instance be used to render a view into its layer before
12043 * starting an animation. If this view is complex, rendering into the layer
12044 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012045 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012046 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012047 *
12048 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012049 */
12050 public void buildLayer() {
12051 if (mLayerType == LAYER_TYPE_NONE) return;
12052
12053 if (mAttachInfo == null) {
12054 throw new IllegalStateException("This view must be attached to a window first");
12055 }
12056
12057 switch (mLayerType) {
12058 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012059 if (mAttachInfo.mHardwareRenderer != null &&
12060 mAttachInfo.mHardwareRenderer.isEnabled() &&
12061 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012062 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012063 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012064 break;
12065 case LAYER_TYPE_SOFTWARE:
12066 buildDrawingCache(true);
12067 break;
12068 }
12069 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012070
Romain Guy9c4b79a2011-11-10 19:23:58 -080012071 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12072 void flushLayer() {
12073 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12074 mHardwareLayer.flush();
12075 }
12076 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012077
12078 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012079 * <p>Returns a hardware layer that can be used to draw this view again
12080 * without executing its draw method.</p>
12081 *
12082 * @return A HardwareLayer ready to render, or null if an error occurred.
12083 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012084 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012085 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12086 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012087 return null;
12088 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012089
Romain Guy9c4b79a2011-11-10 19:23:58 -080012090 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012091
12092 final int width = mRight - mLeft;
12093 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012094
Romain Guy6c319ca2011-01-11 14:29:25 -080012095 if (width == 0 || height == 0) {
12096 return null;
12097 }
12098
12099 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12100 if (mHardwareLayer == null) {
12101 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12102 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012103 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012104 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12105 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012106 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012107 }
12108
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012109 // The layer is not valid if the underlying GPU resources cannot be allocated
12110 if (!mHardwareLayer.isValid()) {
12111 return null;
12112 }
12113
Chet Haasea1cff502012-02-21 13:43:44 -080012114 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012115 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012116 }
12117
12118 return mHardwareLayer;
12119 }
Romain Guy171c5922011-01-06 10:04:23 -080012120
Romain Guy589b0bb2011-10-10 13:57:47 -070012121 /**
12122 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012123 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012124 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012125 *
12126 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012127 * @see #LAYER_TYPE_HARDWARE
12128 */
Romain Guya998dff2012-03-23 18:58:36 -070012129 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012130 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012131 AttachInfo info = mAttachInfo;
12132 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012133 info.mHardwareRenderer.isEnabled() &&
12134 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012135 mHardwareLayer.destroy();
12136 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012137
Romain Guy9c4b79a2011-11-10 19:23:58 -080012138 invalidate(true);
12139 invalidateParentCaches();
12140 }
Romain Guy65b345f2011-07-27 18:51:50 -070012141 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012142 }
Romain Guy65b345f2011-07-27 18:51:50 -070012143 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012144 }
12145
Romain Guy171c5922011-01-06 10:04:23 -080012146 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012147 * Destroys all hardware rendering resources. This method is invoked
12148 * when the system needs to reclaim resources. Upon execution of this
12149 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012150 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012151 * Note: you <strong>must</strong> call
12152 * <code>super.destroyHardwareResources()</code> when overriding
12153 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012154 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012155 * @hide
12156 */
12157 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012158 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012159 }
12160
12161 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12163 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12164 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12165 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12166 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12167 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012168 *
Romain Guy171c5922011-01-06 10:04:23 -080012169 * <p>Enabling the drawing cache is similar to
12170 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012171 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12172 * drawing cache has no effect on rendering because the system uses a different mechanism
12173 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12174 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12175 * for information on how to enable software and hardware layers.</p>
12176 *
12177 * <p>This API can be used to manually generate
12178 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12179 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 *
12181 * @param enabled true to enable the drawing cache, false otherwise
12182 *
12183 * @see #isDrawingCacheEnabled()
12184 * @see #getDrawingCache()
12185 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012186 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 */
12188 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012189 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12191 }
12192
12193 /**
12194 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12195 *
12196 * @return true if the drawing cache is enabled
12197 *
12198 * @see #setDrawingCacheEnabled(boolean)
12199 * @see #getDrawingCache()
12200 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012201 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012202 public boolean isDrawingCacheEnabled() {
12203 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12204 }
12205
12206 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012207 * Debugging utility which recursively outputs the dirty state of a view and its
12208 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012209 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012210 * @hide
12211 */
Romain Guy676b1732011-02-14 14:45:33 -080012212 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012213 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12214 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12215 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12216 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12217 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12218 if (clear) {
12219 mPrivateFlags &= clearMask;
12220 }
12221 if (this instanceof ViewGroup) {
12222 ViewGroup parent = (ViewGroup) this;
12223 final int count = parent.getChildCount();
12224 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012225 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012226 child.outputDirtyFlags(indent + " ", clear, clearMask);
12227 }
12228 }
12229 }
12230
12231 /**
12232 * This method is used by ViewGroup to cause its children to restore or recreate their
12233 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12234 * to recreate its own display list, which would happen if it went through the normal
12235 * draw/dispatchDraw mechanisms.
12236 *
12237 * @hide
12238 */
12239 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012240
12241 /**
12242 * A view that is not attached or hardware accelerated cannot create a display list.
12243 * This method checks these conditions and returns the appropriate result.
12244 *
12245 * @return true if view has the ability to create a display list, false otherwise.
12246 *
12247 * @hide
12248 */
12249 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012250 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012251 }
Joe Malin32736f02011-01-19 16:14:20 -080012252
Chet Haasedaf98e92011-01-10 14:10:36 -080012253 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012254 * @return The HardwareRenderer associated with that view or null if hardware rendering
12255 * is not supported or this this has not been attached to a window.
12256 *
12257 * @hide
12258 */
12259 public HardwareRenderer getHardwareRenderer() {
12260 if (mAttachInfo != null) {
12261 return mAttachInfo.mHardwareRenderer;
12262 }
12263 return null;
12264 }
12265
12266 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012267 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12268 * Otherwise, the same display list will be returned (after having been rendered into
12269 * along the way, depending on the invalidation state of the view).
12270 *
12271 * @param displayList The previous version of this displayList, could be null.
12272 * @param isLayer Whether the requester of the display list is a layer. If so,
12273 * the view will avoid creating a layer inside the resulting display list.
12274 * @return A new or reused DisplayList object.
12275 */
12276 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12277 if (!canHaveDisplayList()) {
12278 return null;
12279 }
12280
12281 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12282 displayList == null || !displayList.isValid() ||
12283 (!isLayer && mRecreateDisplayList))) {
12284 // Don't need to recreate the display list, just need to tell our
12285 // children to restore/recreate theirs
12286 if (displayList != null && displayList.isValid() &&
12287 !isLayer && !mRecreateDisplayList) {
12288 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12289 mPrivateFlags &= ~DIRTY_MASK;
12290 dispatchGetDisplayList();
12291
12292 return displayList;
12293 }
12294
12295 if (!isLayer) {
12296 // If we got here, we're recreating it. Mark it as such to ensure that
12297 // we copy in child display lists into ours in drawChild()
12298 mRecreateDisplayList = true;
12299 }
12300 if (displayList == null) {
12301 final String name = getClass().getSimpleName();
12302 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12303 // If we're creating a new display list, make sure our parent gets invalidated
12304 // since they will need to recreate their display list to account for this
12305 // new child display list.
12306 invalidateParentCaches();
12307 }
12308
12309 boolean caching = false;
12310 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012311 int width = mRight - mLeft;
12312 int height = mBottom - mTop;
12313
12314 try {
12315 canvas.setViewport(width, height);
12316 // The dirty rect should always be null for a display list
12317 canvas.onPreDraw(null);
12318 int layerType = (
12319 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12320 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012321 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012322 if (layerType == LAYER_TYPE_HARDWARE) {
12323 final HardwareLayer layer = getHardwareLayer();
12324 if (layer != null && layer.isValid()) {
12325 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12326 } else {
12327 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12328 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12329 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12330 }
12331 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012332 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012333 buildDrawingCache(true);
12334 Bitmap cache = getDrawingCache(true);
12335 if (cache != null) {
12336 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12337 caching = true;
12338 }
Chet Haasea1cff502012-02-21 13:43:44 -080012339 }
Chet Haasea1cff502012-02-21 13:43:44 -080012340 } else {
12341
12342 computeScroll();
12343
Chet Haasea1cff502012-02-21 13:43:44 -080012344 canvas.translate(-mScrollX, -mScrollY);
12345 if (!isLayer) {
12346 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12347 mPrivateFlags &= ~DIRTY_MASK;
12348 }
12349
12350 // Fast path for layouts with no backgrounds
12351 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12352 dispatchDraw(canvas);
12353 } else {
12354 draw(canvas);
12355 }
12356 }
12357 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012358 canvas.onPostDraw();
12359
12360 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012361 displayList.setCaching(caching);
12362 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012363 displayList.setLeftTopRightBottom(0, 0, width, height);
12364 } else {
12365 setDisplayListProperties(displayList);
12366 }
12367 }
12368 } else if (!isLayer) {
12369 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12370 mPrivateFlags &= ~DIRTY_MASK;
12371 }
12372
12373 return displayList;
12374 }
12375
12376 /**
12377 * Get the DisplayList for the HardwareLayer
12378 *
12379 * @param layer The HardwareLayer whose DisplayList we want
12380 * @return A DisplayList fopr the specified HardwareLayer
12381 */
12382 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12383 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12384 layer.setDisplayList(displayList);
12385 return displayList;
12386 }
12387
12388
12389 /**
Romain Guyb051e892010-09-28 19:09:36 -070012390 * <p>Returns a display list that can be used to draw this view again
12391 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012392 *
Romain Guyb051e892010-09-28 19:09:36 -070012393 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012394 *
12395 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012396 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012397 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012398 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012399 return mDisplayList;
12400 }
12401
Romain Guy38c2ece2012-05-24 14:20:56 -070012402 private void clearDisplayList() {
12403 if (mDisplayList != null) {
12404 mDisplayList.invalidate();
12405 mDisplayList.clear();
12406 }
12407 }
12408
Romain Guyb051e892010-09-28 19:09:36 -070012409 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012410 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012411 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012412 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012413 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012414 * @see #getDrawingCache(boolean)
12415 */
12416 public Bitmap getDrawingCache() {
12417 return getDrawingCache(false);
12418 }
12419
12420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12422 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12423 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12424 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12425 * request the drawing cache by calling this method and draw it on screen if the
12426 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012427 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012428 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12429 * this method will create a bitmap of the same size as this view. Because this bitmap
12430 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12431 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12432 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12433 * size than the view. This implies that your application must be able to handle this
12434 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012435 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012436 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12437 * the current density of the screen when the application is in compatibility
12438 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012439 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012440 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012442 * @see #setDrawingCacheEnabled(boolean)
12443 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012444 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 * @see #destroyDrawingCache()
12446 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012447 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012448 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12449 return null;
12450 }
12451 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012452 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012453 }
Romain Guy02890fd2010-08-06 17:58:44 -070012454 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012455 }
12456
12457 /**
12458 * <p>Frees the resources used by the drawing cache. If you call
12459 * {@link #buildDrawingCache()} manually without calling
12460 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12461 * should cleanup the cache with this method afterwards.</p>
12462 *
12463 * @see #setDrawingCacheEnabled(boolean)
12464 * @see #buildDrawingCache()
12465 * @see #getDrawingCache()
12466 */
12467 public void destroyDrawingCache() {
12468 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012469 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012470 mDrawingCache = null;
12471 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012472 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012473 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012474 mUnscaledDrawingCache = null;
12475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 }
12477
12478 /**
12479 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012480 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 * view will always be drawn on top of a solid color.
12482 *
12483 * @param color The background color to use for the drawing cache's bitmap
12484 *
12485 * @see #setDrawingCacheEnabled(boolean)
12486 * @see #buildDrawingCache()
12487 * @see #getDrawingCache()
12488 */
12489 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012490 if (color != mDrawingCacheBackgroundColor) {
12491 mDrawingCacheBackgroundColor = color;
12492 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 }
12495
12496 /**
12497 * @see #setDrawingCacheBackgroundColor(int)
12498 *
12499 * @return The background color to used for the drawing cache's bitmap
12500 */
12501 public int getDrawingCacheBackgroundColor() {
12502 return mDrawingCacheBackgroundColor;
12503 }
12504
12505 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012506 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012507 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012508 * @see #buildDrawingCache(boolean)
12509 */
12510 public void buildDrawingCache() {
12511 buildDrawingCache(false);
12512 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012513
Romain Guyfbd8f692009-06-26 14:51:58 -070012514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12516 *
12517 * <p>If you call {@link #buildDrawingCache()} manually without calling
12518 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12519 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012521 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12522 * this method will create a bitmap of the same size as this view. Because this bitmap
12523 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12524 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12525 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12526 * size than the view. This implies that your application must be able to handle this
12527 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012528 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012529 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12530 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012531 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012532 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012533 *
12534 * @see #getDrawingCache()
12535 * @see #destroyDrawingCache()
12536 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012537 public void buildDrawingCache(boolean autoScale) {
12538 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012539 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012540 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012541
Romain Guy8506ab42009-06-11 17:35:47 -070012542 int width = mRight - mLeft;
12543 int height = mBottom - mTop;
12544
12545 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012546 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012547
Romain Guye1123222009-06-29 14:24:56 -070012548 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012549 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12550 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012552
12553 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012554 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012555 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012556
12557 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012558 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012559 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12561 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012562 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012563 return;
12564 }
12565
12566 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012567 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012568
12569 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012570 Bitmap.Config quality;
12571 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012572 // Never pick ARGB_4444 because it looks awful
12573 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012574 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12575 case DRAWING_CACHE_QUALITY_AUTO:
12576 quality = Bitmap.Config.ARGB_8888;
12577 break;
12578 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012579 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 break;
12581 case DRAWING_CACHE_QUALITY_HIGH:
12582 quality = Bitmap.Config.ARGB_8888;
12583 break;
12584 default:
12585 quality = Bitmap.Config.ARGB_8888;
12586 break;
12587 }
12588 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012589 // Optimization for translucent windows
12590 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012591 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592 }
12593
12594 // Try to cleanup memory
12595 if (bitmap != null) bitmap.recycle();
12596
12597 try {
12598 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012599 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012600 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012601 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012602 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012603 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012604 }
Adam Powell26153a32010-11-08 15:22:27 -080012605 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012606 } catch (OutOfMemoryError e) {
12607 // If there is not enough memory to create the bitmap cache, just
12608 // ignore the issue as bitmap caches are not required to draw the
12609 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012610 if (autoScale) {
12611 mDrawingCache = null;
12612 } else {
12613 mUnscaledDrawingCache = null;
12614 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012615 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012616 return;
12617 }
12618
12619 clear = drawingCacheBackgroundColor != 0;
12620 }
12621
12622 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012623 if (attachInfo != null) {
12624 canvas = attachInfo.mCanvas;
12625 if (canvas == null) {
12626 canvas = new Canvas();
12627 }
12628 canvas.setBitmap(bitmap);
12629 // Temporarily clobber the cached Canvas in case one of our children
12630 // is also using a drawing cache. Without this, the children would
12631 // steal the canvas by attaching their own bitmap to it and bad, bad
12632 // thing would happen (invisible views, corrupted drawings, etc.)
12633 attachInfo.mCanvas = null;
12634 } else {
12635 // This case should hopefully never or seldom happen
12636 canvas = new Canvas(bitmap);
12637 }
12638
12639 if (clear) {
12640 bitmap.eraseColor(drawingCacheBackgroundColor);
12641 }
12642
12643 computeScroll();
12644 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012645
Romain Guye1123222009-06-29 14:24:56 -070012646 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012647 final float scale = attachInfo.mApplicationScale;
12648 canvas.scale(scale, scale);
12649 }
Joe Malin32736f02011-01-19 16:14:20 -080012650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012651 canvas.translate(-mScrollX, -mScrollY);
12652
Romain Guy5bcdff42009-05-14 21:27:18 -070012653 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012654 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12655 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012656 mPrivateFlags |= DRAWING_CACHE_VALID;
12657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658
12659 // Fast path for layouts with no backgrounds
12660 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012661 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012662 dispatchDraw(canvas);
12663 } else {
12664 draw(canvas);
12665 }
12666
12667 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012668 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012669
12670 if (attachInfo != null) {
12671 // Restore the cached Canvas for our siblings
12672 attachInfo.mCanvas = canvas;
12673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012674 }
12675 }
12676
12677 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012678 * Create a snapshot of the view into a bitmap. We should probably make
12679 * some form of this public, but should think about the API.
12680 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012681 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012682 int width = mRight - mLeft;
12683 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012684
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012685 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012686 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012687 width = (int) ((width * scale) + 0.5f);
12688 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012689
Romain Guy8c11e312009-09-14 15:15:30 -070012690 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012691 if (bitmap == null) {
12692 throw new OutOfMemoryError();
12693 }
12694
Romain Guyc529d8d2011-09-06 15:01:39 -070012695 Resources resources = getResources();
12696 if (resources != null) {
12697 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12698 }
Joe Malin32736f02011-01-19 16:14:20 -080012699
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012700 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012701 if (attachInfo != null) {
12702 canvas = attachInfo.mCanvas;
12703 if (canvas == null) {
12704 canvas = new Canvas();
12705 }
12706 canvas.setBitmap(bitmap);
12707 // Temporarily clobber the cached Canvas in case one of our children
12708 // is also using a drawing cache. Without this, the children would
12709 // steal the canvas by attaching their own bitmap to it and bad, bad
12710 // things would happen (invisible views, corrupted drawings, etc.)
12711 attachInfo.mCanvas = null;
12712 } else {
12713 // This case should hopefully never or seldom happen
12714 canvas = new Canvas(bitmap);
12715 }
12716
Romain Guy5bcdff42009-05-14 21:27:18 -070012717 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012718 bitmap.eraseColor(backgroundColor);
12719 }
12720
12721 computeScroll();
12722 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012723 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012724 canvas.translate(-mScrollX, -mScrollY);
12725
Romain Guy5bcdff42009-05-14 21:27:18 -070012726 // Temporarily remove the dirty mask
12727 int flags = mPrivateFlags;
12728 mPrivateFlags &= ~DIRTY_MASK;
12729
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012730 // Fast path for layouts with no backgrounds
12731 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12732 dispatchDraw(canvas);
12733 } else {
12734 draw(canvas);
12735 }
12736
Romain Guy5bcdff42009-05-14 21:27:18 -070012737 mPrivateFlags = flags;
12738
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012739 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012740 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012741
12742 if (attachInfo != null) {
12743 // Restore the cached Canvas for our siblings
12744 attachInfo.mCanvas = canvas;
12745 }
Romain Guy8506ab42009-06-11 17:35:47 -070012746
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012747 return bitmap;
12748 }
12749
12750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 * Indicates whether this View is currently in edit mode. A View is usually
12752 * in edit mode when displayed within a developer tool. For instance, if
12753 * this View is being drawn by a visual user interface builder, this method
12754 * should return true.
12755 *
12756 * Subclasses should check the return value of this method to provide
12757 * different behaviors if their normal behavior might interfere with the
12758 * host environment. For instance: the class spawns a thread in its
12759 * constructor, the drawing code relies on device-specific features, etc.
12760 *
12761 * This method is usually checked in the drawing code of custom widgets.
12762 *
12763 * @return True if this View is in edit mode, false otherwise.
12764 */
12765 public boolean isInEditMode() {
12766 return false;
12767 }
12768
12769 /**
12770 * If the View draws content inside its padding and enables fading edges,
12771 * it needs to support padding offsets. Padding offsets are added to the
12772 * fading edges to extend the length of the fade so that it covers pixels
12773 * drawn inside the padding.
12774 *
12775 * Subclasses of this class should override this method if they need
12776 * to draw content inside the padding.
12777 *
12778 * @return True if padding offset must be applied, false otherwise.
12779 *
12780 * @see #getLeftPaddingOffset()
12781 * @see #getRightPaddingOffset()
12782 * @see #getTopPaddingOffset()
12783 * @see #getBottomPaddingOffset()
12784 *
12785 * @since CURRENT
12786 */
12787 protected boolean isPaddingOffsetRequired() {
12788 return false;
12789 }
12790
12791 /**
12792 * Amount by which to extend the left fading region. Called only when
12793 * {@link #isPaddingOffsetRequired()} returns true.
12794 *
12795 * @return The left padding offset in pixels.
12796 *
12797 * @see #isPaddingOffsetRequired()
12798 *
12799 * @since CURRENT
12800 */
12801 protected int getLeftPaddingOffset() {
12802 return 0;
12803 }
12804
12805 /**
12806 * Amount by which to extend the right fading region. Called only when
12807 * {@link #isPaddingOffsetRequired()} returns true.
12808 *
12809 * @return The right padding offset in pixels.
12810 *
12811 * @see #isPaddingOffsetRequired()
12812 *
12813 * @since CURRENT
12814 */
12815 protected int getRightPaddingOffset() {
12816 return 0;
12817 }
12818
12819 /**
12820 * Amount by which to extend the top fading region. Called only when
12821 * {@link #isPaddingOffsetRequired()} returns true.
12822 *
12823 * @return The top padding offset in pixels.
12824 *
12825 * @see #isPaddingOffsetRequired()
12826 *
12827 * @since CURRENT
12828 */
12829 protected int getTopPaddingOffset() {
12830 return 0;
12831 }
12832
12833 /**
12834 * Amount by which to extend the bottom fading region. Called only when
12835 * {@link #isPaddingOffsetRequired()} returns true.
12836 *
12837 * @return The bottom padding offset in pixels.
12838 *
12839 * @see #isPaddingOffsetRequired()
12840 *
12841 * @since CURRENT
12842 */
12843 protected int getBottomPaddingOffset() {
12844 return 0;
12845 }
12846
12847 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012848 * @hide
12849 * @param offsetRequired
12850 */
12851 protected int getFadeTop(boolean offsetRequired) {
12852 int top = mPaddingTop;
12853 if (offsetRequired) top += getTopPaddingOffset();
12854 return top;
12855 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012856
Romain Guyf2fc4602011-07-19 15:20:03 -070012857 /**
12858 * @hide
12859 * @param offsetRequired
12860 */
12861 protected int getFadeHeight(boolean offsetRequired) {
12862 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012863 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012864 return mBottom - mTop - mPaddingBottom - padding;
12865 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012866
Romain Guyf2fc4602011-07-19 15:20:03 -070012867 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012868 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012869 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012870 *
Romain Guy2bffd262010-09-12 17:40:02 -070012871 * <p>Even if this method returns true, it does not mean that every call
12872 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12873 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012874 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012875 * window is hardware accelerated,
12876 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12877 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012878 *
Romain Guy2bffd262010-09-12 17:40:02 -070012879 * @return True if the view is attached to a window and the window is
12880 * hardware accelerated; false in any other case.
12881 */
12882 public boolean isHardwareAccelerated() {
12883 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12884 }
Joe Malin32736f02011-01-19 16:14:20 -080012885
Romain Guy2bffd262010-09-12 17:40:02 -070012886 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012887 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12888 * case of an active Animation being run on the view.
12889 */
12890 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12891 Animation a, boolean scalingRequired) {
12892 Transformation invalidationTransform;
12893 final int flags = parent.mGroupFlags;
12894 final boolean initialized = a.isInitialized();
12895 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012896 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012897 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012898 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012899 onAnimationStart();
12900 }
12901
12902 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12903 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12904 if (parent.mInvalidationTransformation == null) {
12905 parent.mInvalidationTransformation = new Transformation();
12906 }
12907 invalidationTransform = parent.mInvalidationTransformation;
12908 a.getTransformation(drawingTime, invalidationTransform, 1f);
12909 } else {
12910 invalidationTransform = parent.mChildTransformation;
12911 }
Romain Guy393a52c2012-05-22 20:21:08 -070012912
Chet Haasebcca79a2012-02-14 08:45:14 -080012913 if (more) {
12914 if (!a.willChangeBounds()) {
12915 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12916 parent.FLAG_OPTIMIZE_INVALIDATE) {
12917 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12918 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12919 // The child need to draw an animation, potentially offscreen, so
12920 // make sure we do not cancel invalidate requests
12921 parent.mPrivateFlags |= DRAW_ANIMATION;
12922 parent.invalidate(mLeft, mTop, mRight, mBottom);
12923 }
12924 } else {
12925 if (parent.mInvalidateRegion == null) {
12926 parent.mInvalidateRegion = new RectF();
12927 }
12928 final RectF region = parent.mInvalidateRegion;
12929 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12930 invalidationTransform);
12931
12932 // The child need to draw an animation, potentially offscreen, so
12933 // make sure we do not cancel invalidate requests
12934 parent.mPrivateFlags |= DRAW_ANIMATION;
12935
12936 final int left = mLeft + (int) region.left;
12937 final int top = mTop + (int) region.top;
12938 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12939 top + (int) (region.height() + .5f));
12940 }
12941 }
12942 return more;
12943 }
12944
Chet Haasea1cff502012-02-21 13:43:44 -080012945 /**
12946 * This method is called by getDisplayList() when a display list is created or re-rendered.
12947 * It sets or resets the current value of all properties on that display list (resetting is
12948 * necessary when a display list is being re-created, because we need to make sure that
12949 * previously-set transform values
12950 */
12951 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012952 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012953 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012954 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012955 if (mParent instanceof ViewGroup) {
12956 displayList.setClipChildren(
12957 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12958 }
Chet Haase9420abd2012-03-29 16:28:32 -070012959 float alpha = 1;
12960 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12961 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12962 ViewGroup parentVG = (ViewGroup) mParent;
12963 final boolean hasTransform =
12964 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12965 if (hasTransform) {
12966 Transformation transform = parentVG.mChildTransformation;
12967 final int transformType = parentVG.mChildTransformation.getTransformationType();
12968 if (transformType != Transformation.TYPE_IDENTITY) {
12969 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12970 alpha = transform.getAlpha();
12971 }
12972 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12973 displayList.setStaticMatrix(transform.getMatrix());
12974 }
12975 }
12976 }
Chet Haasea1cff502012-02-21 13:43:44 -080012977 }
12978 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012979 alpha *= mTransformationInfo.mAlpha;
12980 if (alpha < 1) {
12981 final int multipliedAlpha = (int) (255 * alpha);
12982 if (onSetAlpha(multipliedAlpha)) {
12983 alpha = 1;
12984 }
12985 }
12986 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012987 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12988 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12989 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12990 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012991 if (mTransformationInfo.mCamera == null) {
12992 mTransformationInfo.mCamera = new Camera();
12993 mTransformationInfo.matrix3D = new Matrix();
12994 }
12995 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012996 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12997 displayList.setPivotX(getPivotX());
12998 displayList.setPivotY(getPivotY());
12999 }
Chet Haase9420abd2012-03-29 16:28:32 -070013000 } else if (alpha < 1) {
13001 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013002 }
13003 }
13004 }
13005
Chet Haasebcca79a2012-02-14 08:45:14 -080013006 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013007 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13008 * This draw() method is an implementation detail and is not intended to be overridden or
13009 * to be called from anywhere else other than ViewGroup.drawChild().
13010 */
13011 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013012 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013013 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013014 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013015 final int flags = parent.mGroupFlags;
13016
Chet Haasea1cff502012-02-21 13:43:44 -080013017 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013018 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013019 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013020 }
13021
13022 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013023 boolean concatMatrix = false;
13024
13025 boolean scalingRequired = false;
13026 boolean caching;
13027 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
13028
13029 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013030 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13031 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013032 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013033 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013034 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13035 } else {
13036 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13037 }
13038
Chet Haasebcca79a2012-02-14 08:45:14 -080013039 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013040 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013041 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013042 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013043 if (concatMatrix) {
13044 mPrivateFlags2 |= VIEW_IS_ANIMATING_TRANSFORM;
13045 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013046 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013047 } else {
13048 if ((mPrivateFlags2 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
13049 mDisplayList != null) {
13050 // No longer animating: clear out old animation matrix
13051 mDisplayList.setAnimationMatrix(null);
13052 mPrivateFlags2 &= ~VIEW_IS_ANIMATING_TRANSFORM;
13053 }
13054 if (!useDisplayListProperties &&
13055 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13056 final boolean hasTransform =
13057 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13058 if (hasTransform) {
13059 final int transformType = parent.mChildTransformation.getTransformationType();
13060 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13061 parent.mChildTransformation : null;
13062 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13063 }
Chet Haase64a48c12012-02-13 16:33:29 -080013064 }
13065 }
13066
13067 concatMatrix |= !childHasIdentityMatrix;
13068
13069 // Sets the flag as early as possible to allow draw() implementations
13070 // to call invalidate() successfully when doing animations
13071 mPrivateFlags |= DRAWN;
13072
Chet Haasebcca79a2012-02-14 08:45:14 -080013073 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013074 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013075 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013076 return more;
13077 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013078 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013079
13080 if (hardwareAccelerated) {
13081 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13082 // retain the flag's value temporarily in the mRecreateDisplayList flag
13083 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13084 mPrivateFlags &= ~INVALIDATED;
13085 }
13086
13087 computeScroll();
13088
13089 final int sx = mScrollX;
13090 final int sy = mScrollY;
13091
13092 DisplayList displayList = null;
13093 Bitmap cache = null;
13094 boolean hasDisplayList = false;
13095 if (caching) {
13096 if (!hardwareAccelerated) {
13097 if (layerType != LAYER_TYPE_NONE) {
13098 layerType = LAYER_TYPE_SOFTWARE;
13099 buildDrawingCache(true);
13100 }
13101 cache = getDrawingCache(true);
13102 } else {
13103 switch (layerType) {
13104 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013105 if (useDisplayListProperties) {
13106 hasDisplayList = canHaveDisplayList();
13107 } else {
13108 buildDrawingCache(true);
13109 cache = getDrawingCache(true);
13110 }
Chet Haase64a48c12012-02-13 16:33:29 -080013111 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013112 case LAYER_TYPE_HARDWARE:
13113 if (useDisplayListProperties) {
13114 hasDisplayList = canHaveDisplayList();
13115 }
13116 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013117 case LAYER_TYPE_NONE:
13118 // Delay getting the display list until animation-driven alpha values are
13119 // set up and possibly passed on to the view
13120 hasDisplayList = canHaveDisplayList();
13121 break;
13122 }
13123 }
13124 }
Chet Haasea1cff502012-02-21 13:43:44 -080013125 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013126 if (useDisplayListProperties) {
13127 displayList = getDisplayList();
13128 if (!displayList.isValid()) {
13129 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13130 // to getDisplayList(), the display list will be marked invalid and we should not
13131 // try to use it again.
13132 displayList = null;
13133 hasDisplayList = false;
13134 useDisplayListProperties = false;
13135 }
13136 }
Chet Haase64a48c12012-02-13 16:33:29 -080013137
13138 final boolean hasNoCache = cache == null || hasDisplayList;
13139 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13140 layerType != LAYER_TYPE_HARDWARE;
13141
Chet Haasea1cff502012-02-21 13:43:44 -080013142 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013143 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013144 restoreTo = canvas.save();
13145 }
Chet Haase64a48c12012-02-13 16:33:29 -080013146 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013147 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013148 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013149 if (!useDisplayListProperties) {
13150 canvas.translate(mLeft, mTop);
13151 }
Chet Haase64a48c12012-02-13 16:33:29 -080013152 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013153 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013154 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013155 restoreTo = canvas.save();
13156 }
Chet Haase64a48c12012-02-13 16:33:29 -080013157 // mAttachInfo cannot be null, otherwise scalingRequired == false
13158 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13159 canvas.scale(scale, scale);
13160 }
13161 }
13162
Chet Haasea1cff502012-02-21 13:43:44 -080013163 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013164 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080013165 if (transformToApply != null || !childHasIdentityMatrix) {
13166 int transX = 0;
13167 int transY = 0;
13168
13169 if (offsetForScroll) {
13170 transX = -sx;
13171 transY = -sy;
13172 }
13173
13174 if (transformToApply != null) {
13175 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013176 if (useDisplayListProperties) {
13177 displayList.setAnimationMatrix(transformToApply.getMatrix());
13178 } else {
13179 // Undo the scroll translation, apply the transformation matrix,
13180 // then redo the scroll translate to get the correct result.
13181 canvas.translate(-transX, -transY);
13182 canvas.concat(transformToApply.getMatrix());
13183 canvas.translate(transX, transY);
13184 }
Chet Haasea1cff502012-02-21 13:43:44 -080013185 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013186 }
13187
13188 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013189 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013190 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080013191 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013192 }
13193 }
13194
Chet Haasea1cff502012-02-21 13:43:44 -080013195 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013196 canvas.translate(-transX, -transY);
13197 canvas.concat(getMatrix());
13198 canvas.translate(transX, transY);
13199 }
13200 }
13201
Chet Haase9420abd2012-03-29 16:28:32 -070013202 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080013203 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013204 if (hasNoCache) {
13205 final int multipliedAlpha = (int) (255 * alpha);
13206 if (!onSetAlpha(multipliedAlpha)) {
13207 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013208 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013209 layerType != LAYER_TYPE_NONE) {
13210 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13211 }
Chet Haase9420abd2012-03-29 16:28:32 -070013212 if (useDisplayListProperties) {
13213 displayList.setAlpha(alpha * getAlpha());
13214 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013215 final int scrollX = hasDisplayList ? 0 : sx;
13216 final int scrollY = hasDisplayList ? 0 : sy;
13217 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13218 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013219 }
13220 } else {
13221 // Alpha is handled by the child directly, clobber the layer's alpha
13222 mPrivateFlags |= ALPHA_SET;
13223 }
13224 }
13225 }
13226 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13227 onSetAlpha(255);
13228 mPrivateFlags &= ~ALPHA_SET;
13229 }
13230
Chet Haasea1cff502012-02-21 13:43:44 -080013231 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13232 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013233 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013234 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013235 } else {
13236 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013237 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013238 } else {
13239 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13240 }
13241 }
13242 }
13243
Chet Haase9420abd2012-03-29 16:28:32 -070013244 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013245 displayList = getDisplayList();
13246 if (!displayList.isValid()) {
13247 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13248 // to getDisplayList(), the display list will be marked invalid and we should not
13249 // try to use it again.
13250 displayList = null;
13251 hasDisplayList = false;
13252 }
13253 }
13254
13255 if (hasNoCache) {
13256 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013257 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013258 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013259 if (layer != null && layer.isValid()) {
13260 mLayerPaint.setAlpha((int) (alpha * 255));
13261 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13262 layerRendered = true;
13263 } else {
13264 final int scrollX = hasDisplayList ? 0 : sx;
13265 final int scrollY = hasDisplayList ? 0 : sy;
13266 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013267 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013268 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13269 }
13270 }
13271
13272 if (!layerRendered) {
13273 if (!hasDisplayList) {
13274 // Fast path for layouts with no backgrounds
13275 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013276 mPrivateFlags &= ~DIRTY_MASK;
13277 dispatchDraw(canvas);
13278 } else {
13279 draw(canvas);
13280 }
13281 } else {
13282 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013283 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013284 }
13285 }
13286 } else if (cache != null) {
13287 mPrivateFlags &= ~DIRTY_MASK;
13288 Paint cachePaint;
13289
13290 if (layerType == LAYER_TYPE_NONE) {
13291 cachePaint = parent.mCachePaint;
13292 if (cachePaint == null) {
13293 cachePaint = new Paint();
13294 cachePaint.setDither(false);
13295 parent.mCachePaint = cachePaint;
13296 }
Chet Haase9420abd2012-03-29 16:28:32 -070013297 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013298 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013299 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13300 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013301 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013302 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013303 }
13304 } else {
13305 cachePaint = mLayerPaint;
13306 cachePaint.setAlpha((int) (alpha * 255));
13307 }
13308 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13309 }
13310
Chet Haasea1cff502012-02-21 13:43:44 -080013311 if (restoreTo >= 0) {
13312 canvas.restoreToCount(restoreTo);
13313 }
Chet Haase64a48c12012-02-13 16:33:29 -080013314
13315 if (a != null && !more) {
13316 if (!hardwareAccelerated && !a.getFillAfter()) {
13317 onSetAlpha(255);
13318 }
13319 parent.finishAnimatingView(this, a);
13320 }
13321
13322 if (more && hardwareAccelerated) {
13323 // invalidation is the trigger to recreate display lists, so if we're using
13324 // display lists to render, force an invalidate to allow the animation to
13325 // continue drawing another frame
13326 parent.invalidate(true);
13327 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13328 // alpha animations should cause the child to recreate its display list
13329 invalidate(true);
13330 }
13331 }
13332
13333 mRecreateDisplayList = false;
13334
13335 return more;
13336 }
13337
13338 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013339 * Manually render this view (and all of its children) to the given Canvas.
13340 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013341 * called. When implementing a view, implement
13342 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13343 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013344 *
13345 * @param canvas The Canvas to which the View is rendered.
13346 */
13347 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013348 final int privateFlags = mPrivateFlags;
13349 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13350 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13351 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013353 /*
13354 * Draw traversal performs several drawing steps which must be executed
13355 * in the appropriate order:
13356 *
13357 * 1. Draw the background
13358 * 2. If necessary, save the canvas' layers to prepare for fading
13359 * 3. Draw view's content
13360 * 4. Draw children
13361 * 5. If necessary, draw the fading edges and restore layers
13362 * 6. Draw decorations (scrollbars for instance)
13363 */
13364
13365 // Step 1, draw the background, if needed
13366 int saveCount;
13367
Romain Guy24443ea2009-05-11 11:56:30 -070013368 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013369 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013370 if (background != null) {
13371 final int scrollX = mScrollX;
13372 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013373
Romain Guy24443ea2009-05-11 11:56:30 -070013374 if (mBackgroundSizeChanged) {
13375 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13376 mBackgroundSizeChanged = false;
13377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013378
Romain Guy24443ea2009-05-11 11:56:30 -070013379 if ((scrollX | scrollY) == 0) {
13380 background.draw(canvas);
13381 } else {
13382 canvas.translate(scrollX, scrollY);
13383 background.draw(canvas);
13384 canvas.translate(-scrollX, -scrollY);
13385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013386 }
13387 }
13388
13389 // skip step 2 & 5 if possible (common case)
13390 final int viewFlags = mViewFlags;
13391 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13392 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13393 if (!verticalEdges && !horizontalEdges) {
13394 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013395 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013396
13397 // Step 4, draw the children
13398 dispatchDraw(canvas);
13399
13400 // Step 6, draw decorations (scrollbars)
13401 onDrawScrollBars(canvas);
13402
13403 // we're done...
13404 return;
13405 }
13406
13407 /*
13408 * Here we do the full fledged routine...
13409 * (this is an uncommon case where speed matters less,
13410 * this is why we repeat some of the tests that have been
13411 * done above)
13412 */
13413
13414 boolean drawTop = false;
13415 boolean drawBottom = false;
13416 boolean drawLeft = false;
13417 boolean drawRight = false;
13418
13419 float topFadeStrength = 0.0f;
13420 float bottomFadeStrength = 0.0f;
13421 float leftFadeStrength = 0.0f;
13422 float rightFadeStrength = 0.0f;
13423
13424 // Step 2, save the canvas' layers
13425 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013426
13427 final boolean offsetRequired = isPaddingOffsetRequired();
13428 if (offsetRequired) {
13429 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013430 }
13431
13432 int left = mScrollX + paddingLeft;
13433 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013434 int top = mScrollY + getFadeTop(offsetRequired);
13435 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013436
13437 if (offsetRequired) {
13438 right += getRightPaddingOffset();
13439 bottom += getBottomPaddingOffset();
13440 }
13441
13442 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013443 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013444 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013445
13446 // clip the fade length if top and bottom fades overlap
13447 // overlapping fades produce odd-looking artifacts
13448 if (verticalEdges && (top + length > bottom - length)) {
13449 length = (bottom - top) / 2;
13450 }
13451
13452 // also clip horizontal fades if necessary
13453 if (horizontalEdges && (left + length > right - length)) {
13454 length = (right - left) / 2;
13455 }
13456
13457 if (verticalEdges) {
13458 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013459 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013460 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013461 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462 }
13463
13464 if (horizontalEdges) {
13465 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013466 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013467 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013468 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013469 }
13470
13471 saveCount = canvas.getSaveCount();
13472
13473 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013474 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13476
13477 if (drawTop) {
13478 canvas.saveLayer(left, top, right, top + length, null, flags);
13479 }
13480
13481 if (drawBottom) {
13482 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13483 }
13484
13485 if (drawLeft) {
13486 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13487 }
13488
13489 if (drawRight) {
13490 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13491 }
13492 } else {
13493 scrollabilityCache.setFadeColor(solidColor);
13494 }
13495
13496 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013497 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013498
13499 // Step 4, draw the children
13500 dispatchDraw(canvas);
13501
13502 // Step 5, draw the fade effect and restore layers
13503 final Paint p = scrollabilityCache.paint;
13504 final Matrix matrix = scrollabilityCache.matrix;
13505 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013506
13507 if (drawTop) {
13508 matrix.setScale(1, fadeHeight * topFadeStrength);
13509 matrix.postTranslate(left, top);
13510 fade.setLocalMatrix(matrix);
13511 canvas.drawRect(left, top, right, top + length, p);
13512 }
13513
13514 if (drawBottom) {
13515 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13516 matrix.postRotate(180);
13517 matrix.postTranslate(left, bottom);
13518 fade.setLocalMatrix(matrix);
13519 canvas.drawRect(left, bottom - length, right, bottom, p);
13520 }
13521
13522 if (drawLeft) {
13523 matrix.setScale(1, fadeHeight * leftFadeStrength);
13524 matrix.postRotate(-90);
13525 matrix.postTranslate(left, top);
13526 fade.setLocalMatrix(matrix);
13527 canvas.drawRect(left, top, left + length, bottom, p);
13528 }
13529
13530 if (drawRight) {
13531 matrix.setScale(1, fadeHeight * rightFadeStrength);
13532 matrix.postRotate(90);
13533 matrix.postTranslate(right, top);
13534 fade.setLocalMatrix(matrix);
13535 canvas.drawRect(right - length, top, right, bottom, p);
13536 }
13537
13538 canvas.restoreToCount(saveCount);
13539
13540 // Step 6, draw decorations (scrollbars)
13541 onDrawScrollBars(canvas);
13542 }
13543
13544 /**
13545 * Override this if your view is known to always be drawn on top of a solid color background,
13546 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13547 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13548 * should be set to 0xFF.
13549 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013550 * @see #setVerticalFadingEdgeEnabled(boolean)
13551 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013552 *
13553 * @return The known solid color background for this view, or 0 if the color may vary
13554 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013555 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013556 public int getSolidColor() {
13557 return 0;
13558 }
13559
13560 /**
13561 * Build a human readable string representation of the specified view flags.
13562 *
13563 * @param flags the view flags to convert to a string
13564 * @return a String representing the supplied flags
13565 */
13566 private static String printFlags(int flags) {
13567 String output = "";
13568 int numFlags = 0;
13569 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13570 output += "TAKES_FOCUS";
13571 numFlags++;
13572 }
13573
13574 switch (flags & VISIBILITY_MASK) {
13575 case INVISIBLE:
13576 if (numFlags > 0) {
13577 output += " ";
13578 }
13579 output += "INVISIBLE";
13580 // USELESS HERE numFlags++;
13581 break;
13582 case GONE:
13583 if (numFlags > 0) {
13584 output += " ";
13585 }
13586 output += "GONE";
13587 // USELESS HERE numFlags++;
13588 break;
13589 default:
13590 break;
13591 }
13592 return output;
13593 }
13594
13595 /**
13596 * Build a human readable string representation of the specified private
13597 * view flags.
13598 *
13599 * @param privateFlags the private view flags to convert to a string
13600 * @return a String representing the supplied flags
13601 */
13602 private static String printPrivateFlags(int privateFlags) {
13603 String output = "";
13604 int numFlags = 0;
13605
13606 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13607 output += "WANTS_FOCUS";
13608 numFlags++;
13609 }
13610
13611 if ((privateFlags & FOCUSED) == FOCUSED) {
13612 if (numFlags > 0) {
13613 output += " ";
13614 }
13615 output += "FOCUSED";
13616 numFlags++;
13617 }
13618
13619 if ((privateFlags & SELECTED) == SELECTED) {
13620 if (numFlags > 0) {
13621 output += " ";
13622 }
13623 output += "SELECTED";
13624 numFlags++;
13625 }
13626
13627 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13628 if (numFlags > 0) {
13629 output += " ";
13630 }
13631 output += "IS_ROOT_NAMESPACE";
13632 numFlags++;
13633 }
13634
13635 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13636 if (numFlags > 0) {
13637 output += " ";
13638 }
13639 output += "HAS_BOUNDS";
13640 numFlags++;
13641 }
13642
13643 if ((privateFlags & DRAWN) == DRAWN) {
13644 if (numFlags > 0) {
13645 output += " ";
13646 }
13647 output += "DRAWN";
13648 // USELESS HERE numFlags++;
13649 }
13650 return output;
13651 }
13652
13653 /**
13654 * <p>Indicates whether or not this view's layout will be requested during
13655 * the next hierarchy layout pass.</p>
13656 *
13657 * @return true if the layout will be forced during next layout pass
13658 */
13659 public boolean isLayoutRequested() {
13660 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13661 }
13662
13663 /**
13664 * Assign a size and position to a view and all of its
13665 * descendants
13666 *
13667 * <p>This is the second phase of the layout mechanism.
13668 * (The first is measuring). In this phase, each parent calls
13669 * layout on all of its children to position them.
13670 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013671 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 *
Chet Haase9c087442011-01-12 16:20:16 -080013673 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674 * Derived classes with children should override
13675 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013676 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013677 *
13678 * @param l Left position, relative to parent
13679 * @param t Top position, relative to parent
13680 * @param r Right position, relative to parent
13681 * @param b Bottom position, relative to parent
13682 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013683 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013684 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013685 int oldL = mLeft;
13686 int oldT = mTop;
13687 int oldB = mBottom;
13688 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013689 boolean changed = setFrame(l, t, r, b);
13690 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013691 onLayout(changed, l, t, r, b);
13692 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013693
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013694 ListenerInfo li = mListenerInfo;
13695 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013696 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013697 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013698 int numListeners = listenersCopy.size();
13699 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013700 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013701 }
13702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013703 }
13704 mPrivateFlags &= ~FORCE_LAYOUT;
13705 }
13706
13707 /**
13708 * Called from layout when this view should
13709 * assign a size and position to each of its children.
13710 *
13711 * Derived classes with children should override
13712 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013713 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714 * @param changed This is a new size or position for this view
13715 * @param left Left position, relative to parent
13716 * @param top Top position, relative to parent
13717 * @param right Right position, relative to parent
13718 * @param bottom Bottom position, relative to parent
13719 */
13720 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13721 }
13722
13723 /**
13724 * Assign a size and position to this view.
13725 *
13726 * This is called from layout.
13727 *
13728 * @param left Left position, relative to parent
13729 * @param top Top position, relative to parent
13730 * @param right Right position, relative to parent
13731 * @param bottom Bottom position, relative to parent
13732 * @return true if the new size and position are different than the
13733 * previous ones
13734 * {@hide}
13735 */
13736 protected boolean setFrame(int left, int top, int right, int bottom) {
13737 boolean changed = false;
13738
13739 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013740 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013741 + right + "," + bottom + ")");
13742 }
13743
13744 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13745 changed = true;
13746
13747 // Remember our drawn bit
13748 int drawn = mPrivateFlags & DRAWN;
13749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750 int oldWidth = mRight - mLeft;
13751 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013752 int newWidth = right - left;
13753 int newHeight = bottom - top;
13754 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13755
13756 // Invalidate our old position
13757 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758
13759 mLeft = left;
13760 mTop = top;
13761 mRight = right;
13762 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013763 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013764 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766
13767 mPrivateFlags |= HAS_BOUNDS;
13768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769
Chet Haase75755e22011-07-18 17:48:25 -070013770 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013771 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13772 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013773 if (mTransformationInfo != null) {
13774 mTransformationInfo.mMatrixDirty = true;
13775 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013777 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13778 }
13779
13780 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13781 // If we are visible, force the DRAWN bit to on so that
13782 // this invalidate will go through (at least to our parent).
13783 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013784 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013785 // the DRAWN bit.
13786 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013787 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013788 // parent display list may need to be recreated based on a change in the bounds
13789 // of any child
13790 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013791 }
13792
13793 // Reset drawn bit to original value (invalidate turns it off)
13794 mPrivateFlags |= drawn;
13795
13796 mBackgroundSizeChanged = true;
13797 }
13798 return changed;
13799 }
13800
13801 /**
13802 * Finalize inflating a view from XML. This is called as the last phase
13803 * of inflation, after all child views have been added.
13804 *
13805 * <p>Even if the subclass overrides onFinishInflate, they should always be
13806 * sure to call the super method, so that we get called.
13807 */
13808 protected void onFinishInflate() {
13809 }
13810
13811 /**
13812 * Returns the resources associated with this view.
13813 *
13814 * @return Resources object.
13815 */
13816 public Resources getResources() {
13817 return mResources;
13818 }
13819
13820 /**
13821 * Invalidates the specified Drawable.
13822 *
13823 * @param drawable the drawable to invalidate
13824 */
13825 public void invalidateDrawable(Drawable drawable) {
13826 if (verifyDrawable(drawable)) {
13827 final Rect dirty = drawable.getBounds();
13828 final int scrollX = mScrollX;
13829 final int scrollY = mScrollY;
13830
13831 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13832 dirty.right + scrollX, dirty.bottom + scrollY);
13833 }
13834 }
13835
13836 /**
13837 * Schedules an action on a drawable to occur at a specified time.
13838 *
13839 * @param who the recipient of the action
13840 * @param what the action to run on the drawable
13841 * @param when the time at which the action must occur. Uses the
13842 * {@link SystemClock#uptimeMillis} timebase.
13843 */
13844 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013845 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013846 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013847 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013848 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13849 Choreographer.CALLBACK_ANIMATION, what, who,
13850 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013851 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013852 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013854 }
13855 }
13856
13857 /**
13858 * Cancels a scheduled action on a drawable.
13859 *
13860 * @param who the recipient of the action
13861 * @param what the action to cancel
13862 */
13863 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013864 if (verifyDrawable(who) && what != null) {
13865 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013866 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13867 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013868 } else {
13869 ViewRootImpl.getRunQueue().removeCallbacks(what);
13870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013871 }
13872 }
13873
13874 /**
13875 * Unschedule any events associated with the given Drawable. This can be
13876 * used when selecting a new Drawable into a view, so that the previous
13877 * one is completely unscheduled.
13878 *
13879 * @param who The Drawable to unschedule.
13880 *
13881 * @see #drawableStateChanged
13882 */
13883 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013884 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013885 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13886 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 }
13888 }
13889
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013890 /**
13891 * Return the layout direction of a given Drawable.
13892 *
13893 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013894 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013895 */
13896 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013897 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013898 }
13899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 /**
13901 * If your view subclass is displaying its own Drawable objects, it should
13902 * override this function and return true for any Drawable it is
13903 * displaying. This allows animations for those drawables to be
13904 * scheduled.
13905 *
13906 * <p>Be sure to call through to the super class when overriding this
13907 * function.
13908 *
13909 * @param who The Drawable to verify. Return true if it is one you are
13910 * displaying, else return the result of calling through to the
13911 * super class.
13912 *
13913 * @return boolean If true than the Drawable is being displayed in the
13914 * view; else false and it is not allowed to animate.
13915 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013916 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13917 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918 */
13919 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013920 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 }
13922
13923 /**
13924 * This function is called whenever the state of the view changes in such
13925 * a way that it impacts the state of drawables being shown.
13926 *
13927 * <p>Be sure to call through to the superclass when overriding this
13928 * function.
13929 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013930 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 */
13932 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013933 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 if (d != null && d.isStateful()) {
13935 d.setState(getDrawableState());
13936 }
13937 }
13938
13939 /**
13940 * Call this to force a view to update its drawable state. This will cause
13941 * drawableStateChanged to be called on this view. Views that are interested
13942 * in the new state should call getDrawableState.
13943 *
13944 * @see #drawableStateChanged
13945 * @see #getDrawableState
13946 */
13947 public void refreshDrawableState() {
13948 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13949 drawableStateChanged();
13950
13951 ViewParent parent = mParent;
13952 if (parent != null) {
13953 parent.childDrawableStateChanged(this);
13954 }
13955 }
13956
13957 /**
13958 * Return an array of resource IDs of the drawable states representing the
13959 * current state of the view.
13960 *
13961 * @return The current drawable state
13962 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013963 * @see Drawable#setState(int[])
13964 * @see #drawableStateChanged()
13965 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013966 */
13967 public final int[] getDrawableState() {
13968 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13969 return mDrawableState;
13970 } else {
13971 mDrawableState = onCreateDrawableState(0);
13972 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13973 return mDrawableState;
13974 }
13975 }
13976
13977 /**
13978 * Generate the new {@link android.graphics.drawable.Drawable} state for
13979 * this view. This is called by the view
13980 * system when the cached Drawable state is determined to be invalid. To
13981 * retrieve the current state, you should use {@link #getDrawableState}.
13982 *
13983 * @param extraSpace if non-zero, this is the number of extra entries you
13984 * would like in the returned array in which you can place your own
13985 * states.
13986 *
13987 * @return Returns an array holding the current {@link Drawable} state of
13988 * the view.
13989 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013990 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013991 */
13992 protected int[] onCreateDrawableState(int extraSpace) {
13993 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13994 mParent instanceof View) {
13995 return ((View) mParent).onCreateDrawableState(extraSpace);
13996 }
13997
13998 int[] drawableState;
13999
14000 int privateFlags = mPrivateFlags;
14001
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014002 int viewStateIndex = 0;
14003 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14004 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14005 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014006 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014007 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14008 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014009 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14010 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014011 // This is set if HW acceleration is requested, even if the current
14012 // process doesn't allow it. This is just to allow app preview
14013 // windows to better match their app.
14014 viewStateIndex |= VIEW_STATE_ACCELERATED;
14015 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014016 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017
Christopher Tate3d4bf172011-03-28 16:16:46 -070014018 final int privateFlags2 = mPrivateFlags2;
14019 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14020 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014022 drawableState = VIEW_STATE_SETS[viewStateIndex];
14023
14024 //noinspection ConstantIfStatement
14025 if (false) {
14026 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14027 Log.i("View", toString()
14028 + " pressed=" + ((privateFlags & PRESSED) != 0)
14029 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14030 + " fo=" + hasFocus()
14031 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014032 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014033 + ": " + Arrays.toString(drawableState));
14034 }
14035
14036 if (extraSpace == 0) {
14037 return drawableState;
14038 }
14039
14040 final int[] fullState;
14041 if (drawableState != null) {
14042 fullState = new int[drawableState.length + extraSpace];
14043 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14044 } else {
14045 fullState = new int[extraSpace];
14046 }
14047
14048 return fullState;
14049 }
14050
14051 /**
14052 * Merge your own state values in <var>additionalState</var> into the base
14053 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014054 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 *
14056 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014057 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058 * own additional state values.
14059 *
14060 * @param additionalState The additional state values you would like
14061 * added to <var>baseState</var>; this array is not modified.
14062 *
14063 * @return As a convenience, the <var>baseState</var> array you originally
14064 * passed into the function is returned.
14065 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014066 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 */
14068 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14069 final int N = baseState.length;
14070 int i = N - 1;
14071 while (i >= 0 && baseState[i] == 0) {
14072 i--;
14073 }
14074 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14075 return baseState;
14076 }
14077
14078 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014079 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14080 * on all Drawable objects associated with this view.
14081 */
14082 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014083 if (mBackground != null) {
14084 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014085 }
14086 }
14087
14088 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014089 * Sets the background color for this view.
14090 * @param color the color of the background
14091 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014092 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014094 if (mBackground instanceof ColorDrawable) {
14095 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014096 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014097 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 }
14100
14101 /**
14102 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014103 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014104 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014106 * @attr ref android.R.styleable#View_background
14107 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014108 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014109 public void setBackgroundResource(int resid) {
14110 if (resid != 0 && resid == mBackgroundResource) {
14111 return;
14112 }
14113
14114 Drawable d= null;
14115 if (resid != 0) {
14116 d = mResources.getDrawable(resid);
14117 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014118 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119
14120 mBackgroundResource = resid;
14121 }
14122
14123 /**
14124 * Set the background to a given Drawable, or remove the background. If the
14125 * background has padding, this View's padding is set to the background's
14126 * padding. However, when a background is removed, this View's padding isn't
14127 * touched. If setting the padding is desired, please use
14128 * {@link #setPadding(int, int, int, int)}.
14129 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014130 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 * background
14132 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014133 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014134 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014135 setBackgroundDrawable(background);
14136 }
14137
14138 /**
14139 * @deprecated use {@link #setBackground(Drawable)} instead
14140 */
14141 @Deprecated
14142 public void setBackgroundDrawable(Drawable background) {
14143 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014144 return;
14145 }
14146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014147 boolean requestLayout = false;
14148
14149 mBackgroundResource = 0;
14150
14151 /*
14152 * Regardless of whether we're setting a new background or not, we want
14153 * to clear the previous drawable.
14154 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014155 if (mBackground != null) {
14156 mBackground.setCallback(null);
14157 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158 }
14159
Philip Milne6c8ea062012-04-03 17:38:43 -070014160 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161 Rect padding = sThreadLocal.get();
14162 if (padding == null) {
14163 padding = new Rect();
14164 sThreadLocal.set(padding);
14165 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014166 if (background.getPadding(padding)) {
14167 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014168 case LAYOUT_DIRECTION_RTL:
14169 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14170 break;
14171 case LAYOUT_DIRECTION_LTR:
14172 default:
14173 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014175 }
14176
14177 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14178 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014179 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14180 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014181 requestLayout = true;
14182 }
14183
Philip Milne6c8ea062012-04-03 17:38:43 -070014184 background.setCallback(this);
14185 if (background.isStateful()) {
14186 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014187 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014188 background.setVisible(getVisibility() == VISIBLE, false);
14189 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014190
14191 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14192 mPrivateFlags &= ~SKIP_DRAW;
14193 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14194 requestLayout = true;
14195 }
14196 } else {
14197 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014198 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199
14200 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14201 /*
14202 * This view ONLY drew the background before and we're removing
14203 * the background, so now it won't draw anything
14204 * (hence we SKIP_DRAW)
14205 */
14206 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14207 mPrivateFlags |= SKIP_DRAW;
14208 }
14209
14210 /*
14211 * When the background is set, we try to apply its padding to this
14212 * View. When the background is removed, we don't touch this View's
14213 * padding. This is noted in the Javadocs. Hence, we don't need to
14214 * requestLayout(), the invalidate() below is sufficient.
14215 */
14216
14217 // The old background's minimum size could have affected this
14218 // View's layout, so let's requestLayout
14219 requestLayout = true;
14220 }
14221
Romain Guy8f1344f52009-05-15 16:03:59 -070014222 computeOpaqueFlags();
14223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 if (requestLayout) {
14225 requestLayout();
14226 }
14227
14228 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014229 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014230 }
14231
14232 /**
14233 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014236 *
14237 * @see #setBackground(Drawable)
14238 *
14239 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 */
14241 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 }
14244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014245 /**
14246 * Sets the padding. The view may add on the space required to display
14247 * the scrollbars, depending on the style and visibility of the scrollbars.
14248 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14249 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14250 * from the values set in this call.
14251 *
14252 * @attr ref android.R.styleable#View_padding
14253 * @attr ref android.R.styleable#View_paddingBottom
14254 * @attr ref android.R.styleable#View_paddingLeft
14255 * @attr ref android.R.styleable#View_paddingRight
14256 * @attr ref android.R.styleable#View_paddingTop
14257 * @param left the left padding in pixels
14258 * @param top the top padding in pixels
14259 * @param right the right padding in pixels
14260 * @param bottom the bottom padding in pixels
14261 */
14262 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014263 mUserPaddingStart = -1;
14264 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014265 mUserPaddingRelative = false;
14266
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014267 internalSetPadding(left, top, right, bottom);
14268 }
14269
14270 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014271 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 mUserPaddingRight = right;
14273 mUserPaddingBottom = bottom;
14274
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014275 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014276 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014277
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014278 // Common case is there are no scroll bars.
14279 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014280 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014281 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014282 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014283 switch (mVerticalScrollbarPosition) {
14284 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014285 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14286 left += offset;
14287 } else {
14288 right += offset;
14289 }
14290 break;
Adam Powell20232d02010-12-08 21:08:53 -080014291 case SCROLLBAR_POSITION_RIGHT:
14292 right += offset;
14293 break;
14294 case SCROLLBAR_POSITION_LEFT:
14295 left += offset;
14296 break;
14297 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014298 }
Adam Powell20232d02010-12-08 21:08:53 -080014299 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014300 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14301 ? 0 : getHorizontalScrollbarHeight();
14302 }
14303 }
Romain Guy8506ab42009-06-11 17:35:47 -070014304
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014305 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014306 changed = true;
14307 mPaddingLeft = left;
14308 }
14309 if (mPaddingTop != top) {
14310 changed = true;
14311 mPaddingTop = top;
14312 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014313 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014314 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014315 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014316 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014317 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014318 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014319 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 }
14321
14322 if (changed) {
14323 requestLayout();
14324 }
14325 }
14326
14327 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014328 * Sets the relative padding. The view may add on the space required to display
14329 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014330 * from the values set in this call.
14331 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014332 * @param start the start padding in pixels
14333 * @param top the top padding in pixels
14334 * @param end the end padding in pixels
14335 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014336 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014337 */
14338 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014339 mUserPaddingStart = start;
14340 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014341 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014342
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014343 switch(getResolvedLayoutDirection()) {
14344 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014345 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014346 break;
14347 case LAYOUT_DIRECTION_LTR:
14348 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014349 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014350 }
14351 }
14352
14353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014354 * Returns the top padding of this view.
14355 *
14356 * @return the top padding in pixels
14357 */
14358 public int getPaddingTop() {
14359 return mPaddingTop;
14360 }
14361
14362 /**
14363 * Returns the bottom padding of this view. If there are inset and enabled
14364 * scrollbars, this value may include the space required to display the
14365 * scrollbars as well.
14366 *
14367 * @return the bottom padding in pixels
14368 */
14369 public int getPaddingBottom() {
14370 return mPaddingBottom;
14371 }
14372
14373 /**
14374 * Returns the left padding of this view. If there are inset and enabled
14375 * scrollbars, this value may include the space required to display the
14376 * scrollbars as well.
14377 *
14378 * @return the left padding in pixels
14379 */
14380 public int getPaddingLeft() {
14381 return mPaddingLeft;
14382 }
14383
14384 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014385 * Returns the start padding of this view depending on its resolved layout direction.
14386 * If there are inset and enabled scrollbars, this value may include the space
14387 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014388 *
14389 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014390 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014391 */
14392 public int getPaddingStart() {
14393 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14394 mPaddingRight : mPaddingLeft;
14395 }
14396
14397 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014398 * Returns the right padding of this view. If there are inset and enabled
14399 * scrollbars, this value may include the space required to display the
14400 * scrollbars as well.
14401 *
14402 * @return the right padding in pixels
14403 */
14404 public int getPaddingRight() {
14405 return mPaddingRight;
14406 }
14407
14408 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014409 * Returns the end padding of this view depending on its resolved layout direction.
14410 * If there are inset and enabled scrollbars, this value may include the space
14411 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014412 *
14413 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014414 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014415 */
14416 public int getPaddingEnd() {
14417 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14418 mPaddingLeft : mPaddingRight;
14419 }
14420
14421 /**
14422 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014423 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014424 *
14425 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014426 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014427 */
14428 public boolean isPaddingRelative() {
14429 return mUserPaddingRelative;
14430 }
14431
14432 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014433 * @hide
14434 */
Philip Milne7a23b492012-04-24 22:12:36 -070014435 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014436 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014437 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014438 }
14439 return mLayoutInsets;
14440 }
14441
14442 /**
14443 * @hide
14444 */
14445 public void setLayoutInsets(Insets layoutInsets) {
14446 mLayoutInsets = layoutInsets;
14447 }
14448
14449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014450 * Changes the selection state of this view. A view can be selected or not.
14451 * Note that selection is not the same as focus. Views are typically
14452 * selected in the context of an AdapterView like ListView or GridView;
14453 * the selected view is the view that is highlighted.
14454 *
14455 * @param selected true if the view must be selected, false otherwise
14456 */
14457 public void setSelected(boolean selected) {
14458 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14459 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014460 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014461 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014462 refreshDrawableState();
14463 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014464 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14465 notifyAccessibilityStateChanged();
14466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 }
14468 }
14469
14470 /**
14471 * Dispatch setSelected to all of this View's children.
14472 *
14473 * @see #setSelected(boolean)
14474 *
14475 * @param selected The new selected state
14476 */
14477 protected void dispatchSetSelected(boolean selected) {
14478 }
14479
14480 /**
14481 * Indicates the selection state of this view.
14482 *
14483 * @return true if the view is selected, false otherwise
14484 */
14485 @ViewDebug.ExportedProperty
14486 public boolean isSelected() {
14487 return (mPrivateFlags & SELECTED) != 0;
14488 }
14489
14490 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014491 * Changes the activated state of this view. A view can be activated or not.
14492 * Note that activation is not the same as selection. Selection is
14493 * a transient property, representing the view (hierarchy) the user is
14494 * currently interacting with. Activation is a longer-term state that the
14495 * user can move views in and out of. For example, in a list view with
14496 * single or multiple selection enabled, the views in the current selection
14497 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14498 * here.) The activated state is propagated down to children of the view it
14499 * is set on.
14500 *
14501 * @param activated true if the view must be activated, false otherwise
14502 */
14503 public void setActivated(boolean activated) {
14504 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14505 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014506 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014507 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014508 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014509 }
14510 }
14511
14512 /**
14513 * Dispatch setActivated to all of this View's children.
14514 *
14515 * @see #setActivated(boolean)
14516 *
14517 * @param activated The new activated state
14518 */
14519 protected void dispatchSetActivated(boolean activated) {
14520 }
14521
14522 /**
14523 * Indicates the activation state of this view.
14524 *
14525 * @return true if the view is activated, false otherwise
14526 */
14527 @ViewDebug.ExportedProperty
14528 public boolean isActivated() {
14529 return (mPrivateFlags & ACTIVATED) != 0;
14530 }
14531
14532 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14534 * observer can be used to get notifications when global events, like
14535 * layout, happen.
14536 *
14537 * The returned ViewTreeObserver observer is not guaranteed to remain
14538 * valid for the lifetime of this View. If the caller of this method keeps
14539 * a long-lived reference to ViewTreeObserver, it should always check for
14540 * the return value of {@link ViewTreeObserver#isAlive()}.
14541 *
14542 * @return The ViewTreeObserver for this view's hierarchy.
14543 */
14544 public ViewTreeObserver getViewTreeObserver() {
14545 if (mAttachInfo != null) {
14546 return mAttachInfo.mTreeObserver;
14547 }
14548 if (mFloatingTreeObserver == null) {
14549 mFloatingTreeObserver = new ViewTreeObserver();
14550 }
14551 return mFloatingTreeObserver;
14552 }
14553
14554 /**
14555 * <p>Finds the topmost view in the current view hierarchy.</p>
14556 *
14557 * @return the topmost view containing this view
14558 */
14559 public View getRootView() {
14560 if (mAttachInfo != null) {
14561 final View v = mAttachInfo.mRootView;
14562 if (v != null) {
14563 return v;
14564 }
14565 }
Romain Guy8506ab42009-06-11 17:35:47 -070014566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014567 View parent = this;
14568
14569 while (parent.mParent != null && parent.mParent instanceof View) {
14570 parent = (View) parent.mParent;
14571 }
14572
14573 return parent;
14574 }
14575
14576 /**
14577 * <p>Computes the coordinates of this view on the screen. The argument
14578 * must be an array of two integers. After the method returns, the array
14579 * contains the x and y location in that order.</p>
14580 *
14581 * @param location an array of two integers in which to hold the coordinates
14582 */
14583 public void getLocationOnScreen(int[] location) {
14584 getLocationInWindow(location);
14585
14586 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014587 if (info != null) {
14588 location[0] += info.mWindowLeft;
14589 location[1] += info.mWindowTop;
14590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014591 }
14592
14593 /**
14594 * <p>Computes the coordinates of this view in its window. The argument
14595 * must be an array of two integers. After the method returns, the array
14596 * contains the x and y location in that order.</p>
14597 *
14598 * @param location an array of two integers in which to hold the coordinates
14599 */
14600 public void getLocationInWindow(int[] location) {
14601 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014602 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014603 }
14604
Gilles Debunne6583ce52011-12-06 18:09:02 -080014605 if (mAttachInfo == null) {
14606 // When the view is not attached to a window, this method does not make sense
14607 location[0] = location[1] = 0;
14608 return;
14609 }
14610
Gilles Debunnecea45132011-11-24 02:19:27 +010014611 float[] position = mAttachInfo.mTmpTransformLocation;
14612 position[0] = position[1] = 0.0f;
14613
14614 if (!hasIdentityMatrix()) {
14615 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014617
Gilles Debunnecea45132011-11-24 02:19:27 +010014618 position[0] += mLeft;
14619 position[1] += mTop;
14620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014621 ViewParent viewParent = mParent;
14622 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014623 final View view = (View) viewParent;
14624
14625 position[0] -= view.mScrollX;
14626 position[1] -= view.mScrollY;
14627
14628 if (!view.hasIdentityMatrix()) {
14629 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014630 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014631
14632 position[0] += view.mLeft;
14633 position[1] += view.mTop;
14634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014635 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014636 }
Romain Guy8506ab42009-06-11 17:35:47 -070014637
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014638 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014639 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014640 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14641 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014643
14644 location[0] = (int) (position[0] + 0.5f);
14645 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014646 }
14647
14648 /**
14649 * {@hide}
14650 * @param id the id of the view to be found
14651 * @return the view of the specified id, null if cannot be found
14652 */
14653 protected View findViewTraversal(int id) {
14654 if (id == mID) {
14655 return this;
14656 }
14657 return null;
14658 }
14659
14660 /**
14661 * {@hide}
14662 * @param tag the tag of the view to be found
14663 * @return the view of specified tag, null if cannot be found
14664 */
14665 protected View findViewWithTagTraversal(Object tag) {
14666 if (tag != null && tag.equals(mTag)) {
14667 return this;
14668 }
14669 return null;
14670 }
14671
14672 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014673 * {@hide}
14674 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014675 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014676 * @return The first view that matches the predicate or null.
14677 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014678 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014679 if (predicate.apply(this)) {
14680 return this;
14681 }
14682 return null;
14683 }
14684
14685 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014686 * Look for a child view with the given id. If this view has the given
14687 * id, return this view.
14688 *
14689 * @param id The id to search for.
14690 * @return The view that has the given id in the hierarchy or null
14691 */
14692 public final View findViewById(int id) {
14693 if (id < 0) {
14694 return null;
14695 }
14696 return findViewTraversal(id);
14697 }
14698
14699 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014700 * Finds a view by its unuque and stable accessibility id.
14701 *
14702 * @param accessibilityId The searched accessibility id.
14703 * @return The found view.
14704 */
14705 final View findViewByAccessibilityId(int accessibilityId) {
14706 if (accessibilityId < 0) {
14707 return null;
14708 }
14709 return findViewByAccessibilityIdTraversal(accessibilityId);
14710 }
14711
14712 /**
14713 * Performs the traversal to find a view by its unuque and stable accessibility id.
14714 *
14715 * <strong>Note:</strong>This method does not stop at the root namespace
14716 * boundary since the user can touch the screen at an arbitrary location
14717 * potentially crossing the root namespace bounday which will send an
14718 * accessibility event to accessibility services and they should be able
14719 * to obtain the event source. Also accessibility ids are guaranteed to be
14720 * unique in the window.
14721 *
14722 * @param accessibilityId The accessibility id.
14723 * @return The found view.
14724 */
14725 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14726 if (getAccessibilityViewId() == accessibilityId) {
14727 return this;
14728 }
14729 return null;
14730 }
14731
14732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014733 * Look for a child view with the given tag. If this view has the given
14734 * tag, return this view.
14735 *
14736 * @param tag The tag to search for, using "tag.equals(getTag())".
14737 * @return The View that has the given tag in the hierarchy or null
14738 */
14739 public final View findViewWithTag(Object tag) {
14740 if (tag == null) {
14741 return null;
14742 }
14743 return findViewWithTagTraversal(tag);
14744 }
14745
14746 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014747 * {@hide}
14748 * Look for a child view that matches the specified predicate.
14749 * If this view matches the predicate, return this view.
14750 *
14751 * @param predicate The predicate to evaluate.
14752 * @return The first view that matches the predicate or null.
14753 */
14754 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014755 return findViewByPredicateTraversal(predicate, null);
14756 }
14757
14758 /**
14759 * {@hide}
14760 * Look for a child view that matches the specified predicate,
14761 * starting with the specified view and its descendents and then
14762 * recusively searching the ancestors and siblings of that view
14763 * until this view is reached.
14764 *
14765 * This method is useful in cases where the predicate does not match
14766 * a single unique view (perhaps multiple views use the same id)
14767 * and we are trying to find the view that is "closest" in scope to the
14768 * starting view.
14769 *
14770 * @param start The view to start from.
14771 * @param predicate The predicate to evaluate.
14772 * @return The first view that matches the predicate or null.
14773 */
14774 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14775 View childToSkip = null;
14776 for (;;) {
14777 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14778 if (view != null || start == this) {
14779 return view;
14780 }
14781
14782 ViewParent parent = start.getParent();
14783 if (parent == null || !(parent instanceof View)) {
14784 return null;
14785 }
14786
14787 childToSkip = start;
14788 start = (View) parent;
14789 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014790 }
14791
14792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014793 * Sets the identifier for this view. The identifier does not have to be
14794 * unique in this view's hierarchy. The identifier should be a positive
14795 * number.
14796 *
14797 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014798 * @see #getId()
14799 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014800 *
14801 * @param id a number used to identify the view
14802 *
14803 * @attr ref android.R.styleable#View_id
14804 */
14805 public void setId(int id) {
14806 mID = id;
14807 }
14808
14809 /**
14810 * {@hide}
14811 *
14812 * @param isRoot true if the view belongs to the root namespace, false
14813 * otherwise
14814 */
14815 public void setIsRootNamespace(boolean isRoot) {
14816 if (isRoot) {
14817 mPrivateFlags |= IS_ROOT_NAMESPACE;
14818 } else {
14819 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14820 }
14821 }
14822
14823 /**
14824 * {@hide}
14825 *
14826 * @return true if the view belongs to the root namespace, false otherwise
14827 */
14828 public boolean isRootNamespace() {
14829 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14830 }
14831
14832 /**
14833 * Returns this view's identifier.
14834 *
14835 * @return a positive integer used to identify the view or {@link #NO_ID}
14836 * if the view has no ID
14837 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014838 * @see #setId(int)
14839 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014840 * @attr ref android.R.styleable#View_id
14841 */
14842 @ViewDebug.CapturedViewProperty
14843 public int getId() {
14844 return mID;
14845 }
14846
14847 /**
14848 * Returns this view's tag.
14849 *
14850 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014851 *
14852 * @see #setTag(Object)
14853 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 */
14855 @ViewDebug.ExportedProperty
14856 public Object getTag() {
14857 return mTag;
14858 }
14859
14860 /**
14861 * Sets the tag associated with this view. A tag can be used to mark
14862 * a view in its hierarchy and does not have to be unique within the
14863 * hierarchy. Tags can also be used to store data within a view without
14864 * resorting to another data structure.
14865 *
14866 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014867 *
14868 * @see #getTag()
14869 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014870 */
14871 public void setTag(final Object tag) {
14872 mTag = tag;
14873 }
14874
14875 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014876 * Returns the tag associated with this view and the specified key.
14877 *
14878 * @param key The key identifying the tag
14879 *
14880 * @return the Object stored in this view as a tag
14881 *
14882 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014883 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014884 */
14885 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014886 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014887 return null;
14888 }
14889
14890 /**
14891 * Sets a tag associated with this view and a key. A tag can be used
14892 * to mark a view in its hierarchy and does not have to be unique within
14893 * the hierarchy. Tags can also be used to store data within a view
14894 * without resorting to another data structure.
14895 *
14896 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014897 * application to ensure it is unique (see the <a
14898 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14899 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014900 * the Android framework or not associated with any package will cause
14901 * an {@link IllegalArgumentException} to be thrown.
14902 *
14903 * @param key The key identifying the tag
14904 * @param tag An Object to tag the view with
14905 *
14906 * @throws IllegalArgumentException If they specified key is not valid
14907 *
14908 * @see #setTag(Object)
14909 * @see #getTag(int)
14910 */
14911 public void setTag(int key, final Object tag) {
14912 // If the package id is 0x00 or 0x01, it's either an undefined package
14913 // or a framework id
14914 if ((key >>> 24) < 2) {
14915 throw new IllegalArgumentException("The key must be an application-specific "
14916 + "resource id.");
14917 }
14918
Adam Powell2b2f6d62011-09-23 11:15:39 -070014919 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014920 }
14921
14922 /**
14923 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14924 * framework id.
14925 *
14926 * @hide
14927 */
14928 public void setTagInternal(int key, Object tag) {
14929 if ((key >>> 24) != 0x1) {
14930 throw new IllegalArgumentException("The key must be a framework-specific "
14931 + "resource id.");
14932 }
14933
Adam Powell2b2f6d62011-09-23 11:15:39 -070014934 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014935 }
14936
Adam Powell2b2f6d62011-09-23 11:15:39 -070014937 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014938 if (mKeyedTags == null) {
14939 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014940 }
14941
Adam Powell7db82ac2011-09-22 19:44:04 -070014942 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014943 }
14944
14945 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014946 * Prints information about this view in the log output, with the tag
14947 * {@link #VIEW_LOG_TAG}.
14948 *
14949 * @hide
14950 */
14951 public void debug() {
14952 debug(0);
14953 }
14954
14955 /**
14956 * Prints information about this view in the log output, with the tag
14957 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14958 * indentation defined by the <code>depth</code>.
14959 *
14960 * @param depth the indentation level
14961 *
14962 * @hide
14963 */
14964 protected void debug(int depth) {
14965 String output = debugIndent(depth - 1);
14966
14967 output += "+ " + this;
14968 int id = getId();
14969 if (id != -1) {
14970 output += " (id=" + id + ")";
14971 }
14972 Object tag = getTag();
14973 if (tag != null) {
14974 output += " (tag=" + tag + ")";
14975 }
14976 Log.d(VIEW_LOG_TAG, output);
14977
14978 if ((mPrivateFlags & FOCUSED) != 0) {
14979 output = debugIndent(depth) + " FOCUSED";
14980 Log.d(VIEW_LOG_TAG, output);
14981 }
14982
14983 output = debugIndent(depth);
14984 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14985 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14986 + "} ";
14987 Log.d(VIEW_LOG_TAG, output);
14988
14989 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14990 || mPaddingBottom != 0) {
14991 output = debugIndent(depth);
14992 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14993 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14994 Log.d(VIEW_LOG_TAG, output);
14995 }
14996
14997 output = debugIndent(depth);
14998 output += "mMeasureWidth=" + mMeasuredWidth +
14999 " mMeasureHeight=" + mMeasuredHeight;
15000 Log.d(VIEW_LOG_TAG, output);
15001
15002 output = debugIndent(depth);
15003 if (mLayoutParams == null) {
15004 output += "BAD! no layout params";
15005 } else {
15006 output = mLayoutParams.debug(output);
15007 }
15008 Log.d(VIEW_LOG_TAG, output);
15009
15010 output = debugIndent(depth);
15011 output += "flags={";
15012 output += View.printFlags(mViewFlags);
15013 output += "}";
15014 Log.d(VIEW_LOG_TAG, output);
15015
15016 output = debugIndent(depth);
15017 output += "privateFlags={";
15018 output += View.printPrivateFlags(mPrivateFlags);
15019 output += "}";
15020 Log.d(VIEW_LOG_TAG, output);
15021 }
15022
15023 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015024 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 *
15026 * @param depth the indentation level
15027 * @return a String containing (depth * 2 + 3) * 2 white spaces
15028 *
15029 * @hide
15030 */
15031 protected static String debugIndent(int depth) {
15032 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15033 for (int i = 0; i < (depth * 2) + 3; i++) {
15034 spaces.append(' ').append(' ');
15035 }
15036 return spaces.toString();
15037 }
15038
15039 /**
15040 * <p>Return the offset of the widget's text baseline from the widget's top
15041 * boundary. If this widget does not support baseline alignment, this
15042 * method returns -1. </p>
15043 *
15044 * @return the offset of the baseline within the widget's bounds or -1
15045 * if baseline alignment is not supported
15046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015047 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015048 public int getBaseline() {
15049 return -1;
15050 }
15051
15052 /**
15053 * Call this when something has changed which has invalidated the
15054 * layout of this view. This will schedule a layout pass of the view
15055 * tree.
15056 */
15057 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015058 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015059 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015060
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015061 if (mLayoutParams != null) {
15062 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15063 }
15064
15065 if (mParent != null && !mParent.isLayoutRequested()) {
15066 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015067 }
15068 }
15069
15070 /**
15071 * Forces this view to be laid out during the next layout pass.
15072 * This method does not call requestLayout() or forceLayout()
15073 * on the parent.
15074 */
15075 public void forceLayout() {
15076 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015077 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015078 }
15079
15080 /**
15081 * <p>
15082 * This is called to find out how big a view should be. The parent
15083 * supplies constraint information in the width and height parameters.
15084 * </p>
15085 *
15086 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015087 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015089 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015090 * </p>
15091 *
15092 *
15093 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15094 * parent
15095 * @param heightMeasureSpec Vertical space requirements as imposed by the
15096 * parent
15097 *
15098 * @see #onMeasure(int, int)
15099 */
15100 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15101 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15102 widthMeasureSpec != mOldWidthMeasureSpec ||
15103 heightMeasureSpec != mOldHeightMeasureSpec) {
15104
15105 // first clears the measured dimension flag
15106 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015108 // measure ourselves, this should set the measured dimension flag back
15109 onMeasure(widthMeasureSpec, heightMeasureSpec);
15110
15111 // flag not set, setMeasuredDimension() was not invoked, we raise
15112 // an exception to warn the developer
15113 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15114 throw new IllegalStateException("onMeasure() did not set the"
15115 + " measured dimension by calling"
15116 + " setMeasuredDimension()");
15117 }
15118
15119 mPrivateFlags |= LAYOUT_REQUIRED;
15120 }
15121
15122 mOldWidthMeasureSpec = widthMeasureSpec;
15123 mOldHeightMeasureSpec = heightMeasureSpec;
15124 }
15125
15126 /**
15127 * <p>
15128 * Measure the view and its content to determine the measured width and the
15129 * measured height. This method is invoked by {@link #measure(int, int)} and
15130 * should be overriden by subclasses to provide accurate and efficient
15131 * measurement of their contents.
15132 * </p>
15133 *
15134 * <p>
15135 * <strong>CONTRACT:</strong> When overriding this method, you
15136 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15137 * measured width and height of this view. Failure to do so will trigger an
15138 * <code>IllegalStateException</code>, thrown by
15139 * {@link #measure(int, int)}. Calling the superclass'
15140 * {@link #onMeasure(int, int)} is a valid use.
15141 * </p>
15142 *
15143 * <p>
15144 * The base class implementation of measure defaults to the background size,
15145 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15146 * override {@link #onMeasure(int, int)} to provide better measurements of
15147 * their content.
15148 * </p>
15149 *
15150 * <p>
15151 * If this method is overridden, it is the subclass's responsibility to make
15152 * sure the measured height and width are at least the view's minimum height
15153 * and width ({@link #getSuggestedMinimumHeight()} and
15154 * {@link #getSuggestedMinimumWidth()}).
15155 * </p>
15156 *
15157 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15158 * The requirements are encoded with
15159 * {@link android.view.View.MeasureSpec}.
15160 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15161 * The requirements are encoded with
15162 * {@link android.view.View.MeasureSpec}.
15163 *
15164 * @see #getMeasuredWidth()
15165 * @see #getMeasuredHeight()
15166 * @see #setMeasuredDimension(int, int)
15167 * @see #getSuggestedMinimumHeight()
15168 * @see #getSuggestedMinimumWidth()
15169 * @see android.view.View.MeasureSpec#getMode(int)
15170 * @see android.view.View.MeasureSpec#getSize(int)
15171 */
15172 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15173 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15174 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15175 }
15176
15177 /**
15178 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15179 * measured width and measured height. Failing to do so will trigger an
15180 * exception at measurement time.</p>
15181 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015182 * @param measuredWidth The measured width of this view. May be a complex
15183 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15184 * {@link #MEASURED_STATE_TOO_SMALL}.
15185 * @param measuredHeight The measured height of this view. May be a complex
15186 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15187 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015188 */
15189 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15190 mMeasuredWidth = measuredWidth;
15191 mMeasuredHeight = measuredHeight;
15192
15193 mPrivateFlags |= MEASURED_DIMENSION_SET;
15194 }
15195
15196 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015197 * Merge two states as returned by {@link #getMeasuredState()}.
15198 * @param curState The current state as returned from a view or the result
15199 * of combining multiple views.
15200 * @param newState The new view state to combine.
15201 * @return Returns a new integer reflecting the combination of the two
15202 * states.
15203 */
15204 public static int combineMeasuredStates(int curState, int newState) {
15205 return curState | newState;
15206 }
15207
15208 /**
15209 * Version of {@link #resolveSizeAndState(int, int, int)}
15210 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15211 */
15212 public static int resolveSize(int size, int measureSpec) {
15213 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15214 }
15215
15216 /**
15217 * Utility to reconcile a desired size and state, with constraints imposed
15218 * by a MeasureSpec. Will take the desired size, unless a different size
15219 * is imposed by the constraints. The returned value is a compound integer,
15220 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15221 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15222 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015223 *
15224 * @param size How big the view wants to be
15225 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015226 * @return Size information bit mask as defined by
15227 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015228 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015229 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015230 int result = size;
15231 int specMode = MeasureSpec.getMode(measureSpec);
15232 int specSize = MeasureSpec.getSize(measureSpec);
15233 switch (specMode) {
15234 case MeasureSpec.UNSPECIFIED:
15235 result = size;
15236 break;
15237 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015238 if (specSize < size) {
15239 result = specSize | MEASURED_STATE_TOO_SMALL;
15240 } else {
15241 result = size;
15242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015243 break;
15244 case MeasureSpec.EXACTLY:
15245 result = specSize;
15246 break;
15247 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015248 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015249 }
15250
15251 /**
15252 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015253 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 * by the MeasureSpec.
15255 *
15256 * @param size Default size for this view
15257 * @param measureSpec Constraints imposed by the parent
15258 * @return The size this view should be.
15259 */
15260 public static int getDefaultSize(int size, int measureSpec) {
15261 int result = size;
15262 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015263 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264
15265 switch (specMode) {
15266 case MeasureSpec.UNSPECIFIED:
15267 result = size;
15268 break;
15269 case MeasureSpec.AT_MOST:
15270 case MeasureSpec.EXACTLY:
15271 result = specSize;
15272 break;
15273 }
15274 return result;
15275 }
15276
15277 /**
15278 * Returns the suggested minimum height that the view should use. This
15279 * returns the maximum of the view's minimum height
15280 * and the background's minimum height
15281 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15282 * <p>
15283 * When being used in {@link #onMeasure(int, int)}, the caller should still
15284 * ensure the returned height is within the requirements of the parent.
15285 *
15286 * @return The suggested minimum height of the view.
15287 */
15288 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015289 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015291 }
15292
15293 /**
15294 * Returns the suggested minimum width that the view should use. This
15295 * returns the maximum of the view's minimum width)
15296 * and the background's minimum width
15297 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15298 * <p>
15299 * When being used in {@link #onMeasure(int, int)}, the caller should still
15300 * ensure the returned width is within the requirements of the parent.
15301 *
15302 * @return The suggested minimum width of the view.
15303 */
15304 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015305 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015307
Philip Milne6c8ea062012-04-03 17:38:43 -070015308 /**
15309 * Returns the minimum height of the view.
15310 *
15311 * @return the minimum height the view will try to be.
15312 *
15313 * @see #setMinimumHeight(int)
15314 *
15315 * @attr ref android.R.styleable#View_minHeight
15316 */
15317 public int getMinimumHeight() {
15318 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015319 }
15320
15321 /**
15322 * Sets the minimum height of the view. It is not guaranteed the view will
15323 * be able to achieve this minimum height (for example, if its parent layout
15324 * constrains it with less available height).
15325 *
15326 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015327 *
15328 * @see #getMinimumHeight()
15329 *
15330 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015331 */
15332 public void setMinimumHeight(int minHeight) {
15333 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015334 requestLayout();
15335 }
15336
15337 /**
15338 * Returns the minimum width of the view.
15339 *
15340 * @return the minimum width the view will try to be.
15341 *
15342 * @see #setMinimumWidth(int)
15343 *
15344 * @attr ref android.R.styleable#View_minWidth
15345 */
15346 public int getMinimumWidth() {
15347 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015348 }
15349
15350 /**
15351 * Sets the minimum width of the view. It is not guaranteed the view will
15352 * be able to achieve this minimum width (for example, if its parent layout
15353 * constrains it with less available width).
15354 *
15355 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015356 *
15357 * @see #getMinimumWidth()
15358 *
15359 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015360 */
15361 public void setMinimumWidth(int minWidth) {
15362 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015363 requestLayout();
15364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015365 }
15366
15367 /**
15368 * Get the animation currently associated with this view.
15369 *
15370 * @return The animation that is currently playing or
15371 * scheduled to play for this view.
15372 */
15373 public Animation getAnimation() {
15374 return mCurrentAnimation;
15375 }
15376
15377 /**
15378 * Start the specified animation now.
15379 *
15380 * @param animation the animation to start now
15381 */
15382 public void startAnimation(Animation animation) {
15383 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15384 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015385 invalidateParentCaches();
15386 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015387 }
15388
15389 /**
15390 * Cancels any animations for this view.
15391 */
15392 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015393 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015394 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015396 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015397 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015398 }
15399
15400 /**
15401 * Sets the next animation to play for this view.
15402 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015403 * {@link #startAnimation(android.view.animation.Animation)} instead.
15404 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 * control over the start time and invalidation, but you
15406 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015407 * 2) the view's parent (which controls animations on its children)
15408 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015409 * start.
15410 *
15411 * @param animation The next animation, or null.
15412 */
15413 public void setAnimation(Animation animation) {
15414 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015416 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015417 // If the screen is off assume the animation start time is now instead of
15418 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15419 // would cause the animation to start when the screen turns back on
15420 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15421 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15422 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015424 animation.reset();
15425 }
15426 }
15427
15428 /**
15429 * Invoked by a parent ViewGroup to notify the start of the animation
15430 * currently associated with this view. If you override this method,
15431 * always call super.onAnimationStart();
15432 *
15433 * @see #setAnimation(android.view.animation.Animation)
15434 * @see #getAnimation()
15435 */
15436 protected void onAnimationStart() {
15437 mPrivateFlags |= ANIMATION_STARTED;
15438 }
15439
15440 /**
15441 * Invoked by a parent ViewGroup to notify the end of the animation
15442 * currently associated with this view. If you override this method,
15443 * always call super.onAnimationEnd();
15444 *
15445 * @see #setAnimation(android.view.animation.Animation)
15446 * @see #getAnimation()
15447 */
15448 protected void onAnimationEnd() {
15449 mPrivateFlags &= ~ANIMATION_STARTED;
15450 }
15451
15452 /**
15453 * Invoked if there is a Transform that involves alpha. Subclass that can
15454 * draw themselves with the specified alpha should return true, and then
15455 * respect that alpha when their onDraw() is called. If this returns false
15456 * then the view may be redirected to draw into an offscreen buffer to
15457 * fulfill the request, which will look fine, but may be slower than if the
15458 * subclass handles it internally. The default implementation returns false.
15459 *
15460 * @param alpha The alpha (0..255) to apply to the view's drawing
15461 * @return true if the view can draw with the specified alpha.
15462 */
15463 protected boolean onSetAlpha(int alpha) {
15464 return false;
15465 }
15466
15467 /**
15468 * This is used by the RootView to perform an optimization when
15469 * the view hierarchy contains one or several SurfaceView.
15470 * SurfaceView is always considered transparent, but its children are not,
15471 * therefore all View objects remove themselves from the global transparent
15472 * region (passed as a parameter to this function).
15473 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015474 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015475 *
15476 * @return Returns true if the effective visibility of the view at this
15477 * point is opaque, regardless of the transparent region; returns false
15478 * if it is possible for underlying windows to be seen behind the view.
15479 *
15480 * {@hide}
15481 */
15482 public boolean gatherTransparentRegion(Region region) {
15483 final AttachInfo attachInfo = mAttachInfo;
15484 if (region != null && attachInfo != null) {
15485 final int pflags = mPrivateFlags;
15486 if ((pflags & SKIP_DRAW) == 0) {
15487 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15488 // remove it from the transparent region.
15489 final int[] location = attachInfo.mTransparentLocation;
15490 getLocationInWindow(location);
15491 region.op(location[0], location[1], location[0] + mRight - mLeft,
15492 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015493 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015494 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15495 // exists, so we remove the background drawable's non-transparent
15496 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015497 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015498 }
15499 }
15500 return true;
15501 }
15502
15503 /**
15504 * Play a sound effect for this view.
15505 *
15506 * <p>The framework will play sound effects for some built in actions, such as
15507 * clicking, but you may wish to play these effects in your widget,
15508 * for instance, for internal navigation.
15509 *
15510 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15511 * {@link #isSoundEffectsEnabled()} is true.
15512 *
15513 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15514 */
15515 public void playSoundEffect(int soundConstant) {
15516 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15517 return;
15518 }
15519 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15520 }
15521
15522 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015523 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015524 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015525 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 *
15527 * <p>The framework will provide haptic feedback for some built in actions,
15528 * such as long presses, but you may wish to provide feedback for your
15529 * own widget.
15530 *
15531 * <p>The feedback will only be performed if
15532 * {@link #isHapticFeedbackEnabled()} is true.
15533 *
15534 * @param feedbackConstant One of the constants defined in
15535 * {@link HapticFeedbackConstants}
15536 */
15537 public boolean performHapticFeedback(int feedbackConstant) {
15538 return performHapticFeedback(feedbackConstant, 0);
15539 }
15540
15541 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015542 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015543 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015544 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015545 *
15546 * @param feedbackConstant One of the constants defined in
15547 * {@link HapticFeedbackConstants}
15548 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15549 */
15550 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15551 if (mAttachInfo == null) {
15552 return false;
15553 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015554 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015555 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015556 && !isHapticFeedbackEnabled()) {
15557 return false;
15558 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015559 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15560 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015561 }
15562
15563 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015564 * Request that the visibility of the status bar or other screen/window
15565 * decorations be changed.
15566 *
15567 * <p>This method is used to put the over device UI into temporary modes
15568 * where the user's attention is focused more on the application content,
15569 * by dimming or hiding surrounding system affordances. This is typically
15570 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15571 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15572 * to be placed behind the action bar (and with these flags other system
15573 * affordances) so that smooth transitions between hiding and showing them
15574 * can be done.
15575 *
15576 * <p>Two representative examples of the use of system UI visibility is
15577 * implementing a content browsing application (like a magazine reader)
15578 * and a video playing application.
15579 *
15580 * <p>The first code shows a typical implementation of a View in a content
15581 * browsing application. In this implementation, the application goes
15582 * into a content-oriented mode by hiding the status bar and action bar,
15583 * and putting the navigation elements into lights out mode. The user can
15584 * then interact with content while in this mode. Such an application should
15585 * provide an easy way for the user to toggle out of the mode (such as to
15586 * check information in the status bar or access notifications). In the
15587 * implementation here, this is done simply by tapping on the content.
15588 *
15589 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15590 * content}
15591 *
15592 * <p>This second code sample shows a typical implementation of a View
15593 * in a video playing application. In this situation, while the video is
15594 * playing the application would like to go into a complete full-screen mode,
15595 * to use as much of the display as possible for the video. When in this state
15596 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015597 * touching on the screen to pop the UI out of full screen mode. See
15598 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015599 *
15600 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15601 * content}
15602 *
15603 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15604 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15605 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15606 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015607 */
15608 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015609 if (visibility != mSystemUiVisibility) {
15610 mSystemUiVisibility = visibility;
15611 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15612 mParent.recomputeViewAttributes(this);
15613 }
Joe Onorato664644d2011-01-23 17:53:23 -080015614 }
15615 }
15616
15617 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015618 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15619 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15620 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15621 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15622 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015623 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015624 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015625 return mSystemUiVisibility;
15626 }
15627
Scott Mainec6331b2011-05-24 16:55:56 -070015628 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015629 * Returns the current system UI visibility that is currently set for
15630 * the entire window. This is the combination of the
15631 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15632 * views in the window.
15633 */
15634 public int getWindowSystemUiVisibility() {
15635 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15636 }
15637
15638 /**
15639 * Override to find out when the window's requested system UI visibility
15640 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15641 * This is different from the callbacks recieved through
15642 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15643 * in that this is only telling you about the local request of the window,
15644 * not the actual values applied by the system.
15645 */
15646 public void onWindowSystemUiVisibilityChanged(int visible) {
15647 }
15648
15649 /**
15650 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15651 * the view hierarchy.
15652 */
15653 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15654 onWindowSystemUiVisibilityChanged(visible);
15655 }
15656
15657 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015658 * Set a listener to receive callbacks when the visibility of the system bar changes.
15659 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15660 */
Joe Onorato664644d2011-01-23 17:53:23 -080015661 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015662 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015663 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15664 mParent.recomputeViewAttributes(this);
15665 }
15666 }
15667
15668 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015669 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15670 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015671 */
15672 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015673 ListenerInfo li = mListenerInfo;
15674 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15675 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015676 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015677 }
15678 }
15679
Dianne Hackborncf675782012-05-10 15:07:24 -070015680 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015681 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15682 if (val != mSystemUiVisibility) {
15683 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015684 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015685 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015686 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015687 }
15688
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015689 /** @hide */
15690 public void setDisabledSystemUiVisibility(int flags) {
15691 if (mAttachInfo != null) {
15692 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15693 mAttachInfo.mDisabledSystemUiVisibility = flags;
15694 if (mParent != null) {
15695 mParent.recomputeViewAttributes(this);
15696 }
15697 }
15698 }
15699 }
15700
Joe Onorato664644d2011-01-23 17:53:23 -080015701 /**
Joe Malin32736f02011-01-19 16:14:20 -080015702 * Creates an image that the system displays during the drag and drop
15703 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15704 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15705 * appearance as the given View. The default also positions the center of the drag shadow
15706 * directly under the touch point. If no View is provided (the constructor with no parameters
15707 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15708 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15709 * default is an invisible drag shadow.
15710 * <p>
15711 * You are not required to use the View you provide to the constructor as the basis of the
15712 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15713 * anything you want as the drag shadow.
15714 * </p>
15715 * <p>
15716 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15717 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15718 * size and position of the drag shadow. It uses this data to construct a
15719 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15720 * so that your application can draw the shadow image in the Canvas.
15721 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015722 *
15723 * <div class="special reference">
15724 * <h3>Developer Guides</h3>
15725 * <p>For a guide to implementing drag and drop features, read the
15726 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15727 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015728 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015729 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015730 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015731
15732 /**
Joe Malin32736f02011-01-19 16:14:20 -080015733 * Constructs a shadow image builder based on a View. By default, the resulting drag
15734 * shadow will have the same appearance and dimensions as the View, with the touch point
15735 * over the center of the View.
15736 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015737 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015738 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015739 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015740 }
15741
Christopher Tate17ed60c2011-01-18 12:50:26 -080015742 /**
15743 * Construct a shadow builder object with no associated View. This
15744 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15745 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15746 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015747 * reference to any View object. If they are not overridden, then the result is an
15748 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015749 */
15750 public DragShadowBuilder() {
15751 mView = new WeakReference<View>(null);
15752 }
15753
15754 /**
15755 * Returns the View object that had been passed to the
15756 * {@link #View.DragShadowBuilder(View)}
15757 * constructor. If that View parameter was {@code null} or if the
15758 * {@link #View.DragShadowBuilder()}
15759 * constructor was used to instantiate the builder object, this method will return
15760 * null.
15761 *
15762 * @return The View object associate with this builder object.
15763 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015764 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015765 final public View getView() {
15766 return mView.get();
15767 }
15768
Christopher Tate2c095f32010-10-04 14:13:40 -070015769 /**
Joe Malin32736f02011-01-19 16:14:20 -080015770 * Provides the metrics for the shadow image. These include the dimensions of
15771 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015772 * be centered under the touch location while dragging.
15773 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015774 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015775 * same as the dimensions of the View itself and centers the shadow under
15776 * the touch point.
15777 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015778 *
Joe Malin32736f02011-01-19 16:14:20 -080015779 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15780 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15781 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15782 * image.
15783 *
15784 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15785 * shadow image that should be underneath the touch point during the drag and drop
15786 * operation. Your application must set {@link android.graphics.Point#x} to the
15787 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015788 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015789 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015790 final View view = mView.get();
15791 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015792 shadowSize.set(view.getWidth(), view.getHeight());
15793 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015794 } else {
15795 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15796 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015797 }
15798
15799 /**
Joe Malin32736f02011-01-19 16:14:20 -080015800 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15801 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015802 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015803 *
Joe Malin32736f02011-01-19 16:14:20 -080015804 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015805 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015806 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015807 final View view = mView.get();
15808 if (view != null) {
15809 view.draw(canvas);
15810 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015811 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015812 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015813 }
15814 }
15815
15816 /**
Joe Malin32736f02011-01-19 16:14:20 -080015817 * Starts a drag and drop operation. When your application calls this method, it passes a
15818 * {@link android.view.View.DragShadowBuilder} object to the system. The
15819 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15820 * to get metrics for the drag shadow, and then calls the object's
15821 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15822 * <p>
15823 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15824 * drag events to all the View objects in your application that are currently visible. It does
15825 * this either by calling the View object's drag listener (an implementation of
15826 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15827 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15828 * Both are passed a {@link android.view.DragEvent} object that has a
15829 * {@link android.view.DragEvent#getAction()} value of
15830 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15831 * </p>
15832 * <p>
15833 * Your application can invoke startDrag() on any attached View object. The View object does not
15834 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15835 * be related to the View the user selected for dragging.
15836 * </p>
15837 * @param data A {@link android.content.ClipData} object pointing to the data to be
15838 * transferred by the drag and drop operation.
15839 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15840 * drag shadow.
15841 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15842 * drop operation. This Object is put into every DragEvent object sent by the system during the
15843 * current drag.
15844 * <p>
15845 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15846 * to the target Views. For example, it can contain flags that differentiate between a
15847 * a copy operation and a move operation.
15848 * </p>
15849 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15850 * so the parameter should be set to 0.
15851 * @return {@code true} if the method completes successfully, or
15852 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15853 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015854 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015855 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015856 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015857 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015858 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015859 }
15860 boolean okay = false;
15861
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015862 Point shadowSize = new Point();
15863 Point shadowTouchPoint = new Point();
15864 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015865
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015866 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15867 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15868 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015869 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015870
Chris Tatea32dcf72010-10-14 12:13:50 -070015871 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015872 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15873 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015874 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015875 Surface surface = new Surface();
15876 try {
15877 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015878 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015879 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015880 + " surface=" + surface);
15881 if (token != null) {
15882 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015883 try {
Chris Tate6b391282010-10-14 15:48:59 -070015884 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015885 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015886 } finally {
15887 surface.unlockCanvasAndPost(canvas);
15888 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015889
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015890 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015891
15892 // Cache the local state object for delivery with DragEvents
15893 root.setLocalDragState(myLocalState);
15894
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015895 // repurpose 'shadowSize' for the last touch point
15896 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015897
Christopher Tatea53146c2010-09-07 11:57:52 -070015898 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015899 shadowSize.x, shadowSize.y,
15900 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015901 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015902
15903 // Off and running! Release our local surface instance; the drag
15904 // shadow surface is now managed by the system process.
15905 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015906 }
15907 } catch (Exception e) {
15908 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15909 surface.destroy();
15910 }
15911
15912 return okay;
15913 }
15914
Christopher Tatea53146c2010-09-07 11:57:52 -070015915 /**
Joe Malin32736f02011-01-19 16:14:20 -080015916 * Handles drag events sent by the system following a call to
15917 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15918 *<p>
15919 * When the system calls this method, it passes a
15920 * {@link android.view.DragEvent} object. A call to
15921 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15922 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15923 * operation.
15924 * @param event The {@link android.view.DragEvent} sent by the system.
15925 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15926 * in DragEvent, indicating the type of drag event represented by this object.
15927 * @return {@code true} if the method was successful, otherwise {@code false}.
15928 * <p>
15929 * The method should return {@code true} in response to an action type of
15930 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15931 * operation.
15932 * </p>
15933 * <p>
15934 * The method should also return {@code true} in response to an action type of
15935 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15936 * {@code false} if it didn't.
15937 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015938 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015939 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015940 return false;
15941 }
15942
15943 /**
Joe Malin32736f02011-01-19 16:14:20 -080015944 * Detects if this View is enabled and has a drag event listener.
15945 * If both are true, then it calls the drag event listener with the
15946 * {@link android.view.DragEvent} it received. If the drag event listener returns
15947 * {@code true}, then dispatchDragEvent() returns {@code true}.
15948 * <p>
15949 * For all other cases, the method calls the
15950 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15951 * method and returns its result.
15952 * </p>
15953 * <p>
15954 * This ensures that a drag event is always consumed, even if the View does not have a drag
15955 * event listener. However, if the View has a listener and the listener returns true, then
15956 * onDragEvent() is not called.
15957 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015958 */
15959 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015960 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015961 ListenerInfo li = mListenerInfo;
15962 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15963 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015964 return true;
15965 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015966 return onDragEvent(event);
15967 }
15968
Christopher Tate3d4bf172011-03-28 16:16:46 -070015969 boolean canAcceptDrag() {
15970 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15971 }
15972
Christopher Tatea53146c2010-09-07 11:57:52 -070015973 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015974 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15975 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015976 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015977 */
15978 public void onCloseSystemDialogs(String reason) {
15979 }
Joe Malin32736f02011-01-19 16:14:20 -080015980
Dianne Hackbornffa42482009-09-23 22:20:11 -070015981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015982 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015983 * update a Region being computed for
15984 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015985 * that any non-transparent parts of the Drawable are removed from the
15986 * given transparent region.
15987 *
15988 * @param dr The Drawable whose transparency is to be applied to the region.
15989 * @param region A Region holding the current transparency information,
15990 * where any parts of the region that are set are considered to be
15991 * transparent. On return, this region will be modified to have the
15992 * transparency information reduced by the corresponding parts of the
15993 * Drawable that are not transparent.
15994 * {@hide}
15995 */
15996 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15997 if (DBG) {
15998 Log.i("View", "Getting transparent region for: " + this);
15999 }
16000 final Region r = dr.getTransparentRegion();
16001 final Rect db = dr.getBounds();
16002 final AttachInfo attachInfo = mAttachInfo;
16003 if (r != null && attachInfo != null) {
16004 final int w = getRight()-getLeft();
16005 final int h = getBottom()-getTop();
16006 if (db.left > 0) {
16007 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16008 r.op(0, 0, db.left, h, Region.Op.UNION);
16009 }
16010 if (db.right < w) {
16011 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16012 r.op(db.right, 0, w, h, Region.Op.UNION);
16013 }
16014 if (db.top > 0) {
16015 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16016 r.op(0, 0, w, db.top, Region.Op.UNION);
16017 }
16018 if (db.bottom < h) {
16019 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16020 r.op(0, db.bottom, w, h, Region.Op.UNION);
16021 }
16022 final int[] location = attachInfo.mTransparentLocation;
16023 getLocationInWindow(location);
16024 r.translate(location[0], location[1]);
16025 region.op(r, Region.Op.INTERSECT);
16026 } else {
16027 region.op(db, Region.Op.DIFFERENCE);
16028 }
16029 }
16030
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016031 private void checkForLongClick(int delayOffset) {
16032 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16033 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016034
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016035 if (mPendingCheckForLongPress == null) {
16036 mPendingCheckForLongPress = new CheckForLongPress();
16037 }
16038 mPendingCheckForLongPress.rememberWindowAttachCount();
16039 postDelayed(mPendingCheckForLongPress,
16040 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016042 }
16043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016044 /**
16045 * Inflate a view from an XML resource. This convenience method wraps the {@link
16046 * LayoutInflater} class, which provides a full range of options for view inflation.
16047 *
16048 * @param context The Context object for your activity or application.
16049 * @param resource The resource ID to inflate
16050 * @param root A view group that will be the parent. Used to properly inflate the
16051 * layout_* parameters.
16052 * @see LayoutInflater
16053 */
16054 public static View inflate(Context context, int resource, ViewGroup root) {
16055 LayoutInflater factory = LayoutInflater.from(context);
16056 return factory.inflate(resource, root);
16057 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016059 /**
Adam Powell637d3372010-08-25 14:37:03 -070016060 * Scroll the view with standard behavior for scrolling beyond the normal
16061 * content boundaries. Views that call this method should override
16062 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16063 * results of an over-scroll operation.
16064 *
16065 * Views can use this method to handle any touch or fling-based scrolling.
16066 *
16067 * @param deltaX Change in X in pixels
16068 * @param deltaY Change in Y in pixels
16069 * @param scrollX Current X scroll value in pixels before applying deltaX
16070 * @param scrollY Current Y scroll value in pixels before applying deltaY
16071 * @param scrollRangeX Maximum content scroll range along the X axis
16072 * @param scrollRangeY Maximum content scroll range along the Y axis
16073 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16074 * along the X axis.
16075 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16076 * along the Y axis.
16077 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16078 * @return true if scrolling was clamped to an over-scroll boundary along either
16079 * axis, false otherwise.
16080 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016081 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016082 protected boolean overScrollBy(int deltaX, int deltaY,
16083 int scrollX, int scrollY,
16084 int scrollRangeX, int scrollRangeY,
16085 int maxOverScrollX, int maxOverScrollY,
16086 boolean isTouchEvent) {
16087 final int overScrollMode = mOverScrollMode;
16088 final boolean canScrollHorizontal =
16089 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16090 final boolean canScrollVertical =
16091 computeVerticalScrollRange() > computeVerticalScrollExtent();
16092 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16093 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16094 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16095 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16096
16097 int newScrollX = scrollX + deltaX;
16098 if (!overScrollHorizontal) {
16099 maxOverScrollX = 0;
16100 }
16101
16102 int newScrollY = scrollY + deltaY;
16103 if (!overScrollVertical) {
16104 maxOverScrollY = 0;
16105 }
16106
16107 // Clamp values if at the limits and record
16108 final int left = -maxOverScrollX;
16109 final int right = maxOverScrollX + scrollRangeX;
16110 final int top = -maxOverScrollY;
16111 final int bottom = maxOverScrollY + scrollRangeY;
16112
16113 boolean clampedX = false;
16114 if (newScrollX > right) {
16115 newScrollX = right;
16116 clampedX = true;
16117 } else if (newScrollX < left) {
16118 newScrollX = left;
16119 clampedX = true;
16120 }
16121
16122 boolean clampedY = false;
16123 if (newScrollY > bottom) {
16124 newScrollY = bottom;
16125 clampedY = true;
16126 } else if (newScrollY < top) {
16127 newScrollY = top;
16128 clampedY = true;
16129 }
16130
16131 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16132
16133 return clampedX || clampedY;
16134 }
16135
16136 /**
16137 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16138 * respond to the results of an over-scroll operation.
16139 *
16140 * @param scrollX New X scroll value in pixels
16141 * @param scrollY New Y scroll value in pixels
16142 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16143 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16144 */
16145 protected void onOverScrolled(int scrollX, int scrollY,
16146 boolean clampedX, boolean clampedY) {
16147 // Intentionally empty.
16148 }
16149
16150 /**
16151 * Returns the over-scroll mode for this view. The result will be
16152 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16153 * (allow over-scrolling only if the view content is larger than the container),
16154 * or {@link #OVER_SCROLL_NEVER}.
16155 *
16156 * @return This view's over-scroll mode.
16157 */
16158 public int getOverScrollMode() {
16159 return mOverScrollMode;
16160 }
16161
16162 /**
16163 * Set the over-scroll mode for this view. Valid over-scroll modes are
16164 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16165 * (allow over-scrolling only if the view content is larger than the container),
16166 * or {@link #OVER_SCROLL_NEVER}.
16167 *
16168 * Setting the over-scroll mode of a view will have an effect only if the
16169 * view is capable of scrolling.
16170 *
16171 * @param overScrollMode The new over-scroll mode for this view.
16172 */
16173 public void setOverScrollMode(int overScrollMode) {
16174 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16175 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16176 overScrollMode != OVER_SCROLL_NEVER) {
16177 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16178 }
16179 mOverScrollMode = overScrollMode;
16180 }
16181
16182 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016183 * Gets a scale factor that determines the distance the view should scroll
16184 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16185 * @return The vertical scroll scale factor.
16186 * @hide
16187 */
16188 protected float getVerticalScrollFactor() {
16189 if (mVerticalScrollFactor == 0) {
16190 TypedValue outValue = new TypedValue();
16191 if (!mContext.getTheme().resolveAttribute(
16192 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16193 throw new IllegalStateException(
16194 "Expected theme to define listPreferredItemHeight.");
16195 }
16196 mVerticalScrollFactor = outValue.getDimension(
16197 mContext.getResources().getDisplayMetrics());
16198 }
16199 return mVerticalScrollFactor;
16200 }
16201
16202 /**
16203 * Gets a scale factor that determines the distance the view should scroll
16204 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16205 * @return The horizontal scroll scale factor.
16206 * @hide
16207 */
16208 protected float getHorizontalScrollFactor() {
16209 // TODO: Should use something else.
16210 return getVerticalScrollFactor();
16211 }
16212
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016213 /**
16214 * Return the value specifying the text direction or policy that was set with
16215 * {@link #setTextDirection(int)}.
16216 *
16217 * @return the defined text direction. It can be one of:
16218 *
16219 * {@link #TEXT_DIRECTION_INHERIT},
16220 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16221 * {@link #TEXT_DIRECTION_ANY_RTL},
16222 * {@link #TEXT_DIRECTION_LTR},
16223 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016224 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016225 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016226 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016227 @ViewDebug.ExportedProperty(category = "text", mapping = {
16228 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16229 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16230 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16231 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16232 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16233 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16234 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016235 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016236 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016237 }
16238
16239 /**
16240 * Set the text direction.
16241 *
16242 * @param textDirection the direction to set. Should be one of:
16243 *
16244 * {@link #TEXT_DIRECTION_INHERIT},
16245 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16246 * {@link #TEXT_DIRECTION_ANY_RTL},
16247 * {@link #TEXT_DIRECTION_LTR},
16248 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016249 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016250 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016251 */
16252 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016253 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016254 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016255 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016256 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016257 // Set the new text direction
16258 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016259 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016260 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016261 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016262 }
16263 }
16264
16265 /**
16266 * Return the resolved text direction.
16267 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016268 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16269 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16270 * up the parent chain of the view. if there is no parent, then it will return the default
16271 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16272 *
16273 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016274 *
Doug Feltcb3791202011-07-07 11:57:48 -070016275 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16276 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016277 * {@link #TEXT_DIRECTION_LTR},
16278 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016279 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016280 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016281 */
16282 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016283 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016284 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016285 resolveTextDirection();
16286 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016287 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016288 }
16289
16290 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016291 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16292 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016293 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016294 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016295 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016296 // Reset any previous text direction resolution
16297 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16298
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016299 if (hasRtlSupport()) {
16300 // Set resolved text direction flag depending on text direction flag
16301 final int textDirection = getTextDirection();
16302 switch(textDirection) {
16303 case TEXT_DIRECTION_INHERIT:
16304 if (canResolveTextDirection()) {
16305 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016306
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016307 // Set current resolved direction to the same value as the parent's one
16308 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16309 switch (parentResolvedDirection) {
16310 case TEXT_DIRECTION_FIRST_STRONG:
16311 case TEXT_DIRECTION_ANY_RTL:
16312 case TEXT_DIRECTION_LTR:
16313 case TEXT_DIRECTION_RTL:
16314 case TEXT_DIRECTION_LOCALE:
16315 mPrivateFlags2 |=
16316 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16317 break;
16318 default:
16319 // Default resolved direction is "first strong" heuristic
16320 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16321 }
16322 } else {
16323 // We cannot do the resolution if there is no parent, so use the default one
16324 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016325 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016326 break;
16327 case TEXT_DIRECTION_FIRST_STRONG:
16328 case TEXT_DIRECTION_ANY_RTL:
16329 case TEXT_DIRECTION_LTR:
16330 case TEXT_DIRECTION_RTL:
16331 case TEXT_DIRECTION_LOCALE:
16332 // Resolved direction is the same as text direction
16333 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16334 break;
16335 default:
16336 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016337 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016338 }
16339 } else {
16340 // Default resolved direction is "first strong" heuristic
16341 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016342 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016343
16344 // Set to resolved
16345 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016346 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016347 }
16348
16349 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016350 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016351 * resolution should override this method.
16352 *
16353 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016354 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016355 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016356 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016357 }
16358
16359 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016360 * Check if text direction resolution can be done.
16361 *
16362 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016363 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016364 */
16365 public boolean canResolveTextDirection() {
16366 switch (getTextDirection()) {
16367 case TEXT_DIRECTION_INHERIT:
16368 return (mParent != null) && (mParent instanceof ViewGroup);
16369 default:
16370 return true;
16371 }
16372 }
16373
16374 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016375 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016376 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016377 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016378 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016379 */
16380 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016381 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016382 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016383 }
16384
16385 /**
16386 * Called when text direction is reset. Subclasses that care about text direction reset should
16387 * override this method and do a reset of the text direction of their children. The default
16388 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016389 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016390 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016391 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016392 }
16393
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016394 /**
16395 * Return the value specifying the text alignment or policy that was set with
16396 * {@link #setTextAlignment(int)}.
16397 *
16398 * @return the defined text alignment. It can be one of:
16399 *
16400 * {@link #TEXT_ALIGNMENT_INHERIT},
16401 * {@link #TEXT_ALIGNMENT_GRAVITY},
16402 * {@link #TEXT_ALIGNMENT_CENTER},
16403 * {@link #TEXT_ALIGNMENT_TEXT_START},
16404 * {@link #TEXT_ALIGNMENT_TEXT_END},
16405 * {@link #TEXT_ALIGNMENT_VIEW_START},
16406 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016407 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016408 */
16409 @ViewDebug.ExportedProperty(category = "text", mapping = {
16410 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16411 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16412 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16413 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16414 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16415 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16416 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16417 })
16418 public int getTextAlignment() {
16419 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16420 }
16421
16422 /**
16423 * Set the text alignment.
16424 *
16425 * @param textAlignment The text alignment to set. Should be one of
16426 *
16427 * {@link #TEXT_ALIGNMENT_INHERIT},
16428 * {@link #TEXT_ALIGNMENT_GRAVITY},
16429 * {@link #TEXT_ALIGNMENT_CENTER},
16430 * {@link #TEXT_ALIGNMENT_TEXT_START},
16431 * {@link #TEXT_ALIGNMENT_TEXT_END},
16432 * {@link #TEXT_ALIGNMENT_VIEW_START},
16433 * {@link #TEXT_ALIGNMENT_VIEW_END}
16434 *
16435 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016436 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016437 */
16438 public void setTextAlignment(int textAlignment) {
16439 if (textAlignment != getTextAlignment()) {
16440 // Reset the current and resolved text alignment
16441 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16442 resetResolvedTextAlignment();
16443 // Set the new text alignment
16444 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16445 // Refresh
16446 requestLayout();
16447 invalidate(true);
16448 }
16449 }
16450
16451 /**
16452 * Return the resolved text alignment.
16453 *
16454 * The resolved text alignment. This needs resolution if the value is
16455 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16456 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16457 *
16458 * @return the resolved text alignment. Returns one of:
16459 *
16460 * {@link #TEXT_ALIGNMENT_GRAVITY},
16461 * {@link #TEXT_ALIGNMENT_CENTER},
16462 * {@link #TEXT_ALIGNMENT_TEXT_START},
16463 * {@link #TEXT_ALIGNMENT_TEXT_END},
16464 * {@link #TEXT_ALIGNMENT_VIEW_START},
16465 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016466 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016467 */
16468 @ViewDebug.ExportedProperty(category = "text", mapping = {
16469 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16470 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16471 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16472 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16473 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16474 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16475 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16476 })
16477 public int getResolvedTextAlignment() {
16478 // If text alignment is not resolved, then resolve it
16479 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16480 resolveTextAlignment();
16481 }
16482 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16483 }
16484
16485 /**
16486 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16487 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016488 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016489 */
16490 public void resolveTextAlignment() {
16491 // Reset any previous text alignment resolution
16492 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16493
16494 if (hasRtlSupport()) {
16495 // Set resolved text alignment flag depending on text alignment flag
16496 final int textAlignment = getTextAlignment();
16497 switch (textAlignment) {
16498 case TEXT_ALIGNMENT_INHERIT:
16499 // Check if we can resolve the text alignment
16500 if (canResolveLayoutDirection() && mParent instanceof View) {
16501 View view = (View) mParent;
16502
16503 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16504 switch (parentResolvedTextAlignment) {
16505 case TEXT_ALIGNMENT_GRAVITY:
16506 case TEXT_ALIGNMENT_TEXT_START:
16507 case TEXT_ALIGNMENT_TEXT_END:
16508 case TEXT_ALIGNMENT_CENTER:
16509 case TEXT_ALIGNMENT_VIEW_START:
16510 case TEXT_ALIGNMENT_VIEW_END:
16511 // Resolved text alignment is the same as the parent resolved
16512 // text alignment
16513 mPrivateFlags2 |=
16514 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16515 break;
16516 default:
16517 // Use default resolved text alignment
16518 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16519 }
16520 }
16521 else {
16522 // We cannot do the resolution if there is no parent so use the default
16523 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16524 }
16525 break;
16526 case TEXT_ALIGNMENT_GRAVITY:
16527 case TEXT_ALIGNMENT_TEXT_START:
16528 case TEXT_ALIGNMENT_TEXT_END:
16529 case TEXT_ALIGNMENT_CENTER:
16530 case TEXT_ALIGNMENT_VIEW_START:
16531 case TEXT_ALIGNMENT_VIEW_END:
16532 // Resolved text alignment is the same as text alignment
16533 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16534 break;
16535 default:
16536 // Use default resolved text alignment
16537 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16538 }
16539 } else {
16540 // Use default resolved text alignment
16541 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16542 }
16543
16544 // Set the resolved
16545 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16546 onResolvedTextAlignmentChanged();
16547 }
16548
16549 /**
16550 * Check if text alignment resolution can be done.
16551 *
16552 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016553 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016554 */
16555 public boolean canResolveTextAlignment() {
16556 switch (getTextAlignment()) {
16557 case TEXT_DIRECTION_INHERIT:
16558 return (mParent != null);
16559 default:
16560 return true;
16561 }
16562 }
16563
16564 /**
16565 * Called when text alignment has been resolved. Subclasses that care about text alignment
16566 * resolution should override this method.
16567 *
16568 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016569 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016570 */
16571 public void onResolvedTextAlignmentChanged() {
16572 }
16573
16574 /**
16575 * Reset resolved text alignment. Text alignment can be resolved with a call to
16576 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16577 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016578 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016579 */
16580 public void resetResolvedTextAlignment() {
16581 // Reset any previous text alignment resolution
16582 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16583 onResolvedTextAlignmentReset();
16584 }
16585
16586 /**
16587 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16588 * override this method and do a reset of the text alignment of their children. The default
16589 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016590 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016591 */
16592 public void onResolvedTextAlignmentReset() {
16593 }
16594
Chet Haaseb39f0512011-05-24 14:36:40 -070016595 //
16596 // Properties
16597 //
16598 /**
16599 * A Property wrapper around the <code>alpha</code> functionality handled by the
16600 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16601 */
Chet Haased47f1532011-12-16 11:18:52 -080016602 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016603 @Override
16604 public void setValue(View object, float value) {
16605 object.setAlpha(value);
16606 }
16607
16608 @Override
16609 public Float get(View object) {
16610 return object.getAlpha();
16611 }
16612 };
16613
16614 /**
16615 * A Property wrapper around the <code>translationX</code> functionality handled by the
16616 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16617 */
Chet Haased47f1532011-12-16 11:18:52 -080016618 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016619 @Override
16620 public void setValue(View object, float value) {
16621 object.setTranslationX(value);
16622 }
16623
16624 @Override
16625 public Float get(View object) {
16626 return object.getTranslationX();
16627 }
16628 };
16629
16630 /**
16631 * A Property wrapper around the <code>translationY</code> functionality handled by the
16632 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16633 */
Chet Haased47f1532011-12-16 11:18:52 -080016634 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016635 @Override
16636 public void setValue(View object, float value) {
16637 object.setTranslationY(value);
16638 }
16639
16640 @Override
16641 public Float get(View object) {
16642 return object.getTranslationY();
16643 }
16644 };
16645
16646 /**
16647 * A Property wrapper around the <code>x</code> functionality handled by the
16648 * {@link View#setX(float)} and {@link View#getX()} methods.
16649 */
Chet Haased47f1532011-12-16 11:18:52 -080016650 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016651 @Override
16652 public void setValue(View object, float value) {
16653 object.setX(value);
16654 }
16655
16656 @Override
16657 public Float get(View object) {
16658 return object.getX();
16659 }
16660 };
16661
16662 /**
16663 * A Property wrapper around the <code>y</code> functionality handled by the
16664 * {@link View#setY(float)} and {@link View#getY()} methods.
16665 */
Chet Haased47f1532011-12-16 11:18:52 -080016666 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016667 @Override
16668 public void setValue(View object, float value) {
16669 object.setY(value);
16670 }
16671
16672 @Override
16673 public Float get(View object) {
16674 return object.getY();
16675 }
16676 };
16677
16678 /**
16679 * A Property wrapper around the <code>rotation</code> functionality handled by the
16680 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16681 */
Chet Haased47f1532011-12-16 11:18:52 -080016682 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016683 @Override
16684 public void setValue(View object, float value) {
16685 object.setRotation(value);
16686 }
16687
16688 @Override
16689 public Float get(View object) {
16690 return object.getRotation();
16691 }
16692 };
16693
16694 /**
16695 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16696 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16697 */
Chet Haased47f1532011-12-16 11:18:52 -080016698 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016699 @Override
16700 public void setValue(View object, float value) {
16701 object.setRotationX(value);
16702 }
16703
16704 @Override
16705 public Float get(View object) {
16706 return object.getRotationX();
16707 }
16708 };
16709
16710 /**
16711 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16712 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16713 */
Chet Haased47f1532011-12-16 11:18:52 -080016714 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016715 @Override
16716 public void setValue(View object, float value) {
16717 object.setRotationY(value);
16718 }
16719
16720 @Override
16721 public Float get(View object) {
16722 return object.getRotationY();
16723 }
16724 };
16725
16726 /**
16727 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16728 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16729 */
Chet Haased47f1532011-12-16 11:18:52 -080016730 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016731 @Override
16732 public void setValue(View object, float value) {
16733 object.setScaleX(value);
16734 }
16735
16736 @Override
16737 public Float get(View object) {
16738 return object.getScaleX();
16739 }
16740 };
16741
16742 /**
16743 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16744 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16745 */
Chet Haased47f1532011-12-16 11:18:52 -080016746 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016747 @Override
16748 public void setValue(View object, float value) {
16749 object.setScaleY(value);
16750 }
16751
16752 @Override
16753 public Float get(View object) {
16754 return object.getScaleY();
16755 }
16756 };
16757
Jeff Brown33bbfd22011-02-24 20:55:35 -080016758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016759 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16760 * Each MeasureSpec represents a requirement for either the width or the height.
16761 * A MeasureSpec is comprised of a size and a mode. There are three possible
16762 * modes:
16763 * <dl>
16764 * <dt>UNSPECIFIED</dt>
16765 * <dd>
16766 * The parent has not imposed any constraint on the child. It can be whatever size
16767 * it wants.
16768 * </dd>
16769 *
16770 * <dt>EXACTLY</dt>
16771 * <dd>
16772 * The parent has determined an exact size for the child. The child is going to be
16773 * given those bounds regardless of how big it wants to be.
16774 * </dd>
16775 *
16776 * <dt>AT_MOST</dt>
16777 * <dd>
16778 * The child can be as large as it wants up to the specified size.
16779 * </dd>
16780 * </dl>
16781 *
16782 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16783 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16784 */
16785 public static class MeasureSpec {
16786 private static final int MODE_SHIFT = 30;
16787 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16788
16789 /**
16790 * Measure specification mode: The parent has not imposed any constraint
16791 * on the child. It can be whatever size it wants.
16792 */
16793 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16794
16795 /**
16796 * Measure specification mode: The parent has determined an exact size
16797 * for the child. The child is going to be given those bounds regardless
16798 * of how big it wants to be.
16799 */
16800 public static final int EXACTLY = 1 << MODE_SHIFT;
16801
16802 /**
16803 * Measure specification mode: The child can be as large as it wants up
16804 * to the specified size.
16805 */
16806 public static final int AT_MOST = 2 << MODE_SHIFT;
16807
16808 /**
16809 * Creates a measure specification based on the supplied size and mode.
16810 *
16811 * The mode must always be one of the following:
16812 * <ul>
16813 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16814 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16815 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16816 * </ul>
16817 *
16818 * @param size the size of the measure specification
16819 * @param mode the mode of the measure specification
16820 * @return the measure specification based on size and mode
16821 */
16822 public static int makeMeasureSpec(int size, int mode) {
16823 return size + mode;
16824 }
16825
16826 /**
16827 * Extracts the mode from the supplied measure specification.
16828 *
16829 * @param measureSpec the measure specification to extract the mode from
16830 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16831 * {@link android.view.View.MeasureSpec#AT_MOST} or
16832 * {@link android.view.View.MeasureSpec#EXACTLY}
16833 */
16834 public static int getMode(int measureSpec) {
16835 return (measureSpec & MODE_MASK);
16836 }
16837
16838 /**
16839 * Extracts the size from the supplied measure specification.
16840 *
16841 * @param measureSpec the measure specification to extract the size from
16842 * @return the size in pixels defined in the supplied measure specification
16843 */
16844 public static int getSize(int measureSpec) {
16845 return (measureSpec & ~MODE_MASK);
16846 }
16847
16848 /**
16849 * Returns a String representation of the specified measure
16850 * specification.
16851 *
16852 * @param measureSpec the measure specification to convert to a String
16853 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16854 */
16855 public static String toString(int measureSpec) {
16856 int mode = getMode(measureSpec);
16857 int size = getSize(measureSpec);
16858
16859 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16860
16861 if (mode == UNSPECIFIED)
16862 sb.append("UNSPECIFIED ");
16863 else if (mode == EXACTLY)
16864 sb.append("EXACTLY ");
16865 else if (mode == AT_MOST)
16866 sb.append("AT_MOST ");
16867 else
16868 sb.append(mode).append(" ");
16869
16870 sb.append(size);
16871 return sb.toString();
16872 }
16873 }
16874
16875 class CheckForLongPress implements Runnable {
16876
16877 private int mOriginalWindowAttachCount;
16878
16879 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016880 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016881 && mOriginalWindowAttachCount == mWindowAttachCount) {
16882 if (performLongClick()) {
16883 mHasPerformedLongPress = true;
16884 }
16885 }
16886 }
16887
16888 public void rememberWindowAttachCount() {
16889 mOriginalWindowAttachCount = mWindowAttachCount;
16890 }
16891 }
Joe Malin32736f02011-01-19 16:14:20 -080016892
Adam Powelle14579b2009-12-16 18:39:52 -080016893 private final class CheckForTap implements Runnable {
16894 public void run() {
16895 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016896 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016897 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016898 }
16899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016900
Adam Powella35d7682010-03-12 14:48:13 -080016901 private final class PerformClick implements Runnable {
16902 public void run() {
16903 performClick();
16904 }
16905 }
16906
Dianne Hackborn63042d62011-01-26 18:56:29 -080016907 /** @hide */
16908 public void hackTurnOffWindowResizeAnim(boolean off) {
16909 mAttachInfo.mTurnOffWindowResizeAnim = off;
16910 }
Joe Malin32736f02011-01-19 16:14:20 -080016911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016912 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016913 * This method returns a ViewPropertyAnimator object, which can be used to animate
16914 * specific properties on this View.
16915 *
16916 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16917 */
16918 public ViewPropertyAnimator animate() {
16919 if (mAnimator == null) {
16920 mAnimator = new ViewPropertyAnimator(this);
16921 }
16922 return mAnimator;
16923 }
16924
16925 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016926 * Interface definition for a callback to be invoked when a hardware key event is
16927 * dispatched to this view. The callback will be invoked before the key event is
16928 * given to the view. This is only useful for hardware keyboards; a software input
16929 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016930 */
16931 public interface OnKeyListener {
16932 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016933 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016934 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016935 * <p>Key presses in software keyboards will generally NOT trigger this method,
16936 * although some may elect to do so in some situations. Do not assume a
16937 * software input method has to be key-based; even if it is, it may use key presses
16938 * in a different way than you expect, so there is no way to reliably catch soft
16939 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016940 *
16941 * @param v The view the key has been dispatched to.
16942 * @param keyCode The code for the physical key that was pressed
16943 * @param event The KeyEvent object containing full information about
16944 * the event.
16945 * @return True if the listener has consumed the event, false otherwise.
16946 */
16947 boolean onKey(View v, int keyCode, KeyEvent event);
16948 }
16949
16950 /**
16951 * Interface definition for a callback to be invoked when a touch event is
16952 * dispatched to this view. The callback will be invoked before the touch
16953 * event is given to the view.
16954 */
16955 public interface OnTouchListener {
16956 /**
16957 * Called when a touch event is dispatched to a view. This allows listeners to
16958 * get a chance to respond before the target view.
16959 *
16960 * @param v The view the touch event has been dispatched to.
16961 * @param event The MotionEvent object containing full information about
16962 * the event.
16963 * @return True if the listener has consumed the event, false otherwise.
16964 */
16965 boolean onTouch(View v, MotionEvent event);
16966 }
16967
16968 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016969 * Interface definition for a callback to be invoked when a hover event is
16970 * dispatched to this view. The callback will be invoked before the hover
16971 * event is given to the view.
16972 */
16973 public interface OnHoverListener {
16974 /**
16975 * Called when a hover event is dispatched to a view. This allows listeners to
16976 * get a chance to respond before the target view.
16977 *
16978 * @param v The view the hover event has been dispatched to.
16979 * @param event The MotionEvent object containing full information about
16980 * the event.
16981 * @return True if the listener has consumed the event, false otherwise.
16982 */
16983 boolean onHover(View v, MotionEvent event);
16984 }
16985
16986 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016987 * Interface definition for a callback to be invoked when a generic motion event is
16988 * dispatched to this view. The callback will be invoked before the generic motion
16989 * event is given to the view.
16990 */
16991 public interface OnGenericMotionListener {
16992 /**
16993 * Called when a generic motion event is dispatched to a view. This allows listeners to
16994 * get a chance to respond before the target view.
16995 *
16996 * @param v The view the generic motion event has been dispatched to.
16997 * @param event The MotionEvent object containing full information about
16998 * the event.
16999 * @return True if the listener has consumed the event, false otherwise.
17000 */
17001 boolean onGenericMotion(View v, MotionEvent event);
17002 }
17003
17004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017005 * Interface definition for a callback to be invoked when a view has been clicked and held.
17006 */
17007 public interface OnLongClickListener {
17008 /**
17009 * Called when a view has been clicked and held.
17010 *
17011 * @param v The view that was clicked and held.
17012 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017013 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017014 */
17015 boolean onLongClick(View v);
17016 }
17017
17018 /**
Chris Tate32affef2010-10-18 15:29:21 -070017019 * Interface definition for a callback to be invoked when a drag is being dispatched
17020 * to this view. The callback will be invoked before the hosting view's own
17021 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17022 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017023 *
17024 * <div class="special reference">
17025 * <h3>Developer Guides</h3>
17026 * <p>For a guide to implementing drag and drop features, read the
17027 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17028 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017029 */
17030 public interface OnDragListener {
17031 /**
17032 * Called when a drag event is dispatched to a view. This allows listeners
17033 * to get a chance to override base View behavior.
17034 *
Joe Malin32736f02011-01-19 16:14:20 -080017035 * @param v The View that received the drag event.
17036 * @param event The {@link android.view.DragEvent} object for the drag event.
17037 * @return {@code true} if the drag event was handled successfully, or {@code false}
17038 * if the drag event was not handled. Note that {@code false} will trigger the View
17039 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017040 */
17041 boolean onDrag(View v, DragEvent event);
17042 }
17043
17044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017045 * Interface definition for a callback to be invoked when the focus state of
17046 * a view changed.
17047 */
17048 public interface OnFocusChangeListener {
17049 /**
17050 * Called when the focus state of a view has changed.
17051 *
17052 * @param v The view whose state has changed.
17053 * @param hasFocus The new focus state of v.
17054 */
17055 void onFocusChange(View v, boolean hasFocus);
17056 }
17057
17058 /**
17059 * Interface definition for a callback to be invoked when a view is clicked.
17060 */
17061 public interface OnClickListener {
17062 /**
17063 * Called when a view has been clicked.
17064 *
17065 * @param v The view that was clicked.
17066 */
17067 void onClick(View v);
17068 }
17069
17070 /**
17071 * Interface definition for a callback to be invoked when the context menu
17072 * for this view is being built.
17073 */
17074 public interface OnCreateContextMenuListener {
17075 /**
17076 * Called when the context menu for this view is being built. It is not
17077 * safe to hold onto the menu after this method returns.
17078 *
17079 * @param menu The context menu that is being built
17080 * @param v The view for which the context menu is being built
17081 * @param menuInfo Extra information about the item for which the
17082 * context menu should be shown. This information will vary
17083 * depending on the class of v.
17084 */
17085 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17086 }
17087
Joe Onorato664644d2011-01-23 17:53:23 -080017088 /**
17089 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017090 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017091 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017092 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017093 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017094 */
17095 public interface OnSystemUiVisibilityChangeListener {
17096 /**
17097 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017098 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017099 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017100 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17101 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17102 * This tells you the <strong>global</strong> state of these UI visibility
17103 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017104 */
17105 public void onSystemUiVisibilityChange(int visibility);
17106 }
17107
Adam Powell4afd62b2011-02-18 15:02:18 -080017108 /**
17109 * Interface definition for a callback to be invoked when this view is attached
17110 * or detached from its window.
17111 */
17112 public interface OnAttachStateChangeListener {
17113 /**
17114 * Called when the view is attached to a window.
17115 * @param v The view that was attached
17116 */
17117 public void onViewAttachedToWindow(View v);
17118 /**
17119 * Called when the view is detached from a window.
17120 * @param v The view that was detached
17121 */
17122 public void onViewDetachedFromWindow(View v);
17123 }
17124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017125 private final class UnsetPressedState implements Runnable {
17126 public void run() {
17127 setPressed(false);
17128 }
17129 }
17130
17131 /**
17132 * Base class for derived classes that want to save and restore their own
17133 * state in {@link android.view.View#onSaveInstanceState()}.
17134 */
17135 public static class BaseSavedState extends AbsSavedState {
17136 /**
17137 * Constructor used when reading from a parcel. Reads the state of the superclass.
17138 *
17139 * @param source
17140 */
17141 public BaseSavedState(Parcel source) {
17142 super(source);
17143 }
17144
17145 /**
17146 * Constructor called by derived classes when creating their SavedState objects
17147 *
17148 * @param superState The state of the superclass of this view
17149 */
17150 public BaseSavedState(Parcelable superState) {
17151 super(superState);
17152 }
17153
17154 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17155 new Parcelable.Creator<BaseSavedState>() {
17156 public BaseSavedState createFromParcel(Parcel in) {
17157 return new BaseSavedState(in);
17158 }
17159
17160 public BaseSavedState[] newArray(int size) {
17161 return new BaseSavedState[size];
17162 }
17163 };
17164 }
17165
17166 /**
17167 * A set of information given to a view when it is attached to its parent
17168 * window.
17169 */
17170 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017171 interface Callbacks {
17172 void playSoundEffect(int effectId);
17173 boolean performHapticFeedback(int effectId, boolean always);
17174 }
17175
17176 /**
17177 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17178 * to a Handler. This class contains the target (View) to invalidate and
17179 * the coordinates of the dirty rectangle.
17180 *
17181 * For performance purposes, this class also implements a pool of up to
17182 * POOL_LIMIT objects that get reused. This reduces memory allocations
17183 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017184 */
Romain Guyd928d682009-03-31 17:52:16 -070017185 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017186 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017187 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17188 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017189 public InvalidateInfo newInstance() {
17190 return new InvalidateInfo();
17191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017192
Romain Guyd928d682009-03-31 17:52:16 -070017193 public void onAcquired(InvalidateInfo element) {
17194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017195
Romain Guyd928d682009-03-31 17:52:16 -070017196 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017197 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017198 }
17199 }, POOL_LIMIT)
17200 );
17201
17202 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017203 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017204
17205 View target;
17206
17207 int left;
17208 int top;
17209 int right;
17210 int bottom;
17211
Romain Guyd928d682009-03-31 17:52:16 -070017212 public void setNextPoolable(InvalidateInfo element) {
17213 mNext = element;
17214 }
17215
17216 public InvalidateInfo getNextPoolable() {
17217 return mNext;
17218 }
17219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017220 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017221 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017222 }
17223
17224 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017225 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017226 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017227
17228 public boolean isPooled() {
17229 return mIsPooled;
17230 }
17231
17232 public void setPooled(boolean isPooled) {
17233 mIsPooled = isPooled;
17234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017235 }
17236
17237 final IWindowSession mSession;
17238
17239 final IWindow mWindow;
17240
17241 final IBinder mWindowToken;
17242
17243 final Callbacks mRootCallbacks;
17244
Romain Guy59a12ca2011-06-09 17:48:21 -070017245 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017247 /**
17248 * The top view of the hierarchy.
17249 */
17250 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017252 IBinder mPanelParentWindowToken;
17253 Surface mSurface;
17254
Romain Guyb051e892010-09-28 19:09:36 -070017255 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017256 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017257 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017258
Romain Guy7e4e5612012-03-05 14:37:29 -080017259 boolean mScreenOn;
17260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017261 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017262 * Scale factor used by the compatibility mode
17263 */
17264 float mApplicationScale;
17265
17266 /**
17267 * Indicates whether the application is in compatibility mode
17268 */
17269 boolean mScalingRequired;
17270
17271 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017272 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017273 */
17274 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017275
Dianne Hackborn63042d62011-01-26 18:56:29 -080017276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017277 * Left position of this view's window
17278 */
17279 int mWindowLeft;
17280
17281 /**
17282 * Top position of this view's window
17283 */
17284 int mWindowTop;
17285
17286 /**
Adam Powell26153a32010-11-08 15:22:27 -080017287 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017288 */
Adam Powell26153a32010-11-08 15:22:27 -080017289 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017290
17291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017292 * For windows that are full-screen but using insets to layout inside
17293 * of the screen decorations, these are the current insets for the
17294 * content of the window.
17295 */
17296 final Rect mContentInsets = new Rect();
17297
17298 /**
17299 * For windows that are full-screen but using insets to layout inside
17300 * of the screen decorations, these are the current insets for the
17301 * actual visible parts of the window.
17302 */
17303 final Rect mVisibleInsets = new Rect();
17304
17305 /**
17306 * The internal insets given by this window. This value is
17307 * supplied by the client (through
17308 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17309 * be given to the window manager when changed to be used in laying
17310 * out windows behind it.
17311 */
17312 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17313 = new ViewTreeObserver.InternalInsetsInfo();
17314
17315 /**
17316 * All views in the window's hierarchy that serve as scroll containers,
17317 * used to determine if the window can be resized or must be panned
17318 * to adjust for a soft input area.
17319 */
17320 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17321
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017322 final KeyEvent.DispatcherState mKeyDispatchState
17323 = new KeyEvent.DispatcherState();
17324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 /**
17326 * Indicates whether the view's window currently has the focus.
17327 */
17328 boolean mHasWindowFocus;
17329
17330 /**
17331 * The current visibility of the window.
17332 */
17333 int mWindowVisibility;
17334
17335 /**
17336 * Indicates the time at which drawing started to occur.
17337 */
17338 long mDrawingTime;
17339
17340 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017341 * Indicates whether or not ignoring the DIRTY_MASK flags.
17342 */
17343 boolean mIgnoreDirtyState;
17344
17345 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017346 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17347 * to avoid clearing that flag prematurely.
17348 */
17349 boolean mSetIgnoreDirtyState = false;
17350
17351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017352 * Indicates whether the view's window is currently in touch mode.
17353 */
17354 boolean mInTouchMode;
17355
17356 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017357 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017358 * the next time it performs a traversal
17359 */
17360 boolean mRecomputeGlobalAttributes;
17361
17362 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017363 * Always report new attributes at next traversal.
17364 */
17365 boolean mForceReportNewAttributes;
17366
17367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017368 * Set during a traveral if any views want to keep the screen on.
17369 */
17370 boolean mKeepScreenOn;
17371
17372 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017373 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17374 */
17375 int mSystemUiVisibility;
17376
17377 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017378 * Hack to force certain system UI visibility flags to be cleared.
17379 */
17380 int mDisabledSystemUiVisibility;
17381
17382 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017383 * Last global system UI visibility reported by the window manager.
17384 */
17385 int mGlobalSystemUiVisibility;
17386
17387 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017388 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17389 * attached.
17390 */
17391 boolean mHasSystemUiListeners;
17392
17393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017394 * Set if the visibility of any views has changed.
17395 */
17396 boolean mViewVisibilityChanged;
17397
17398 /**
17399 * Set to true if a view has been scrolled.
17400 */
17401 boolean mViewScrollChanged;
17402
17403 /**
17404 * Global to the view hierarchy used as a temporary for dealing with
17405 * x/y points in the transparent region computations.
17406 */
17407 final int[] mTransparentLocation = new int[2];
17408
17409 /**
17410 * Global to the view hierarchy used as a temporary for dealing with
17411 * x/y points in the ViewGroup.invalidateChild implementation.
17412 */
17413 final int[] mInvalidateChildLocation = new int[2];
17414
Chet Haasec3aa3612010-06-17 08:50:37 -070017415
17416 /**
17417 * Global to the view hierarchy used as a temporary for dealing with
17418 * x/y location when view is transformed.
17419 */
17420 final float[] mTmpTransformLocation = new float[2];
17421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017422 /**
17423 * The view tree observer used to dispatch global events like
17424 * layout, pre-draw, touch mode change, etc.
17425 */
17426 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17427
17428 /**
17429 * A Canvas used by the view hierarchy to perform bitmap caching.
17430 */
17431 Canvas mCanvas;
17432
17433 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017434 * The view root impl.
17435 */
17436 final ViewRootImpl mViewRootImpl;
17437
17438 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017439 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017440 * handler can be used to pump events in the UI events queue.
17441 */
17442 final Handler mHandler;
17443
17444 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017445 * Temporary for use in computing invalidate rectangles while
17446 * calling up the hierarchy.
17447 */
17448 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017449
17450 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017451 * Temporary for use in computing hit areas with transformed views
17452 */
17453 final RectF mTmpTransformRect = new RectF();
17454
17455 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017456 * Temporary list for use in collecting focusable descendents of a view.
17457 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017458 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017460 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017461 * The id of the window for accessibility purposes.
17462 */
17463 int mAccessibilityWindowId = View.NO_ID;
17464
17465 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017466 * Whether to ingore not exposed for accessibility Views when
17467 * reporting the view tree to accessibility services.
17468 */
17469 boolean mIncludeNotImportantViews;
17470
17471 /**
17472 * The drawable for highlighting accessibility focus.
17473 */
17474 Drawable mAccessibilityFocusDrawable;
17475
17476 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017477 * Show where the margins, bounds and layout bounds are for each view.
17478 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017479 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017480
17481 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017482 * Point used to compute visible regions.
17483 */
17484 final Point mPoint = new Point();
17485
17486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017487 * Creates a new set of attachment information with the specified
17488 * events handler and thread.
17489 *
17490 * @param handler the events handler the view must use
17491 */
17492 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017493 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017494 mSession = session;
17495 mWindow = window;
17496 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017497 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017498 mHandler = handler;
17499 mRootCallbacks = effectPlayer;
17500 }
17501 }
17502
17503 /**
17504 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17505 * is supported. This avoids keeping too many unused fields in most
17506 * instances of View.</p>
17507 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017508 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017509
Mike Cleronf116bf82009-09-27 19:14:12 -070017510 /**
17511 * Scrollbars are not visible
17512 */
17513 public static final int OFF = 0;
17514
17515 /**
17516 * Scrollbars are visible
17517 */
17518 public static final int ON = 1;
17519
17520 /**
17521 * Scrollbars are fading away
17522 */
17523 public static final int FADING = 2;
17524
17525 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017527 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017528 public int scrollBarDefaultDelayBeforeFade;
17529 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017530
17531 public int scrollBarSize;
17532 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017533 public float[] interpolatorValues;
17534 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017535
17536 public final Paint paint;
17537 public final Matrix matrix;
17538 public Shader shader;
17539
Mike Cleronf116bf82009-09-27 19:14:12 -070017540 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17541
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017542 private static final float[] OPAQUE = { 255 };
17543 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017544
Mike Cleronf116bf82009-09-27 19:14:12 -070017545 /**
17546 * When fading should start. This time moves into the future every time
17547 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17548 */
17549 public long fadeStartTime;
17550
17551
17552 /**
17553 * The current state of the scrollbars: ON, OFF, or FADING
17554 */
17555 public int state = OFF;
17556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017557 private int mLastColor;
17558
Mike Cleronf116bf82009-09-27 19:14:12 -070017559 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017560 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17561 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017562 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17563 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017564
17565 paint = new Paint();
17566 matrix = new Matrix();
17567 // use use a height of 1, and then wack the matrix each time we
17568 // actually use it.
17569 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017571 paint.setShader(shader);
17572 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017573 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017574 }
Romain Guy8506ab42009-06-11 17:35:47 -070017575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017576 public void setFadeColor(int color) {
17577 if (color != 0 && color != mLastColor) {
17578 mLastColor = color;
17579 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017580
Romain Guye55e1a72009-08-27 10:42:26 -070017581 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17582 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017584 paint.setShader(shader);
17585 // Restore the default transfer mode (src_over)
17586 paint.setXfermode(null);
17587 }
17588 }
Joe Malin32736f02011-01-19 16:14:20 -080017589
Mike Cleronf116bf82009-09-27 19:14:12 -070017590 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017591 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017592 if (now >= fadeStartTime) {
17593
17594 // the animation fades the scrollbars out by changing
17595 // the opacity (alpha) from fully opaque to fully
17596 // transparent
17597 int nextFrame = (int) now;
17598 int framesCount = 0;
17599
17600 Interpolator interpolator = scrollBarInterpolator;
17601
17602 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017603 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017604
17605 // End transparent
17606 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017607 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017608
17609 state = FADING;
17610
17611 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017612 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017613 }
17614 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017615 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017616
Svetoslav Ganova0156172011-06-26 17:55:44 -070017617 /**
17618 * Resuable callback for sending
17619 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17620 */
17621 private class SendViewScrolledAccessibilityEvent implements Runnable {
17622 public volatile boolean mIsPending;
17623
17624 public void run() {
17625 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17626 mIsPending = false;
17627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017628 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017629
17630 /**
17631 * <p>
17632 * This class represents a delegate that can be registered in a {@link View}
17633 * to enhance accessibility support via composition rather via inheritance.
17634 * It is specifically targeted to widget developers that extend basic View
17635 * classes i.e. classes in package android.view, that would like their
17636 * applications to be backwards compatible.
17637 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017638 * <div class="special reference">
17639 * <h3>Developer Guides</h3>
17640 * <p>For more information about making applications accessible, read the
17641 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17642 * developer guide.</p>
17643 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017644 * <p>
17645 * A scenario in which a developer would like to use an accessibility delegate
17646 * is overriding a method introduced in a later API version then the minimal API
17647 * version supported by the application. For example, the method
17648 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17649 * in API version 4 when the accessibility APIs were first introduced. If a
17650 * developer would like his application to run on API version 4 devices (assuming
17651 * all other APIs used by the application are version 4 or lower) and take advantage
17652 * of this method, instead of overriding the method which would break the application's
17653 * backwards compatibility, he can override the corresponding method in this
17654 * delegate and register the delegate in the target View if the API version of
17655 * the system is high enough i.e. the API version is same or higher to the API
17656 * version that introduced
17657 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17658 * </p>
17659 * <p>
17660 * Here is an example implementation:
17661 * </p>
17662 * <code><pre><p>
17663 * if (Build.VERSION.SDK_INT >= 14) {
17664 * // If the API version is equal of higher than the version in
17665 * // which onInitializeAccessibilityNodeInfo was introduced we
17666 * // register a delegate with a customized implementation.
17667 * View view = findViewById(R.id.view_id);
17668 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17669 * public void onInitializeAccessibilityNodeInfo(View host,
17670 * AccessibilityNodeInfo info) {
17671 * // Let the default implementation populate the info.
17672 * super.onInitializeAccessibilityNodeInfo(host, info);
17673 * // Set some other information.
17674 * info.setEnabled(host.isEnabled());
17675 * }
17676 * });
17677 * }
17678 * </code></pre></p>
17679 * <p>
17680 * This delegate contains methods that correspond to the accessibility methods
17681 * in View. If a delegate has been specified the implementation in View hands
17682 * off handling to the corresponding method in this delegate. The default
17683 * implementation the delegate methods behaves exactly as the corresponding
17684 * method in View for the case of no accessibility delegate been set. Hence,
17685 * to customize the behavior of a View method, clients can override only the
17686 * corresponding delegate method without altering the behavior of the rest
17687 * accessibility related methods of the host view.
17688 * </p>
17689 */
17690 public static class AccessibilityDelegate {
17691
17692 /**
17693 * Sends an accessibility event of the given type. If accessibility is not
17694 * enabled this method has no effect.
17695 * <p>
17696 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17697 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17698 * been set.
17699 * </p>
17700 *
17701 * @param host The View hosting the delegate.
17702 * @param eventType The type of the event to send.
17703 *
17704 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17705 */
17706 public void sendAccessibilityEvent(View host, int eventType) {
17707 host.sendAccessibilityEventInternal(eventType);
17708 }
17709
17710 /**
alanv8eeefef2012-05-07 16:57:53 -070017711 * Performs the specified accessibility action on the view. For
17712 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17713 * <p>
17714 * The default implementation behaves as
17715 * {@link View#performAccessibilityAction(int, Bundle)
17716 * View#performAccessibilityAction(int, Bundle)} for the case of
17717 * no accessibility delegate been set.
17718 * </p>
17719 *
17720 * @param action The action to perform.
17721 * @return Whether the action was performed.
17722 *
17723 * @see View#performAccessibilityAction(int, Bundle)
17724 * View#performAccessibilityAction(int, Bundle)
17725 */
17726 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17727 return host.performAccessibilityActionInternal(action, args);
17728 }
17729
17730 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017731 * Sends an accessibility event. This method behaves exactly as
17732 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17733 * empty {@link AccessibilityEvent} and does not perform a check whether
17734 * accessibility is enabled.
17735 * <p>
17736 * The default implementation behaves as
17737 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17738 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17739 * the case of no accessibility delegate been set.
17740 * </p>
17741 *
17742 * @param host The View hosting the delegate.
17743 * @param event The event to send.
17744 *
17745 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17746 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17747 */
17748 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17749 host.sendAccessibilityEventUncheckedInternal(event);
17750 }
17751
17752 /**
17753 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17754 * to its children for adding their text content to the event.
17755 * <p>
17756 * The default implementation behaves as
17757 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17758 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17759 * the case of no accessibility delegate been set.
17760 * </p>
17761 *
17762 * @param host The View hosting the delegate.
17763 * @param event The event.
17764 * @return True if the event population was completed.
17765 *
17766 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17767 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17768 */
17769 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17770 return host.dispatchPopulateAccessibilityEventInternal(event);
17771 }
17772
17773 /**
17774 * Gives a chance to the host View to populate the accessibility event with its
17775 * text content.
17776 * <p>
17777 * The default implementation behaves as
17778 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17779 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17780 * the case of no accessibility delegate been set.
17781 * </p>
17782 *
17783 * @param host The View hosting the delegate.
17784 * @param event The accessibility event which to populate.
17785 *
17786 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17787 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17788 */
17789 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17790 host.onPopulateAccessibilityEventInternal(event);
17791 }
17792
17793 /**
17794 * Initializes an {@link AccessibilityEvent} with information about the
17795 * the host View which is the event source.
17796 * <p>
17797 * The default implementation behaves as
17798 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17799 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17800 * the case of no accessibility delegate been set.
17801 * </p>
17802 *
17803 * @param host The View hosting the delegate.
17804 * @param event The event to initialize.
17805 *
17806 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17807 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17808 */
17809 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17810 host.onInitializeAccessibilityEventInternal(event);
17811 }
17812
17813 /**
17814 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17815 * <p>
17816 * The default implementation behaves as
17817 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17818 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17819 * the case of no accessibility delegate been set.
17820 * </p>
17821 *
17822 * @param host The View hosting the delegate.
17823 * @param info The instance to initialize.
17824 *
17825 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17826 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17827 */
17828 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17829 host.onInitializeAccessibilityNodeInfoInternal(info);
17830 }
17831
17832 /**
17833 * Called when a child of the host View has requested sending an
17834 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17835 * to augment the event.
17836 * <p>
17837 * The default implementation behaves as
17838 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17839 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17840 * the case of no accessibility delegate been set.
17841 * </p>
17842 *
17843 * @param host The View hosting the delegate.
17844 * @param child The child which requests sending the event.
17845 * @param event The event to be sent.
17846 * @return True if the event should be sent
17847 *
17848 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17849 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17850 */
17851 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17852 AccessibilityEvent event) {
17853 return host.onRequestSendAccessibilityEventInternal(child, event);
17854 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017855
17856 /**
17857 * Gets the provider for managing a virtual view hierarchy rooted at this View
17858 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17859 * that explore the window content.
17860 * <p>
17861 * The default implementation behaves as
17862 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17863 * the case of no accessibility delegate been set.
17864 * </p>
17865 *
17866 * @return The provider.
17867 *
17868 * @see AccessibilityNodeProvider
17869 */
17870 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17871 return null;
17872 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017874}