blob: 48819a115c03c461030428b4904cb659f799b222 [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 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002133 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2134 * is used to check whether later changes to the view's transform should invalidate the
2135 * view to force the quickReject test to run again.
2136 */
Chet Haase21433372012-06-05 07:54:09 -07002137 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002138
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002139 // Accessiblity constants for mPrivateFlags2
2140
2141 /**
2142 * Shift for the bits in {@link #mPrivateFlags2} related to the
2143 * "accessibilityFocusable" attribute.
2144 */
Chet Haase21433372012-06-05 07:54:09 -07002145 static final int ACCESSIBILITY_FOCUSABLE_SHIFT = 29;
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002146
2147 /**
2148 * The system determines whether the view can take accessibility focus - default (recommended).
2149 * <p>
2150 * Such a view is consideted by the focus search if it is:
2151 * <ul>
2152 * <li>
2153 * Important for accessibility and actionable (clickable, long clickable, focusable)
2154 * </li>
2155 * <li>
2156 * Important for accessibility, not actionable (clickable, long clickable, focusable),
2157 * and does not have an actionable predecessor.
2158 * </li>
2159 * </ul>
2160 * An accessibility srvice can request putting accessibility focus on such a view.
2161 * </p>
2162 *
2163 * @hide
2164 */
2165 public static final int ACCESSIBILITY_FOCUSABLE_AUTO = 0x00000000;
2166
2167 /**
2168 * The view can take accessibility focus.
2169 * <p>
2170 * A view that can take accessibility focus is always considered during focus
2171 * search and an accessibility service can request putting accessibility focus
2172 * on it.
2173 * </p>
2174 *
2175 * @hide
2176 */
2177 public static final int ACCESSIBILITY_FOCUSABLE_YES = 0x00000001;
2178
2179 /**
2180 * The view can not take accessibility focus.
2181 * <p>
2182 * A view that can not take accessibility focus is never considered during focus
2183 * search and an accessibility service can not request putting accessibility focus
2184 * on it.
2185 * </p>
2186 *
2187 * @hide
2188 */
2189 public static final int ACCESSIBILITY_FOCUSABLE_NO = 0x00000002;
2190
2191 /**
2192 * The default whether the view is accessiblity focusable.
2193 */
2194 static final int ACCESSIBILITY_FOCUSABLE_DEFAULT = ACCESSIBILITY_FOCUSABLE_AUTO;
2195
2196 /**
2197 * Mask for obtainig the bits which specifies how to determine
2198 * whether a view is accessibility focusable.
2199 */
2200 static final int ACCESSIBILITY_FOCUSABLE_MASK = (ACCESSIBILITY_FOCUSABLE_AUTO
2201 | ACCESSIBILITY_FOCUSABLE_YES | ACCESSIBILITY_FOCUSABLE_NO)
2202 << ACCESSIBILITY_FOCUSABLE_SHIFT;
2203
2204
Christopher Tate3d4bf172011-03-28 16:16:46 -07002205 /* End of masks for mPrivateFlags2 */
2206
Chet Haase21433372012-06-05 07:54:09 -07002207 /* Masks for mPrivateFlags3 */
2208
2209 /**
2210 * Flag indicating that view has a transform animation set on it. This is used to track whether
2211 * an animation is cleared between successive frames, in order to tell the associated
2212 * DisplayList to clear its animation matrix.
2213 */
2214 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2215
2216 /**
2217 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2218 * animation is cleared between successive frames, in order to tell the associated
2219 * DisplayList to restore its alpha value.
2220 */
2221 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2222
2223
2224 /* End of masks for mPrivateFlags3 */
2225
Christopher Tate3d4bf172011-03-28 16:16:46 -07002226 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2227
Chet Haasedaf98e92011-01-10 14:10:36 -08002228 /**
Adam Powell637d3372010-08-25 14:37:03 -07002229 * Always allow a user to over-scroll this view, provided it is a
2230 * view that can scroll.
2231 *
2232 * @see #getOverScrollMode()
2233 * @see #setOverScrollMode(int)
2234 */
2235 public static final int OVER_SCROLL_ALWAYS = 0;
2236
2237 /**
2238 * Allow a user to over-scroll this view only if the content is large
2239 * enough to meaningfully scroll, provided it is a view that can scroll.
2240 *
2241 * @see #getOverScrollMode()
2242 * @see #setOverScrollMode(int)
2243 */
2244 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2245
2246 /**
2247 * Never allow a user to over-scroll this view.
2248 *
2249 * @see #getOverScrollMode()
2250 * @see #setOverScrollMode(int)
2251 */
2252 public static final int OVER_SCROLL_NEVER = 2;
2253
2254 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002255 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2256 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002257 *
Joe Malin32736f02011-01-19 16:14:20 -08002258 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002259 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002260 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002261
2262 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002263 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2264 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002265 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002266 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002267 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002268 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002269 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002270 *
Joe Malin32736f02011-01-19 16:14:20 -08002271 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002272 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002273 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2274
2275 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002276 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2277 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002278 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002279 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002280 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2281 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2282 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002283 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002284 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2285 * window flags) for displaying content using every last pixel on the display.
2286 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002287 * <p>There is a limitation: because navigation controls are so important, the least user
2288 * interaction will cause them to reappear immediately. When this happens, both
2289 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2290 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002291 *
2292 * @see #setSystemUiVisibility(int)
2293 */
2294 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2295
2296 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002297 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2298 * into the normal fullscreen mode so that its content can take over the screen
2299 * while still allowing the user to interact with the application.
2300 *
2301 * <p>This has the same visual effect as
2302 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2303 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2304 * meaning that non-critical screen decorations (such as the status bar) will be
2305 * hidden while the user is in the View's window, focusing the experience on
2306 * that content. Unlike the window flag, if you are using ActionBar in
2307 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2308 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2309 * hide the action bar.
2310 *
2311 * <p>This approach to going fullscreen is best used over the window flag when
2312 * it is a transient state -- that is, the application does this at certain
2313 * points in its user interaction where it wants to allow the user to focus
2314 * on content, but not as a continuous state. For situations where the application
2315 * would like to simply stay full screen the entire time (such as a game that
2316 * wants to take over the screen), the
2317 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2318 * is usually a better approach. The state set here will be removed by the system
2319 * in various situations (such as the user moving to another application) like
2320 * the other system UI states.
2321 *
2322 * <p>When using this flag, the application should provide some easy facility
2323 * for the user to go out of it. A common example would be in an e-book
2324 * reader, where tapping on the screen brings back whatever screen and UI
2325 * decorations that had been hidden while the user was immersed in reading
2326 * the book.
2327 *
2328 * @see #setSystemUiVisibility(int)
2329 */
2330 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2331
2332 /**
2333 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2334 * flags, we would like a stable view of the content insets given to
2335 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2336 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002337 * as a continuous state. In the stock Android UI this is the space for
2338 * the system bar, nav bar, and status bar, but not more transient elements
2339 * such as an input method.
2340 *
2341 * The stable layout your UI sees is based on the system UI modes you can
2342 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2343 * then you will get a stable layout for changes of the
2344 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2345 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2346 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2347 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2348 * with a stable layout. (Note that you should avoid using
2349 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2350 *
2351 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2352 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2353 * then a hidden status bar will be considered a "stable" state for purposes
2354 * here. This allows your UI to continually hide the status bar, while still
2355 * using the system UI flags to hide the action bar while still retaining
2356 * a stable layout. Note that changing the window fullscreen flag will never
2357 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002358 *
2359 * <p>If you are using ActionBar in
2360 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2361 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2362 * insets it adds to those given to the application.
2363 */
2364 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
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_HIDE_NAVIGATION}, 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 the navagation system UI through the
2374 * {@link #fitSystemWindows(Rect)} method.
2375 */
2376 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2377
2378 /**
2379 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2380 * to be layed out as if it has requested
2381 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2382 * allows it to avoid artifacts when switching in and out of that mode, at
2383 * the expense that some of its user interface may be covered by screen
2384 * decorations when they are shown. You can perform layout of your inner
2385 * UI elements to account for non-fullscreen system UI through the
2386 * {@link #fitSystemWindows(Rect)} method.
2387 */
2388 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2389
2390 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002391 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2392 */
2393 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2394
2395 /**
2396 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2397 */
2398 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002399
2400 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002401 * @hide
2402 *
2403 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2404 * out of the public fields to keep the undefined bits out of the developer's way.
2405 *
2406 * Flag to make the status bar not expandable. Unless you also
2407 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2408 */
2409 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2410
2411 /**
2412 * @hide
2413 *
2414 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2415 * out of the public fields to keep the undefined bits out of the developer's way.
2416 *
2417 * Flag to hide notification icons and scrolling ticker text.
2418 */
2419 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2420
2421 /**
2422 * @hide
2423 *
2424 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2425 * out of the public fields to keep the undefined bits out of the developer's way.
2426 *
2427 * Flag to disable incoming notification alerts. This will not block
2428 * icons, but it will block sound, vibrating and other visual or aural notifications.
2429 */
2430 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
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 only the scrolling ticker. Note that
2439 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2440 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2441 */
2442 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2443
2444 /**
2445 * @hide
2446 *
2447 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2448 * out of the public fields to keep the undefined bits out of the developer's way.
2449 *
2450 * Flag to hide the center system info area.
2451 */
2452 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2453
2454 /**
2455 * @hide
2456 *
2457 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2458 * out of the public fields to keep the undefined bits out of the developer's way.
2459 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002460 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002461 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2462 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002463 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002464
2465 /**
2466 * @hide
2467 *
2468 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2469 * out of the public fields to keep the undefined bits out of the developer's way.
2470 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002471 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002472 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2473 */
2474 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2475
2476 /**
2477 * @hide
2478 *
2479 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2480 * out of the public fields to keep the undefined bits out of the developer's way.
2481 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002482 * Flag to hide only the clock. You might use this if your activity has
2483 * its own clock making the status bar's clock redundant.
2484 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002485 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2486
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002487 /**
2488 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002489 *
2490 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2491 * out of the public fields to keep the undefined bits out of the developer's way.
2492 *
2493 * Flag to hide only the recent apps button. Don't use this
2494 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2495 */
2496 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2497
2498 /**
2499 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002500 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002501 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002502
2503 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002504 * These are the system UI flags that can be cleared by events outside
2505 * of an application. Currently this is just the ability to tap on the
2506 * screen while hiding the navigation bar to have it return.
2507 * @hide
2508 */
2509 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002510 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2511 | SYSTEM_UI_FLAG_FULLSCREEN;
2512
2513 /**
2514 * Flags that can impact the layout in relation to system UI.
2515 */
2516 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2517 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2518 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002519
2520 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002521 * Find views that render the specified text.
2522 *
2523 * @see #findViewsWithText(ArrayList, CharSequence, int)
2524 */
2525 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2526
2527 /**
2528 * Find find views that contain the specified content description.
2529 *
2530 * @see #findViewsWithText(ArrayList, CharSequence, int)
2531 */
2532 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2533
2534 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002535 * Find views that contain {@link AccessibilityNodeProvider}. Such
2536 * a View is a root of virtual view hierarchy and may contain the searched
2537 * text. If this flag is set Views with providers are automatically
2538 * added and it is a responsibility of the client to call the APIs of
2539 * the provider to determine whether the virtual tree rooted at this View
2540 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2541 * represeting the virtual views with this text.
2542 *
2543 * @see #findViewsWithText(ArrayList, CharSequence, int)
2544 *
2545 * @hide
2546 */
2547 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2548
2549 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002550 * The undefined cursor position.
2551 */
2552 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2553
2554 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002555 * Indicates that the screen has changed state and is now off.
2556 *
2557 * @see #onScreenStateChanged(int)
2558 */
2559 public static final int SCREEN_STATE_OFF = 0x0;
2560
2561 /**
2562 * Indicates that the screen has changed state and is now on.
2563 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002564 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002565 */
2566 public static final int SCREEN_STATE_ON = 0x1;
2567
2568 /**
Adam Powell637d3372010-08-25 14:37:03 -07002569 * Controls the over-scroll mode for this view.
2570 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2571 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2572 * and {@link #OVER_SCROLL_NEVER}.
2573 */
2574 private int mOverScrollMode;
2575
2576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 * The parent this view is attached to.
2578 * {@hide}
2579 *
2580 * @see #getParent()
2581 */
2582 protected ViewParent mParent;
2583
2584 /**
2585 * {@hide}
2586 */
2587 AttachInfo mAttachInfo;
2588
2589 /**
2590 * {@hide}
2591 */
Romain Guy809a7f62009-05-14 15:44:42 -07002592 @ViewDebug.ExportedProperty(flagMapping = {
2593 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2594 name = "FORCE_LAYOUT"),
2595 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2596 name = "LAYOUT_REQUIRED"),
2597 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002598 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002599 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2600 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2601 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2602 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2603 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002605 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002606 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607
2608 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002609 * This view's request for the visibility of the status bar.
2610 * @hide
2611 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002612 @ViewDebug.ExportedProperty(flagMapping = {
2613 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2614 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2615 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2616 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2617 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2618 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2619 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2620 equals = SYSTEM_UI_FLAG_VISIBLE,
2621 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2622 })
Joe Onorato664644d2011-01-23 17:53:23 -08002623 int mSystemUiVisibility;
2624
2625 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002626 * Reference count for transient state.
2627 * @see #setHasTransientState(boolean)
2628 */
2629 int mTransientStateCount = 0;
2630
2631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 * Count of how many windows this view has been attached to.
2633 */
2634 int mWindowAttachCount;
2635
2636 /**
2637 * The layout parameters associated with this view and used by the parent
2638 * {@link android.view.ViewGroup} to determine how this view should be
2639 * laid out.
2640 * {@hide}
2641 */
2642 protected ViewGroup.LayoutParams mLayoutParams;
2643
2644 /**
2645 * The view flags hold various views states.
2646 * {@hide}
2647 */
2648 @ViewDebug.ExportedProperty
2649 int mViewFlags;
2650
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002651 static class TransformationInfo {
2652 /**
2653 * The transform matrix for the View. This transform is calculated internally
2654 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2655 * is used by default. Do *not* use this variable directly; instead call
2656 * getMatrix(), which will automatically recalculate the matrix if necessary
2657 * to get the correct matrix based on the latest rotation and scale properties.
2658 */
2659 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002660
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002661 /**
2662 * The transform matrix for the View. This transform is calculated internally
2663 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2664 * is used by default. Do *not* use this variable directly; instead call
2665 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2666 * to get the correct matrix based on the latest rotation and scale properties.
2667 */
2668 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002669
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002670 /**
2671 * An internal variable that tracks whether we need to recalculate the
2672 * transform matrix, based on whether the rotation or scaleX/Y properties
2673 * have changed since the matrix was last calculated.
2674 */
2675 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002676
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002677 /**
2678 * An internal variable that tracks whether we need to recalculate the
2679 * transform matrix, based on whether the rotation or scaleX/Y properties
2680 * have changed since the matrix was last calculated.
2681 */
2682 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002683
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002684 /**
2685 * A variable that tracks whether we need to recalculate the
2686 * transform matrix, based on whether the rotation or scaleX/Y properties
2687 * have changed since the matrix was last calculated. This variable
2688 * is only valid after a call to updateMatrix() or to a function that
2689 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2690 */
2691 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002692
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002693 /**
2694 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2695 */
2696 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002697
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002698 /**
2699 * This matrix is used when computing the matrix for 3D rotations.
2700 */
2701 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002702
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002703 /**
2704 * These prev values are used to recalculate a centered pivot point when necessary. The
2705 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2706 * set), so thes values are only used then as well.
2707 */
2708 private int mPrevWidth = -1;
2709 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002710
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002711 /**
2712 * The degrees rotation around the vertical axis through the pivot point.
2713 */
2714 @ViewDebug.ExportedProperty
2715 float mRotationY = 0f;
2716
2717 /**
2718 * The degrees rotation around the horizontal axis through the pivot point.
2719 */
2720 @ViewDebug.ExportedProperty
2721 float mRotationX = 0f;
2722
2723 /**
2724 * The degrees rotation around the pivot point.
2725 */
2726 @ViewDebug.ExportedProperty
2727 float mRotation = 0f;
2728
2729 /**
2730 * The amount of translation of the object away from its left property (post-layout).
2731 */
2732 @ViewDebug.ExportedProperty
2733 float mTranslationX = 0f;
2734
2735 /**
2736 * The amount of translation of the object away from its top property (post-layout).
2737 */
2738 @ViewDebug.ExportedProperty
2739 float mTranslationY = 0f;
2740
2741 /**
2742 * The amount of scale in the x direction around the pivot point. A
2743 * value of 1 means no scaling is applied.
2744 */
2745 @ViewDebug.ExportedProperty
2746 float mScaleX = 1f;
2747
2748 /**
2749 * The amount of scale in the y direction around the pivot point. A
2750 * value of 1 means no scaling is applied.
2751 */
2752 @ViewDebug.ExportedProperty
2753 float mScaleY = 1f;
2754
2755 /**
Chet Haasea33de552012-02-03 16:28:24 -08002756 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002757 */
2758 @ViewDebug.ExportedProperty
2759 float mPivotX = 0f;
2760
2761 /**
Chet Haasea33de552012-02-03 16:28:24 -08002762 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002763 */
2764 @ViewDebug.ExportedProperty
2765 float mPivotY = 0f;
2766
2767 /**
2768 * The opacity of the View. This is a value from 0 to 1, where 0 means
2769 * completely transparent and 1 means completely opaque.
2770 */
2771 @ViewDebug.ExportedProperty
2772 float mAlpha = 1f;
2773 }
2774
2775 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002776
Joe Malin32736f02011-01-19 16:14:20 -08002777 private boolean mLastIsOpaque;
2778
Chet Haasefd2b0022010-08-06 13:08:56 -07002779 /**
2780 * Convenience value to check for float values that are close enough to zero to be considered
2781 * zero.
2782 */
Romain Guy2542d192010-08-18 11:47:12 -07002783 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002784
2785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 * The distance in pixels from the left edge of this view's parent
2787 * to the left edge of this view.
2788 * {@hide}
2789 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002790 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 protected int mLeft;
2792 /**
2793 * The distance in pixels from the left edge of this view's parent
2794 * to the right edge of this view.
2795 * {@hide}
2796 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002797 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 protected int mRight;
2799 /**
2800 * The distance in pixels from the top edge of this view's parent
2801 * to the top edge of this view.
2802 * {@hide}
2803 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002804 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 protected int mTop;
2806 /**
2807 * The distance in pixels from the top edge of this view's parent
2808 * to the bottom edge of this view.
2809 * {@hide}
2810 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002811 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 protected int mBottom;
2813
2814 /**
2815 * The offset, in pixels, by which the content of this view is scrolled
2816 * horizontally.
2817 * {@hide}
2818 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002819 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 protected int mScrollX;
2821 /**
2822 * The offset, in pixels, by which the content of this view is scrolled
2823 * vertically.
2824 * {@hide}
2825 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002826 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 protected int mScrollY;
2828
2829 /**
2830 * The left padding in pixels, that is the distance in pixels between the
2831 * left edge of this view and the left edge of its content.
2832 * {@hide}
2833 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002834 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 protected int mPaddingLeft;
2836 /**
2837 * The right padding in pixels, that is the distance in pixels between the
2838 * right edge of this view and the right edge of its content.
2839 * {@hide}
2840 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002841 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 protected int mPaddingRight;
2843 /**
2844 * The top padding in pixels, that is the distance in pixels between the
2845 * top edge of this view and the top edge of its content.
2846 * {@hide}
2847 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002848 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 protected int mPaddingTop;
2850 /**
2851 * The bottom padding in pixels, that is the distance in pixels between the
2852 * bottom edge of this view and the bottom edge of its content.
2853 * {@hide}
2854 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002855 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 protected int mPaddingBottom;
2857
2858 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002859 * The layout insets in pixels, that is the distance in pixels between the
2860 * visible edges of this view its bounds.
2861 */
2862 private Insets mLayoutInsets;
2863
2864 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002865 * Briefly describes the view and is primarily used for accessibility support.
2866 */
2867 private CharSequence mContentDescription;
2868
2869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002871 *
2872 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002874 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002875 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876
2877 /**
2878 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002879 *
2880 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002882 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002883 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002885 /**
Adam Powell20232d02010-12-08 21:08:53 -08002886 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002887 *
2888 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002889 */
2890 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002891 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002892
2893 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002894 * Cache if the user padding is relative.
2895 *
2896 */
2897 @ViewDebug.ExportedProperty(category = "padding")
2898 boolean mUserPaddingRelative;
2899
2900 /**
2901 * Cache the paddingStart set by the user to append to the scrollbar's size.
2902 *
2903 */
2904 @ViewDebug.ExportedProperty(category = "padding")
2905 int mUserPaddingStart;
2906
2907 /**
2908 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2909 *
2910 */
2911 @ViewDebug.ExportedProperty(category = "padding")
2912 int mUserPaddingEnd;
2913
2914 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002915 * @hide
2916 */
2917 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2918 /**
2919 * @hide
2920 */
2921 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
Philip Milne6c8ea062012-04-03 17:38:43 -07002923 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924
2925 private int mBackgroundResource;
2926 private boolean mBackgroundSizeChanged;
2927
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002928 static class ListenerInfo {
2929 /**
2930 * Listener used to dispatch focus change events.
2931 * This field should be made private, so it is hidden from the SDK.
2932 * {@hide}
2933 */
2934 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002936 /**
2937 * Listeners for layout change events.
2938 */
2939 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002940
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002941 /**
2942 * Listeners for attach events.
2943 */
2944 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002945
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002946 /**
2947 * Listener used to dispatch click events.
2948 * This field should be made private, so it is hidden from the SDK.
2949 * {@hide}
2950 */
2951 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002953 /**
2954 * Listener used to dispatch long click events.
2955 * This field should be made private, so it is hidden from the SDK.
2956 * {@hide}
2957 */
2958 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 /**
2961 * Listener used to build the context menu.
2962 * This field should be made private, so it is hidden from the SDK.
2963 * {@hide}
2964 */
2965 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002967 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002969 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002971 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002972
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002973 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002974
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002975 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002976
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002977 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2978 }
2979
2980 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 /**
2983 * The application environment this view lives in.
2984 * This field should be made private, so it is hidden from the SDK.
2985 * {@hide}
2986 */
2987 protected Context mContext;
2988
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002989 private final Resources mResources;
2990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 private ScrollabilityCache mScrollCache;
2992
2993 private int[] mDrawableState = null;
2994
Romain Guy0211a0a2011-02-14 16:34:59 -08002995 /**
2996 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002997 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002998 * @hide
2999 */
3000 public boolean mCachingFailed;
3001
Romain Guy02890fd2010-08-06 17:58:44 -07003002 private Bitmap mDrawingCache;
3003 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003004 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003005 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006
3007 /**
3008 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3009 * the user may specify which view to go to next.
3010 */
3011 private int mNextFocusLeftId = View.NO_ID;
3012
3013 /**
3014 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3015 * the user may specify which view to go to next.
3016 */
3017 private int mNextFocusRightId = View.NO_ID;
3018
3019 /**
3020 * When this view has focus and the next focus is {@link #FOCUS_UP},
3021 * the user may specify which view to go to next.
3022 */
3023 private int mNextFocusUpId = View.NO_ID;
3024
3025 /**
3026 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3027 * the user may specify which view to go to next.
3028 */
3029 private int mNextFocusDownId = View.NO_ID;
3030
Jeff Brown4e6319b2010-12-13 10:36:51 -08003031 /**
3032 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3033 * the user may specify which view to go to next.
3034 */
3035 int mNextFocusForwardId = View.NO_ID;
3036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003038 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003039 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003040 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 private UnsetPressedState mUnsetPressedState;
3043
3044 /**
3045 * Whether the long press's action has been invoked. The tap's action is invoked on the
3046 * up event while a long press is invoked as soon as the long press duration is reached, so
3047 * a long press could be performed before the tap is checked, in which case the tap's action
3048 * should not be invoked.
3049 */
3050 private boolean mHasPerformedLongPress;
3051
3052 /**
3053 * The minimum height of the view. We'll try our best to have the height
3054 * of this view to at least this amount.
3055 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003056 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 private int mMinHeight;
3058
3059 /**
3060 * The minimum width of the view. We'll try our best to have the width
3061 * of this view to at least this amount.
3062 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003063 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 private int mMinWidth;
3065
3066 /**
3067 * The delegate to handle touch events that are physically in this view
3068 * but should be handled by another view.
3069 */
3070 private TouchDelegate mTouchDelegate = null;
3071
3072 /**
3073 * Solid color to use as a background when creating the drawing cache. Enables
3074 * the cache to use 16 bit bitmaps instead of 32 bit.
3075 */
3076 private int mDrawingCacheBackgroundColor = 0;
3077
3078 /**
3079 * Special tree observer used when mAttachInfo is null.
3080 */
3081 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003082
Adam Powelle14579b2009-12-16 18:39:52 -08003083 /**
3084 * Cache the touch slop from the context that created the view.
3085 */
3086 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003089 * Object that handles automatic animation of view properties.
3090 */
3091 private ViewPropertyAnimator mAnimator = null;
3092
3093 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003094 * Flag indicating that a drag can cross window boundaries. When
3095 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3096 * with this flag set, all visible applications will be able to participate
3097 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003098 *
3099 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003100 */
3101 public static final int DRAG_FLAG_GLOBAL = 1;
3102
3103 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003104 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3105 */
3106 private float mVerticalScrollFactor;
3107
3108 /**
Adam Powell20232d02010-12-08 21:08:53 -08003109 * Position of the vertical scroll bar.
3110 */
3111 private int mVerticalScrollbarPosition;
3112
3113 /**
3114 * Position the scroll bar at the default position as determined by the system.
3115 */
3116 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3117
3118 /**
3119 * Position the scroll bar along the left edge.
3120 */
3121 public static final int SCROLLBAR_POSITION_LEFT = 1;
3122
3123 /**
3124 * Position the scroll bar along the right edge.
3125 */
3126 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3127
3128 /**
Romain Guy171c5922011-01-06 10:04:23 -08003129 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003130 *
3131 * @see #getLayerType()
3132 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003133 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003134 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003135 */
3136 public static final int LAYER_TYPE_NONE = 0;
3137
3138 /**
3139 * <p>Indicates that the view has a software layer. A software layer is backed
3140 * by a bitmap and causes the view to be rendered using Android's software
3141 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003142 *
Romain Guy171c5922011-01-06 10:04:23 -08003143 * <p>Software layers have various usages:</p>
3144 * <p>When the application is not using hardware acceleration, a software layer
3145 * is useful to apply a specific color filter and/or blending mode and/or
3146 * translucency to a view and all its children.</p>
3147 * <p>When the application is using hardware acceleration, a software layer
3148 * is useful to render drawing primitives not supported by the hardware
3149 * accelerated pipeline. It can also be used to cache a complex view tree
3150 * into a texture and reduce the complexity of drawing operations. For instance,
3151 * when animating a complex view tree with a translation, a software layer can
3152 * be used to render the view tree only once.</p>
3153 * <p>Software layers should be avoided when the affected view tree updates
3154 * often. Every update will require to re-render the software layer, which can
3155 * potentially be slow (particularly when hardware acceleration is turned on
3156 * since the layer will have to be uploaded into a hardware texture after every
3157 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003158 *
3159 * @see #getLayerType()
3160 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003161 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003162 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003163 */
3164 public static final int LAYER_TYPE_SOFTWARE = 1;
3165
3166 /**
3167 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3168 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3169 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3170 * rendering pipeline, but only if hardware acceleration is turned on for the
3171 * view hierarchy. When hardware acceleration is turned off, hardware layers
3172 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003173 *
Romain Guy171c5922011-01-06 10:04:23 -08003174 * <p>A hardware layer is useful to apply a specific color filter and/or
3175 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003176 * <p>A hardware layer can be used to cache a complex view tree into a
3177 * texture and reduce the complexity of drawing operations. For instance,
3178 * when animating a complex view tree with a translation, a hardware layer can
3179 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003180 * <p>A hardware layer can also be used to increase the rendering quality when
3181 * rotation transformations are applied on a view. It can also be used to
3182 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003183 *
3184 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003185 * @see #setLayerType(int, android.graphics.Paint)
3186 * @see #LAYER_TYPE_NONE
3187 * @see #LAYER_TYPE_SOFTWARE
3188 */
3189 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003190
Romain Guy3aaff3a2011-01-12 14:18:47 -08003191 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3192 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3193 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3194 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3195 })
Romain Guy171c5922011-01-06 10:04:23 -08003196 int mLayerType = LAYER_TYPE_NONE;
3197 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003198 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003199
3200 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003201 * Set to true when the view is sending hover accessibility events because it
3202 * is the innermost hovered view.
3203 */
3204 private boolean mSendingHoverAccessibilityEvents;
3205
3206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 * Simple constructor to use when creating a view from code.
3208 *
3209 * @param context The Context the view is running in, through which it can
3210 * access the current theme, resources, etc.
3211 */
3212 public View(Context context) {
3213 mContext = context;
3214 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003215 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003216 // Set layout and text direction defaults
3217 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003218 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003219 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07003220 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT) |
3221 (ACCESSIBILITY_FOCUSABLE_DEFAULT << ACCESSIBILITY_FOCUSABLE_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003222 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003223 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003224 mUserPaddingStart = -1;
3225 mUserPaddingEnd = -1;
3226 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003230 * Delegate for injecting accessiblity functionality.
3231 */
3232 AccessibilityDelegate mAccessibilityDelegate;
3233
3234 /**
3235 * Consistency verifier for debugging purposes.
3236 * @hide
3237 */
3238 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3239 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3240 new InputEventConsistencyVerifier(this, 0) : null;
3241
3242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 * Constructor that is called when inflating a view from XML. This is called
3244 * when a view is being constructed from an XML file, supplying attributes
3245 * that were specified in the XML file. This version uses a default style of
3246 * 0, so the only attribute values applied are those in the Context's Theme
3247 * and the given AttributeSet.
3248 *
3249 * <p>
3250 * The method onFinishInflate() will be called after all children have been
3251 * added.
3252 *
3253 * @param context The Context the view is running in, through which it can
3254 * access the current theme, resources, etc.
3255 * @param attrs The attributes of the XML tag that is inflating the view.
3256 * @see #View(Context, AttributeSet, int)
3257 */
3258 public View(Context context, AttributeSet attrs) {
3259 this(context, attrs, 0);
3260 }
3261
3262 /**
3263 * Perform inflation from XML and apply a class-specific base style. This
3264 * constructor of View allows subclasses to use their own base style when
3265 * they are inflating. For example, a Button class's constructor would call
3266 * this version of the super class constructor and supply
3267 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3268 * the theme's button style to modify all of the base view attributes (in
3269 * particular its background) as well as the Button class's attributes.
3270 *
3271 * @param context The Context the view is running in, through which it can
3272 * access the current theme, resources, etc.
3273 * @param attrs The attributes of the XML tag that is inflating the view.
3274 * @param defStyle The default style to apply to this view. If 0, no style
3275 * will be applied (beyond what is included in the theme). This may
3276 * either be an attribute resource, whose value will be retrieved
3277 * from the current theme, or an explicit style resource.
3278 * @see #View(Context, AttributeSet)
3279 */
3280 public View(Context context, AttributeSet attrs, int defStyle) {
3281 this(context);
3282
3283 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3284 defStyle, 0);
3285
3286 Drawable background = null;
3287
3288 int leftPadding = -1;
3289 int topPadding = -1;
3290 int rightPadding = -1;
3291 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003292 int startPadding = -1;
3293 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294
3295 int padding = -1;
3296
3297 int viewFlagValues = 0;
3298 int viewFlagMasks = 0;
3299
3300 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 int x = 0;
3303 int y = 0;
3304
Chet Haase73066682010-11-29 15:55:32 -08003305 float tx = 0;
3306 float ty = 0;
3307 float rotation = 0;
3308 float rotationX = 0;
3309 float rotationY = 0;
3310 float sx = 1f;
3311 float sy = 1f;
3312 boolean transformSet = false;
3313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3315
Adam Powell637d3372010-08-25 14:37:03 -07003316 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 final int N = a.getIndexCount();
3318 for (int i = 0; i < N; i++) {
3319 int attr = a.getIndex(i);
3320 switch (attr) {
3321 case com.android.internal.R.styleable.View_background:
3322 background = a.getDrawable(attr);
3323 break;
3324 case com.android.internal.R.styleable.View_padding:
3325 padding = a.getDimensionPixelSize(attr, -1);
3326 break;
3327 case com.android.internal.R.styleable.View_paddingLeft:
3328 leftPadding = a.getDimensionPixelSize(attr, -1);
3329 break;
3330 case com.android.internal.R.styleable.View_paddingTop:
3331 topPadding = a.getDimensionPixelSize(attr, -1);
3332 break;
3333 case com.android.internal.R.styleable.View_paddingRight:
3334 rightPadding = a.getDimensionPixelSize(attr, -1);
3335 break;
3336 case com.android.internal.R.styleable.View_paddingBottom:
3337 bottomPadding = a.getDimensionPixelSize(attr, -1);
3338 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003339 case com.android.internal.R.styleable.View_paddingStart:
3340 startPadding = a.getDimensionPixelSize(attr, -1);
3341 break;
3342 case com.android.internal.R.styleable.View_paddingEnd:
3343 endPadding = a.getDimensionPixelSize(attr, -1);
3344 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 case com.android.internal.R.styleable.View_scrollX:
3346 x = a.getDimensionPixelOffset(attr, 0);
3347 break;
3348 case com.android.internal.R.styleable.View_scrollY:
3349 y = a.getDimensionPixelOffset(attr, 0);
3350 break;
Chet Haase73066682010-11-29 15:55:32 -08003351 case com.android.internal.R.styleable.View_alpha:
3352 setAlpha(a.getFloat(attr, 1f));
3353 break;
3354 case com.android.internal.R.styleable.View_transformPivotX:
3355 setPivotX(a.getDimensionPixelOffset(attr, 0));
3356 break;
3357 case com.android.internal.R.styleable.View_transformPivotY:
3358 setPivotY(a.getDimensionPixelOffset(attr, 0));
3359 break;
3360 case com.android.internal.R.styleable.View_translationX:
3361 tx = a.getDimensionPixelOffset(attr, 0);
3362 transformSet = true;
3363 break;
3364 case com.android.internal.R.styleable.View_translationY:
3365 ty = a.getDimensionPixelOffset(attr, 0);
3366 transformSet = true;
3367 break;
3368 case com.android.internal.R.styleable.View_rotation:
3369 rotation = a.getFloat(attr, 0);
3370 transformSet = true;
3371 break;
3372 case com.android.internal.R.styleable.View_rotationX:
3373 rotationX = a.getFloat(attr, 0);
3374 transformSet = true;
3375 break;
3376 case com.android.internal.R.styleable.View_rotationY:
3377 rotationY = a.getFloat(attr, 0);
3378 transformSet = true;
3379 break;
3380 case com.android.internal.R.styleable.View_scaleX:
3381 sx = a.getFloat(attr, 1f);
3382 transformSet = true;
3383 break;
3384 case com.android.internal.R.styleable.View_scaleY:
3385 sy = a.getFloat(attr, 1f);
3386 transformSet = true;
3387 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 case com.android.internal.R.styleable.View_id:
3389 mID = a.getResourceId(attr, NO_ID);
3390 break;
3391 case com.android.internal.R.styleable.View_tag:
3392 mTag = a.getText(attr);
3393 break;
3394 case com.android.internal.R.styleable.View_fitsSystemWindows:
3395 if (a.getBoolean(attr, false)) {
3396 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3397 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3398 }
3399 break;
3400 case com.android.internal.R.styleable.View_focusable:
3401 if (a.getBoolean(attr, false)) {
3402 viewFlagValues |= FOCUSABLE;
3403 viewFlagMasks |= FOCUSABLE_MASK;
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_focusableInTouchMode:
3407 if (a.getBoolean(attr, false)) {
3408 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3409 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3410 }
3411 break;
3412 case com.android.internal.R.styleable.View_clickable:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= CLICKABLE;
3415 viewFlagMasks |= CLICKABLE;
3416 }
3417 break;
3418 case com.android.internal.R.styleable.View_longClickable:
3419 if (a.getBoolean(attr, false)) {
3420 viewFlagValues |= LONG_CLICKABLE;
3421 viewFlagMasks |= LONG_CLICKABLE;
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_saveEnabled:
3425 if (!a.getBoolean(attr, true)) {
3426 viewFlagValues |= SAVE_DISABLED;
3427 viewFlagMasks |= SAVE_DISABLED_MASK;
3428 }
3429 break;
3430 case com.android.internal.R.styleable.View_duplicateParentState:
3431 if (a.getBoolean(attr, false)) {
3432 viewFlagValues |= DUPLICATE_PARENT_STATE;
3433 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3434 }
3435 break;
3436 case com.android.internal.R.styleable.View_visibility:
3437 final int visibility = a.getInt(attr, 0);
3438 if (visibility != 0) {
3439 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3440 viewFlagMasks |= VISIBILITY_MASK;
3441 }
3442 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003443 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003444 // Clear any layout direction flags (included resolved bits) already set
3445 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3446 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003447 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003448 final int value = (layoutDirection != -1) ?
3449 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3450 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003451 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 case com.android.internal.R.styleable.View_drawingCacheQuality:
3453 final int cacheQuality = a.getInt(attr, 0);
3454 if (cacheQuality != 0) {
3455 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3456 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3457 }
3458 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003459 case com.android.internal.R.styleable.View_contentDescription:
3460 mContentDescription = a.getString(attr);
3461 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3463 if (!a.getBoolean(attr, true)) {
3464 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3465 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3466 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003467 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3469 if (!a.getBoolean(attr, true)) {
3470 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3471 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3472 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003473 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 case R.styleable.View_scrollbars:
3475 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3476 if (scrollbars != SCROLLBARS_NONE) {
3477 viewFlagValues |= scrollbars;
3478 viewFlagMasks |= SCROLLBARS_MASK;
3479 initializeScrollbars(a);
3480 }
3481 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003482 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003484 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3485 // Ignore the attribute starting with ICS
3486 break;
3487 }
3488 // With builds < ICS, fall through and apply fading edges
3489 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3491 if (fadingEdge != FADING_EDGE_NONE) {
3492 viewFlagValues |= fadingEdge;
3493 viewFlagMasks |= FADING_EDGE_MASK;
3494 initializeFadingEdge(a);
3495 }
3496 break;
3497 case R.styleable.View_scrollbarStyle:
3498 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3499 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3500 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3501 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3502 }
3503 break;
3504 case R.styleable.View_isScrollContainer:
3505 setScrollContainer = true;
3506 if (a.getBoolean(attr, false)) {
3507 setScrollContainer(true);
3508 }
3509 break;
3510 case com.android.internal.R.styleable.View_keepScreenOn:
3511 if (a.getBoolean(attr, false)) {
3512 viewFlagValues |= KEEP_SCREEN_ON;
3513 viewFlagMasks |= KEEP_SCREEN_ON;
3514 }
3515 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003516 case R.styleable.View_filterTouchesWhenObscured:
3517 if (a.getBoolean(attr, false)) {
3518 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3519 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3520 }
3521 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 case R.styleable.View_nextFocusLeft:
3523 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3524 break;
3525 case R.styleable.View_nextFocusRight:
3526 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3527 break;
3528 case R.styleable.View_nextFocusUp:
3529 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3530 break;
3531 case R.styleable.View_nextFocusDown:
3532 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3533 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003534 case R.styleable.View_nextFocusForward:
3535 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3536 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 case R.styleable.View_minWidth:
3538 mMinWidth = a.getDimensionPixelSize(attr, 0);
3539 break;
3540 case R.styleable.View_minHeight:
3541 mMinHeight = a.getDimensionPixelSize(attr, 0);
3542 break;
Romain Guy9a817362009-05-01 10:57:14 -07003543 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003544 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003545 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003546 + "be used within a restricted context");
3547 }
3548
Romain Guy9a817362009-05-01 10:57:14 -07003549 final String handlerName = a.getString(attr);
3550 if (handlerName != null) {
3551 setOnClickListener(new OnClickListener() {
3552 private Method mHandler;
3553
3554 public void onClick(View v) {
3555 if (mHandler == null) {
3556 try {
3557 mHandler = getContext().getClass().getMethod(handlerName,
3558 View.class);
3559 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003560 int id = getId();
3561 String idText = id == NO_ID ? "" : " with id '"
3562 + getContext().getResources().getResourceEntryName(
3563 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003564 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003565 handlerName + "(View) in the activity "
3566 + getContext().getClass() + " for onClick handler"
3567 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003568 }
3569 }
3570
3571 try {
3572 mHandler.invoke(getContext(), View.this);
3573 } catch (IllegalAccessException e) {
3574 throw new IllegalStateException("Could not execute non "
3575 + "public method of the activity", e);
3576 } catch (InvocationTargetException e) {
3577 throw new IllegalStateException("Could not execute "
3578 + "method of the activity", e);
3579 }
3580 }
3581 });
3582 }
3583 break;
Adam Powell637d3372010-08-25 14:37:03 -07003584 case R.styleable.View_overScrollMode:
3585 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3586 break;
Adam Powell20232d02010-12-08 21:08:53 -08003587 case R.styleable.View_verticalScrollbarPosition:
3588 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3589 break;
Romain Guy171c5922011-01-06 10:04:23 -08003590 case R.styleable.View_layerType:
3591 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3592 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003593 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003594 // Clear any text direction flag already set
3595 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3596 // Set the text direction flags depending on the value of the attribute
3597 final int textDirection = a.getInt(attr, -1);
3598 if (textDirection != -1) {
3599 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3600 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003601 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003602 case R.styleable.View_textAlignment:
3603 // Clear any text alignment flag already set
3604 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3605 // Set the text alignment flag depending on the value of the attribute
3606 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3607 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3608 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003609 case R.styleable.View_importantForAccessibility:
3610 setImportantForAccessibility(a.getInt(attr,
3611 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613 }
3614
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003615 a.recycle();
3616
Adam Powell637d3372010-08-25 14:37:03 -07003617 setOverScrollMode(overScrollMode);
3618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003620 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 }
3622
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003623 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3624 // layout direction). Those cached values will be used later during padding resolution.
3625 mUserPaddingStart = startPadding;
3626 mUserPaddingEnd = endPadding;
3627
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003628 updateUserPaddingRelative();
3629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 if (padding >= 0) {
3631 leftPadding = padding;
3632 topPadding = padding;
3633 rightPadding = padding;
3634 bottomPadding = padding;
3635 }
3636
3637 // If the user specified the padding (either with android:padding or
3638 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3639 // use the default padding or the padding from the background drawable
3640 // (stored at this point in mPadding*)
3641 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3642 topPadding >= 0 ? topPadding : mPaddingTop,
3643 rightPadding >= 0 ? rightPadding : mPaddingRight,
3644 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3645
3646 if (viewFlagMasks != 0) {
3647 setFlags(viewFlagValues, viewFlagMasks);
3648 }
3649
3650 // Needs to be called after mViewFlags is set
3651 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3652 recomputePadding();
3653 }
3654
3655 if (x != 0 || y != 0) {
3656 scrollTo(x, y);
3657 }
3658
Chet Haase73066682010-11-29 15:55:32 -08003659 if (transformSet) {
3660 setTranslationX(tx);
3661 setTranslationY(ty);
3662 setRotation(rotation);
3663 setRotationX(rotationX);
3664 setRotationY(rotationY);
3665 setScaleX(sx);
3666 setScaleY(sy);
3667 }
3668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3670 setScrollContainer(true);
3671 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003672
3673 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003676 private void updateUserPaddingRelative() {
3677 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3678 }
3679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 /**
3681 * Non-public constructor for use in testing
3682 */
3683 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003684 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 /**
3688 * <p>
3689 * Initializes the fading edges from a given set of styled attributes. This
3690 * method should be called by subclasses that need fading edges and when an
3691 * instance of these subclasses is created programmatically rather than
3692 * being inflated from XML. This method is automatically called when the XML
3693 * is inflated.
3694 * </p>
3695 *
3696 * @param a the styled attributes set to initialize the fading edges from
3697 */
3698 protected void initializeFadingEdge(TypedArray a) {
3699 initScrollCache();
3700
3701 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3702 R.styleable.View_fadingEdgeLength,
3703 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3704 }
3705
3706 /**
3707 * Returns the size of the vertical faded edges used to indicate that more
3708 * content in this view is visible.
3709 *
3710 * @return The size in pixels of the vertical faded edge or 0 if vertical
3711 * faded edges are not enabled for this view.
3712 * @attr ref android.R.styleable#View_fadingEdgeLength
3713 */
3714 public int getVerticalFadingEdgeLength() {
3715 if (isVerticalFadingEdgeEnabled()) {
3716 ScrollabilityCache cache = mScrollCache;
3717 if (cache != null) {
3718 return cache.fadingEdgeLength;
3719 }
3720 }
3721 return 0;
3722 }
3723
3724 /**
3725 * Set the size of the faded edge used to indicate that more content in this
3726 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003727 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3728 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3729 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 *
3731 * @param length The size in pixels of the faded edge used to indicate that more
3732 * content in this view is visible.
3733 */
3734 public void setFadingEdgeLength(int length) {
3735 initScrollCache();
3736 mScrollCache.fadingEdgeLength = length;
3737 }
3738
3739 /**
3740 * Returns the size of the horizontal faded edges used to indicate that more
3741 * content in this view is visible.
3742 *
3743 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3744 * faded edges are not enabled for this view.
3745 * @attr ref android.R.styleable#View_fadingEdgeLength
3746 */
3747 public int getHorizontalFadingEdgeLength() {
3748 if (isHorizontalFadingEdgeEnabled()) {
3749 ScrollabilityCache cache = mScrollCache;
3750 if (cache != null) {
3751 return cache.fadingEdgeLength;
3752 }
3753 }
3754 return 0;
3755 }
3756
3757 /**
3758 * Returns the width of the vertical scrollbar.
3759 *
3760 * @return The width in pixels of the vertical scrollbar or 0 if there
3761 * is no vertical scrollbar.
3762 */
3763 public int getVerticalScrollbarWidth() {
3764 ScrollabilityCache cache = mScrollCache;
3765 if (cache != null) {
3766 ScrollBarDrawable scrollBar = cache.scrollBar;
3767 if (scrollBar != null) {
3768 int size = scrollBar.getSize(true);
3769 if (size <= 0) {
3770 size = cache.scrollBarSize;
3771 }
3772 return size;
3773 }
3774 return 0;
3775 }
3776 return 0;
3777 }
3778
3779 /**
3780 * Returns the height of the horizontal scrollbar.
3781 *
3782 * @return The height in pixels of the horizontal scrollbar or 0 if
3783 * there is no horizontal scrollbar.
3784 */
3785 protected int getHorizontalScrollbarHeight() {
3786 ScrollabilityCache cache = mScrollCache;
3787 if (cache != null) {
3788 ScrollBarDrawable scrollBar = cache.scrollBar;
3789 if (scrollBar != null) {
3790 int size = scrollBar.getSize(false);
3791 if (size <= 0) {
3792 size = cache.scrollBarSize;
3793 }
3794 return size;
3795 }
3796 return 0;
3797 }
3798 return 0;
3799 }
3800
3801 /**
3802 * <p>
3803 * Initializes the scrollbars from a given set of styled attributes. This
3804 * method should be called by subclasses that need scrollbars and when an
3805 * instance of these subclasses is created programmatically rather than
3806 * being inflated from XML. This method is automatically called when the XML
3807 * is inflated.
3808 * </p>
3809 *
3810 * @param a the styled attributes set to initialize the scrollbars from
3811 */
3812 protected void initializeScrollbars(TypedArray a) {
3813 initScrollCache();
3814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003816
Mike Cleronf116bf82009-09-27 19:14:12 -07003817 if (scrollabilityCache.scrollBar == null) {
3818 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3819 }
Joe Malin32736f02011-01-19 16:14:20 -08003820
Romain Guy8bda2482010-03-02 11:42:11 -08003821 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822
Mike Cleronf116bf82009-09-27 19:14:12 -07003823 if (!fadeScrollbars) {
3824 scrollabilityCache.state = ScrollabilityCache.ON;
3825 }
3826 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003827
3828
Mike Cleronf116bf82009-09-27 19:14:12 -07003829 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3830 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3831 .getScrollBarFadeDuration());
3832 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3833 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3834 ViewConfiguration.getScrollDefaultDelay());
3835
Joe Malin32736f02011-01-19 16:14:20 -08003836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3838 com.android.internal.R.styleable.View_scrollbarSize,
3839 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3840
3841 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3842 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3843
3844 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3845 if (thumb != null) {
3846 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3847 }
3848
3849 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3850 false);
3851 if (alwaysDraw) {
3852 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3853 }
3854
3855 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3856 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3857
3858 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3859 if (thumb != null) {
3860 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3861 }
3862
3863 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3864 false);
3865 if (alwaysDraw) {
3866 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3867 }
3868
3869 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003870 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 }
3872
3873 /**
3874 * <p>
3875 * Initalizes the scrollability cache if necessary.
3876 * </p>
3877 */
3878 private void initScrollCache() {
3879 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003880 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 }
3882 }
3883
Philip Milne6c8ea062012-04-03 17:38:43 -07003884 private ScrollabilityCache getScrollCache() {
3885 initScrollCache();
3886 return mScrollCache;
3887 }
3888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 /**
Adam Powell20232d02010-12-08 21:08:53 -08003890 * Set the position of the vertical scroll bar. Should be one of
3891 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3892 * {@link #SCROLLBAR_POSITION_RIGHT}.
3893 *
3894 * @param position Where the vertical scroll bar should be positioned.
3895 */
3896 public void setVerticalScrollbarPosition(int position) {
3897 if (mVerticalScrollbarPosition != position) {
3898 mVerticalScrollbarPosition = position;
3899 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003900 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003901 }
3902 }
3903
3904 /**
3905 * @return The position where the vertical scroll bar will show, if applicable.
3906 * @see #setVerticalScrollbarPosition(int)
3907 */
3908 public int getVerticalScrollbarPosition() {
3909 return mVerticalScrollbarPosition;
3910 }
3911
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003912 ListenerInfo getListenerInfo() {
3913 if (mListenerInfo != null) {
3914 return mListenerInfo;
3915 }
3916 mListenerInfo = new ListenerInfo();
3917 return mListenerInfo;
3918 }
3919
Adam Powell20232d02010-12-08 21:08:53 -08003920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 * Register a callback to be invoked when focus of this view changed.
3922 *
3923 * @param l The callback that will run.
3924 */
3925 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003926 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 }
3928
3929 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003930 * Add a listener that will be called when the bounds of the view change due to
3931 * layout processing.
3932 *
3933 * @param listener The listener that will be called when layout bounds change.
3934 */
3935 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003936 ListenerInfo li = getListenerInfo();
3937 if (li.mOnLayoutChangeListeners == null) {
3938 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003939 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003940 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3941 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003942 }
Chet Haase21cd1382010-09-01 17:42:29 -07003943 }
3944
3945 /**
3946 * Remove a listener for layout changes.
3947 *
3948 * @param listener The listener for layout bounds change.
3949 */
3950 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003951 ListenerInfo li = mListenerInfo;
3952 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003953 return;
3954 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003956 }
3957
3958 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003959 * Add a listener for attach state changes.
3960 *
3961 * This listener will be called whenever this view is attached or detached
3962 * from a window. Remove the listener using
3963 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3964 *
3965 * @param listener Listener to attach
3966 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3967 */
3968 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003969 ListenerInfo li = getListenerInfo();
3970 if (li.mOnAttachStateChangeListeners == null) {
3971 li.mOnAttachStateChangeListeners
3972 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003973 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003974 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003975 }
3976
3977 /**
3978 * Remove a listener for attach state changes. The listener will receive no further
3979 * notification of window attach/detach events.
3980 *
3981 * @param listener Listener to remove
3982 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3983 */
3984 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003985 ListenerInfo li = mListenerInfo;
3986 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003987 return;
3988 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003989 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003990 }
3991
3992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 * Returns the focus-change callback registered for this view.
3994 *
3995 * @return The callback, or null if one is not registered.
3996 */
3997 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003998 ListenerInfo li = mListenerInfo;
3999 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
4001
4002 /**
4003 * Register a callback to be invoked when this view is clicked. If this view is not
4004 * clickable, it becomes clickable.
4005 *
4006 * @param l The callback that will run
4007 *
4008 * @see #setClickable(boolean)
4009 */
4010 public void setOnClickListener(OnClickListener l) {
4011 if (!isClickable()) {
4012 setClickable(true);
4013 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004014 getListenerInfo().mOnClickListener = l;
4015 }
4016
4017 /**
4018 * Return whether this view has an attached OnClickListener. Returns
4019 * true if there is a listener, false if there is none.
4020 */
4021 public boolean hasOnClickListeners() {
4022 ListenerInfo li = mListenerInfo;
4023 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 }
4025
4026 /**
4027 * Register a callback to be invoked when this view is clicked and held. If this view is not
4028 * long clickable, it becomes long clickable.
4029 *
4030 * @param l The callback that will run
4031 *
4032 * @see #setLongClickable(boolean)
4033 */
4034 public void setOnLongClickListener(OnLongClickListener l) {
4035 if (!isLongClickable()) {
4036 setLongClickable(true);
4037 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 /**
4042 * Register a callback to be invoked when the context menu for this view is
4043 * being built. If this view is not long clickable, it becomes long clickable.
4044 *
4045 * @param l The callback that will run
4046 *
4047 */
4048 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4049 if (!isLongClickable()) {
4050 setLongClickable(true);
4051 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004052 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 }
4054
4055 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004056 * Call this view's OnClickListener, if it is defined. Performs all normal
4057 * actions associated with clicking: reporting accessibility event, playing
4058 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 *
4060 * @return True there was an assigned OnClickListener that was called, false
4061 * otherwise is returned.
4062 */
4063 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004064 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4065
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004066 ListenerInfo li = mListenerInfo;
4067 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 return true;
4071 }
4072
4073 return false;
4074 }
4075
4076 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004077 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4078 * this only calls the listener, and does not do any associated clicking
4079 * actions like reporting an accessibility event.
4080 *
4081 * @return True there was an assigned OnClickListener that was called, false
4082 * otherwise is returned.
4083 */
4084 public boolean callOnClick() {
4085 ListenerInfo li = mListenerInfo;
4086 if (li != null && li.mOnClickListener != null) {
4087 li.mOnClickListener.onClick(this);
4088 return true;
4089 }
4090 return false;
4091 }
4092
4093 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004094 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4095 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004097 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 */
4099 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004100 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004103 ListenerInfo li = mListenerInfo;
4104 if (li != null && li.mOnLongClickListener != null) {
4105 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 if (!handled) {
4108 handled = showContextMenu();
4109 }
4110 if (handled) {
4111 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4112 }
4113 return handled;
4114 }
4115
4116 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004117 * Performs button-related actions during a touch down event.
4118 *
4119 * @param event The event.
4120 * @return True if the down was consumed.
4121 *
4122 * @hide
4123 */
4124 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4125 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4126 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4127 return true;
4128 }
4129 }
4130 return false;
4131 }
4132
4133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * Bring up the context menu for this view.
4135 *
4136 * @return Whether a context menu was displayed.
4137 */
4138 public boolean showContextMenu() {
4139 return getParent().showContextMenuForChild(this);
4140 }
4141
4142 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004143 * Bring up the context menu for this view, referring to the item under the specified point.
4144 *
4145 * @param x The referenced x coordinate.
4146 * @param y The referenced y coordinate.
4147 * @param metaState The keyboard modifiers that were pressed.
4148 * @return Whether a context menu was displayed.
4149 *
4150 * @hide
4151 */
4152 public boolean showContextMenu(float x, float y, int metaState) {
4153 return showContextMenu();
4154 }
4155
4156 /**
Adam Powell6e346362010-07-23 10:18:23 -07004157 * Start an action mode.
4158 *
4159 * @param callback Callback that will control the lifecycle of the action mode
4160 * @return The new action mode if it is started, null otherwise
4161 *
4162 * @see ActionMode
4163 */
4164 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004165 ViewParent parent = getParent();
4166 if (parent == null) return null;
4167 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004168 }
4169
4170 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004171 * Register a callback to be invoked when a hardware key is pressed in this view.
4172 * Key presses in software input methods will generally not trigger the methods of
4173 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 * @param l the key listener to attach to this view
4175 */
4176 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004177 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 }
4179
4180 /**
4181 * Register a callback to be invoked when a touch event is sent to this view.
4182 * @param l the touch listener to attach to this view
4183 */
4184 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004185 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187
4188 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004189 * Register a callback to be invoked when a generic motion event is sent to this view.
4190 * @param l the generic motion listener to attach to this view
4191 */
4192 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004193 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004194 }
4195
4196 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004197 * Register a callback to be invoked when a hover event is sent to this view.
4198 * @param l the hover listener to attach to this view
4199 */
4200 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004201 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004202 }
4203
4204 /**
Joe Malin32736f02011-01-19 16:14:20 -08004205 * Register a drag event listener callback object for this View. The parameter is
4206 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4207 * View, the system calls the
4208 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4209 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004210 */
4211 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004212 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004213 }
4214
4215 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004216 * Give this view focus. This will cause
4217 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 *
4219 * Note: this does not check whether this {@link View} should get focus, it just
4220 * gives it focus no matter what. It should only be called internally by framework
4221 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4222 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004223 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4224 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 * focus moved when requestFocus() is called. It may not always
4226 * apply, in which case use the default View.FOCUS_DOWN.
4227 * @param previouslyFocusedRect The rectangle of the view that had focus
4228 * prior in this View's coordinate system.
4229 */
4230 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4231 if (DBG) {
4232 System.out.println(this + " requestFocus()");
4233 }
4234
4235 if ((mPrivateFlags & FOCUSED) == 0) {
4236 mPrivateFlags |= FOCUSED;
4237
4238 if (mParent != null) {
4239 mParent.requestChildFocus(this, this);
4240 }
4241
4242 onFocusChanged(true, direction, previouslyFocusedRect);
4243 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004244
4245 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4246 notifyAccessibilityStateChanged();
4247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
4250
4251 /**
4252 * Request that a rectangle of this view be visible on the screen,
4253 * scrolling if necessary just enough.
4254 *
4255 * <p>A View should call this if it maintains some notion of which part
4256 * of its content is interesting. For example, a text editing view
4257 * should call this when its cursor moves.
4258 *
4259 * @param rectangle The rectangle.
4260 * @return Whether any parent scrolled.
4261 */
4262 public boolean requestRectangleOnScreen(Rect rectangle) {
4263 return requestRectangleOnScreen(rectangle, false);
4264 }
4265
4266 /**
4267 * Request that a rectangle of this view be visible on the screen,
4268 * scrolling if necessary just enough.
4269 *
4270 * <p>A View should call this if it maintains some notion of which part
4271 * of its content is interesting. For example, a text editing view
4272 * should call this when its cursor moves.
4273 *
4274 * <p>When <code>immediate</code> is set to true, scrolling will not be
4275 * animated.
4276 *
4277 * @param rectangle The rectangle.
4278 * @param immediate True to forbid animated scrolling, false otherwise
4279 * @return Whether any parent scrolled.
4280 */
4281 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4282 View child = this;
4283 ViewParent parent = mParent;
4284 boolean scrolled = false;
4285 while (parent != null) {
4286 scrolled |= parent.requestChildRectangleOnScreen(child,
4287 rectangle, immediate);
4288
4289 // offset rect so next call has the rectangle in the
4290 // coordinate system of its direct child.
4291 rectangle.offset(child.getLeft(), child.getTop());
4292 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4293
4294 if (!(parent instanceof View)) {
4295 break;
4296 }
Romain Guy8506ab42009-06-11 17:35:47 -07004297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 child = (View) parent;
4299 parent = child.getParent();
4300 }
4301 return scrolled;
4302 }
4303
4304 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004305 * Called when this view wants to give up focus. If focus is cleared
4306 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4307 * <p>
4308 * <strong>Note:</strong> When a View clears focus the framework is trying
4309 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004310 * View is the first from the top that can take focus, then all callbacks
4311 * related to clearing focus will be invoked after wich the framework will
4312 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004313 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 */
4315 public void clearFocus() {
4316 if (DBG) {
4317 System.out.println(this + " clearFocus()");
4318 }
4319
4320 if ((mPrivateFlags & FOCUSED) != 0) {
4321 mPrivateFlags &= ~FOCUSED;
4322
4323 if (mParent != null) {
4324 mParent.clearChildFocus(this);
4325 }
4326
4327 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004330
4331 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004332
4333 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4334 notifyAccessibilityStateChanged();
4335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
4338
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004339 void ensureInputFocusOnFirstFocusable() {
4340 View root = getRootView();
4341 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004342 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 }
4344 }
4345
4346 /**
4347 * Called internally by the view system when a new view is getting focus.
4348 * This is what clears the old focus.
4349 */
4350 void unFocus() {
4351 if (DBG) {
4352 System.out.println(this + " unFocus()");
4353 }
4354
4355 if ((mPrivateFlags & FOCUSED) != 0) {
4356 mPrivateFlags &= ~FOCUSED;
4357
4358 onFocusChanged(false, 0, null);
4359 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004360
4361 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4362 notifyAccessibilityStateChanged();
4363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 }
4365 }
4366
4367 /**
4368 * Returns true if this view has focus iteself, or is the ancestor of the
4369 * view that has focus.
4370 *
4371 * @return True if this view has or contains focus, false otherwise.
4372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 public boolean hasFocus() {
4375 return (mPrivateFlags & FOCUSED) != 0;
4376 }
4377
4378 /**
4379 * Returns true if this view is focusable or if it contains a reachable View
4380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4381 * is a View whose parents do not block descendants focus.
4382 *
4383 * Only {@link #VISIBLE} views are considered focusable.
4384 *
4385 * @return True if the view is focusable or if the view contains a focusable
4386 * View, false otherwise.
4387 *
4388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4389 */
4390 public boolean hasFocusable() {
4391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4392 }
4393
4394 /**
4395 * Called by the view system when the focus state of this view changes.
4396 * When the focus change event is caused by directional navigation, direction
4397 * and previouslyFocusedRect provide insight into where the focus is coming from.
4398 * When overriding, be sure to call up through to the super class so that
4399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 * @param gainFocus True if the View has focus; false otherwise.
4402 * @param direction The direction focus has moved when requestFocus()
4403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4408 * system, of the previously focused view. If applicable, this will be
4409 * passed in as finer grained information about where the focus is coming
4410 * from (in addition to direction). Will be <code>null</code> otherwise.
4411 */
4412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004413 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004414 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4415 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004416 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004417 }
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 InputMethodManager imm = InputMethodManager.peekInstance();
4420 if (!gainFocus) {
4421 if (isPressed()) {
4422 setPressed(false);
4423 }
4424 if (imm != null && mAttachInfo != null
4425 && mAttachInfo.mHasWindowFocus) {
4426 imm.focusOut(this);
4427 }
Romain Guya2431d02009-04-30 16:30:00 -07004428 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 } else if (imm != null && mAttachInfo != null
4430 && mAttachInfo.mHasWindowFocus) {
4431 imm.focusIn(this);
4432 }
Romain Guy8506ab42009-06-11 17:35:47 -07004433
Romain Guy0fd89bf2011-01-26 15:41:30 -08004434 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004435 ListenerInfo li = mListenerInfo;
4436 if (li != null && li.mOnFocusChangeListener != null) {
4437 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
Joe Malin32736f02011-01-19 16:14:20 -08004439
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004440 if (mAttachInfo != null) {
4441 mAttachInfo.mKeyDispatchState.reset(this);
4442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 }
4444
4445 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004446 * Sends an accessibility event of the given type. If accessiiblity is
4447 * not enabled this method has no effect. The default implementation calls
4448 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4449 * to populate information about the event source (this View), then calls
4450 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4451 * populate the text content of the event source including its descendants,
4452 * and last calls
4453 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4454 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004455 * <p>
4456 * If an {@link AccessibilityDelegate} has been specified via calling
4457 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4458 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4459 * responsible for handling this call.
4460 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 *
Scott Mainb303d832011-10-12 16:45:18 -07004462 * @param eventType The type of the event to send, as defined by several types from
4463 * {@link android.view.accessibility.AccessibilityEvent}, such as
4464 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4465 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004466 *
4467 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4468 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4469 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004470 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004471 */
4472 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004473 if (mAccessibilityDelegate != null) {
4474 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4475 } else {
4476 sendAccessibilityEventInternal(eventType);
4477 }
4478 }
4479
4480 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004481 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4482 * {@link AccessibilityEvent} to make an announcement which is related to some
4483 * sort of a context change for which none of the events representing UI transitions
4484 * is a good fit. For example, announcing a new page in a book. If accessibility
4485 * is not enabled this method does nothing.
4486 *
4487 * @param text The announcement text.
4488 */
4489 public void announceForAccessibility(CharSequence text) {
4490 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4491 AccessibilityEvent event = AccessibilityEvent.obtain(
4492 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4493 event.getText().add(text);
4494 sendAccessibilityEventUnchecked(event);
4495 }
4496 }
4497
4498 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004499 * @see #sendAccessibilityEvent(int)
4500 *
4501 * Note: Called from the default {@link AccessibilityDelegate}.
4502 */
4503 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004504 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4505 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4506 }
4507 }
4508
4509 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004510 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4511 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004512 * perform a check whether accessibility is enabled.
4513 * <p>
4514 * If an {@link AccessibilityDelegate} has been specified via calling
4515 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4516 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4517 * is responsible for handling this call.
4518 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004519 *
4520 * @param event The event to send.
4521 *
4522 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004523 */
4524 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004525 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004526 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004527 } else {
4528 sendAccessibilityEventUncheckedInternal(event);
4529 }
4530 }
4531
4532 /**
4533 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4534 *
4535 * Note: Called from the default {@link AccessibilityDelegate}.
4536 */
4537 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004538 if (!isShown()) {
4539 return;
4540 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004541 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004542 // Only a subset of accessibility events populates text content.
4543 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4544 dispatchPopulateAccessibilityEvent(event);
4545 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004546 // Intercept accessibility focus events fired by virtual nodes to keep
4547 // track of accessibility focus position in such nodes.
4548 final int eventType = event.getEventType();
4549 switch (eventType) {
4550 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4551 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4552 event.getSourceNodeId());
4553 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4554 ViewRootImpl viewRootImpl = getViewRootImpl();
4555 if (viewRootImpl != null) {
4556 viewRootImpl.setAccessibilityFocusedHost(this);
4557 }
4558 }
4559 } break;
4560 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4561 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4562 event.getSourceNodeId());
4563 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4564 ViewRootImpl viewRootImpl = getViewRootImpl();
4565 if (viewRootImpl != null) {
4566 viewRootImpl.setAccessibilityFocusedHost(null);
4567 }
4568 }
4569 } break;
4570 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004571 // In the beginning we called #isShown(), so we know that getParent() is not null.
4572 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004573 }
4574
4575 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004576 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4577 * to its children for adding their text content to the event. Note that the
4578 * event text is populated in a separate dispatch path since we add to the
4579 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004580 * A typical implementation will call
4581 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4582 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4583 * on each child. Override this method if custom population of the event text
4584 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004585 * <p>
4586 * If an {@link AccessibilityDelegate} has been specified via calling
4587 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4588 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4589 * is responsible for handling this call.
4590 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004591 * <p>
4592 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4593 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4594 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004595 *
4596 * @param event The event.
4597 *
4598 * @return True if the event population was completed.
4599 */
4600 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004601 if (mAccessibilityDelegate != null) {
4602 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4603 } else {
4604 return dispatchPopulateAccessibilityEventInternal(event);
4605 }
4606 }
4607
4608 /**
4609 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4610 *
4611 * Note: Called from the default {@link AccessibilityDelegate}.
4612 */
4613 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004614 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004615 return false;
4616 }
4617
4618 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004619 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004620 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004621 * text content. While this method is free to modify event
4622 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004623 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4624 * <p>
4625 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004626 * to the text added by the super implementation:
4627 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004628 * super.onPopulateAccessibilityEvent(event);
4629 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4630 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4631 * mCurrentDate.getTimeInMillis(), flags);
4632 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004633 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004634 * <p>
4635 * If an {@link AccessibilityDelegate} has been specified via calling
4636 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4637 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4638 * is responsible for handling this call.
4639 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004640 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4641 * information to the event, in case the default implementation has basic information to add.
4642 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004643 *
4644 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004645 *
4646 * @see #sendAccessibilityEvent(int)
4647 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004648 */
4649 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004650 if (mAccessibilityDelegate != null) {
4651 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4652 } else {
4653 onPopulateAccessibilityEventInternal(event);
4654 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004655 }
4656
4657 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4659 *
4660 * Note: Called from the default {@link AccessibilityDelegate}.
4661 */
4662 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4663
4664 }
4665
4666 /**
4667 * Initializes an {@link AccessibilityEvent} with information about
4668 * this View which is the event source. In other words, the source of
4669 * an accessibility event is the view whose state change triggered firing
4670 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004671 * <p>
4672 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004673 * to properties set by the super implementation:
4674 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4675 * super.onInitializeAccessibilityEvent(event);
4676 * event.setPassword(true);
4677 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004678 * <p>
4679 * If an {@link AccessibilityDelegate} has been specified via calling
4680 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4681 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4682 * is responsible for handling this call.
4683 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004684 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4685 * information to the event, in case the default implementation has basic information to add.
4686 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004687 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004688 *
4689 * @see #sendAccessibilityEvent(int)
4690 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4691 */
4692 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004693 if (mAccessibilityDelegate != null) {
4694 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4695 } else {
4696 onInitializeAccessibilityEventInternal(event);
4697 }
4698 }
4699
4700 /**
4701 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4702 *
4703 * Note: Called from the default {@link AccessibilityDelegate}.
4704 */
4705 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004706 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004707 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004708 event.setPackageName(getContext().getPackageName());
4709 event.setEnabled(isEnabled());
4710 event.setContentDescription(mContentDescription);
4711
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004712 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004713 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004714 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4715 FOCUSABLES_ALL);
4716 event.setItemCount(focusablesTempList.size());
4717 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4718 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004719 }
4720 }
4721
4722 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004723 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4724 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4725 * This method is responsible for obtaining an accessibility node info from a
4726 * pool of reusable instances and calling
4727 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4728 * initialize the former.
4729 * <p>
4730 * Note: The client is responsible for recycling the obtained instance by calling
4731 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4732 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004733 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004734 * @return A populated {@link AccessibilityNodeInfo}.
4735 *
4736 * @see AccessibilityNodeInfo
4737 */
4738 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004739 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4740 if (provider != null) {
4741 return provider.createAccessibilityNodeInfo(View.NO_ID);
4742 } else {
4743 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4744 onInitializeAccessibilityNodeInfo(info);
4745 return info;
4746 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004747 }
4748
4749 /**
4750 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4751 * The base implementation sets:
4752 * <ul>
4753 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004754 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4755 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004756 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4757 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4758 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4759 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4760 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4761 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4762 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4763 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4764 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4765 * </ul>
4766 * <p>
4767 * Subclasses should override this method, call the super implementation,
4768 * and set additional attributes.
4769 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004770 * <p>
4771 * If an {@link AccessibilityDelegate} has been specified via calling
4772 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4773 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4774 * is responsible for handling this call.
4775 * </p>
4776 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004777 * @param info The instance to initialize.
4778 */
4779 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004780 if (mAccessibilityDelegate != null) {
4781 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4782 } else {
4783 onInitializeAccessibilityNodeInfoInternal(info);
4784 }
4785 }
4786
4787 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004788 * Gets the location of this view in screen coordintates.
4789 *
4790 * @param outRect The output location
4791 */
4792 private void getBoundsOnScreen(Rect outRect) {
4793 if (mAttachInfo == null) {
4794 return;
4795 }
4796
4797 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004798 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004799
4800 if (!hasIdentityMatrix()) {
4801 getMatrix().mapRect(position);
4802 }
4803
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004804 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004805
4806 ViewParent parent = mParent;
4807 while (parent instanceof View) {
4808 View parentView = (View) parent;
4809
4810 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4811
4812 if (!parentView.hasIdentityMatrix()) {
4813 parentView.getMatrix().mapRect(position);
4814 }
4815
4816 position.offset(parentView.mLeft, parentView.mTop);
4817
4818 parent = parentView.mParent;
4819 }
4820
4821 if (parent instanceof ViewRootImpl) {
4822 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4823 position.offset(0, -viewRootImpl.mCurScrollY);
4824 }
4825
4826 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4827
4828 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4829 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4830 }
4831
4832 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004833 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4834 *
4835 * Note: Called from the default {@link AccessibilityDelegate}.
4836 */
4837 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004838 Rect bounds = mAttachInfo.mTmpInvalRect;
4839 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004840 info.setBoundsInParent(bounds);
4841
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004842 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004843 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004844
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004845 ViewParent parent = getParentForAccessibility();
4846 if (parent instanceof View) {
4847 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004848 }
4849
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004850 info.setVisibleToUser(isVisibleToUser());
4851
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004852 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004853 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004854 info.setContentDescription(getContentDescription());
4855
4856 info.setEnabled(isEnabled());
4857 info.setClickable(isClickable());
4858 info.setFocusable(isFocusable());
4859 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004860 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004861 info.setSelected(isSelected());
4862 info.setLongClickable(isLongClickable());
4863
4864 // TODO: These make sense only if we are in an AdapterView but all
4865 // views can be selected. Maybe from accessiiblity perspective
4866 // we should report as selectable view in an AdapterView.
4867 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4868 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4869
4870 if (isFocusable()) {
4871 if (isFocused()) {
4872 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4873 } else {
4874 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4875 }
4876 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004877
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004878 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004879 final int mode = getAccessibilityFocusable();
4880 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4881 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4882 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004883 } else {
4884 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4885 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004886
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004887 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004888 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4889 }
4890
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004891 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004892 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4893 }
4894
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004895 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004896 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4897 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4898 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004899 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4900 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004901 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004902 }
4903
4904 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004905 * Computes whether this view is visible to the user. Such a view is
4906 * attached, visible, all its predecessors are visible, it is not clipped
4907 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004908 *
4909 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004910 *
4911 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004912 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004913 protected boolean isVisibleToUser() {
4914 return isVisibleToUser(null);
4915 }
4916
4917 /**
4918 * Computes whether the given portion of this view is visible to the user. Such a view is
4919 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4920 * the specified portion is not clipped entirely by its predecessors.
4921 *
4922 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4923 * <code>null</code>, and the entire view will be tested in this case.
4924 * When <code>true</code> is returned by the function, the actual visible
4925 * region will be stored in this parameter; that is, if boundInView is fully
4926 * contained within the view, no modification will be made, otherwise regions
4927 * outside of the visible area of the view will be clipped.
4928 *
4929 * @return Whether the specified portion of the view is visible on the screen.
4930 *
4931 * @hide
4932 */
4933 protected boolean isVisibleToUser(Rect boundInView) {
4934 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4935 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004936 // The first two checks are made also made by isShown() which
4937 // however traverses the tree up to the parent to catch that.
4938 // Therefore, we do some fail fast check to minimize the up
4939 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004940 boolean isVisible = mAttachInfo != null
4941 && mAttachInfo.mWindowVisibility == View.VISIBLE
4942 && getAlpha() > 0
4943 && isShown()
4944 && getGlobalVisibleRect(visibleRect, offset);
4945 if (isVisible && boundInView != null) {
4946 visibleRect.offset(-offset.x, -offset.y);
4947 isVisible &= boundInView.intersect(visibleRect);
4948 }
4949 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004950 }
4951
4952 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004953 * Sets a delegate for implementing accessibility support via compositon as
4954 * opposed to inheritance. The delegate's primary use is for implementing
4955 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4956 *
4957 * @param delegate The delegate instance.
4958 *
4959 * @see AccessibilityDelegate
4960 */
4961 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4962 mAccessibilityDelegate = delegate;
4963 }
4964
4965 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004966 * Gets the provider for managing a virtual view hierarchy rooted at this View
4967 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4968 * that explore the window content.
4969 * <p>
4970 * If this method returns an instance, this instance is responsible for managing
4971 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4972 * View including the one representing the View itself. Similarly the returned
4973 * instance is responsible for performing accessibility actions on any virtual
4974 * view or the root view itself.
4975 * </p>
4976 * <p>
4977 * If an {@link AccessibilityDelegate} has been specified via calling
4978 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4979 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4980 * is responsible for handling this call.
4981 * </p>
4982 *
4983 * @return The provider.
4984 *
4985 * @see AccessibilityNodeProvider
4986 */
4987 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4988 if (mAccessibilityDelegate != null) {
4989 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4990 } else {
4991 return null;
4992 }
4993 }
4994
4995 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004996 * Gets the unique identifier of this view on the screen for accessibility purposes.
4997 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4998 *
4999 * @return The view accessibility id.
5000 *
5001 * @hide
5002 */
5003 public int getAccessibilityViewId() {
5004 if (mAccessibilityViewId == NO_ID) {
5005 mAccessibilityViewId = sNextAccessibilityViewId++;
5006 }
5007 return mAccessibilityViewId;
5008 }
5009
5010 /**
5011 * Gets the unique identifier of the window in which this View reseides.
5012 *
5013 * @return The window accessibility id.
5014 *
5015 * @hide
5016 */
5017 public int getAccessibilityWindowId() {
5018 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5019 }
5020
5021 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005022 * Gets the {@link View} description. It briefly describes the view and is
5023 * primarily used for accessibility support. Set this property to enable
5024 * better accessibility support for your application. This is especially
5025 * true for views that do not have textual representation (For example,
5026 * ImageButton).
5027 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005028 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005029 *
5030 * @attr ref android.R.styleable#View_contentDescription
5031 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005032 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005033 public CharSequence getContentDescription() {
5034 return mContentDescription;
5035 }
5036
5037 /**
5038 * Sets the {@link View} description. It briefly describes the view and is
5039 * primarily used for accessibility support. Set this property to enable
5040 * better accessibility support for your application. This is especially
5041 * true for views that do not have textual representation (For example,
5042 * ImageButton).
5043 *
5044 * @param contentDescription The content description.
5045 *
5046 * @attr ref android.R.styleable#View_contentDescription
5047 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005048 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005049 public void setContentDescription(CharSequence contentDescription) {
5050 mContentDescription = contentDescription;
5051 }
5052
5053 /**
Romain Guya2431d02009-04-30 16:30:00 -07005054 * Invoked whenever this view loses focus, either by losing window focus or by losing
5055 * focus within its window. This method can be used to clear any state tied to the
5056 * focus. For instance, if a button is held pressed with the trackball and the window
5057 * loses focus, this method can be used to cancel the press.
5058 *
5059 * Subclasses of View overriding this method should always call super.onFocusLost().
5060 *
5061 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005062 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005063 *
5064 * @hide pending API council approval
5065 */
5066 protected void onFocusLost() {
5067 resetPressedState();
5068 }
5069
5070 private void resetPressedState() {
5071 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5072 return;
5073 }
5074
5075 if (isPressed()) {
5076 setPressed(false);
5077
5078 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005079 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005080 }
5081 }
5082 }
5083
5084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 * Returns true if this view has focus
5086 *
5087 * @return True if this view has focus, false otherwise.
5088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005089 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 public boolean isFocused() {
5091 return (mPrivateFlags & FOCUSED) != 0;
5092 }
5093
5094 /**
5095 * Find the view in the hierarchy rooted at this view that currently has
5096 * focus.
5097 *
5098 * @return The view that currently has focus, or null if no focused view can
5099 * be found.
5100 */
5101 public View findFocus() {
5102 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5103 }
5104
5105 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005106 * Indicates whether this view is one of the set of scrollable containers in
5107 * its window.
5108 *
5109 * @return whether this view is one of the set of scrollable containers in
5110 * its window
5111 *
5112 * @attr ref android.R.styleable#View_isScrollContainer
5113 */
5114 public boolean isScrollContainer() {
5115 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5116 }
5117
5118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 * Change whether this view is one of the set of scrollable containers in
5120 * its window. This will be used to determine whether the window can
5121 * resize or must pan when a soft input area is open -- scrollable
5122 * containers allow the window to use resize mode since the container
5123 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005124 *
5125 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005126 */
5127 public void setScrollContainer(boolean isScrollContainer) {
5128 if (isScrollContainer) {
5129 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5130 mAttachInfo.mScrollContainers.add(this);
5131 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5132 }
5133 mPrivateFlags |= SCROLL_CONTAINER;
5134 } else {
5135 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5136 mAttachInfo.mScrollContainers.remove(this);
5137 }
5138 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5139 }
5140 }
5141
5142 /**
5143 * Returns the quality of the drawing cache.
5144 *
5145 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5146 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5147 *
5148 * @see #setDrawingCacheQuality(int)
5149 * @see #setDrawingCacheEnabled(boolean)
5150 * @see #isDrawingCacheEnabled()
5151 *
5152 * @attr ref android.R.styleable#View_drawingCacheQuality
5153 */
5154 public int getDrawingCacheQuality() {
5155 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5156 }
5157
5158 /**
5159 * Set the drawing cache quality of this view. This value is used only when the
5160 * drawing cache is enabled
5161 *
5162 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5163 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5164 *
5165 * @see #getDrawingCacheQuality()
5166 * @see #setDrawingCacheEnabled(boolean)
5167 * @see #isDrawingCacheEnabled()
5168 *
5169 * @attr ref android.R.styleable#View_drawingCacheQuality
5170 */
5171 public void setDrawingCacheQuality(int quality) {
5172 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5173 }
5174
5175 /**
5176 * Returns whether the screen should remain on, corresponding to the current
5177 * value of {@link #KEEP_SCREEN_ON}.
5178 *
5179 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5180 *
5181 * @see #setKeepScreenOn(boolean)
5182 *
5183 * @attr ref android.R.styleable#View_keepScreenOn
5184 */
5185 public boolean getKeepScreenOn() {
5186 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5187 }
5188
5189 /**
5190 * Controls whether the screen should remain on, modifying the
5191 * value of {@link #KEEP_SCREEN_ON}.
5192 *
5193 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5194 *
5195 * @see #getKeepScreenOn()
5196 *
5197 * @attr ref android.R.styleable#View_keepScreenOn
5198 */
5199 public void setKeepScreenOn(boolean keepScreenOn) {
5200 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5201 }
5202
5203 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005204 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5205 * @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 -08005206 *
5207 * @attr ref android.R.styleable#View_nextFocusLeft
5208 */
5209 public int getNextFocusLeftId() {
5210 return mNextFocusLeftId;
5211 }
5212
5213 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005214 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5215 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5216 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 *
5218 * @attr ref android.R.styleable#View_nextFocusLeft
5219 */
5220 public void setNextFocusLeftId(int nextFocusLeftId) {
5221 mNextFocusLeftId = nextFocusLeftId;
5222 }
5223
5224 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005225 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5226 * @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 -08005227 *
5228 * @attr ref android.R.styleable#View_nextFocusRight
5229 */
5230 public int getNextFocusRightId() {
5231 return mNextFocusRightId;
5232 }
5233
5234 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005235 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5236 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5237 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 *
5239 * @attr ref android.R.styleable#View_nextFocusRight
5240 */
5241 public void setNextFocusRightId(int nextFocusRightId) {
5242 mNextFocusRightId = nextFocusRightId;
5243 }
5244
5245 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005246 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5247 * @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 -08005248 *
5249 * @attr ref android.R.styleable#View_nextFocusUp
5250 */
5251 public int getNextFocusUpId() {
5252 return mNextFocusUpId;
5253 }
5254
5255 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005256 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5257 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5258 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 *
5260 * @attr ref android.R.styleable#View_nextFocusUp
5261 */
5262 public void setNextFocusUpId(int nextFocusUpId) {
5263 mNextFocusUpId = nextFocusUpId;
5264 }
5265
5266 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005267 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5268 * @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 -08005269 *
5270 * @attr ref android.R.styleable#View_nextFocusDown
5271 */
5272 public int getNextFocusDownId() {
5273 return mNextFocusDownId;
5274 }
5275
5276 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005277 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5278 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5279 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 *
5281 * @attr ref android.R.styleable#View_nextFocusDown
5282 */
5283 public void setNextFocusDownId(int nextFocusDownId) {
5284 mNextFocusDownId = nextFocusDownId;
5285 }
5286
5287 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005288 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5289 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5290 *
5291 * @attr ref android.R.styleable#View_nextFocusForward
5292 */
5293 public int getNextFocusForwardId() {
5294 return mNextFocusForwardId;
5295 }
5296
5297 /**
5298 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5299 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5300 * decide automatically.
5301 *
5302 * @attr ref android.R.styleable#View_nextFocusForward
5303 */
5304 public void setNextFocusForwardId(int nextFocusForwardId) {
5305 mNextFocusForwardId = nextFocusForwardId;
5306 }
5307
5308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005309 * Returns the visibility of this view and all of its ancestors
5310 *
5311 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5312 */
5313 public boolean isShown() {
5314 View current = this;
5315 //noinspection ConstantConditions
5316 do {
5317 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5318 return false;
5319 }
5320 ViewParent parent = current.mParent;
5321 if (parent == null) {
5322 return false; // We are not attached to the view root
5323 }
5324 if (!(parent instanceof View)) {
5325 return true;
5326 }
5327 current = (View) parent;
5328 } while (current != null);
5329
5330 return false;
5331 }
5332
5333 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005334 * Called by the view hierarchy when the content insets for a window have
5335 * changed, to allow it to adjust its content to fit within those windows.
5336 * The content insets tell you the space that the status bar, input method,
5337 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005339 * <p>You do not normally need to deal with this function, since the default
5340 * window decoration given to applications takes care of applying it to the
5341 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5342 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5343 * and your content can be placed under those system elements. You can then
5344 * use this method within your view hierarchy if you have parts of your UI
5345 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005347 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005348 * inset's to the view's padding, consuming that content (modifying the
5349 * insets to be 0), and returning true. This behavior is off by default, but can
5350 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5351 *
5352 * <p>This function's traversal down the hierarchy is depth-first. The same content
5353 * insets object is propagated down the hierarchy, so any changes made to it will
5354 * be seen by all following views (including potentially ones above in
5355 * the hierarchy since this is a depth-first traversal). The first view
5356 * that returns true will abort the entire traversal.
5357 *
5358 * <p>The default implementation works well for a situation where it is
5359 * used with a container that covers the entire window, allowing it to
5360 * apply the appropriate insets to its content on all edges. If you need
5361 * a more complicated layout (such as two different views fitting system
5362 * windows, one on the top of the window, and one on the bottom),
5363 * you can override the method and handle the insets however you would like.
5364 * Note that the insets provided by the framework are always relative to the
5365 * far edges of the window, not accounting for the location of the called view
5366 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005367 * where the layout will place the view, as it is done before layout happens.)
5368 *
5369 * <p>Note: unlike many View methods, there is no dispatch phase to this
5370 * call. If you are overriding it in a ViewGroup and want to allow the
5371 * call to continue to your children, you must be sure to call the super
5372 * implementation.
5373 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005374 * <p>Here is a sample layout that makes use of fitting system windows
5375 * to have controls for a video view placed inside of the window decorations
5376 * that it hides and shows. This can be used with code like the second
5377 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5378 *
5379 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5380 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005381 * @param insets Current content insets of the window. Prior to
5382 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5383 * the insets or else you and Android will be unhappy.
5384 *
5385 * @return Return true if this view applied the insets and it should not
5386 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005387 * @see #getFitsSystemWindows()
5388 * @see #setFitsSystemWindows()
5389 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 */
5391 protected boolean fitSystemWindows(Rect insets) {
5392 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005393 mUserPaddingStart = -1;
5394 mUserPaddingEnd = -1;
5395 mUserPaddingRelative = false;
5396 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5397 || mAttachInfo == null
5398 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5399 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5400 return true;
5401 } else {
5402 internalSetPadding(0, 0, 0, 0);
5403 return false;
5404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005405 }
5406 return false;
5407 }
5408
5409 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005410 * Sets whether or not this view should account for system screen decorations
5411 * such as the status bar and inset its content; that is, controlling whether
5412 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5413 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005414 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005415 * <p>Note that if you are providing your own implementation of
5416 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5417 * flag to true -- your implementation will be overriding the default
5418 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005419 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005420 * @param fitSystemWindows If true, then the default implementation of
5421 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005422 *
5423 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005424 * @see #getFitsSystemWindows()
5425 * @see #fitSystemWindows(Rect)
5426 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005427 */
5428 public void setFitsSystemWindows(boolean fitSystemWindows) {
5429 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5430 }
5431
5432 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005433 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005434 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5435 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005436 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005437 * @return Returns true if the default implementation of
5438 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005439 *
5440 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005441 * @see #setFitsSystemWindows()
5442 * @see #fitSystemWindows(Rect)
5443 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005444 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005445 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005446 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5447 }
5448
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005449 /** @hide */
5450 public boolean fitsSystemWindows() {
5451 return getFitsSystemWindows();
5452 }
5453
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005454 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005455 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5456 */
5457 public void requestFitSystemWindows() {
5458 if (mParent != null) {
5459 mParent.requestFitSystemWindows();
5460 }
5461 }
5462
5463 /**
5464 * For use by PhoneWindow to make its own system window fitting optional.
5465 * @hide
5466 */
5467 public void makeOptionalFitsSystemWindows() {
5468 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5469 }
5470
5471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 * Returns the visibility status for this view.
5473 *
5474 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5475 * @attr ref android.R.styleable#View_visibility
5476 */
5477 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005478 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5479 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5480 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 })
5482 public int getVisibility() {
5483 return mViewFlags & VISIBILITY_MASK;
5484 }
5485
5486 /**
5487 * Set the enabled state of this view.
5488 *
5489 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5490 * @attr ref android.R.styleable#View_visibility
5491 */
5492 @RemotableViewMethod
5493 public void setVisibility(int visibility) {
5494 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005495 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496 }
5497
5498 /**
5499 * Returns the enabled status for this view. The interpretation of the
5500 * enabled state varies by subclass.
5501 *
5502 * @return True if this view is enabled, false otherwise.
5503 */
5504 @ViewDebug.ExportedProperty
5505 public boolean isEnabled() {
5506 return (mViewFlags & ENABLED_MASK) == ENABLED;
5507 }
5508
5509 /**
5510 * Set the enabled state of this view. The interpretation of the enabled
5511 * state varies by subclass.
5512 *
5513 * @param enabled True if this view is enabled, false otherwise.
5514 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005515 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005517 if (enabled == isEnabled()) return;
5518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5520
5521 /*
5522 * The View most likely has to change its appearance, so refresh
5523 * the drawable state.
5524 */
5525 refreshDrawableState();
5526
5527 // Invalidate too, since the default behavior for views is to be
5528 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005529 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 }
5531
5532 /**
5533 * Set whether this view can receive the focus.
5534 *
5535 * Setting this to false will also ensure that this view is not focusable
5536 * in touch mode.
5537 *
5538 * @param focusable If true, this view can receive the focus.
5539 *
5540 * @see #setFocusableInTouchMode(boolean)
5541 * @attr ref android.R.styleable#View_focusable
5542 */
5543 public void setFocusable(boolean focusable) {
5544 if (!focusable) {
5545 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5546 }
5547 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5548 }
5549
5550 /**
5551 * Set whether this view can receive focus while in touch mode.
5552 *
5553 * Setting this to true will also ensure that this view is focusable.
5554 *
5555 * @param focusableInTouchMode If true, this view can receive the focus while
5556 * in touch mode.
5557 *
5558 * @see #setFocusable(boolean)
5559 * @attr ref android.R.styleable#View_focusableInTouchMode
5560 */
5561 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5562 // Focusable in touch mode should always be set before the focusable flag
5563 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5564 // which, in touch mode, will not successfully request focus on this view
5565 // because the focusable in touch mode flag is not set
5566 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5567 if (focusableInTouchMode) {
5568 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5569 }
5570 }
5571
5572 /**
5573 * Set whether this view should have sound effects enabled for events such as
5574 * clicking and touching.
5575 *
5576 * <p>You may wish to disable sound effects for a view if you already play sounds,
5577 * for instance, a dial key that plays dtmf tones.
5578 *
5579 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5580 * @see #isSoundEffectsEnabled()
5581 * @see #playSoundEffect(int)
5582 * @attr ref android.R.styleable#View_soundEffectsEnabled
5583 */
5584 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5585 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5586 }
5587
5588 /**
5589 * @return whether this view should have sound effects enabled for events such as
5590 * clicking and touching.
5591 *
5592 * @see #setSoundEffectsEnabled(boolean)
5593 * @see #playSoundEffect(int)
5594 * @attr ref android.R.styleable#View_soundEffectsEnabled
5595 */
5596 @ViewDebug.ExportedProperty
5597 public boolean isSoundEffectsEnabled() {
5598 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5599 }
5600
5601 /**
5602 * Set whether this view should have haptic feedback for events such as
5603 * long presses.
5604 *
5605 * <p>You may wish to disable haptic feedback if your view already controls
5606 * its own haptic feedback.
5607 *
5608 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5609 * @see #isHapticFeedbackEnabled()
5610 * @see #performHapticFeedback(int)
5611 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5612 */
5613 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5614 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5615 }
5616
5617 /**
5618 * @return whether this view should have haptic feedback enabled for events
5619 * long presses.
5620 *
5621 * @see #setHapticFeedbackEnabled(boolean)
5622 * @see #performHapticFeedback(int)
5623 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5624 */
5625 @ViewDebug.ExportedProperty
5626 public boolean isHapticFeedbackEnabled() {
5627 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5628 }
5629
5630 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005631 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005632 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005633 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5634 * {@link #LAYOUT_DIRECTION_RTL},
5635 * {@link #LAYOUT_DIRECTION_INHERIT} or
5636 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005637 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005638 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005639 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005640 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005641 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005642 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5643 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5644 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5645 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005646 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005647 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005648 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005649 }
5650
5651 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005652 * Set the layout direction for this view. This will propagate a reset of layout direction
5653 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005654 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005655 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5656 * {@link #LAYOUT_DIRECTION_RTL},
5657 * {@link #LAYOUT_DIRECTION_INHERIT} or
5658 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005659 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005660 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005661 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005662 */
5663 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005664 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005665 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005666 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005667 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005668 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005669 // Set the new layout direction (filtered) and ask for a layout pass
5670 mPrivateFlags2 |=
5671 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5672 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005673 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005674 }
5675
5676 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005677 * Returns the resolved layout direction for this view.
5678 *
5679 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005680 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005681 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005682 */
5683 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005684 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5685 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005686 })
5687 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005688 // The layout diretion will be resolved only if needed
5689 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5690 resolveLayoutDirection();
5691 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005692 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005693 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5694 }
5695
5696 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005697 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5698 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005699 *
5700 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005701 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005702 */
5703 @ViewDebug.ExportedProperty(category = "layout")
5704 public boolean isLayoutRtl() {
5705 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5706 }
5707
5708 /**
Adam Powell539ee872012-02-03 19:00:49 -08005709 * Indicates whether the view is currently tracking transient state that the
5710 * app should not need to concern itself with saving and restoring, but that
5711 * the framework should take special note to preserve when possible.
5712 *
Adam Powell785c4472012-05-02 21:25:39 -07005713 * <p>A view with transient state cannot be trivially rebound from an external
5714 * data source, such as an adapter binding item views in a list. This may be
5715 * because the view is performing an animation, tracking user selection
5716 * of content, or similar.</p>
5717 *
Adam Powell539ee872012-02-03 19:00:49 -08005718 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005719 */
5720 @ViewDebug.ExportedProperty(category = "layout")
5721 public boolean hasTransientState() {
5722 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5723 }
5724
5725 /**
5726 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005727 * framework should attempt to preserve when possible. This flag is reference counted,
5728 * so every call to setHasTransientState(true) should be paired with a later call
5729 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005730 *
Adam Powell785c4472012-05-02 21:25:39 -07005731 * <p>A view with transient state cannot be trivially rebound from an external
5732 * data source, such as an adapter binding item views in a list. This may be
5733 * because the view is performing an animation, tracking user selection
5734 * of content, or similar.</p>
5735 *
Adam Powell539ee872012-02-03 19:00:49 -08005736 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005737 */
5738 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005739 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5740 mTransientStateCount - 1;
5741 if (mTransientStateCount < 0) {
5742 mTransientStateCount = 0;
5743 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5744 "unmatched pair of setHasTransientState calls");
5745 }
5746 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005747 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005748 // update flag if we've just incremented up from 0 or decremented down to 0
5749 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5750 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5751 if (mParent != null) {
5752 try {
5753 mParent.childHasTransientStateChanged(this, hasTransientState);
5754 } catch (AbstractMethodError e) {
5755 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5756 " does not fully implement ViewParent", e);
5757 }
Adam Powell539ee872012-02-03 19:00:49 -08005758 }
5759 }
5760 }
5761
5762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 * If this view doesn't do any drawing on its own, set this flag to
5764 * allow further optimizations. By default, this flag is not set on
5765 * View, but could be set on some View subclasses such as ViewGroup.
5766 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005767 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5768 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005769 *
5770 * @param willNotDraw whether or not this View draw on its own
5771 */
5772 public void setWillNotDraw(boolean willNotDraw) {
5773 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5774 }
5775
5776 /**
5777 * Returns whether or not this View draws on its own.
5778 *
5779 * @return true if this view has nothing to draw, false otherwise
5780 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005781 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 public boolean willNotDraw() {
5783 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5784 }
5785
5786 /**
5787 * When a View's drawing cache is enabled, drawing is redirected to an
5788 * offscreen bitmap. Some views, like an ImageView, must be able to
5789 * bypass this mechanism if they already draw a single bitmap, to avoid
5790 * unnecessary usage of the memory.
5791 *
5792 * @param willNotCacheDrawing true if this view does not cache its
5793 * drawing, false otherwise
5794 */
5795 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5796 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5797 }
5798
5799 /**
5800 * Returns whether or not this View can cache its drawing or not.
5801 *
5802 * @return true if this view does not cache its drawing, false otherwise
5803 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005804 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 public boolean willNotCacheDrawing() {
5806 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5807 }
5808
5809 /**
5810 * Indicates whether this view reacts to click events or not.
5811 *
5812 * @return true if the view is clickable, false otherwise
5813 *
5814 * @see #setClickable(boolean)
5815 * @attr ref android.R.styleable#View_clickable
5816 */
5817 @ViewDebug.ExportedProperty
5818 public boolean isClickable() {
5819 return (mViewFlags & CLICKABLE) == CLICKABLE;
5820 }
5821
5822 /**
5823 * Enables or disables click events for this view. When a view
5824 * is clickable it will change its state to "pressed" on every click.
5825 * Subclasses should set the view clickable to visually react to
5826 * user's clicks.
5827 *
5828 * @param clickable true to make the view clickable, false otherwise
5829 *
5830 * @see #isClickable()
5831 * @attr ref android.R.styleable#View_clickable
5832 */
5833 public void setClickable(boolean clickable) {
5834 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5835 }
5836
5837 /**
5838 * Indicates whether this view reacts to long click events or not.
5839 *
5840 * @return true if the view is long clickable, false otherwise
5841 *
5842 * @see #setLongClickable(boolean)
5843 * @attr ref android.R.styleable#View_longClickable
5844 */
5845 public boolean isLongClickable() {
5846 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5847 }
5848
5849 /**
5850 * Enables or disables long click events for this view. When a view is long
5851 * clickable it reacts to the user holding down the button for a longer
5852 * duration than a tap. This event can either launch the listener or a
5853 * context menu.
5854 *
5855 * @param longClickable true to make the view long clickable, false otherwise
5856 * @see #isLongClickable()
5857 * @attr ref android.R.styleable#View_longClickable
5858 */
5859 public void setLongClickable(boolean longClickable) {
5860 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5861 }
5862
5863 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005864 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 *
5866 * @see #isClickable()
5867 * @see #setClickable(boolean)
5868 *
5869 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5870 * the View's internal state from a previously set "pressed" state.
5871 */
5872 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005873 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 if (pressed) {
5876 mPrivateFlags |= PRESSED;
5877 } else {
5878 mPrivateFlags &= ~PRESSED;
5879 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005880
5881 if (needsRefresh) {
5882 refreshDrawableState();
5883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 dispatchSetPressed(pressed);
5885 }
5886
5887 /**
5888 * Dispatch setPressed to all of this View's children.
5889 *
5890 * @see #setPressed(boolean)
5891 *
5892 * @param pressed The new pressed state
5893 */
5894 protected void dispatchSetPressed(boolean pressed) {
5895 }
5896
5897 /**
5898 * Indicates whether the view is currently in pressed state. Unless
5899 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5900 * the pressed state.
5901 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005902 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 * @see #isClickable()
5904 * @see #setClickable(boolean)
5905 *
5906 * @return true if the view is currently pressed, false otherwise
5907 */
5908 public boolean isPressed() {
5909 return (mPrivateFlags & PRESSED) == PRESSED;
5910 }
5911
5912 /**
5913 * Indicates whether this view will save its state (that is,
5914 * whether its {@link #onSaveInstanceState} method will be called).
5915 *
5916 * @return Returns true if the view state saving is enabled, else false.
5917 *
5918 * @see #setSaveEnabled(boolean)
5919 * @attr ref android.R.styleable#View_saveEnabled
5920 */
5921 public boolean isSaveEnabled() {
5922 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5923 }
5924
5925 /**
5926 * Controls whether the saving of this view's state is
5927 * enabled (that is, whether its {@link #onSaveInstanceState} method
5928 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005929 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 * for its state to be saved. This flag can only disable the
5931 * saving of this view; any child views may still have their state saved.
5932 *
5933 * @param enabled Set to false to <em>disable</em> state saving, or true
5934 * (the default) to allow it.
5935 *
5936 * @see #isSaveEnabled()
5937 * @see #setId(int)
5938 * @see #onSaveInstanceState()
5939 * @attr ref android.R.styleable#View_saveEnabled
5940 */
5941 public void setSaveEnabled(boolean enabled) {
5942 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5943 }
5944
Jeff Brown85a31762010-09-01 17:01:00 -07005945 /**
5946 * Gets whether the framework should discard touches when the view's
5947 * window is obscured by another visible window.
5948 * Refer to the {@link View} security documentation for more details.
5949 *
5950 * @return True if touch filtering is enabled.
5951 *
5952 * @see #setFilterTouchesWhenObscured(boolean)
5953 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5954 */
5955 @ViewDebug.ExportedProperty
5956 public boolean getFilterTouchesWhenObscured() {
5957 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5958 }
5959
5960 /**
5961 * Sets whether the framework should discard touches when the view's
5962 * window is obscured by another visible window.
5963 * Refer to the {@link View} security documentation for more details.
5964 *
5965 * @param enabled True if touch filtering should be enabled.
5966 *
5967 * @see #getFilterTouchesWhenObscured
5968 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5969 */
5970 public void setFilterTouchesWhenObscured(boolean enabled) {
5971 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5972 FILTER_TOUCHES_WHEN_OBSCURED);
5973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974
5975 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005976 * Indicates whether the entire hierarchy under this view will save its
5977 * state when a state saving traversal occurs from its parent. The default
5978 * is true; if false, these views will not be saved unless
5979 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5980 *
5981 * @return Returns true if the view state saving from parent is enabled, else false.
5982 *
5983 * @see #setSaveFromParentEnabled(boolean)
5984 */
5985 public boolean isSaveFromParentEnabled() {
5986 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5987 }
5988
5989 /**
5990 * Controls whether the entire hierarchy under this view will save its
5991 * state when a state saving traversal occurs from its parent. The default
5992 * is true; if false, these views will not be saved unless
5993 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5994 *
5995 * @param enabled Set to false to <em>disable</em> state saving, or true
5996 * (the default) to allow it.
5997 *
5998 * @see #isSaveFromParentEnabled()
5999 * @see #setId(int)
6000 * @see #onSaveInstanceState()
6001 */
6002 public void setSaveFromParentEnabled(boolean enabled) {
6003 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6004 }
6005
6006
6007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 * Returns whether this View is able to take focus.
6009 *
6010 * @return True if this view can take focus, or false otherwise.
6011 * @attr ref android.R.styleable#View_focusable
6012 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006013 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 public final boolean isFocusable() {
6015 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6016 }
6017
6018 /**
6019 * When a view is focusable, it may not want to take focus when in touch mode.
6020 * For example, a button would like focus when the user is navigating via a D-pad
6021 * so that the user can click on it, but once the user starts touching the screen,
6022 * the button shouldn't take focus
6023 * @return Whether the view is focusable in touch mode.
6024 * @attr ref android.R.styleable#View_focusableInTouchMode
6025 */
6026 @ViewDebug.ExportedProperty
6027 public final boolean isFocusableInTouchMode() {
6028 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6029 }
6030
6031 /**
6032 * Find the nearest view in the specified direction that can take focus.
6033 * This does not actually give focus to that view.
6034 *
6035 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6036 *
6037 * @return The nearest focusable in the specified direction, or null if none
6038 * can be found.
6039 */
6040 public View focusSearch(int direction) {
6041 if (mParent != null) {
6042 return mParent.focusSearch(this, direction);
6043 } else {
6044 return null;
6045 }
6046 }
6047
6048 /**
6049 * This method is the last chance for the focused view and its ancestors to
6050 * respond to an arrow key. This is called when the focused view did not
6051 * consume the key internally, nor could the view system find a new view in
6052 * the requested direction to give focus to.
6053 *
6054 * @param focused The currently focused view.
6055 * @param direction The direction focus wants to move. One of FOCUS_UP,
6056 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6057 * @return True if the this view consumed this unhandled move.
6058 */
6059 public boolean dispatchUnhandledMove(View focused, int direction) {
6060 return false;
6061 }
6062
6063 /**
6064 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006065 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006067 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6068 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 * @return The user specified next view, or null if there is none.
6070 */
6071 View findUserSetNextFocus(View root, int direction) {
6072 switch (direction) {
6073 case FOCUS_LEFT:
6074 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006075 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 case FOCUS_RIGHT:
6077 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006078 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 case FOCUS_UP:
6080 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006081 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 case FOCUS_DOWN:
6083 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006084 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006085 case FOCUS_FORWARD:
6086 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006087 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006088 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006089 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006090 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006091 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006092 @Override
6093 public boolean apply(View t) {
6094 return t.mNextFocusForwardId == id;
6095 }
6096 });
6097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 }
6099 return null;
6100 }
6101
Jeff Brown4dfbec22011-08-15 14:55:37 -07006102 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6103 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6104 @Override
6105 public boolean apply(View t) {
6106 return t.mID == childViewId;
6107 }
6108 });
6109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 if (result == null) {
6111 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6112 + "by user for id " + childViewId);
6113 }
6114 return result;
6115 }
6116
6117 /**
6118 * Find and return all focusable views that are descendants of this view,
6119 * possibly including this view if it is focusable itself.
6120 *
6121 * @param direction The direction of the focus
6122 * @return A list of focusable views
6123 */
6124 public ArrayList<View> getFocusables(int direction) {
6125 ArrayList<View> result = new ArrayList<View>(24);
6126 addFocusables(result, direction);
6127 return result;
6128 }
6129
6130 /**
6131 * Add any focusable views that are descendants of this view (possibly
6132 * including this view if it is focusable itself) to views. If we are in touch mode,
6133 * only add views that are also focusable in touch mode.
6134 *
6135 * @param views Focusable views found so far
6136 * @param direction The direction of the focus
6137 */
6138 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006139 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141
svetoslavganov75986cf2009-05-14 22:28:01 -07006142 /**
6143 * Adds any focusable views that are descendants of this view (possibly
6144 * including this view if it is focusable itself) to views. This method
6145 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006146 * only views focusable in touch mode if we are in touch mode or
6147 * only views that can take accessibility focus if accessibility is enabeld
6148 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006149 *
6150 * @param views Focusable views found so far or null if all we are interested is
6151 * the number of focusables.
6152 * @param direction The direction of the focus.
6153 * @param focusableMode The type of focusables to be added.
6154 *
6155 * @see #FOCUSABLES_ALL
6156 * @see #FOCUSABLES_TOUCH_MODE
6157 */
6158 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006159 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006160 return;
6161 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006162 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006163 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006164 views.add(this);
6165 return;
6166 }
6167 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006168 if (!isFocusable()) {
6169 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006170 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006171 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6172 && isInTouchMode() && !isFocusableInTouchMode()) {
6173 return;
6174 }
6175 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 }
6177
6178 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006179 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006180 * The search is performed by either the text that the View renders or the content
6181 * description that describes the view for accessibility purposes and the view does
6182 * not render or both. Clients can specify how the search is to be performed via
6183 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6184 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006185 *
6186 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006187 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006188 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006189 * @see #FIND_VIEWS_WITH_TEXT
6190 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6191 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006192 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006193 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006194 if (getAccessibilityNodeProvider() != null) {
6195 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6196 outViews.add(this);
6197 }
6198 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006199 && (searched != null && searched.length() > 0)
6200 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006201 String searchedLowerCase = searched.toString().toLowerCase();
6202 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6203 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6204 outViews.add(this);
6205 }
6206 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006207 }
6208
6209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006210 * Find and return all touchable views that are descendants of this view,
6211 * possibly including this view if it is touchable itself.
6212 *
6213 * @return A list of touchable views
6214 */
6215 public ArrayList<View> getTouchables() {
6216 ArrayList<View> result = new ArrayList<View>();
6217 addTouchables(result);
6218 return result;
6219 }
6220
6221 /**
6222 * Add any touchable views that are descendants of this view (possibly
6223 * including this view if it is touchable itself) to views.
6224 *
6225 * @param views Touchable views found so far
6226 */
6227 public void addTouchables(ArrayList<View> views) {
6228 final int viewFlags = mViewFlags;
6229
6230 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6231 && (viewFlags & ENABLED_MASK) == ENABLED) {
6232 views.add(this);
6233 }
6234 }
6235
6236 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006237 * Returns whether this View is accessibility focused.
6238 *
6239 * @return True if this View is accessibility focused.
6240 */
6241 boolean isAccessibilityFocused() {
6242 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6243 }
6244
6245 /**
6246 * Call this to try to give accessibility focus to this view.
6247 *
6248 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6249 * returns false or the view is no visible or the view already has accessibility
6250 * focus.
6251 *
6252 * See also {@link #focusSearch(int)}, which is what you call to say that you
6253 * have focus, and you want your parent to look for the next one.
6254 *
6255 * @return Whether this view actually took accessibility focus.
6256 *
6257 * @hide
6258 */
6259 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006260 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6261 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006262 return false;
6263 }
6264 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6265 return false;
6266 }
6267 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6268 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6269 ViewRootImpl viewRootImpl = getViewRootImpl();
6270 if (viewRootImpl != null) {
6271 viewRootImpl.setAccessibilityFocusedHost(this);
6272 }
6273 invalidate();
6274 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6275 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006276 return true;
6277 }
6278 return false;
6279 }
6280
6281 /**
6282 * Call this to try to clear accessibility focus of this view.
6283 *
6284 * See also {@link #focusSearch(int)}, which is what you call to say that you
6285 * have focus, and you want your parent to look for the next one.
6286 *
6287 * @hide
6288 */
6289 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006290 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6291 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006292 invalidate();
6293 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6294 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006295 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006296 // Clear the global reference of accessibility focus if this
6297 // view or any of its descendants had accessibility focus.
6298 ViewRootImpl viewRootImpl = getViewRootImpl();
6299 if (viewRootImpl != null) {
6300 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6301 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
6302 viewRootImpl.setAccessibilityFocusedHost(null);
6303 }
6304 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006305 }
6306
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006307 private void requestAccessibilityFocusFromHover() {
6308 if (includeForAccessibility() && isActionableForAccessibility()) {
6309 requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006310 requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006311 } else {
6312 if (mParent != null) {
6313 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6314 if (nextFocus != null) {
6315 nextFocus.requestAccessibilityFocus();
Svetoslav Ganovf76a83c2012-05-21 15:32:17 -07006316 nextFocus.requestFocusNoSearch(View.FOCUS_DOWN, null);
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006317 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006318 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006319 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006320 }
6321
Svetoslav Ganova90e4512012-06-01 19:02:32 -07006322 private boolean canTakeAccessibilityFocusFromHover() {
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006323 if (includeForAccessibility() && isActionableForAccessibility()) {
6324 return true;
6325 }
6326 if (mParent != null) {
6327 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006328 }
6329 return false;
6330 }
6331
6332 /**
6333 * Clears accessibility focus without calling any callback methods
6334 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6335 * is used for clearing accessibility focus when giving this focus to
6336 * another view.
6337 */
6338 void clearAccessibilityFocusNoCallbacks() {
6339 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6340 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6341 invalidate();
6342 }
6343 }
6344
6345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 * Call this to try to give focus to a specific view or to one of its
6347 * descendants.
6348 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006349 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6350 * false), or if it is focusable and it is not focusable in touch mode
6351 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006353 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 * have focus, and you want your parent to look for the next one.
6355 *
6356 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6357 * {@link #FOCUS_DOWN} and <code>null</code>.
6358 *
6359 * @return Whether this view or one of its descendants actually took focus.
6360 */
6361 public final boolean requestFocus() {
6362 return requestFocus(View.FOCUS_DOWN);
6363 }
6364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 /**
6366 * Call this to try to give focus to a specific view or to one of its
6367 * descendants and give it a hint about what direction focus is heading.
6368 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006369 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6370 * false), or if it is focusable and it is not focusable in touch mode
6371 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006373 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 * have focus, and you want your parent to look for the next one.
6375 *
6376 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6377 * <code>null</code> set for the previously focused rectangle.
6378 *
6379 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6380 * @return Whether this view or one of its descendants actually took focus.
6381 */
6382 public final boolean requestFocus(int direction) {
6383 return requestFocus(direction, null);
6384 }
6385
6386 /**
6387 * Call this to try to give focus to a specific view or to one of its descendants
6388 * and give it hints about the direction and a specific rectangle that the focus
6389 * is coming from. The rectangle can help give larger views a finer grained hint
6390 * about where focus is coming from, and therefore, where to show selection, or
6391 * forward focus change internally.
6392 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006393 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6394 * false), or if it is focusable and it is not focusable in touch mode
6395 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 *
6397 * A View will not take focus if it is not visible.
6398 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006399 * A View will not take focus if one of its parents has
6400 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6401 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006403 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 * have focus, and you want your parent to look for the next one.
6405 *
6406 * You may wish to override this method if your custom {@link View} has an internal
6407 * {@link View} that it wishes to forward the request to.
6408 *
6409 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6410 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6411 * to give a finer grained hint about where focus is coming from. May be null
6412 * if there is no hint.
6413 * @return Whether this view or one of its descendants actually took focus.
6414 */
6415 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006416 return requestFocusNoSearch(direction, previouslyFocusedRect);
6417 }
6418
6419 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 // need to be focusable
6421 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6422 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6423 return false;
6424 }
6425
6426 // need to be focusable in touch mode if in touch mode
6427 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006428 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6429 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 }
6431
6432 // need to not have any parents blocking us
6433 if (hasAncestorThatBlocksDescendantFocus()) {
6434 return false;
6435 }
6436
6437 handleFocusGainInternal(direction, previouslyFocusedRect);
6438 return true;
6439 }
6440
6441 /**
6442 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6443 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6444 * touch mode to request focus when they are touched.
6445 *
6446 * @return Whether this view or one of its descendants actually took focus.
6447 *
6448 * @see #isInTouchMode()
6449 *
6450 */
6451 public final boolean requestFocusFromTouch() {
6452 // Leave touch mode if we need to
6453 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006454 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006455 if (viewRoot != null) {
6456 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 }
6458 }
6459 return requestFocus(View.FOCUS_DOWN);
6460 }
6461
6462 /**
6463 * @return Whether any ancestor of this view blocks descendant focus.
6464 */
6465 private boolean hasAncestorThatBlocksDescendantFocus() {
6466 ViewParent ancestor = mParent;
6467 while (ancestor instanceof ViewGroup) {
6468 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6469 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6470 return true;
6471 } else {
6472 ancestor = vgAncestor.getParent();
6473 }
6474 }
6475 return false;
6476 }
6477
6478 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006479 * Gets the mode for determining whether this View is important for accessibility
6480 * which is if it fires accessibility events and if it is reported to
6481 * accessibility services that query the screen.
6482 *
6483 * @return The mode for determining whether a View is important for accessibility.
6484 *
6485 * @attr ref android.R.styleable#View_importantForAccessibility
6486 *
6487 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6488 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6489 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6490 */
6491 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006492 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6493 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6494 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006495 })
6496 public int getImportantForAccessibility() {
6497 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6498 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6499 }
6500
6501 /**
6502 * Sets how to determine whether this view is important for accessibility
6503 * which is if it fires accessibility events and if it is reported to
6504 * accessibility services that query the screen.
6505 *
6506 * @param mode How to determine whether this view is important for accessibility.
6507 *
6508 * @attr ref android.R.styleable#View_importantForAccessibility
6509 *
6510 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6511 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6512 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6513 */
6514 public void setImportantForAccessibility(int mode) {
6515 if (mode != getImportantForAccessibility()) {
6516 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6517 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6518 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6519 notifyAccessibilityStateChanged();
6520 }
6521 }
6522
6523 /**
6524 * Gets whether this view should be exposed for accessibility.
6525 *
6526 * @return Whether the view is exposed for accessibility.
6527 *
6528 * @hide
6529 */
6530 public boolean isImportantForAccessibility() {
6531 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6532 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6533 switch (mode) {
6534 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6535 return true;
6536 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6537 return false;
6538 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6539 return isActionableForAccessibility() || hasListenersForAccessibility();
6540 default:
6541 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6542 + mode);
6543 }
6544 }
6545
6546 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006547 * Gets the mode for determining whether this View can take accessibility focus.
6548 *
6549 * @return The mode for determining whether a View can take accessibility focus.
6550 *
6551 * @attr ref android.R.styleable#View_accessibilityFocusable
6552 *
6553 * @see #ACCESSIBILITY_FOCUSABLE_YES
6554 * @see #ACCESSIBILITY_FOCUSABLE_NO
6555 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6556 *
6557 * @hide
6558 */
6559 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6560 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6561 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6562 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6563 })
6564 public int getAccessibilityFocusable() {
6565 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6566 }
6567
6568 /**
6569 * Sets how to determine whether this view can take accessibility focus.
6570 *
6571 * @param mode How to determine whether this view can take accessibility focus.
6572 *
6573 * @attr ref android.R.styleable#View_accessibilityFocusable
6574 *
6575 * @see #ACCESSIBILITY_FOCUSABLE_YES
6576 * @see #ACCESSIBILITY_FOCUSABLE_NO
6577 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6578 *
6579 * @hide
6580 */
6581 public void setAccessibilityFocusable(int mode) {
6582 if (mode != getAccessibilityFocusable()) {
6583 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6584 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6585 & ACCESSIBILITY_FOCUSABLE_MASK;
6586 notifyAccessibilityStateChanged();
6587 }
6588 }
6589
6590 /**
6591 * Gets whether this view can take accessibility focus.
6592 *
6593 * @return Whether the view can take accessibility focus.
6594 *
6595 * @hide
6596 */
6597 public boolean isAccessibilityFocusable() {
6598 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6599 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6600 switch (mode) {
6601 case ACCESSIBILITY_FOCUSABLE_YES:
6602 return true;
6603 case ACCESSIBILITY_FOCUSABLE_NO:
6604 return false;
6605 case ACCESSIBILITY_FOCUSABLE_AUTO:
6606 return canTakeAccessibilityFocusFromHover()
6607 || getAccessibilityNodeProvider() != null;
6608 default:
6609 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6610 }
6611 }
6612
6613 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006614 * Gets the parent for accessibility purposes. Note that the parent for
6615 * accessibility is not necessary the immediate parent. It is the first
6616 * predecessor that is important for accessibility.
6617 *
6618 * @return The parent for accessibility purposes.
6619 */
6620 public ViewParent getParentForAccessibility() {
6621 if (mParent instanceof View) {
6622 View parentView = (View) mParent;
6623 if (parentView.includeForAccessibility()) {
6624 return mParent;
6625 } else {
6626 return mParent.getParentForAccessibility();
6627 }
6628 }
6629 return null;
6630 }
6631
6632 /**
6633 * Adds the children of a given View for accessibility. Since some Views are
6634 * not important for accessibility the children for accessibility are not
6635 * necessarily direct children of the riew, rather they are the first level of
6636 * descendants important for accessibility.
6637 *
6638 * @param children The list of children for accessibility.
6639 */
6640 public void addChildrenForAccessibility(ArrayList<View> children) {
6641 if (includeForAccessibility()) {
6642 children.add(this);
6643 }
6644 }
6645
6646 /**
6647 * Whether to regard this view for accessibility. A view is regarded for
6648 * accessibility if it is important for accessibility or the querying
6649 * accessibility service has explicitly requested that view not
6650 * important for accessibility are regarded.
6651 *
6652 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006653 *
6654 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006655 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006656 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006657 if (mAttachInfo != null) {
6658 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006659 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006660 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006661 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006662 }
6663 return false;
6664 }
6665
6666 /**
6667 * Returns whether the View is considered actionable from
6668 * accessibility perspective. Such view are important for
6669 * accessiiblity.
6670 *
6671 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006672 *
6673 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006674 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006675 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006676 return (isClickable() || isLongClickable() || isFocusable());
6677 }
6678
6679 /**
6680 * Returns whether the View has registered callbacks wich makes it
6681 * important for accessiiblity.
6682 *
6683 * @return True if the view is actionable for accessibility.
6684 */
6685 private boolean hasListenersForAccessibility() {
6686 ListenerInfo info = getListenerInfo();
6687 return mTouchDelegate != null || info.mOnKeyListener != null
6688 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6689 || info.mOnHoverListener != null || info.mOnDragListener != null;
6690 }
6691
6692 /**
6693 * Notifies accessibility services that some view's important for
6694 * accessibility state has changed. Note that such notifications
6695 * are made at most once every
6696 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6697 * to avoid unnecessary load to the system. Also once a view has
6698 * made a notifucation this method is a NOP until the notification has
6699 * been sent to clients.
6700 *
6701 * @hide
6702 *
6703 * TODO: Makse sure this method is called for any view state change
6704 * that is interesting for accessilility purposes.
6705 */
6706 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006707 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6708 return;
6709 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006710 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6711 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6712 if (mParent != null) {
6713 mParent.childAccessibilityStateChanged(this);
6714 }
6715 }
6716 }
6717
6718 /**
6719 * Reset the state indicating the this view has requested clients
6720 * interested in its accessiblity state to be notified.
6721 *
6722 * @hide
6723 */
6724 public void resetAccessibilityStateChanged() {
6725 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6726 }
6727
6728 /**
6729 * Performs the specified accessibility action on the view. For
6730 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006731 * <p>
6732 * If an {@link AccessibilityDelegate} has been specified via calling
6733 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6734 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6735 * is responsible for handling this call.
6736 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006737 *
6738 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006739 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006740 * @return Whether the action was performed.
6741 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006742 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006743 if (mAccessibilityDelegate != null) {
6744 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6745 } else {
6746 return performAccessibilityActionInternal(action, arguments);
6747 }
6748 }
6749
6750 /**
6751 * @see #performAccessibilityAction(int, Bundle)
6752 *
6753 * Note: Called from the default {@link AccessibilityDelegate}.
6754 */
6755 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006756 switch (action) {
6757 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006758 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006759 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006760 }
6761 } break;
6762 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6763 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006764 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006765 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006766 } break;
6767 case AccessibilityNodeInfo.ACTION_FOCUS: {
6768 if (!hasFocus()) {
6769 // Get out of touch mode since accessibility
6770 // wants to move focus around.
6771 getViewRootImpl().ensureTouchMode(false);
6772 return requestFocus();
6773 }
6774 } break;
6775 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6776 if (hasFocus()) {
6777 clearFocus();
6778 return !isFocused();
6779 }
6780 } break;
6781 case AccessibilityNodeInfo.ACTION_SELECT: {
6782 if (!isSelected()) {
6783 setSelected(true);
6784 return isSelected();
6785 }
6786 } break;
6787 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6788 if (isSelected()) {
6789 setSelected(false);
6790 return !isSelected();
6791 }
6792 } break;
6793 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006794 final int mode = getAccessibilityFocusable();
6795 if (!isAccessibilityFocused()
6796 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6797 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006798 return requestAccessibilityFocus();
6799 }
6800 } break;
6801 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6802 if (isAccessibilityFocused()) {
6803 clearAccessibilityFocus();
6804 return true;
6805 }
6806 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006807 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6808 if (arguments != null) {
6809 final int granularity = arguments.getInt(
6810 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6811 return nextAtGranularity(granularity);
6812 }
6813 } break;
6814 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6815 if (arguments != null) {
6816 final int granularity = arguments.getInt(
6817 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6818 return previousAtGranularity(granularity);
6819 }
6820 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006821 }
6822 return false;
6823 }
6824
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006825 private boolean nextAtGranularity(int granularity) {
6826 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006827 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006828 return false;
6829 }
6830 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6831 if (iterator == null) {
6832 return false;
6833 }
6834 final int current = getAccessibilityCursorPosition();
6835 final int[] range = iterator.following(current);
6836 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006837 return false;
6838 }
6839 final int start = range[0];
6840 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006841 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006842 sendViewTextTraversedAtGranularityEvent(
6843 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6844 granularity, start, end);
6845 return true;
6846 }
6847
6848 private boolean previousAtGranularity(int granularity) {
6849 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006850 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006851 return false;
6852 }
6853 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6854 if (iterator == null) {
6855 return false;
6856 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006857 int current = getAccessibilityCursorPosition();
6858 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6859 current = text.length();
6860 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6861 // When traversing by character we always put the cursor after the character
6862 // to ease edit and have to compensate before asking the for previous segment.
6863 current--;
6864 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006865 final int[] range = iterator.preceding(current);
6866 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006867 return false;
6868 }
6869 final int start = range[0];
6870 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006871 // Always put the cursor after the character to ease edit.
6872 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6873 setAccessibilityCursorPosition(end);
6874 } else {
6875 setAccessibilityCursorPosition(start);
6876 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006877 sendViewTextTraversedAtGranularityEvent(
6878 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6879 granularity, start, end);
6880 return true;
6881 }
6882
6883 /**
6884 * Gets the text reported for accessibility purposes.
6885 *
6886 * @return The accessibility text.
6887 *
6888 * @hide
6889 */
6890 public CharSequence getIterableTextForAccessibility() {
6891 return mContentDescription;
6892 }
6893
6894 /**
6895 * @hide
6896 */
6897 public int getAccessibilityCursorPosition() {
6898 return mAccessibilityCursorPosition;
6899 }
6900
6901 /**
6902 * @hide
6903 */
6904 public void setAccessibilityCursorPosition(int position) {
6905 mAccessibilityCursorPosition = position;
6906 }
6907
6908 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6909 int fromIndex, int toIndex) {
6910 if (mParent == null) {
6911 return;
6912 }
6913 AccessibilityEvent event = AccessibilityEvent.obtain(
6914 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6915 onInitializeAccessibilityEvent(event);
6916 onPopulateAccessibilityEvent(event);
6917 event.setFromIndex(fromIndex);
6918 event.setToIndex(toIndex);
6919 event.setAction(action);
6920 event.setMovementGranularity(granularity);
6921 mParent.requestSendAccessibilityEvent(this, event);
6922 }
6923
6924 /**
6925 * @hide
6926 */
6927 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6928 switch (granularity) {
6929 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6930 CharSequence text = getIterableTextForAccessibility();
6931 if (text != null && text.length() > 0) {
6932 CharacterTextSegmentIterator iterator =
6933 CharacterTextSegmentIterator.getInstance(mContext);
6934 iterator.initialize(text.toString());
6935 return iterator;
6936 }
6937 } break;
6938 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6939 CharSequence text = getIterableTextForAccessibility();
6940 if (text != null && text.length() > 0) {
6941 WordTextSegmentIterator iterator =
6942 WordTextSegmentIterator.getInstance(mContext);
6943 iterator.initialize(text.toString());
6944 return iterator;
6945 }
6946 } break;
6947 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6948 CharSequence text = getIterableTextForAccessibility();
6949 if (text != null && text.length() > 0) {
6950 ParagraphTextSegmentIterator iterator =
6951 ParagraphTextSegmentIterator.getInstance();
6952 iterator.initialize(text.toString());
6953 return iterator;
6954 }
6955 } break;
6956 }
6957 return null;
6958 }
6959
Svetoslav Ganov42138042012-03-20 11:51:39 -07006960 /**
Romain Guya440b002010-02-24 15:57:54 -08006961 * @hide
6962 */
6963 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006964 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006965 clearDisplayList();
6966
Romain Guya440b002010-02-24 15:57:54 -08006967 onStartTemporaryDetach();
6968 }
6969
6970 /**
6971 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6973 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006974 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 */
6976 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006977 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006978 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006979 }
6980
6981 /**
6982 * @hide
6983 */
6984 public void dispatchFinishTemporaryDetach() {
6985 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 }
Romain Guy8506ab42009-06-11 17:35:47 -07006987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 /**
6989 * Called after {@link #onStartTemporaryDetach} when the container is done
6990 * changing the view.
6991 */
6992 public void onFinishTemporaryDetach() {
6993 }
Romain Guy8506ab42009-06-11 17:35:47 -07006994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006996 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6997 * for this view's window. Returns null if the view is not currently attached
6998 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006999 * just use the standard high-level event callbacks like
7000 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007001 */
7002 public KeyEvent.DispatcherState getKeyDispatcherState() {
7003 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7004 }
Joe Malin32736f02011-01-19 16:14:20 -08007005
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 * Dispatch a key event before it is processed by any input method
7008 * associated with the view hierarchy. This can be used to intercept
7009 * key events in special situations before the IME consumes them; a
7010 * typical example would be handling the BACK key to update the application's
7011 * UI instead of allowing the IME to see it and close itself.
7012 *
7013 * @param event The key event to be dispatched.
7014 * @return True if the event was handled, false otherwise.
7015 */
7016 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7017 return onKeyPreIme(event.getKeyCode(), event);
7018 }
7019
7020 /**
7021 * Dispatch a key event to the next view on the focus path. This path runs
7022 * from the top of the view tree down to the currently focused view. If this
7023 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7024 * the next node down the focus path. This method also fires any key
7025 * listeners.
7026 *
7027 * @param event The key event to be dispatched.
7028 * @return True if the event was handled, false otherwise.
7029 */
7030 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007031 if (mInputEventConsistencyVerifier != null) {
7032 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007034
Jeff Brown21bc5c92011-02-28 18:27:14 -08007035 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007036 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007037 ListenerInfo li = mListenerInfo;
7038 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7039 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007040 return true;
7041 }
7042
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007043 if (event.dispatch(this, mAttachInfo != null
7044 ? mAttachInfo.mKeyDispatchState : null, this)) {
7045 return true;
7046 }
7047
7048 if (mInputEventConsistencyVerifier != null) {
7049 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7050 }
7051 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 }
7053
7054 /**
7055 * Dispatches a key shortcut event.
7056 *
7057 * @param event The key event to be dispatched.
7058 * @return True if the event was handled by the view, false otherwise.
7059 */
7060 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7061 return onKeyShortcut(event.getKeyCode(), event);
7062 }
7063
7064 /**
7065 * Pass the touch screen motion event down to the target view, or this
7066 * view if it is the target.
7067 *
7068 * @param event The motion event to be dispatched.
7069 * @return True if the event was handled by the view, false otherwise.
7070 */
7071 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007072 if (mInputEventConsistencyVerifier != null) {
7073 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7074 }
7075
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007076 if (onFilterTouchEventForSecurity(event)) {
7077 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007078 ListenerInfo li = mListenerInfo;
7079 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7080 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007081 return true;
7082 }
7083
7084 if (onTouchEvent(event)) {
7085 return true;
7086 }
Jeff Brown85a31762010-09-01 17:01:00 -07007087 }
7088
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007089 if (mInputEventConsistencyVerifier != null) {
7090 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007092 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 }
7094
7095 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007096 * Filter the touch event to apply security policies.
7097 *
7098 * @param event The motion event to be filtered.
7099 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007100 *
Jeff Brown85a31762010-09-01 17:01:00 -07007101 * @see #getFilterTouchesWhenObscured
7102 */
7103 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007104 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007105 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7106 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7107 // Window is obscured, drop this touch.
7108 return false;
7109 }
7110 return true;
7111 }
7112
7113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 * Pass a trackball motion event down to the focused view.
7115 *
7116 * @param event The motion event to be dispatched.
7117 * @return True if the event was handled by the view, false otherwise.
7118 */
7119 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007120 if (mInputEventConsistencyVerifier != null) {
7121 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7122 }
7123
Romain Guy02ccac62011-06-24 13:20:23 -07007124 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 }
7126
7127 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007128 * Dispatch a generic motion event.
7129 * <p>
7130 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7131 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007132 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007133 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007134 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007135 *
7136 * @param event The motion event to be dispatched.
7137 * @return True if the event was handled by the view, false otherwise.
7138 */
7139 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007140 if (mInputEventConsistencyVerifier != null) {
7141 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7142 }
7143
Jeff Browna032cc02011-03-07 16:56:21 -08007144 final int source = event.getSource();
7145 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7146 final int action = event.getAction();
7147 if (action == MotionEvent.ACTION_HOVER_ENTER
7148 || action == MotionEvent.ACTION_HOVER_MOVE
7149 || action == MotionEvent.ACTION_HOVER_EXIT) {
7150 if (dispatchHoverEvent(event)) {
7151 return true;
7152 }
7153 } else if (dispatchGenericPointerEvent(event)) {
7154 return true;
7155 }
7156 } else if (dispatchGenericFocusedEvent(event)) {
7157 return true;
7158 }
7159
Jeff Brown10b62902011-06-20 16:40:37 -07007160 if (dispatchGenericMotionEventInternal(event)) {
7161 return true;
7162 }
7163
7164 if (mInputEventConsistencyVerifier != null) {
7165 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7166 }
7167 return false;
7168 }
7169
7170 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007171 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007172 ListenerInfo li = mListenerInfo;
7173 if (li != null && li.mOnGenericMotionListener != null
7174 && (mViewFlags & ENABLED_MASK) == ENABLED
7175 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007176 return true;
7177 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007178
7179 if (onGenericMotionEvent(event)) {
7180 return true;
7181 }
7182
7183 if (mInputEventConsistencyVerifier != null) {
7184 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7185 }
7186 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007187 }
7188
7189 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007190 * Dispatch a hover event.
7191 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007192 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007193 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007194 * </p>
7195 *
7196 * @param event The motion event to be dispatched.
7197 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007198 */
7199 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007200 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007201 ListenerInfo li = mListenerInfo;
7202 if (li != null && li.mOnHoverListener != null
7203 && (mViewFlags & ENABLED_MASK) == ENABLED
7204 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007205 return true;
7206 }
7207
Jeff Browna032cc02011-03-07 16:56:21 -08007208 return onHoverEvent(event);
7209 }
7210
7211 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007212 * Returns true if the view has a child to which it has recently sent
7213 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7214 * it does not have a hovered child, then it must be the innermost hovered view.
7215 * @hide
7216 */
7217 protected boolean hasHoveredChild() {
7218 return false;
7219 }
7220
7221 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007222 * Dispatch a generic motion event to the view under the first pointer.
7223 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007224 * Do not call this method directly.
7225 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007226 * </p>
7227 *
7228 * @param event The motion event to be dispatched.
7229 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007230 */
7231 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7232 return false;
7233 }
7234
7235 /**
7236 * Dispatch a generic motion event to the currently focused view.
7237 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007238 * Do not call this method directly.
7239 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007240 * </p>
7241 *
7242 * @param event The motion event to be dispatched.
7243 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007244 */
7245 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7246 return false;
7247 }
7248
7249 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007250 * Dispatch a pointer event.
7251 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007252 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7253 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7254 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007255 * and should not be expected to handle other pointing device features.
7256 * </p>
7257 *
7258 * @param event The motion event to be dispatched.
7259 * @return True if the event was handled by the view, false otherwise.
7260 * @hide
7261 */
7262 public final boolean dispatchPointerEvent(MotionEvent event) {
7263 if (event.isTouchEvent()) {
7264 return dispatchTouchEvent(event);
7265 } else {
7266 return dispatchGenericMotionEvent(event);
7267 }
7268 }
7269
7270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 * Called when the window containing this view gains or loses window focus.
7272 * ViewGroups should override to route to their children.
7273 *
7274 * @param hasFocus True if the window containing this view now has focus,
7275 * false otherwise.
7276 */
7277 public void dispatchWindowFocusChanged(boolean hasFocus) {
7278 onWindowFocusChanged(hasFocus);
7279 }
7280
7281 /**
7282 * Called when the window containing this view gains or loses focus. Note
7283 * that this is separate from view focus: to receive key events, both
7284 * your view and its window must have focus. If a window is displayed
7285 * on top of yours that takes input focus, then your own window will lose
7286 * focus but the view focus will remain unchanged.
7287 *
7288 * @param hasWindowFocus True if the window containing this view now has
7289 * focus, false otherwise.
7290 */
7291 public void onWindowFocusChanged(boolean hasWindowFocus) {
7292 InputMethodManager imm = InputMethodManager.peekInstance();
7293 if (!hasWindowFocus) {
7294 if (isPressed()) {
7295 setPressed(false);
7296 }
7297 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7298 imm.focusOut(this);
7299 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007300 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007301 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007302 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7304 imm.focusIn(this);
7305 }
7306 refreshDrawableState();
7307 }
7308
7309 /**
7310 * Returns true if this view is in a window that currently has window focus.
7311 * Note that this is not the same as the view itself having focus.
7312 *
7313 * @return True if this view is in a window that currently has window focus.
7314 */
7315 public boolean hasWindowFocus() {
7316 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7317 }
7318
7319 /**
Adam Powell326d8082009-12-09 15:10:07 -08007320 * Dispatch a view visibility change down the view hierarchy.
7321 * ViewGroups should override to route to their children.
7322 * @param changedView The view whose visibility changed. Could be 'this' or
7323 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007324 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7325 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007326 */
7327 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7328 onVisibilityChanged(changedView, visibility);
7329 }
7330
7331 /**
7332 * Called when the visibility of the view or an ancestor of the view is changed.
7333 * @param changedView The view whose visibility changed. Could be 'this' or
7334 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007335 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7336 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007337 */
7338 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007339 if (visibility == VISIBLE) {
7340 if (mAttachInfo != null) {
7341 initialAwakenScrollBars();
7342 } else {
7343 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7344 }
7345 }
Adam Powell326d8082009-12-09 15:10:07 -08007346 }
7347
7348 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007349 * Dispatch a hint about whether this view is displayed. 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 public void dispatchDisplayHint(int hint) {
7358 onDisplayHint(hint);
7359 }
7360
7361 /**
7362 * Gives this view a hint about whether is displayed or not. For instance, when
7363 * a View moves out of the screen, it might receives a display hint indicating
7364 * the view is not displayed. Applications should not <em>rely</em> on this hint
7365 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007366 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007367 * @param hint A hint about whether or not this view is displayed:
7368 * {@link #VISIBLE} or {@link #INVISIBLE}.
7369 */
7370 protected void onDisplayHint(int hint) {
7371 }
7372
7373 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 * Dispatch a window visibility change down the view hierarchy.
7375 * ViewGroups should override to route to their children.
7376 *
7377 * @param visibility The new visibility of the window.
7378 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007379 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 */
7381 public void dispatchWindowVisibilityChanged(int visibility) {
7382 onWindowVisibilityChanged(visibility);
7383 }
7384
7385 /**
7386 * Called when the window containing has change its visibility
7387 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7388 * that this tells you whether or not your window is being made visible
7389 * to the window manager; this does <em>not</em> tell you whether or not
7390 * your window is obscured by other windows on the screen, even if it
7391 * is itself visible.
7392 *
7393 * @param visibility The new visibility of the window.
7394 */
7395 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007396 if (visibility == VISIBLE) {
7397 initialAwakenScrollBars();
7398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 }
7400
7401 /**
7402 * Returns the current visibility of the window this view is attached to
7403 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7404 *
7405 * @return Returns the current visibility of the view's window.
7406 */
7407 public int getWindowVisibility() {
7408 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7409 }
7410
7411 /**
7412 * Retrieve the overall visible display size in which the window this view is
7413 * attached to has been positioned in. This takes into account screen
7414 * decorations above the window, for both cases where the window itself
7415 * is being position inside of them or the window is being placed under
7416 * then and covered insets are used for the window to position its content
7417 * inside. In effect, this tells you the available area where content can
7418 * be placed and remain visible to users.
7419 *
7420 * <p>This function requires an IPC back to the window manager to retrieve
7421 * the requested information, so should not be used in performance critical
7422 * code like drawing.
7423 *
7424 * @param outRect Filled in with the visible display frame. If the view
7425 * is not attached to a window, this is simply the raw display size.
7426 */
7427 public void getWindowVisibleDisplayFrame(Rect outRect) {
7428 if (mAttachInfo != null) {
7429 try {
7430 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7431 } catch (RemoteException e) {
7432 return;
7433 }
7434 // XXX This is really broken, and probably all needs to be done
7435 // in the window manager, and we need to know more about whether
7436 // we want the area behind or in front of the IME.
7437 final Rect insets = mAttachInfo.mVisibleInsets;
7438 outRect.left += insets.left;
7439 outRect.top += insets.top;
7440 outRect.right -= insets.right;
7441 outRect.bottom -= insets.bottom;
7442 return;
7443 }
7444 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007445 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 }
7447
7448 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007449 * Dispatch a notification about a resource configuration change down
7450 * the view hierarchy.
7451 * ViewGroups should override to route to their children.
7452 *
7453 * @param newConfig The new resource configuration.
7454 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007455 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007456 */
7457 public void dispatchConfigurationChanged(Configuration newConfig) {
7458 onConfigurationChanged(newConfig);
7459 }
7460
7461 /**
7462 * Called when the current configuration of the resources being used
7463 * by the application have changed. You can use this to decide when
7464 * to reload resources that can changed based on orientation and other
7465 * configuration characterstics. You only need to use this if you are
7466 * not relying on the normal {@link android.app.Activity} mechanism of
7467 * recreating the activity instance upon a configuration change.
7468 *
7469 * @param newConfig The new resource configuration.
7470 */
7471 protected void onConfigurationChanged(Configuration newConfig) {
7472 }
7473
7474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007475 * Private function to aggregate all per-view attributes in to the view
7476 * root.
7477 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007478 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7479 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 }
7481
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007482 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7483 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007484 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007485 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007486 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007487 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007488 ListenerInfo li = mListenerInfo;
7489 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007490 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 }
7493 }
7494
7495 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007496 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007498 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7499 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 ai.mRecomputeGlobalAttributes = true;
7501 }
7502 }
7503 }
7504
7505 /**
7506 * Returns whether the device is currently in touch mode. Touch mode is entered
7507 * once the user begins interacting with the device by touch, and affects various
7508 * things like whether focus is always visible to the user.
7509 *
7510 * @return Whether the device is in touch mode.
7511 */
7512 @ViewDebug.ExportedProperty
7513 public boolean isInTouchMode() {
7514 if (mAttachInfo != null) {
7515 return mAttachInfo.mInTouchMode;
7516 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007517 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 }
7519 }
7520
7521 /**
7522 * Returns the context the view is running in, through which it can
7523 * access the current theme, resources, etc.
7524 *
7525 * @return The view's Context.
7526 */
7527 @ViewDebug.CapturedViewProperty
7528 public final Context getContext() {
7529 return mContext;
7530 }
7531
7532 /**
7533 * Handle a key event before it is processed by any input method
7534 * associated with the view hierarchy. This can be used to intercept
7535 * key events in special situations before the IME consumes them; a
7536 * typical example would be handling the BACK key to update the application's
7537 * UI instead of allowing the IME to see it and close itself.
7538 *
7539 * @param keyCode The value in event.getKeyCode().
7540 * @param event Description of the key event.
7541 * @return If you handled the event, return true. If you want to allow the
7542 * event to be handled by the next receiver, return false.
7543 */
7544 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7545 return false;
7546 }
7547
7548 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007549 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7550 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007551 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7552 * is released, if the view is enabled and clickable.
7553 *
Jean Chalard405bc512012-05-29 19:12:34 +09007554 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7555 * although some may elect to do so in some situations. Do not rely on this to
7556 * catch software key presses.
7557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 * @param keyCode A key code that represents the button pressed, from
7559 * {@link android.view.KeyEvent}.
7560 * @param event The KeyEvent object that defines the button action.
7561 */
7562 public boolean onKeyDown(int keyCode, KeyEvent event) {
7563 boolean result = false;
7564
7565 switch (keyCode) {
7566 case KeyEvent.KEYCODE_DPAD_CENTER:
7567 case KeyEvent.KEYCODE_ENTER: {
7568 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7569 return true;
7570 }
7571 // Long clickable items don't necessarily have to be clickable
7572 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7573 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7574 (event.getRepeatCount() == 0)) {
7575 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007576 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 return true;
7578 }
7579 break;
7580 }
7581 }
7582 return result;
7583 }
7584
7585 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007586 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7587 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7588 * the event).
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.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007592 */
7593 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7594 return false;
7595 }
7596
7597 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007598 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7599 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7601 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007602 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7603 * although some may elect to do so in some situations. Do not rely on this to
7604 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605 *
7606 * @param keyCode A key code that represents the button pressed, from
7607 * {@link android.view.KeyEvent}.
7608 * @param event The KeyEvent object that defines the button action.
7609 */
7610 public boolean onKeyUp(int keyCode, KeyEvent event) {
7611 boolean result = false;
7612
7613 switch (keyCode) {
7614 case KeyEvent.KEYCODE_DPAD_CENTER:
7615 case KeyEvent.KEYCODE_ENTER: {
7616 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7617 return true;
7618 }
7619 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7620 setPressed(false);
7621
7622 if (!mHasPerformedLongPress) {
7623 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007624 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625
7626 result = performClick();
7627 }
7628 }
7629 break;
7630 }
7631 }
7632 return result;
7633 }
7634
7635 /**
7636 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7637 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7638 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007639 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7640 * although some may elect to do so in some situations. Do not rely on this to
7641 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007642 *
7643 * @param keyCode A key code that represents the button pressed, from
7644 * {@link android.view.KeyEvent}.
7645 * @param repeatCount The number of times the action was made.
7646 * @param event The KeyEvent object that defines the button action.
7647 */
7648 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7649 return false;
7650 }
7651
7652 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007653 * Called on the focused view when a key shortcut event is not handled.
7654 * Override this method to implement local key shortcuts for the View.
7655 * Key shortcuts can also be implemented by setting the
7656 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 *
7658 * @param keyCode The value in event.getKeyCode().
7659 * @param event Description of the key event.
7660 * @return If you handled the event, return true. If you want to allow the
7661 * event to be handled by the next receiver, return false.
7662 */
7663 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7664 return false;
7665 }
7666
7667 /**
7668 * Check whether the called view is a text editor, in which case it
7669 * would make sense to automatically display a soft input window for
7670 * it. Subclasses should override this if they implement
7671 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007672 * a call on that method would return a non-null InputConnection, and
7673 * they are really a first-class editor that the user would normally
7674 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007675 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007676 * <p>The default implementation always returns false. This does
7677 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7678 * will not be called or the user can not otherwise perform edits on your
7679 * view; it is just a hint to the system that this is not the primary
7680 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007681 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 * @return Returns true if this view is a text editor, else false.
7683 */
7684 public boolean onCheckIsTextEditor() {
7685 return false;
7686 }
Romain Guy8506ab42009-06-11 17:35:47 -07007687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688 /**
7689 * Create a new InputConnection for an InputMethod to interact
7690 * with the view. The default implementation returns null, since it doesn't
7691 * support input methods. You can override this to implement such support.
7692 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 * <p>When implementing this, you probably also want to implement
7695 * {@link #onCheckIsTextEditor()} to indicate you will return a
7696 * non-null InputConnection.
7697 *
7698 * @param outAttrs Fill in with attribute information about the connection.
7699 */
7700 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7701 return null;
7702 }
7703
7704 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007705 * Called by the {@link android.view.inputmethod.InputMethodManager}
7706 * when a view who is not the current
7707 * input connection target is trying to make a call on the manager. The
7708 * default implementation returns false; you can override this to return
7709 * true for certain views if you are performing InputConnection proxying
7710 * to them.
7711 * @param view The View that is making the InputMethodManager call.
7712 * @return Return true to allow the call, false to reject.
7713 */
7714 public boolean checkInputConnectionProxy(View view) {
7715 return false;
7716 }
Romain Guy8506ab42009-06-11 17:35:47 -07007717
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007718 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 * Show the context menu for this view. It is not safe to hold on to the
7720 * menu after returning from this method.
7721 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007722 * You should normally not overload this method. Overload
7723 * {@link #onCreateContextMenu(ContextMenu)} or define an
7724 * {@link OnCreateContextMenuListener} to add items to the context menu.
7725 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007726 * @param menu The context menu to populate
7727 */
7728 public void createContextMenu(ContextMenu menu) {
7729 ContextMenuInfo menuInfo = getContextMenuInfo();
7730
7731 // Sets the current menu info so all items added to menu will have
7732 // my extra info set.
7733 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7734
7735 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007736 ListenerInfo li = mListenerInfo;
7737 if (li != null && li.mOnCreateContextMenuListener != null) {
7738 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007739 }
7740
7741 // Clear the extra information so subsequent items that aren't mine don't
7742 // have my extra info.
7743 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7744
7745 if (mParent != null) {
7746 mParent.createContextMenu(menu);
7747 }
7748 }
7749
7750 /**
7751 * Views should implement this if they have extra information to associate
7752 * with the context menu. The return result is supplied as a parameter to
7753 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7754 * callback.
7755 *
7756 * @return Extra information about the item for which the context menu
7757 * should be shown. This information will vary across different
7758 * subclasses of View.
7759 */
7760 protected ContextMenuInfo getContextMenuInfo() {
7761 return null;
7762 }
7763
7764 /**
7765 * Views should implement this if the view itself is going to add items to
7766 * the context menu.
7767 *
7768 * @param menu the context menu to populate
7769 */
7770 protected void onCreateContextMenu(ContextMenu menu) {
7771 }
7772
7773 /**
7774 * Implement this method to handle trackball motion events. The
7775 * <em>relative</em> movement of the trackball since the last event
7776 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7777 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7778 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7779 * they will often be fractional values, representing the more fine-grained
7780 * movement information available from a trackball).
7781 *
7782 * @param event The motion event.
7783 * @return True if the event was handled, false otherwise.
7784 */
7785 public boolean onTrackballEvent(MotionEvent event) {
7786 return false;
7787 }
7788
7789 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007790 * Implement this method to handle generic motion events.
7791 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007792 * Generic motion events describe joystick movements, mouse hovers, track pad
7793 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007794 * {@link MotionEvent#getSource() source} of the motion event specifies
7795 * the class of input that was received. Implementations of this method
7796 * must examine the bits in the source before processing the event.
7797 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007798 * </p><p>
7799 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7800 * are delivered to the view under the pointer. All other generic motion events are
7801 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007802 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007803 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007804 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007805 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7806 * // process the joystick movement...
7807 * return true;
7808 * }
7809 * }
7810 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7811 * switch (event.getAction()) {
7812 * case MotionEvent.ACTION_HOVER_MOVE:
7813 * // process the mouse hover movement...
7814 * return true;
7815 * case MotionEvent.ACTION_SCROLL:
7816 * // process the scroll wheel movement...
7817 * return true;
7818 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007819 * }
7820 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007821 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007822 *
7823 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007824 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007825 */
7826 public boolean onGenericMotionEvent(MotionEvent event) {
7827 return false;
7828 }
7829
7830 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007831 * Implement this method to handle hover events.
7832 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007833 * This method is called whenever a pointer is hovering into, over, or out of the
7834 * bounds of a view and the view is not currently being touched.
7835 * Hover events are represented as pointer events with action
7836 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7837 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7838 * </p>
7839 * <ul>
7840 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7841 * when the pointer enters the bounds of the view.</li>
7842 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7843 * when the pointer has already entered the bounds of the view and has moved.</li>
7844 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7845 * when the pointer has exited the bounds of the view or when the pointer is
7846 * about to go down due to a button click, tap, or similar user action that
7847 * causes the view to be touched.</li>
7848 * </ul>
7849 * <p>
7850 * The view should implement this method to return true to indicate that it is
7851 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007852 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007853 * The default implementation calls {@link #setHovered} to update the hovered state
7854 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007855 * is enabled and is clickable. The default implementation also sends hover
7856 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007857 * </p>
7858 *
7859 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007860 * @return True if the view handled the hover event.
7861 *
7862 * @see #isHovered
7863 * @see #setHovered
7864 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007865 */
7866 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007867 // The root view may receive hover (or touch) events that are outside the bounds of
7868 // the window. This code ensures that we only send accessibility events for
7869 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007870 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007871 if (!mSendingHoverAccessibilityEvents) {
7872 if ((action == MotionEvent.ACTION_HOVER_ENTER
7873 || action == MotionEvent.ACTION_HOVER_MOVE)
7874 && !hasHoveredChild()
7875 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007876 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007877 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07007878 requestAccessibilityFocusFromHover();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007879 }
7880 } else {
7881 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007882 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007883 && !pointInView(event.getX(), event.getY()))) {
7884 mSendingHoverAccessibilityEvents = false;
7885 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007886 // If the window does not have input focus we take away accessibility
7887 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007888 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007889 getViewRootImpl().setAccessibilityFocusedHost(null);
7890 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007891 }
Jeff Browna1b24182011-07-28 13:38:24 -07007892 }
7893
Jeff Brown87b7f802011-06-21 18:35:45 -07007894 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007895 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007896 case MotionEvent.ACTION_HOVER_ENTER:
7897 setHovered(true);
7898 break;
7899 case MotionEvent.ACTION_HOVER_EXIT:
7900 setHovered(false);
7901 break;
7902 }
Jeff Browna1b24182011-07-28 13:38:24 -07007903
7904 // Dispatch the event to onGenericMotionEvent before returning true.
7905 // This is to provide compatibility with existing applications that
7906 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7907 // break because of the new default handling for hoverable views
7908 // in onHoverEvent.
7909 // Note that onGenericMotionEvent will be called by default when
7910 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7911 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007912 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007913 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007914
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007915 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007916 }
7917
7918 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007919 * Returns true if the view should handle {@link #onHoverEvent}
7920 * by calling {@link #setHovered} to change its hovered state.
7921 *
7922 * @return True if the view is hoverable.
7923 */
7924 private boolean isHoverable() {
7925 final int viewFlags = mViewFlags;
7926 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7927 return false;
7928 }
7929
7930 return (viewFlags & CLICKABLE) == CLICKABLE
7931 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7932 }
7933
7934 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007935 * Returns true if the view is currently hovered.
7936 *
7937 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007938 *
7939 * @see #setHovered
7940 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007941 */
Jeff Brown10b62902011-06-20 16:40:37 -07007942 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007943 public boolean isHovered() {
7944 return (mPrivateFlags & HOVERED) != 0;
7945 }
7946
7947 /**
7948 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007949 * <p>
7950 * Calling this method also changes the drawable state of the view. This
7951 * enables the view to react to hover by using different drawable resources
7952 * to change its appearance.
7953 * </p><p>
7954 * The {@link #onHoverChanged} method is called when the hovered state changes.
7955 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007956 *
7957 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007958 *
7959 * @see #isHovered
7960 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007961 */
7962 public void setHovered(boolean hovered) {
7963 if (hovered) {
7964 if ((mPrivateFlags & HOVERED) == 0) {
7965 mPrivateFlags |= HOVERED;
7966 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007967 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007968 }
7969 } else {
7970 if ((mPrivateFlags & HOVERED) != 0) {
7971 mPrivateFlags &= ~HOVERED;
7972 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007973 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007974 }
7975 }
7976 }
7977
7978 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007979 * Implement this method to handle hover state changes.
7980 * <p>
7981 * This method is called whenever the hover state changes as a result of a
7982 * call to {@link #setHovered}.
7983 * </p>
7984 *
7985 * @param hovered The current hover state, as returned by {@link #isHovered}.
7986 *
7987 * @see #isHovered
7988 * @see #setHovered
7989 */
7990 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007991 }
7992
7993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007994 * Implement this method to handle touch screen motion events.
7995 *
7996 * @param event The motion event.
7997 * @return True if the event was handled, false otherwise.
7998 */
7999 public boolean onTouchEvent(MotionEvent event) {
8000 final int viewFlags = mViewFlags;
8001
8002 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008003 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008004 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 // A disabled view that is clickable still consumes the touch
8007 // events, it just doesn't respond to them.
8008 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8009 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8010 }
8011
8012 if (mTouchDelegate != null) {
8013 if (mTouchDelegate.onTouchEvent(event)) {
8014 return true;
8015 }
8016 }
8017
8018 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8019 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8020 switch (event.getAction()) {
8021 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008022 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8023 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 // take focus if we don't have it already and we should in
8025 // touch mode.
8026 boolean focusTaken = false;
8027 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8028 focusTaken = requestFocus();
8029 }
8030
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008031 if (prepressed) {
8032 // The button is being released before we actually
8033 // showed it as pressed. Make it show the pressed
8034 // state now (before scheduling the click) to ensure
8035 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008036 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008037 }
Joe Malin32736f02011-01-19 16:14:20 -08008038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 if (!mHasPerformedLongPress) {
8040 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008041 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042
8043 // Only perform take click actions if we were in the pressed state
8044 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008045 // Use a Runnable and post this rather than calling
8046 // performClick directly. This lets other visual state
8047 // of the view update before click actions start.
8048 if (mPerformClick == null) {
8049 mPerformClick = new PerformClick();
8050 }
8051 if (!post(mPerformClick)) {
8052 performClick();
8053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 }
8055 }
8056
8057 if (mUnsetPressedState == null) {
8058 mUnsetPressedState = new UnsetPressedState();
8059 }
8060
Adam Powelle14579b2009-12-16 18:39:52 -08008061 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008062 postDelayed(mUnsetPressedState,
8063 ViewConfiguration.getPressedStateDuration());
8064 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065 // If the post failed, unpress right now
8066 mUnsetPressedState.run();
8067 }
Adam Powelle14579b2009-12-16 18:39:52 -08008068 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 }
8070 break;
8071
8072 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008073 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008074
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008075 if (performButtonActionOnTouchDown(event)) {
8076 break;
8077 }
8078
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008079 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008080 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008081
8082 // For views inside a scrolling container, delay the pressed feedback for
8083 // a short period in case this is a scroll.
8084 if (isInScrollingContainer) {
8085 mPrivateFlags |= PREPRESSED;
8086 if (mPendingCheckForTap == null) {
8087 mPendingCheckForTap = new CheckForTap();
8088 }
8089 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8090 } else {
8091 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008092 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008093 checkForLongClick(0);
8094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095 break;
8096
8097 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008098 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008099 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 break;
8101
8102 case MotionEvent.ACTION_MOVE:
8103 final int x = (int) event.getX();
8104 final int y = (int) event.getY();
8105
8106 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008107 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008108 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008109 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008110 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008111 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008112 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113
Adam Powell4d6f0662012-02-21 15:11:11 -08008114 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 }
8117 break;
8118 }
8119 return true;
8120 }
8121
8122 return false;
8123 }
8124
8125 /**
Adam Powell10298662011-08-14 18:26:30 -07008126 * @hide
8127 */
8128 public boolean isInScrollingContainer() {
8129 ViewParent p = getParent();
8130 while (p != null && p instanceof ViewGroup) {
8131 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8132 return true;
8133 }
8134 p = p.getParent();
8135 }
8136 return false;
8137 }
8138
8139 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008140 * Remove the longpress detection timer.
8141 */
8142 private void removeLongPressCallback() {
8143 if (mPendingCheckForLongPress != null) {
8144 removeCallbacks(mPendingCheckForLongPress);
8145 }
8146 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008147
8148 /**
8149 * Remove the pending click action
8150 */
8151 private void removePerformClickCallback() {
8152 if (mPerformClick != null) {
8153 removeCallbacks(mPerformClick);
8154 }
8155 }
8156
Adam Powelle14579b2009-12-16 18:39:52 -08008157 /**
Romain Guya440b002010-02-24 15:57:54 -08008158 * Remove the prepress detection timer.
8159 */
8160 private void removeUnsetPressCallback() {
8161 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8162 setPressed(false);
8163 removeCallbacks(mUnsetPressedState);
8164 }
8165 }
8166
8167 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008168 * Remove the tap detection timer.
8169 */
8170 private void removeTapCallback() {
8171 if (mPendingCheckForTap != null) {
8172 mPrivateFlags &= ~PREPRESSED;
8173 removeCallbacks(mPendingCheckForTap);
8174 }
8175 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008176
8177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 * Cancels a pending long press. Your subclass can use this if you
8179 * want the context menu to come up if the user presses and holds
8180 * at the same place, but you don't want it to come up if they press
8181 * and then move around enough to cause scrolling.
8182 */
8183 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008184 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008185
8186 /*
8187 * The prepressed state handled by the tap callback is a display
8188 * construct, but the tap callback will post a long press callback
8189 * less its own timeout. Remove it here.
8190 */
8191 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 }
8193
8194 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008195 * Remove the pending callback for sending a
8196 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8197 */
8198 private void removeSendViewScrolledAccessibilityEventCallback() {
8199 if (mSendViewScrolledAccessibilityEvent != null) {
8200 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008201 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008202 }
8203 }
8204
8205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 * Sets the TouchDelegate for this View.
8207 */
8208 public void setTouchDelegate(TouchDelegate delegate) {
8209 mTouchDelegate = delegate;
8210 }
8211
8212 /**
8213 * Gets the TouchDelegate for this View.
8214 */
8215 public TouchDelegate getTouchDelegate() {
8216 return mTouchDelegate;
8217 }
8218
8219 /**
8220 * Set flags controlling behavior of this view.
8221 *
8222 * @param flags Constant indicating the value which should be set
8223 * @param mask Constant indicating the bit range that should be changed
8224 */
8225 void setFlags(int flags, int mask) {
8226 int old = mViewFlags;
8227 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8228
8229 int changed = mViewFlags ^ old;
8230 if (changed == 0) {
8231 return;
8232 }
8233 int privateFlags = mPrivateFlags;
8234
8235 /* Check if the FOCUSABLE bit has changed */
8236 if (((changed & FOCUSABLE_MASK) != 0) &&
8237 ((privateFlags & HAS_BOUNDS) !=0)) {
8238 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8239 && ((privateFlags & FOCUSED) != 0)) {
8240 /* Give up focus if we are no longer focusable */
8241 clearFocus();
8242 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8243 && ((privateFlags & FOCUSED) == 0)) {
8244 /*
8245 * Tell the view system that we are now available to take focus
8246 * if no one else already has it.
8247 */
8248 if (mParent != null) mParent.focusableViewAvailable(this);
8249 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008250 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8251 notifyAccessibilityStateChanged();
8252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253 }
8254
8255 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8256 if ((changed & VISIBILITY_MASK) != 0) {
8257 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008258 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008259 * it was not visible. Marking it drawn ensures that the invalidation will
8260 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008262 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008263 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264
8265 needGlobalAttributesUpdate(true);
8266
8267 // a view becoming visible is worth notifying the parent
8268 // about in case nothing has focus. even if this specific view
8269 // isn't focusable, it may contain something that is, so let
8270 // the root view try to give this focus if nothing else does.
8271 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8272 mParent.focusableViewAvailable(this);
8273 }
8274 }
8275 }
8276
8277 /* Check if the GONE bit has changed */
8278 if ((changed & GONE) != 0) {
8279 needGlobalAttributesUpdate(false);
8280 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281
Romain Guyecd80ee2009-12-03 17:13:02 -08008282 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8283 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008284 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008285 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008286 if (mParent instanceof View) {
8287 // GONE views noop invalidation, so invalidate the parent
8288 ((View) mParent).invalidate(true);
8289 }
8290 // Mark the view drawn to ensure that it gets invalidated properly the next
8291 // time it is visible and gets invalidated
8292 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 }
8294 if (mAttachInfo != null) {
8295 mAttachInfo.mViewVisibilityChanged = true;
8296 }
8297 }
8298
8299 /* Check if the VISIBLE bit has changed */
8300 if ((changed & INVISIBLE) != 0) {
8301 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008302 /*
8303 * If this view is becoming invisible, set the DRAWN flag so that
8304 * the next invalidate() will not be skipped.
8305 */
8306 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307
8308 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008309 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008310 if (getRootView() != this) {
8311 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008312 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 }
8314 }
8315 if (mAttachInfo != null) {
8316 mAttachInfo.mViewVisibilityChanged = true;
8317 }
8318 }
8319
Adam Powell326d8082009-12-09 15:10:07 -08008320 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008321 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008322 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8323 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008324 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008325 } else if (mParent != null) {
8326 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008327 }
Adam Powell326d8082009-12-09 15:10:07 -08008328 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8329 }
8330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8332 destroyDrawingCache();
8333 }
8334
8335 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8336 destroyDrawingCache();
8337 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008338 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 }
8340
8341 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8342 destroyDrawingCache();
8343 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8344 }
8345
8346 if ((changed & DRAW_MASK) != 0) {
8347 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008348 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 mPrivateFlags &= ~SKIP_DRAW;
8350 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8351 } else {
8352 mPrivateFlags |= SKIP_DRAW;
8353 }
8354 } else {
8355 mPrivateFlags &= ~SKIP_DRAW;
8356 }
8357 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008358 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008359 }
8360
8361 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008362 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 mParent.recomputeViewAttributes(this);
8364 }
8365 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008366
8367 if (AccessibilityManager.getInstance(mContext).isEnabled()
8368 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8369 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8370 notifyAccessibilityStateChanged();
8371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008372 }
8373
8374 /**
8375 * Change the view's z order in the tree, so it's on top of other sibling
8376 * views
8377 */
8378 public void bringToFront() {
8379 if (mParent != null) {
8380 mParent.bringChildToFront(this);
8381 }
8382 }
8383
8384 /**
8385 * This is called in response to an internal scroll in this view (i.e., the
8386 * view scrolled its own contents). This is typically as a result of
8387 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8388 * called.
8389 *
8390 * @param l Current horizontal scroll origin.
8391 * @param t Current vertical scroll origin.
8392 * @param oldl Previous horizontal scroll origin.
8393 * @param oldt Previous vertical scroll origin.
8394 */
8395 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008396 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8397 postSendViewScrolledAccessibilityEventCallback();
8398 }
8399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008400 mBackgroundSizeChanged = true;
8401
8402 final AttachInfo ai = mAttachInfo;
8403 if (ai != null) {
8404 ai.mViewScrollChanged = true;
8405 }
8406 }
8407
8408 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008409 * Interface definition for a callback to be invoked when the layout bounds of a view
8410 * changes due to layout processing.
8411 */
8412 public interface OnLayoutChangeListener {
8413 /**
8414 * Called when the focus state of a view has changed.
8415 *
8416 * @param v The view whose state has changed.
8417 * @param left The new value of the view's left property.
8418 * @param top The new value of the view's top property.
8419 * @param right The new value of the view's right property.
8420 * @param bottom The new value of the view's bottom property.
8421 * @param oldLeft The previous value of the view's left property.
8422 * @param oldTop The previous value of the view's top property.
8423 * @param oldRight The previous value of the view's right property.
8424 * @param oldBottom The previous value of the view's bottom property.
8425 */
8426 void onLayoutChange(View v, int left, int top, int right, int bottom,
8427 int oldLeft, int oldTop, int oldRight, int oldBottom);
8428 }
8429
8430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 * This is called during layout when the size of this view has changed. If
8432 * you were just added to the view hierarchy, you're called with the old
8433 * values of 0.
8434 *
8435 * @param w Current width of this view.
8436 * @param h Current height of this view.
8437 * @param oldw Old width of this view.
8438 * @param oldh Old height of this view.
8439 */
8440 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8441 }
8442
8443 /**
8444 * Called by draw to draw the child views. This may be overridden
8445 * by derived classes to gain control just before its children are drawn
8446 * (but after its own view has been drawn).
8447 * @param canvas the canvas on which to draw the view
8448 */
8449 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 }
8452
8453 /**
8454 * Gets the parent of this view. Note that the parent is a
8455 * ViewParent and not necessarily a View.
8456 *
8457 * @return Parent of this view.
8458 */
8459 public final ViewParent getParent() {
8460 return mParent;
8461 }
8462
8463 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008464 * Set the horizontal scrolled position of your view. This will cause a call to
8465 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8466 * invalidated.
8467 * @param value the x position to scroll to
8468 */
8469 public void setScrollX(int value) {
8470 scrollTo(value, mScrollY);
8471 }
8472
8473 /**
8474 * Set the vertical scrolled position of your view. This will cause a call to
8475 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8476 * invalidated.
8477 * @param value the y position to scroll to
8478 */
8479 public void setScrollY(int value) {
8480 scrollTo(mScrollX, value);
8481 }
8482
8483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 * Return the scrolled left position of this view. This is the left edge of
8485 * the displayed part of your view. You do not need to draw any pixels
8486 * farther left, since those are outside of the frame of your view on
8487 * screen.
8488 *
8489 * @return The left edge of the displayed part of your view, in pixels.
8490 */
8491 public final int getScrollX() {
8492 return mScrollX;
8493 }
8494
8495 /**
8496 * Return the scrolled top position of this view. This is the top edge of
8497 * the displayed part of your view. You do not need to draw any pixels above
8498 * it, since those are outside of the frame of your view on screen.
8499 *
8500 * @return The top edge of the displayed part of your view, in pixels.
8501 */
8502 public final int getScrollY() {
8503 return mScrollY;
8504 }
8505
8506 /**
8507 * Return the width of the your view.
8508 *
8509 * @return The width of your view, in pixels.
8510 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008511 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 public final int getWidth() {
8513 return mRight - mLeft;
8514 }
8515
8516 /**
8517 * Return the height of your view.
8518 *
8519 * @return The height of your view, in pixels.
8520 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008521 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 public final int getHeight() {
8523 return mBottom - mTop;
8524 }
8525
8526 /**
8527 * Return the visible drawing bounds of your view. Fills in the output
8528 * rectangle with the values from getScrollX(), getScrollY(),
8529 * getWidth(), and getHeight().
8530 *
8531 * @param outRect The (scrolled) drawing bounds of the view.
8532 */
8533 public void getDrawingRect(Rect outRect) {
8534 outRect.left = mScrollX;
8535 outRect.top = mScrollY;
8536 outRect.right = mScrollX + (mRight - mLeft);
8537 outRect.bottom = mScrollY + (mBottom - mTop);
8538 }
8539
8540 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008541 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8542 * raw width component (that is the result is masked by
8543 * {@link #MEASURED_SIZE_MASK}).
8544 *
8545 * @return The raw measured width of this view.
8546 */
8547 public final int getMeasuredWidth() {
8548 return mMeasuredWidth & MEASURED_SIZE_MASK;
8549 }
8550
8551 /**
8552 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008553 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008554 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008555 * This should be used during measurement and layout calculations only. Use
8556 * {@link #getWidth()} to see how wide a view is after layout.
8557 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008558 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008560 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 return mMeasuredWidth;
8562 }
8563
8564 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008565 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8566 * raw width component (that is the result is masked by
8567 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008569 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008570 */
8571 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008572 return mMeasuredHeight & MEASURED_SIZE_MASK;
8573 }
8574
8575 /**
8576 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008577 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008578 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8579 * This should be used during measurement and layout calculations only. Use
8580 * {@link #getHeight()} to see how wide a view is after layout.
8581 *
8582 * @return The measured width of this view as a bit mask.
8583 */
8584 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008585 return mMeasuredHeight;
8586 }
8587
8588 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008589 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8590 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8591 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8592 * and the height component is at the shifted bits
8593 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8594 */
8595 public final int getMeasuredState() {
8596 return (mMeasuredWidth&MEASURED_STATE_MASK)
8597 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8598 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8599 }
8600
8601 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008602 * The transform matrix of this view, which is calculated based on the current
8603 * roation, scale, and pivot properties.
8604 *
8605 * @see #getRotation()
8606 * @see #getScaleX()
8607 * @see #getScaleY()
8608 * @see #getPivotX()
8609 * @see #getPivotY()
8610 * @return The current transform matrix for the view
8611 */
8612 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008613 if (mTransformationInfo != null) {
8614 updateMatrix();
8615 return mTransformationInfo.mMatrix;
8616 }
8617 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008618 }
8619
8620 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008621 * Utility function to determine if the value is far enough away from zero to be
8622 * considered non-zero.
8623 * @param value A floating point value to check for zero-ness
8624 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8625 */
8626 private static boolean nonzero(float value) {
8627 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8628 }
8629
8630 /**
Jeff Brown86671742010-09-30 20:00:15 -07008631 * Returns true if the transform matrix is the identity matrix.
8632 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008633 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008634 * @return True if the transform matrix is the identity matrix, false otherwise.
8635 */
Jeff Brown86671742010-09-30 20:00:15 -07008636 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008637 if (mTransformationInfo != null) {
8638 updateMatrix();
8639 return mTransformationInfo.mMatrixIsIdentity;
8640 }
8641 return true;
8642 }
8643
8644 void ensureTransformationInfo() {
8645 if (mTransformationInfo == null) {
8646 mTransformationInfo = new TransformationInfo();
8647 }
Jeff Brown86671742010-09-30 20:00:15 -07008648 }
8649
8650 /**
8651 * Recomputes the transform matrix if necessary.
8652 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008653 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008654 final TransformationInfo info = mTransformationInfo;
8655 if (info == null) {
8656 return;
8657 }
8658 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008659 // transform-related properties have changed since the last time someone
8660 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008661
8662 // Figure out if we need to update the pivot point
8663 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008664 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8665 info.mPrevWidth = mRight - mLeft;
8666 info.mPrevHeight = mBottom - mTop;
8667 info.mPivotX = info.mPrevWidth / 2f;
8668 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008669 }
8670 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008671 info.mMatrix.reset();
8672 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8673 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8674 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8675 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008676 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008677 if (info.mCamera == null) {
8678 info.mCamera = new Camera();
8679 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008680 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008681 info.mCamera.save();
8682 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8683 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8684 info.mCamera.getMatrix(info.matrix3D);
8685 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8686 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8687 info.mPivotY + info.mTranslationY);
8688 info.mMatrix.postConcat(info.matrix3D);
8689 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008690 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008691 info.mMatrixDirty = false;
8692 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8693 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008694 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008695 }
8696
8697 /**
8698 * Utility method to retrieve the inverse of the current mMatrix property.
8699 * We cache the matrix to avoid recalculating it when transform properties
8700 * have not changed.
8701 *
8702 * @return The inverse of the current matrix of this view.
8703 */
Jeff Brown86671742010-09-30 20:00:15 -07008704 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 final TransformationInfo info = mTransformationInfo;
8706 if (info != null) {
8707 updateMatrix();
8708 if (info.mInverseMatrixDirty) {
8709 if (info.mInverseMatrix == null) {
8710 info.mInverseMatrix = new Matrix();
8711 }
8712 info.mMatrix.invert(info.mInverseMatrix);
8713 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008714 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008715 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008716 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008717 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008718 }
8719
8720 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008721 * Gets the distance along the Z axis from the camera to this view.
8722 *
8723 * @see #setCameraDistance(float)
8724 *
8725 * @return The distance along the Z axis.
8726 */
8727 public float getCameraDistance() {
8728 ensureTransformationInfo();
8729 final float dpi = mResources.getDisplayMetrics().densityDpi;
8730 final TransformationInfo info = mTransformationInfo;
8731 if (info.mCamera == null) {
8732 info.mCamera = new Camera();
8733 info.matrix3D = new Matrix();
8734 }
8735 return -(info.mCamera.getLocationZ() * dpi);
8736 }
8737
8738 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008739 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8740 * views are drawn) from the camera to this view. The camera's distance
8741 * affects 3D transformations, for instance rotations around the X and Y
8742 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008743 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008744 * use a camera distance that's greater than the height (X axis rotation) or
8745 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008746 *
Romain Guya5364ee2011-02-24 14:46:04 -08008747 * <p>The distance of the camera from the view plane can have an affect on the
8748 * perspective distortion of the view when it is rotated around the x or y axis.
8749 * For example, a large distance will result in a large viewing angle, and there
8750 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008751 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008752 * also result in some drawing artifacts if the rotated view ends up partially
8753 * behind the camera (which is why the recommendation is to use a distance at
8754 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008755 *
Romain Guya5364ee2011-02-24 14:46:04 -08008756 * <p>The distance is expressed in "depth pixels." The default distance depends
8757 * on the screen density. For instance, on a medium density display, the
8758 * default distance is 1280. On a high density display, the default distance
8759 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008760 *
Romain Guya5364ee2011-02-24 14:46:04 -08008761 * <p>If you want to specify a distance that leads to visually consistent
8762 * results across various densities, use the following formula:</p>
8763 * <pre>
8764 * float scale = context.getResources().getDisplayMetrics().density;
8765 * view.setCameraDistance(distance * scale);
8766 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008767 *
Romain Guya5364ee2011-02-24 14:46:04 -08008768 * <p>The density scale factor of a high density display is 1.5,
8769 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008770 *
Romain Guya5364ee2011-02-24 14:46:04 -08008771 * @param distance The distance in "depth pixels", if negative the opposite
8772 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008773 *
8774 * @see #setRotationX(float)
8775 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008776 */
8777 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008778 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008779
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008780 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008781 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008782 final TransformationInfo info = mTransformationInfo;
8783 if (info.mCamera == null) {
8784 info.mCamera = new Camera();
8785 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008786 }
8787
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008788 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8789 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008790
Chet Haase9d1992d2012-03-13 11:03:25 -07008791 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008792 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008793 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008794 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008795 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8796 // View was rejected last time it was drawn by its parent; this may have changed
8797 invalidateParentIfNeeded();
8798 }
Romain Guya5364ee2011-02-24 14:46:04 -08008799 }
8800
8801 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008802 * The degrees that the view is rotated around the pivot point.
8803 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008804 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008805 * @see #getPivotX()
8806 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008807 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008808 * @return The degrees of rotation.
8809 */
Chet Haasea5531132012-02-02 13:41:44 -08008810 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008811 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008812 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008813 }
8814
8815 /**
Chet Haase897247b2010-09-09 14:54:47 -07008816 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8817 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008818 *
8819 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008820 *
8821 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008822 * @see #getPivotX()
8823 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008824 * @see #setRotationX(float)
8825 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008826 *
8827 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008828 */
8829 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008830 ensureTransformationInfo();
8831 final TransformationInfo info = mTransformationInfo;
8832 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008833 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008834 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008835 info.mRotation = rotation;
8836 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008837 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008838 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008839 mDisplayList.setRotation(rotation);
8840 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008841 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8842 // View was rejected last time it was drawn by its parent; this may have changed
8843 invalidateParentIfNeeded();
8844 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008845 }
8846 }
8847
8848 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008849 * The degrees that the view is rotated around the vertical axis through the pivot point.
8850 *
8851 * @see #getPivotX()
8852 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008853 * @see #setRotationY(float)
8854 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008855 * @return The degrees of Y rotation.
8856 */
Chet Haasea5531132012-02-02 13:41:44 -08008857 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008858 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008859 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008860 }
8861
8862 /**
Chet Haase897247b2010-09-09 14:54:47 -07008863 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8864 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8865 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008866 *
Romain Guya5364ee2011-02-24 14:46:04 -08008867 * When rotating large views, it is recommended to adjust the camera distance
8868 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008869 *
8870 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008871 *
8872 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008873 * @see #getPivotX()
8874 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008875 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008876 * @see #setRotationX(float)
8877 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008878 *
8879 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008880 */
8881 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008882 ensureTransformationInfo();
8883 final TransformationInfo info = mTransformationInfo;
8884 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008885 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008886 info.mRotationY = rotationY;
8887 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008888 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008889 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008890 mDisplayList.setRotationY(rotationY);
8891 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008892 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8893 // View was rejected last time it was drawn by its parent; this may have changed
8894 invalidateParentIfNeeded();
8895 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008896 }
8897 }
8898
8899 /**
8900 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8901 *
8902 * @see #getPivotX()
8903 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008904 * @see #setRotationX(float)
8905 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008906 * @return The degrees of X rotation.
8907 */
Chet Haasea5531132012-02-02 13:41:44 -08008908 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008909 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008910 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008911 }
8912
8913 /**
Chet Haase897247b2010-09-09 14:54:47 -07008914 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8915 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8916 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008917 *
Romain Guya5364ee2011-02-24 14:46:04 -08008918 * When rotating large views, it is recommended to adjust the camera distance
8919 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008920 *
8921 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008922 *
8923 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008924 * @see #getPivotX()
8925 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008926 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008927 * @see #setRotationY(float)
8928 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008929 *
8930 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008931 */
8932 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008933 ensureTransformationInfo();
8934 final TransformationInfo info = mTransformationInfo;
8935 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008936 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008937 info.mRotationX = rotationX;
8938 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008940 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008941 mDisplayList.setRotationX(rotationX);
8942 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008943 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8944 // View was rejected last time it was drawn by its parent; this may have changed
8945 invalidateParentIfNeeded();
8946 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008947 }
8948 }
8949
8950 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008951 * The amount that the view is scaled in x around the pivot point, as a proportion of
8952 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8953 *
Joe Onorato93162322010-09-16 15:42:01 -04008954 * <p>By default, this is 1.0f.
8955 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 * @see #getPivotX()
8957 * @see #getPivotY()
8958 * @return The scaling factor.
8959 */
Chet Haasea5531132012-02-02 13:41:44 -08008960 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008961 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008962 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008963 }
8964
8965 /**
8966 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8967 * the view's unscaled width. A value of 1 means that no scaling is applied.
8968 *
8969 * @param scaleX The scaling factor.
8970 * @see #getPivotX()
8971 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008972 *
8973 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 */
8975 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008976 ensureTransformationInfo();
8977 final TransformationInfo info = mTransformationInfo;
8978 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008979 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 info.mScaleX = scaleX;
8981 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008982 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008983 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008984 mDisplayList.setScaleX(scaleX);
8985 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008986 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8987 // View was rejected last time it was drawn by its parent; this may have changed
8988 invalidateParentIfNeeded();
8989 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008990 }
8991 }
8992
8993 /**
8994 * The amount that the view is scaled in y around the pivot point, as a proportion of
8995 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8996 *
Joe Onorato93162322010-09-16 15:42:01 -04008997 * <p>By default, this is 1.0f.
8998 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008999 * @see #getPivotX()
9000 * @see #getPivotY()
9001 * @return The scaling factor.
9002 */
Chet Haasea5531132012-02-02 13:41:44 -08009003 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009004 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009005 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009006 }
9007
9008 /**
9009 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9010 * the view's unscaled width. A value of 1 means that no scaling is applied.
9011 *
9012 * @param scaleY The scaling factor.
9013 * @see #getPivotX()
9014 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009015 *
9016 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 */
9018 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009019 ensureTransformationInfo();
9020 final TransformationInfo info = mTransformationInfo;
9021 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009022 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 info.mScaleY = scaleY;
9024 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009025 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009026 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009027 mDisplayList.setScaleY(scaleY);
9028 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009029 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9030 // View was rejected last time it was drawn by its parent; this may have changed
9031 invalidateParentIfNeeded();
9032 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009033 }
9034 }
9035
9036 /**
9037 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9038 * and {@link #setScaleX(float) scaled}.
9039 *
9040 * @see #getRotation()
9041 * @see #getScaleX()
9042 * @see #getScaleY()
9043 * @see #getPivotY()
9044 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009045 *
9046 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009047 */
Chet Haasea5531132012-02-02 13:41:44 -08009048 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009049 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009050 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009051 }
9052
9053 /**
9054 * Sets the x location of the point around which the view is
9055 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009056 * By default, the pivot point is centered on the object.
9057 * Setting this property disables this behavior and causes the view to use only the
9058 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009059 *
9060 * @param pivotX The x location of the pivot point.
9061 * @see #getRotation()
9062 * @see #getScaleX()
9063 * @see #getScaleY()
9064 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009065 *
9066 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009067 */
9068 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009069 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009070 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009071 final TransformationInfo info = mTransformationInfo;
9072 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009073 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009074 info.mPivotX = pivotX;
9075 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009076 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009077 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009078 mDisplayList.setPivotX(pivotX);
9079 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009080 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9081 // View was rejected last time it was drawn by its parent; this may have changed
9082 invalidateParentIfNeeded();
9083 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009084 }
9085 }
9086
9087 /**
9088 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9089 * and {@link #setScaleY(float) scaled}.
9090 *
9091 * @see #getRotation()
9092 * @see #getScaleX()
9093 * @see #getScaleY()
9094 * @see #getPivotY()
9095 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009096 *
9097 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009098 */
Chet Haasea5531132012-02-02 13:41:44 -08009099 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009100 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009101 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009102 }
9103
9104 /**
9105 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009106 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9107 * Setting this property disables this behavior and causes the view to use only the
9108 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009109 *
9110 * @param pivotY The y location of the pivot point.
9111 * @see #getRotation()
9112 * @see #getScaleX()
9113 * @see #getScaleY()
9114 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009115 *
9116 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009117 */
9118 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009119 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009120 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009121 final TransformationInfo info = mTransformationInfo;
9122 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009123 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009124 info.mPivotY = pivotY;
9125 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009126 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009127 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009128 mDisplayList.setPivotY(pivotY);
9129 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009130 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9131 // View was rejected last time it was drawn by its parent; this may have changed
9132 invalidateParentIfNeeded();
9133 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009134 }
9135 }
9136
9137 /**
9138 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9139 * completely transparent and 1 means the view is completely opaque.
9140 *
Joe Onorato93162322010-09-16 15:42:01 -04009141 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009142 * @return The opacity of the view.
9143 */
Chet Haasea5531132012-02-02 13:41:44 -08009144 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009145 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009146 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009147 }
9148
9149 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009150 * Returns whether this View has content which overlaps. This function, intended to be
9151 * overridden by specific View types, is an optimization when alpha is set on a view. If
9152 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9153 * and then composited it into place, which can be expensive. If the view has no overlapping
9154 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9155 * An example of overlapping rendering is a TextView with a background image, such as a
9156 * Button. An example of non-overlapping rendering is a TextView with no background, or
9157 * an ImageView with only the foreground image. The default implementation returns true;
9158 * subclasses should override if they have cases which can be optimized.
9159 *
9160 * @return true if the content in this view might overlap, false otherwise.
9161 */
9162 public boolean hasOverlappingRendering() {
9163 return true;
9164 }
9165
9166 /**
Romain Guy171c5922011-01-06 10:04:23 -08009167 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9168 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009169 *
Romain Guy171c5922011-01-06 10:04:23 -08009170 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9171 * responsible for applying the opacity itself. Otherwise, calling this method is
9172 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009173 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 *
Chet Haasea5531132012-02-02 13:41:44 -08009175 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9176 * performance implications. It is generally best to use the alpha property sparingly and
9177 * transiently, as in the case of fading animations.</p>
9178 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009179 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009180 *
Joe Malin32736f02011-01-19 16:14:20 -08009181 * @see #setLayerType(int, android.graphics.Paint)
9182 *
Chet Haase73066682010-11-29 15:55:32 -08009183 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009184 */
9185 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009186 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009187 if (mTransformationInfo.mAlpha != alpha) {
9188 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009189 if (onSetAlpha((int) (alpha * 255))) {
9190 mPrivateFlags |= ALPHA_SET;
9191 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009192 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009193 invalidate(true);
9194 } else {
9195 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009196 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009197 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009198 mDisplayList.setAlpha(alpha);
9199 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009200 }
Chet Haaseed032702010-10-01 14:05:54 -07009201 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009202 }
9203
9204 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009205 * Faster version of setAlpha() which performs the same steps except there are
9206 * no calls to invalidate(). The caller of this function should perform proper invalidation
9207 * on the parent and this object. The return value indicates whether the subclass handles
9208 * alpha (the return value for onSetAlpha()).
9209 *
9210 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009211 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9212 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009213 */
9214 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009215 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009216 if (mTransformationInfo.mAlpha != alpha) {
9217 mTransformationInfo.mAlpha = alpha;
9218 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9219 if (subclassHandlesAlpha) {
9220 mPrivateFlags |= ALPHA_SET;
9221 return true;
9222 } else {
9223 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009224 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009225 mDisplayList.setAlpha(alpha);
9226 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009227 }
Chet Haasea00f3862011-02-22 06:34:40 -08009228 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009229 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009230 }
9231
9232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 * Top position of this view relative to its parent.
9234 *
9235 * @return The top of this view, in pixels.
9236 */
9237 @ViewDebug.CapturedViewProperty
9238 public final int getTop() {
9239 return mTop;
9240 }
9241
9242 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009243 * Sets the top position of this view relative to its parent. This method is meant to be called
9244 * by the layout system and should not generally be called otherwise, because the property
9245 * may be changed at any time by the layout.
9246 *
9247 * @param top The top of this view, in pixels.
9248 */
9249 public final void setTop(int top) {
9250 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009251 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009252 final boolean matrixIsIdentity = mTransformationInfo == null
9253 || mTransformationInfo.mMatrixIsIdentity;
9254 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009255 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009256 int minTop;
9257 int yLoc;
9258 if (top < mTop) {
9259 minTop = top;
9260 yLoc = top - mTop;
9261 } else {
9262 minTop = mTop;
9263 yLoc = 0;
9264 }
Chet Haasee9140a72011-02-16 16:23:29 -08009265 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009266 }
9267 } else {
9268 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009269 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009270 }
9271
Chet Haaseed032702010-10-01 14:05:54 -07009272 int width = mRight - mLeft;
9273 int oldHeight = mBottom - mTop;
9274
Chet Haase21cd1382010-09-01 17:42:29 -07009275 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009276 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009277 mDisplayList.setTop(mTop);
9278 }
Chet Haase21cd1382010-09-01 17:42:29 -07009279
Chet Haaseed032702010-10-01 14:05:54 -07009280 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9281
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009282 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009283 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9284 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009285 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009286 }
Chet Haase21cd1382010-09-01 17:42:29 -07009287 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009288 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009289 }
Chet Haase55dbb652010-12-21 20:15:08 -08009290 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009291 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009292 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9293 // View was rejected last time it was drawn by its parent; this may have changed
9294 invalidateParentIfNeeded();
9295 }
Chet Haase21cd1382010-09-01 17:42:29 -07009296 }
9297 }
9298
9299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 * Bottom position of this view relative to its parent.
9301 *
9302 * @return The bottom of this view, in pixels.
9303 */
9304 @ViewDebug.CapturedViewProperty
9305 public final int getBottom() {
9306 return mBottom;
9307 }
9308
9309 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009310 * True if this view has changed since the last time being drawn.
9311 *
9312 * @return The dirty state of this view.
9313 */
9314 public boolean isDirty() {
9315 return (mPrivateFlags & DIRTY_MASK) != 0;
9316 }
9317
9318 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009319 * Sets the bottom position of this view relative to its parent. This method is meant to be
9320 * called by the layout system and should not generally be called otherwise, because the
9321 * property may be changed at any time by the layout.
9322 *
9323 * @param bottom The bottom of this view, in pixels.
9324 */
9325 public final void setBottom(int bottom) {
9326 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009327 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009328 final boolean matrixIsIdentity = mTransformationInfo == null
9329 || mTransformationInfo.mMatrixIsIdentity;
9330 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009331 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009332 int maxBottom;
9333 if (bottom < mBottom) {
9334 maxBottom = mBottom;
9335 } else {
9336 maxBottom = bottom;
9337 }
Chet Haasee9140a72011-02-16 16:23:29 -08009338 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009339 }
9340 } else {
9341 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009342 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009343 }
9344
Chet Haaseed032702010-10-01 14:05:54 -07009345 int width = mRight - mLeft;
9346 int oldHeight = mBottom - mTop;
9347
Chet Haase21cd1382010-09-01 17:42:29 -07009348 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009349 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009350 mDisplayList.setBottom(mBottom);
9351 }
Chet Haase21cd1382010-09-01 17:42:29 -07009352
Chet Haaseed032702010-10-01 14:05:54 -07009353 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9354
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009355 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009356 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9357 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009358 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009359 }
Chet Haase21cd1382010-09-01 17:42:29 -07009360 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009361 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009362 }
Chet Haase55dbb652010-12-21 20:15:08 -08009363 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009364 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009365 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9366 // View was rejected last time it was drawn by its parent; this may have changed
9367 invalidateParentIfNeeded();
9368 }
Chet Haase21cd1382010-09-01 17:42:29 -07009369 }
9370 }
9371
9372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 * Left position of this view relative to its parent.
9374 *
9375 * @return The left edge of this view, in pixels.
9376 */
9377 @ViewDebug.CapturedViewProperty
9378 public final int getLeft() {
9379 return mLeft;
9380 }
9381
9382 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009383 * Sets the left position of this view relative to its parent. This method is meant to be called
9384 * by the layout system and should not generally be called otherwise, because the property
9385 * may be changed at any time by the layout.
9386 *
9387 * @param left The bottom of this view, in pixels.
9388 */
9389 public final void setLeft(int left) {
9390 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009391 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009392 final boolean matrixIsIdentity = mTransformationInfo == null
9393 || mTransformationInfo.mMatrixIsIdentity;
9394 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009395 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009396 int minLeft;
9397 int xLoc;
9398 if (left < mLeft) {
9399 minLeft = left;
9400 xLoc = left - mLeft;
9401 } else {
9402 minLeft = mLeft;
9403 xLoc = 0;
9404 }
Chet Haasee9140a72011-02-16 16:23:29 -08009405 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009406 }
9407 } else {
9408 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009409 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009410 }
9411
Chet Haaseed032702010-10-01 14:05:54 -07009412 int oldWidth = mRight - mLeft;
9413 int height = mBottom - mTop;
9414
Chet Haase21cd1382010-09-01 17:42:29 -07009415 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009416 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009417 mDisplayList.setLeft(left);
9418 }
Chet Haase21cd1382010-09-01 17:42:29 -07009419
Chet Haaseed032702010-10-01 14:05:54 -07009420 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9421
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009422 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009423 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9424 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009425 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009426 }
Chet Haase21cd1382010-09-01 17:42:29 -07009427 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009428 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009429 }
Chet Haase55dbb652010-12-21 20:15:08 -08009430 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009431 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009432 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9433 // View was rejected last time it was drawn by its parent; this may have changed
9434 invalidateParentIfNeeded();
9435 }
Chet Haase21cd1382010-09-01 17:42:29 -07009436 }
9437 }
9438
9439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 * Right position of this view relative to its parent.
9441 *
9442 * @return The right edge of this view, in pixels.
9443 */
9444 @ViewDebug.CapturedViewProperty
9445 public final int getRight() {
9446 return mRight;
9447 }
9448
9449 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009450 * Sets the right position of this view relative to its parent. This method is meant to be called
9451 * by the layout system and should not generally be called otherwise, because the property
9452 * may be changed at any time by the layout.
9453 *
9454 * @param right The bottom of this view, in pixels.
9455 */
9456 public final void setRight(int right) {
9457 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009458 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009459 final boolean matrixIsIdentity = mTransformationInfo == null
9460 || mTransformationInfo.mMatrixIsIdentity;
9461 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009462 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009463 int maxRight;
9464 if (right < mRight) {
9465 maxRight = mRight;
9466 } else {
9467 maxRight = right;
9468 }
Chet Haasee9140a72011-02-16 16:23:29 -08009469 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009470 }
9471 } else {
9472 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009473 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009474 }
9475
Chet Haaseed032702010-10-01 14:05:54 -07009476 int oldWidth = mRight - mLeft;
9477 int height = mBottom - mTop;
9478
Chet Haase21cd1382010-09-01 17:42:29 -07009479 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009480 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009481 mDisplayList.setRight(mRight);
9482 }
Chet Haase21cd1382010-09-01 17:42:29 -07009483
Chet Haaseed032702010-10-01 14:05:54 -07009484 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9485
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009486 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009487 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9488 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009489 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009490 }
Chet Haase21cd1382010-09-01 17:42:29 -07009491 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009492 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009493 }
Chet Haase55dbb652010-12-21 20:15:08 -08009494 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009495 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009496 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9497 // View was rejected last time it was drawn by its parent; this may have changed
9498 invalidateParentIfNeeded();
9499 }
Chet Haase21cd1382010-09-01 17:42:29 -07009500 }
9501 }
9502
9503 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009504 * The visual x position of this view, in pixels. This is equivalent to the
9505 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009506 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009507 *
Chet Haasedf030d22010-07-30 17:22:38 -07009508 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009509 */
Chet Haasea5531132012-02-02 13:41:44 -08009510 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009511 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009512 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009513 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009514
Chet Haasedf030d22010-07-30 17:22:38 -07009515 /**
9516 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9517 * {@link #setTranslationX(float) translationX} property to be the difference between
9518 * the x value passed in and the current {@link #getLeft() left} property.
9519 *
9520 * @param x The visual x position of this view, in pixels.
9521 */
9522 public void setX(float x) {
9523 setTranslationX(x - mLeft);
9524 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009525
Chet Haasedf030d22010-07-30 17:22:38 -07009526 /**
9527 * The visual y position of this view, in pixels. This is equivalent to the
9528 * {@link #setTranslationY(float) translationY} property plus the current
9529 * {@link #getTop() top} property.
9530 *
9531 * @return The visual y position of this view, in pixels.
9532 */
Chet Haasea5531132012-02-02 13:41:44 -08009533 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009534 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009535 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009536 }
9537
9538 /**
9539 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9540 * {@link #setTranslationY(float) translationY} property to be the difference between
9541 * the y value passed in and the current {@link #getTop() top} property.
9542 *
9543 * @param y The visual y position of this view, in pixels.
9544 */
9545 public void setY(float y) {
9546 setTranslationY(y - mTop);
9547 }
9548
9549
9550 /**
9551 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9552 * This position is post-layout, in addition to wherever the object's
9553 * layout placed it.
9554 *
9555 * @return The horizontal position of this view relative to its left position, in pixels.
9556 */
Chet Haasea5531132012-02-02 13:41:44 -08009557 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009558 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009559 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009560 }
9561
9562 /**
9563 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9564 * This effectively positions the object post-layout, in addition to wherever the object's
9565 * layout placed it.
9566 *
9567 * @param translationX The horizontal position of this view relative to its left position,
9568 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009569 *
9570 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009571 */
9572 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009573 ensureTransformationInfo();
9574 final TransformationInfo info = mTransformationInfo;
9575 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009576 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009577 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009578 info.mTranslationX = translationX;
9579 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009580 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009581 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009582 mDisplayList.setTranslationX(translationX);
9583 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009584 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9585 // View was rejected last time it was drawn by its parent; this may have changed
9586 invalidateParentIfNeeded();
9587 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009588 }
9589 }
9590
9591 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009592 * The horizontal location of this view relative to its {@link #getTop() top} position.
9593 * This position is 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 * @return The vertical position of this view relative to its top position,
9597 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009598 */
Chet Haasea5531132012-02-02 13:41:44 -08009599 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009600 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009601 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009602 }
9603
9604 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009605 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9606 * This effectively positions the object post-layout, in addition to wherever the object's
9607 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009608 *
Chet Haasedf030d22010-07-30 17:22:38 -07009609 * @param translationY The vertical position of this view relative to its top position,
9610 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009611 *
9612 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009613 */
Chet Haasedf030d22010-07-30 17:22:38 -07009614 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009615 ensureTransformationInfo();
9616 final TransformationInfo info = mTransformationInfo;
9617 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009618 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009619 info.mTranslationY = translationY;
9620 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009621 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009622 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009623 mDisplayList.setTranslationY(translationY);
9624 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009625 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9626 // View was rejected last time it was drawn by its parent; this may have changed
9627 invalidateParentIfNeeded();
9628 }
Chet Haasedf030d22010-07-30 17:22:38 -07009629 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009630 }
9631
9632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 * Hit rectangle in parent's coordinates
9634 *
9635 * @param outRect The hit rectangle of the view.
9636 */
9637 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009638 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009639 final TransformationInfo info = mTransformationInfo;
9640 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009641 outRect.set(mLeft, mTop, mRight, mBottom);
9642 } else {
9643 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009644 tmpRect.set(-info.mPivotX, -info.mPivotY,
9645 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9646 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009647 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9648 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009649 }
9650 }
9651
9652 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009653 * Determines whether the given point, in local coordinates is inside the view.
9654 */
9655 /*package*/ final boolean pointInView(float localX, float localY) {
9656 return localX >= 0 && localX < (mRight - mLeft)
9657 && localY >= 0 && localY < (mBottom - mTop);
9658 }
9659
9660 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009661 * Utility method to determine whether the given point, in local coordinates,
9662 * is inside the view, where the area of the view is expanded by the slop factor.
9663 * This method is called while processing touch-move events to determine if the event
9664 * is still within the view.
9665 */
9666 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009667 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009668 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 }
9670
9671 /**
9672 * When a view has focus and the user navigates away from it, the next view is searched for
9673 * starting from the rectangle filled in by this method.
9674 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009675 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9676 * of the view. However, if your view maintains some idea of internal selection,
9677 * such as a cursor, or a selected row or column, you should override this method and
9678 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009679 *
9680 * @param r The rectangle to fill in, in this view's coordinates.
9681 */
9682 public void getFocusedRect(Rect r) {
9683 getDrawingRect(r);
9684 }
9685
9686 /**
9687 * If some part of this view is not clipped by any of its parents, then
9688 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009689 * coordinates (without taking possible View rotations into account), offset
9690 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9691 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 *
9693 * @param r If true is returned, r holds the global coordinates of the
9694 * visible portion of this view.
9695 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9696 * between this view and its root. globalOffet may be null.
9697 * @return true if r is non-empty (i.e. part of the view is visible at the
9698 * root level.
9699 */
9700 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9701 int width = mRight - mLeft;
9702 int height = mBottom - mTop;
9703 if (width > 0 && height > 0) {
9704 r.set(0, 0, width, height);
9705 if (globalOffset != null) {
9706 globalOffset.set(-mScrollX, -mScrollY);
9707 }
9708 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9709 }
9710 return false;
9711 }
9712
9713 public final boolean getGlobalVisibleRect(Rect r) {
9714 return getGlobalVisibleRect(r, null);
9715 }
9716
9717 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009718 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009719 if (getGlobalVisibleRect(r, offset)) {
9720 r.offset(-offset.x, -offset.y); // make r local
9721 return true;
9722 }
9723 return false;
9724 }
9725
9726 /**
9727 * Offset this view's vertical location by the specified number of pixels.
9728 *
9729 * @param offset the number of pixels to offset the view by
9730 */
9731 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009732 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009733 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009734 final boolean matrixIsIdentity = mTransformationInfo == null
9735 || mTransformationInfo.mMatrixIsIdentity;
9736 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009737 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009738 invalidateViewProperty(false, false);
9739 } else {
9740 final ViewParent p = mParent;
9741 if (p != null && mAttachInfo != null) {
9742 final Rect r = mAttachInfo.mTmpInvalRect;
9743 int minTop;
9744 int maxBottom;
9745 int yLoc;
9746 if (offset < 0) {
9747 minTop = mTop + offset;
9748 maxBottom = mBottom;
9749 yLoc = offset;
9750 } else {
9751 minTop = mTop;
9752 maxBottom = mBottom + offset;
9753 yLoc = 0;
9754 }
9755 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9756 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009757 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009758 }
9759 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009760 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009761 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009762
Chet Haasec3aa3612010-06-17 08:50:37 -07009763 mTop += offset;
9764 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009765 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009766 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009767 invalidateViewProperty(false, false);
9768 } else {
9769 if (!matrixIsIdentity) {
9770 invalidateViewProperty(false, true);
9771 }
9772 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009773 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009775 }
9776
9777 /**
9778 * Offset this view's horizontal location by the specified amount of pixels.
9779 *
9780 * @param offset the numer of pixels to offset the view by
9781 */
9782 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009783 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009784 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009785 final boolean matrixIsIdentity = mTransformationInfo == null
9786 || mTransformationInfo.mMatrixIsIdentity;
9787 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009788 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009789 invalidateViewProperty(false, false);
9790 } else {
9791 final ViewParent p = mParent;
9792 if (p != null && mAttachInfo != null) {
9793 final Rect r = mAttachInfo.mTmpInvalRect;
9794 int minLeft;
9795 int maxRight;
9796 if (offset < 0) {
9797 minLeft = mLeft + offset;
9798 maxRight = mRight;
9799 } else {
9800 minLeft = mLeft;
9801 maxRight = mRight + offset;
9802 }
9803 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9804 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009805 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009806 }
9807 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009808 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009809 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009810
Chet Haasec3aa3612010-06-17 08:50:37 -07009811 mLeft += offset;
9812 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009813 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009814 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009815 invalidateViewProperty(false, false);
9816 } else {
9817 if (!matrixIsIdentity) {
9818 invalidateViewProperty(false, true);
9819 }
9820 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009821 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 }
9824
9825 /**
9826 * Get the LayoutParams associated with this view. All views should have
9827 * layout parameters. These supply parameters to the <i>parent</i> of this
9828 * view specifying how it should be arranged. There are many subclasses of
9829 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9830 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009831 *
9832 * This method may return null if this View is not attached to a parent
9833 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9834 * was not invoked successfully. When a View is attached to a parent
9835 * ViewGroup, this method must not return null.
9836 *
9837 * @return The LayoutParams associated with this view, or null if no
9838 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009840 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 public ViewGroup.LayoutParams getLayoutParams() {
9842 return mLayoutParams;
9843 }
9844
9845 /**
9846 * Set the layout parameters associated with this view. These supply
9847 * parameters to the <i>parent</i> of this view specifying how it should be
9848 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9849 * correspond to the different subclasses of ViewGroup that are responsible
9850 * for arranging their children.
9851 *
Romain Guy01c174b2011-02-22 11:51:06 -08009852 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 */
9854 public void setLayoutParams(ViewGroup.LayoutParams params) {
9855 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009856 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 }
9858 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009859 if (mParent instanceof ViewGroup) {
9860 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009862 requestLayout();
9863 }
9864
9865 /**
9866 * Set the scrolled position of your view. This will cause a call to
9867 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9868 * invalidated.
9869 * @param x the x position to scroll to
9870 * @param y the y position to scroll to
9871 */
9872 public void scrollTo(int x, int y) {
9873 if (mScrollX != x || mScrollY != y) {
9874 int oldX = mScrollX;
9875 int oldY = mScrollY;
9876 mScrollX = x;
9877 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009878 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009880 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009881 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009883 }
9884 }
9885
9886 /**
9887 * Move the scrolled position of your view. This will cause a call to
9888 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9889 * invalidated.
9890 * @param x the amount of pixels to scroll by horizontally
9891 * @param y the amount of pixels to scroll by vertically
9892 */
9893 public void scrollBy(int x, int y) {
9894 scrollTo(mScrollX + x, mScrollY + y);
9895 }
9896
9897 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009898 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9899 * animation to fade the scrollbars out after a default delay. If a subclass
9900 * provides animated scrolling, the start delay should equal the duration
9901 * of the scrolling animation.</p>
9902 *
9903 * <p>The animation starts only if at least one of the scrollbars is
9904 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9905 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9906 * this method returns true, and false otherwise. If the animation is
9907 * started, this method calls {@link #invalidate()}; in that case the
9908 * caller should not call {@link #invalidate()}.</p>
9909 *
9910 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009911 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009912 *
9913 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9914 * and {@link #scrollTo(int, int)}.</p>
9915 *
9916 * @return true if the animation is played, false otherwise
9917 *
9918 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009919 * @see #scrollBy(int, int)
9920 * @see #scrollTo(int, int)
9921 * @see #isHorizontalScrollBarEnabled()
9922 * @see #isVerticalScrollBarEnabled()
9923 * @see #setHorizontalScrollBarEnabled(boolean)
9924 * @see #setVerticalScrollBarEnabled(boolean)
9925 */
9926 protected boolean awakenScrollBars() {
9927 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009928 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009929 }
9930
9931 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009932 * Trigger the scrollbars to draw.
9933 * This method differs from awakenScrollBars() only in its default duration.
9934 * initialAwakenScrollBars() will show the scroll bars for longer than
9935 * usual to give the user more of a chance to notice them.
9936 *
9937 * @return true if the animation is played, false otherwise.
9938 */
9939 private boolean initialAwakenScrollBars() {
9940 return mScrollCache != null &&
9941 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9942 }
9943
9944 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009945 * <p>
9946 * Trigger the scrollbars to draw. When invoked this method starts an
9947 * animation to fade the scrollbars out after a fixed delay. If a subclass
9948 * provides animated scrolling, the start delay should equal the duration of
9949 * the scrolling animation.
9950 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009951 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009952 * <p>
9953 * The animation starts only if at least one of the scrollbars is enabled,
9954 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9955 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9956 * this method returns true, and false otherwise. If the animation is
9957 * started, this method calls {@link #invalidate()}; in that case the caller
9958 * should not call {@link #invalidate()}.
9959 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009960 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009961 * <p>
9962 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009963 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009964 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009965 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009966 * @param startDelay the delay, in milliseconds, after which the animation
9967 * should start; when the delay is 0, the animation starts
9968 * immediately
9969 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009970 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009971 * @see #scrollBy(int, int)
9972 * @see #scrollTo(int, int)
9973 * @see #isHorizontalScrollBarEnabled()
9974 * @see #isVerticalScrollBarEnabled()
9975 * @see #setHorizontalScrollBarEnabled(boolean)
9976 * @see #setVerticalScrollBarEnabled(boolean)
9977 */
9978 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009979 return awakenScrollBars(startDelay, true);
9980 }
Joe Malin32736f02011-01-19 16:14:20 -08009981
Mike Cleron290947b2009-09-29 18:34:32 -07009982 /**
9983 * <p>
9984 * Trigger the scrollbars to draw. When invoked this method starts an
9985 * animation to fade the scrollbars out after a fixed delay. If a subclass
9986 * provides animated scrolling, the start delay should equal the duration of
9987 * the scrolling animation.
9988 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009989 *
Mike Cleron290947b2009-09-29 18:34:32 -07009990 * <p>
9991 * The animation starts only if at least one of the scrollbars is enabled,
9992 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9993 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9994 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009995 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009996 * is set to true; in that case the caller
9997 * should not call {@link #invalidate()}.
9998 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009999 *
Mike Cleron290947b2009-09-29 18:34:32 -070010000 * <p>
10001 * This method should be invoked everytime a subclass directly updates the
10002 * scroll parameters.
10003 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010004 *
Mike Cleron290947b2009-09-29 18:34:32 -070010005 * @param startDelay the delay, in milliseconds, after which the animation
10006 * should start; when the delay is 0, the animation starts
10007 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010008 *
Mike Cleron290947b2009-09-29 18:34:32 -070010009 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010010 *
Mike Cleron290947b2009-09-29 18:34:32 -070010011 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010012 *
Mike Cleron290947b2009-09-29 18:34:32 -070010013 * @see #scrollBy(int, int)
10014 * @see #scrollTo(int, int)
10015 * @see #isHorizontalScrollBarEnabled()
10016 * @see #isVerticalScrollBarEnabled()
10017 * @see #setHorizontalScrollBarEnabled(boolean)
10018 * @see #setVerticalScrollBarEnabled(boolean)
10019 */
10020 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010021 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010022
Mike Cleronf116bf82009-09-27 19:14:12 -070010023 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10024 return false;
10025 }
10026
10027 if (scrollCache.scrollBar == null) {
10028 scrollCache.scrollBar = new ScrollBarDrawable();
10029 }
10030
10031 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10032
Mike Cleron290947b2009-09-29 18:34:32 -070010033 if (invalidate) {
10034 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010035 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010036 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010037
10038 if (scrollCache.state == ScrollabilityCache.OFF) {
10039 // FIXME: this is copied from WindowManagerService.
10040 // We should get this value from the system when it
10041 // is possible to do so.
10042 final int KEY_REPEAT_FIRST_DELAY = 750;
10043 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10044 }
10045
10046 // Tell mScrollCache when we should start fading. This may
10047 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010048 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010049 scrollCache.fadeStartTime = fadeStartTime;
10050 scrollCache.state = ScrollabilityCache.ON;
10051
10052 // Schedule our fader to run, unscheduling any old ones first
10053 if (mAttachInfo != null) {
10054 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10055 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10056 }
10057
10058 return true;
10059 }
10060
10061 return false;
10062 }
10063
10064 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010065 * Do not invalidate views which are not visible and which are not running an animation. They
10066 * will not get drawn and they should not set dirty flags as if they will be drawn
10067 */
10068 private boolean skipInvalidate() {
10069 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10070 (!(mParent instanceof ViewGroup) ||
10071 !((ViewGroup) mParent).isViewTransitioning(this));
10072 }
10073 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010074 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010075 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10076 * in the future. This must be called from a UI thread. To call from a non-UI
10077 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 *
10079 * WARNING: This method is destructive to dirty.
10080 * @param dirty the rectangle representing the bounds of the dirty region
10081 */
10082 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010083 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010084 return;
10085 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010086 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010087 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10088 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010089 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010090 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010091 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 final ViewParent p = mParent;
10093 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010094 //noinspection PointlessBooleanExpression,ConstantConditions
10095 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10096 if (p != null && ai != null && ai.mHardwareAccelerated) {
10097 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010098 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010099 p.invalidateChild(this, null);
10100 return;
10101 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 if (p != null && ai != null) {
10104 final int scrollX = mScrollX;
10105 final int scrollY = mScrollY;
10106 final Rect r = ai.mTmpInvalRect;
10107 r.set(dirty.left - scrollX, dirty.top - scrollY,
10108 dirty.right - scrollX, dirty.bottom - scrollY);
10109 mParent.invalidateChild(this, r);
10110 }
10111 }
10112 }
10113
10114 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010115 * 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 -080010116 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010117 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10118 * will be called at some point in the future. This must be called from
10119 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010120 * @param l the left position of the dirty region
10121 * @param t the top position of the dirty region
10122 * @param r the right position of the dirty region
10123 * @param b the bottom position of the dirty region
10124 */
10125 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010126 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010127 return;
10128 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010129 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010130 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10131 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010133 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010134 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 final ViewParent p = mParent;
10136 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010137 //noinspection PointlessBooleanExpression,ConstantConditions
10138 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10139 if (p != null && ai != null && ai.mHardwareAccelerated) {
10140 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010141 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010142 p.invalidateChild(this, null);
10143 return;
10144 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 if (p != null && ai != null && l < r && t < b) {
10147 final int scrollX = mScrollX;
10148 final int scrollY = mScrollY;
10149 final Rect tmpr = ai.mTmpInvalRect;
10150 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10151 p.invalidateChild(this, tmpr);
10152 }
10153 }
10154 }
10155
10156 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010157 * Invalidate the whole view. If the view is visible,
10158 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10159 * the future. This must be called from a UI thread. To call from a non-UI thread,
10160 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010161 */
10162 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010163 invalidate(true);
10164 }
Joe Malin32736f02011-01-19 16:14:20 -080010165
Chet Haaseed032702010-10-01 14:05:54 -070010166 /**
10167 * This is where the invalidate() work actually happens. A full invalidate()
10168 * causes the drawing cache to be invalidated, but this function can be called with
10169 * invalidateCache set to false to skip that invalidation step for cases that do not
10170 * need it (for example, a component that remains at the same dimensions with the same
10171 * content).
10172 *
10173 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10174 * well. This is usually true for a full invalidate, but may be set to false if the
10175 * View's contents or dimensions have not changed.
10176 */
Romain Guy849d0a32011-02-01 17:20:48 -080010177 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010178 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010179 return;
10180 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010181 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010182 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010183 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10184 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010185 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010186 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010187 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010188 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010189 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010191 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010192 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010193 //noinspection PointlessBooleanExpression,ConstantConditions
10194 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10195 if (p != null && ai != null && ai.mHardwareAccelerated) {
10196 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010197 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010198 p.invalidateChild(this, null);
10199 return;
10200 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010201 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 if (p != null && ai != null) {
10204 final Rect r = ai.mTmpInvalRect;
10205 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10206 // Don't call invalidate -- we don't want to internally scroll
10207 // our own bounds
10208 p.invalidateChild(this, r);
10209 }
10210 }
10211 }
10212
10213 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010214 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10215 * set any flags or handle all of the cases handled by the default invalidation methods.
10216 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10217 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10218 * walk up the hierarchy, transforming the dirty rect as necessary.
10219 *
10220 * The method also handles normal invalidation logic if display list properties are not
10221 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10222 * backup approach, to handle these cases used in the various property-setting methods.
10223 *
10224 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10225 * are not being used in this view
10226 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10227 * list properties are not being used in this view
10228 */
10229 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010230 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010231 if (invalidateParent) {
10232 invalidateParentCaches();
10233 }
10234 if (forceRedraw) {
10235 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10236 }
10237 invalidate(false);
10238 } else {
10239 final AttachInfo ai = mAttachInfo;
10240 final ViewParent p = mParent;
10241 if (p != null && ai != null) {
10242 final Rect r = ai.mTmpInvalRect;
10243 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10244 if (mParent instanceof ViewGroup) {
10245 ((ViewGroup) mParent).invalidateChildFast(this, r);
10246 } else {
10247 mParent.invalidateChild(this, r);
10248 }
10249 }
10250 }
10251 }
10252
10253 /**
10254 * Utility method to transform a given Rect by the current matrix of this view.
10255 */
10256 void transformRect(final Rect rect) {
10257 if (!getMatrix().isIdentity()) {
10258 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10259 boundingRect.set(rect);
10260 getMatrix().mapRect(boundingRect);
10261 rect.set((int) (boundingRect.left - 0.5f),
10262 (int) (boundingRect.top - 0.5f),
10263 (int) (boundingRect.right + 0.5f),
10264 (int) (boundingRect.bottom + 0.5f));
10265 }
10266 }
10267
10268 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010269 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010270 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10271 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010272 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10273 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010274 *
10275 * @hide
10276 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010277 protected void invalidateParentCaches() {
10278 if (mParent instanceof View) {
10279 ((View) mParent).mPrivateFlags |= INVALIDATED;
10280 }
10281 }
Joe Malin32736f02011-01-19 16:14:20 -080010282
Romain Guy0fd89bf2011-01-26 15:41:30 -080010283 /**
10284 * Used to indicate that the parent of this view should be invalidated. This functionality
10285 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10286 * which is necessary when various parent-managed properties of the view change, such as
10287 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10288 * an invalidation event to the parent.
10289 *
10290 * @hide
10291 */
10292 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010293 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010294 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010295 }
10296 }
10297
10298 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010299 * Indicates whether this View is opaque. An opaque View guarantees that it will
10300 * draw all the pixels overlapping its bounds using a fully opaque color.
10301 *
10302 * Subclasses of View should override this method whenever possible to indicate
10303 * whether an instance is opaque. Opaque Views are treated in a special way by
10304 * the View hierarchy, possibly allowing it to perform optimizations during
10305 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010306 *
Romain Guy24443ea2009-05-11 11:56:30 -070010307 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010308 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010309 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010310 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010311 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010312 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10313 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010314 }
10315
Adam Powell20232d02010-12-08 21:08:53 -080010316 /**
10317 * @hide
10318 */
10319 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010320 // Opaque if:
10321 // - Has a background
10322 // - Background is opaque
10323 // - Doesn't have scrollbars or scrollbars are inside overlay
10324
Philip Milne6c8ea062012-04-03 17:38:43 -070010325 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010326 mPrivateFlags |= OPAQUE_BACKGROUND;
10327 } else {
10328 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10329 }
10330
10331 final int flags = mViewFlags;
10332 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10333 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10334 mPrivateFlags |= OPAQUE_SCROLLBARS;
10335 } else {
10336 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10337 }
10338 }
10339
10340 /**
10341 * @hide
10342 */
10343 protected boolean hasOpaqueScrollbars() {
10344 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010345 }
10346
10347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 * @return A handler associated with the thread running the View. This
10349 * handler can be used to pump events in the UI events queue.
10350 */
10351 public Handler getHandler() {
10352 if (mAttachInfo != null) {
10353 return mAttachInfo.mHandler;
10354 }
10355 return null;
10356 }
10357
10358 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010359 * Gets the view root associated with the View.
10360 * @return The view root, or null if none.
10361 * @hide
10362 */
10363 public ViewRootImpl getViewRootImpl() {
10364 if (mAttachInfo != null) {
10365 return mAttachInfo.mViewRootImpl;
10366 }
10367 return null;
10368 }
10369
10370 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010371 * <p>Causes the Runnable to be added to the message queue.
10372 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010373 *
Romain Guye63a4f32011-08-11 11:33:31 -070010374 * <p>This method can be invoked from outside of the UI thread
10375 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 *
10377 * @param action The Runnable that will be executed.
10378 *
10379 * @return Returns true if the Runnable was successfully placed in to the
10380 * message queue. Returns false on failure, usually because the
10381 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010382 *
10383 * @see #postDelayed
10384 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 */
10386 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010387 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010388 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010389 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010391 // Assume that post will succeed later
10392 ViewRootImpl.getRunQueue().post(action);
10393 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 }
10395
10396 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010397 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010399 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010400 *
Romain Guye63a4f32011-08-11 11:33:31 -070010401 * <p>This method can be invoked from outside of the UI thread
10402 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 *
10404 * @param action The Runnable that will be executed.
10405 * @param delayMillis The delay (in milliseconds) until the Runnable
10406 * will be executed.
10407 *
10408 * @return true if the Runnable was successfully placed in to the
10409 * message queue. Returns false on failure, usually because the
10410 * looper processing the message queue is exiting. Note that a
10411 * result of true does not mean the Runnable will be processed --
10412 * if the looper is quit before the delivery time of the message
10413 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010414 *
10415 * @see #post
10416 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 */
10418 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010419 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010420 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010421 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010423 // Assume that post will succeed later
10424 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10425 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 }
10427
10428 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010429 * <p>Causes the Runnable to execute on the next animation time step.
10430 * The runnable will be run on the user interface thread.</p>
10431 *
10432 * <p>This method can be invoked from outside of the UI thread
10433 * only when this View is attached to a window.</p>
10434 *
10435 * @param action The Runnable that will be executed.
10436 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010437 * @see #postOnAnimationDelayed
10438 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010439 */
10440 public void postOnAnimation(Runnable action) {
10441 final AttachInfo attachInfo = mAttachInfo;
10442 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010443 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10444 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010445 } else {
10446 // Assume that post will succeed later
10447 ViewRootImpl.getRunQueue().post(action);
10448 }
10449 }
10450
10451 /**
10452 * <p>Causes the Runnable to execute on the next animation time step,
10453 * after the specified amount of time elapses.
10454 * The runnable will be run on the user interface thread.</p>
10455 *
10456 * <p>This method can be invoked from outside of the UI thread
10457 * only when this View is attached to a window.</p>
10458 *
10459 * @param action The Runnable that will be executed.
10460 * @param delayMillis The delay (in milliseconds) until the Runnable
10461 * will be executed.
10462 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010463 * @see #postOnAnimation
10464 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010465 */
10466 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10467 final AttachInfo attachInfo = mAttachInfo;
10468 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010469 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10470 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010471 } else {
10472 // Assume that post will succeed later
10473 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10474 }
10475 }
10476
10477 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010478 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010479 *
Romain Guye63a4f32011-08-11 11:33:31 -070010480 * <p>This method can be invoked from outside of the UI thread
10481 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 *
10483 * @param action The Runnable to remove from the message handling queue
10484 *
10485 * @return true if this view could ask the Handler to remove the Runnable,
10486 * false otherwise. When the returned value is true, the Runnable
10487 * may or may not have been actually removed from the message queue
10488 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010489 *
10490 * @see #post
10491 * @see #postDelayed
10492 * @see #postOnAnimation
10493 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 */
10495 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010496 if (action != null) {
10497 final AttachInfo attachInfo = mAttachInfo;
10498 if (attachInfo != null) {
10499 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010500 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10501 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010502 } else {
10503 // Assume that post will succeed later
10504 ViewRootImpl.getRunQueue().removeCallbacks(action);
10505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 return true;
10508 }
10509
10510 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010511 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10512 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 *
Romain Guye63a4f32011-08-11 11:33:31 -070010514 * <p>This method can be invoked from outside of the UI thread
10515 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010516 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010518 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 */
10520 public void postInvalidate() {
10521 postInvalidateDelayed(0);
10522 }
10523
10524 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010525 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10526 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010527 *
Romain Guye63a4f32011-08-11 11:33:31 -070010528 * <p>This method can be invoked from outside of the UI thread
10529 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 *
10531 * @param left The left coordinate of the rectangle to invalidate.
10532 * @param top The top coordinate of the rectangle to invalidate.
10533 * @param right The right coordinate of the rectangle to invalidate.
10534 * @param bottom The bottom coordinate of the rectangle to invalidate.
10535 *
10536 * @see #invalidate(int, int, int, int)
10537 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010538 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 */
10540 public void postInvalidate(int left, int top, int right, int bottom) {
10541 postInvalidateDelayed(0, left, top, right, bottom);
10542 }
10543
10544 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010545 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10546 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010547 *
Romain Guye63a4f32011-08-11 11:33:31 -070010548 * <p>This method can be invoked from outside of the UI thread
10549 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 *
10551 * @param delayMilliseconds the duration in milliseconds to delay the
10552 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010553 *
10554 * @see #invalidate()
10555 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 */
10557 public void postInvalidateDelayed(long delayMilliseconds) {
10558 // We try only with the AttachInfo because there's no point in invalidating
10559 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010560 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010561 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010562 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 }
10564 }
10565
10566 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010567 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10568 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010569 *
Romain Guye63a4f32011-08-11 11:33:31 -070010570 * <p>This method can be invoked from outside of the UI thread
10571 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010572 *
10573 * @param delayMilliseconds the duration in milliseconds to delay the
10574 * invalidation by
10575 * @param left The left coordinate of the rectangle to invalidate.
10576 * @param top The top coordinate of the rectangle to invalidate.
10577 * @param right The right coordinate of the rectangle to invalidate.
10578 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010579 *
10580 * @see #invalidate(int, int, int, int)
10581 * @see #invalidate(Rect)
10582 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 */
10584 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10585 int right, int bottom) {
10586
10587 // We try only with the AttachInfo because there's no point in invalidating
10588 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010590 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010591 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10592 info.target = this;
10593 info.left = left;
10594 info.top = top;
10595 info.right = right;
10596 info.bottom = bottom;
10597
Jeff Browna175a5b2012-02-15 19:18:31 -080010598 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 }
10600 }
10601
10602 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010603 * <p>Cause an invalidate to happen on the next animation time step, typically the
10604 * next display frame.</p>
10605 *
10606 * <p>This method can be invoked from outside of the UI thread
10607 * only when this View is attached to a window.</p>
10608 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010609 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010610 */
10611 public void postInvalidateOnAnimation() {
10612 // We try only with the AttachInfo because there's no point in invalidating
10613 // if we are not attached to our window
10614 final AttachInfo attachInfo = mAttachInfo;
10615 if (attachInfo != null) {
10616 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10617 }
10618 }
10619
10620 /**
10621 * <p>Cause an invalidate of the specified area to happen on the next animation
10622 * time step, typically the next display frame.</p>
10623 *
10624 * <p>This method can be invoked from outside of the UI thread
10625 * only when this View is attached to a window.</p>
10626 *
10627 * @param left The left coordinate of the rectangle to invalidate.
10628 * @param top The top coordinate of the rectangle to invalidate.
10629 * @param right The right coordinate of the rectangle to invalidate.
10630 * @param bottom The bottom coordinate of the rectangle to invalidate.
10631 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010632 * @see #invalidate(int, int, int, int)
10633 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010634 */
10635 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10636 // We try only with the AttachInfo because there's no point in invalidating
10637 // if we are not attached to our window
10638 final AttachInfo attachInfo = mAttachInfo;
10639 if (attachInfo != null) {
10640 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10641 info.target = this;
10642 info.left = left;
10643 info.top = top;
10644 info.right = right;
10645 info.bottom = bottom;
10646
10647 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10648 }
10649 }
10650
10651 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010652 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10653 * This event is sent at most once every
10654 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10655 */
10656 private void postSendViewScrolledAccessibilityEventCallback() {
10657 if (mSendViewScrolledAccessibilityEvent == null) {
10658 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10659 }
10660 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10661 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10662 postDelayed(mSendViewScrolledAccessibilityEvent,
10663 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10664 }
10665 }
10666
10667 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 * Called by a parent to request that a child update its values for mScrollX
10669 * and mScrollY if necessary. This will typically be done if the child is
10670 * animating a scroll using a {@link android.widget.Scroller Scroller}
10671 * object.
10672 */
10673 public void computeScroll() {
10674 }
10675
10676 /**
10677 * <p>Indicate whether the horizontal edges are faded when the view is
10678 * scrolled horizontally.</p>
10679 *
10680 * @return true if the horizontal edges should are faded on scroll, false
10681 * otherwise
10682 *
10683 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010684 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010685 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 */
10687 public boolean isHorizontalFadingEdgeEnabled() {
10688 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10689 }
10690
10691 /**
10692 * <p>Define whether the horizontal edges should be faded when this view
10693 * is scrolled horizontally.</p>
10694 *
10695 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10696 * be faded when the view is scrolled
10697 * horizontally
10698 *
10699 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010700 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010701 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 */
10703 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10704 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10705 if (horizontalFadingEdgeEnabled) {
10706 initScrollCache();
10707 }
10708
10709 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10710 }
10711 }
10712
10713 /**
10714 * <p>Indicate whether the vertical edges are faded when the view is
10715 * scrolled horizontally.</p>
10716 *
10717 * @return true if the vertical edges should are faded on scroll, false
10718 * otherwise
10719 *
10720 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010721 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010722 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 */
10724 public boolean isVerticalFadingEdgeEnabled() {
10725 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10726 }
10727
10728 /**
10729 * <p>Define whether the vertical edges should be faded when this view
10730 * is scrolled vertically.</p>
10731 *
10732 * @param verticalFadingEdgeEnabled true if the vertical edges should
10733 * be faded when the view is scrolled
10734 * vertically
10735 *
10736 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010737 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010738 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010739 */
10740 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10741 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10742 if (verticalFadingEdgeEnabled) {
10743 initScrollCache();
10744 }
10745
10746 mViewFlags ^= FADING_EDGE_VERTICAL;
10747 }
10748 }
10749
10750 /**
10751 * Returns the strength, or intensity, of the top faded edge. The strength is
10752 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10753 * returns 0.0 or 1.0 but no value in between.
10754 *
10755 * Subclasses should override this method to provide a smoother fade transition
10756 * when scrolling occurs.
10757 *
10758 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10759 */
10760 protected float getTopFadingEdgeStrength() {
10761 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10762 }
10763
10764 /**
10765 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10766 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10767 * returns 0.0 or 1.0 but no value in between.
10768 *
10769 * Subclasses should override this method to provide a smoother fade transition
10770 * when scrolling occurs.
10771 *
10772 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10773 */
10774 protected float getBottomFadingEdgeStrength() {
10775 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10776 computeVerticalScrollRange() ? 1.0f : 0.0f;
10777 }
10778
10779 /**
10780 * Returns the strength, or intensity, of the left faded edge. The strength is
10781 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10782 * returns 0.0 or 1.0 but no value in between.
10783 *
10784 * Subclasses should override this method to provide a smoother fade transition
10785 * when scrolling occurs.
10786 *
10787 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10788 */
10789 protected float getLeftFadingEdgeStrength() {
10790 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10791 }
10792
10793 /**
10794 * Returns the strength, or intensity, of the right faded edge. The strength is
10795 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10796 * returns 0.0 or 1.0 but no value in between.
10797 *
10798 * Subclasses should override this method to provide a smoother fade transition
10799 * when scrolling occurs.
10800 *
10801 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10802 */
10803 protected float getRightFadingEdgeStrength() {
10804 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10805 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10806 }
10807
10808 /**
10809 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10810 * scrollbar is not drawn by default.</p>
10811 *
10812 * @return true if the horizontal scrollbar should be painted, false
10813 * otherwise
10814 *
10815 * @see #setHorizontalScrollBarEnabled(boolean)
10816 */
10817 public boolean isHorizontalScrollBarEnabled() {
10818 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10819 }
10820
10821 /**
10822 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10823 * scrollbar is not drawn by default.</p>
10824 *
10825 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10826 * be painted
10827 *
10828 * @see #isHorizontalScrollBarEnabled()
10829 */
10830 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10831 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10832 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010833 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010834 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 }
10836 }
10837
10838 /**
10839 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10840 * scrollbar is not drawn by default.</p>
10841 *
10842 * @return true if the vertical scrollbar should be painted, false
10843 * otherwise
10844 *
10845 * @see #setVerticalScrollBarEnabled(boolean)
10846 */
10847 public boolean isVerticalScrollBarEnabled() {
10848 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10849 }
10850
10851 /**
10852 * <p>Define whether the vertical scrollbar should be drawn or not. The
10853 * scrollbar is not drawn by default.</p>
10854 *
10855 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10856 * be painted
10857 *
10858 * @see #isVerticalScrollBarEnabled()
10859 */
10860 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10861 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10862 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010863 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010864 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010865 }
10866 }
10867
Adam Powell20232d02010-12-08 21:08:53 -080010868 /**
10869 * @hide
10870 */
10871 protected void recomputePadding() {
10872 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 }
Joe Malin32736f02011-01-19 16:14:20 -080010874
Mike Cleronfe81d382009-09-28 14:22:16 -070010875 /**
10876 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010877 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010878 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010879 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010880 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010881 */
10882 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10883 initScrollCache();
10884 final ScrollabilityCache scrollabilityCache = mScrollCache;
10885 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010886 if (fadeScrollbars) {
10887 scrollabilityCache.state = ScrollabilityCache.OFF;
10888 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010889 scrollabilityCache.state = ScrollabilityCache.ON;
10890 }
10891 }
Joe Malin32736f02011-01-19 16:14:20 -080010892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010893 /**
Joe Malin32736f02011-01-19 16:14:20 -080010894 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010895 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010896 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010897 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010898 *
10899 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010900 */
10901 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010902 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010903 }
Joe Malin32736f02011-01-19 16:14:20 -080010904
Mike Cleron52f0a642009-09-28 18:21:37 -070010905 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010906 *
10907 * Returns the delay before scrollbars fade.
10908 *
10909 * @return the delay before scrollbars fade
10910 *
10911 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10912 */
10913 public int getScrollBarDefaultDelayBeforeFade() {
10914 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10915 mScrollCache.scrollBarDefaultDelayBeforeFade;
10916 }
10917
10918 /**
10919 * Define the delay before scrollbars fade.
10920 *
10921 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10922 *
10923 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10924 */
10925 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10926 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10927 }
10928
10929 /**
10930 *
10931 * Returns the scrollbar fade duration.
10932 *
10933 * @return the scrollbar fade duration
10934 *
10935 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10936 */
10937 public int getScrollBarFadeDuration() {
10938 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10939 mScrollCache.scrollBarFadeDuration;
10940 }
10941
10942 /**
10943 * Define the scrollbar fade duration.
10944 *
10945 * @param scrollBarFadeDuration - the scrollbar fade duration
10946 *
10947 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10948 */
10949 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10950 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10951 }
10952
10953 /**
10954 *
10955 * Returns the scrollbar size.
10956 *
10957 * @return the scrollbar size
10958 *
10959 * @attr ref android.R.styleable#View_scrollbarSize
10960 */
10961 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010962 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010963 mScrollCache.scrollBarSize;
10964 }
10965
10966 /**
10967 * Define the scrollbar size.
10968 *
10969 * @param scrollBarSize - the scrollbar size
10970 *
10971 * @attr ref android.R.styleable#View_scrollbarSize
10972 */
10973 public void setScrollBarSize(int scrollBarSize) {
10974 getScrollCache().scrollBarSize = scrollBarSize;
10975 }
10976
10977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010978 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10979 * inset. When inset, they add to the padding of the view. And the scrollbars
10980 * can be drawn inside the padding area or on the edge of the view. For example,
10981 * if a view has a background drawable and you want to draw the scrollbars
10982 * inside the padding specified by the drawable, you can use
10983 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10984 * appear at the edge of the view, ignoring the padding, then you can use
10985 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10986 * @param style the style of the scrollbars. Should be one of
10987 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10988 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10989 * @see #SCROLLBARS_INSIDE_OVERLAY
10990 * @see #SCROLLBARS_INSIDE_INSET
10991 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10992 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010993 *
10994 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 */
10996 public void setScrollBarStyle(int style) {
10997 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10998 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010999 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011000 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 }
11002 }
11003
11004 /**
11005 * <p>Returns the current scrollbar style.</p>
11006 * @return the current scrollbar style
11007 * @see #SCROLLBARS_INSIDE_OVERLAY
11008 * @see #SCROLLBARS_INSIDE_INSET
11009 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11010 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011011 *
11012 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011013 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011014 @ViewDebug.ExportedProperty(mapping = {
11015 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11016 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11017 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11018 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11019 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 public int getScrollBarStyle() {
11021 return mViewFlags & SCROLLBARS_STYLE_MASK;
11022 }
11023
11024 /**
11025 * <p>Compute the horizontal range that the horizontal scrollbar
11026 * represents.</p>
11027 *
11028 * <p>The range is expressed in arbitrary units that must be the same as the
11029 * units used by {@link #computeHorizontalScrollExtent()} and
11030 * {@link #computeHorizontalScrollOffset()}.</p>
11031 *
11032 * <p>The default range is the drawing width of this view.</p>
11033 *
11034 * @return the total horizontal range represented by the horizontal
11035 * scrollbar
11036 *
11037 * @see #computeHorizontalScrollExtent()
11038 * @see #computeHorizontalScrollOffset()
11039 * @see android.widget.ScrollBarDrawable
11040 */
11041 protected int computeHorizontalScrollRange() {
11042 return getWidth();
11043 }
11044
11045 /**
11046 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11047 * within the horizontal range. This value is used to compute the position
11048 * of the thumb within the scrollbar's track.</p>
11049 *
11050 * <p>The range is expressed in arbitrary units that must be the same as the
11051 * units used by {@link #computeHorizontalScrollRange()} and
11052 * {@link #computeHorizontalScrollExtent()}.</p>
11053 *
11054 * <p>The default offset is the scroll offset of this view.</p>
11055 *
11056 * @return the horizontal offset of the scrollbar's thumb
11057 *
11058 * @see #computeHorizontalScrollRange()
11059 * @see #computeHorizontalScrollExtent()
11060 * @see android.widget.ScrollBarDrawable
11061 */
11062 protected int computeHorizontalScrollOffset() {
11063 return mScrollX;
11064 }
11065
11066 /**
11067 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11068 * within the horizontal range. This value is used to compute the length
11069 * of the thumb within the scrollbar's track.</p>
11070 *
11071 * <p>The range is expressed in arbitrary units that must be the same as the
11072 * units used by {@link #computeHorizontalScrollRange()} and
11073 * {@link #computeHorizontalScrollOffset()}.</p>
11074 *
11075 * <p>The default extent is the drawing width of this view.</p>
11076 *
11077 * @return the horizontal extent of the scrollbar's thumb
11078 *
11079 * @see #computeHorizontalScrollRange()
11080 * @see #computeHorizontalScrollOffset()
11081 * @see android.widget.ScrollBarDrawable
11082 */
11083 protected int computeHorizontalScrollExtent() {
11084 return getWidth();
11085 }
11086
11087 /**
11088 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11089 *
11090 * <p>The range is expressed in arbitrary units that must be the same as the
11091 * units used by {@link #computeVerticalScrollExtent()} and
11092 * {@link #computeVerticalScrollOffset()}.</p>
11093 *
11094 * @return the total vertical range represented by the vertical scrollbar
11095 *
11096 * <p>The default range is the drawing height of this view.</p>
11097 *
11098 * @see #computeVerticalScrollExtent()
11099 * @see #computeVerticalScrollOffset()
11100 * @see android.widget.ScrollBarDrawable
11101 */
11102 protected int computeVerticalScrollRange() {
11103 return getHeight();
11104 }
11105
11106 /**
11107 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11108 * within the horizontal range. This value is used to compute the position
11109 * of the thumb within the scrollbar's track.</p>
11110 *
11111 * <p>The range is expressed in arbitrary units that must be the same as the
11112 * units used by {@link #computeVerticalScrollRange()} and
11113 * {@link #computeVerticalScrollExtent()}.</p>
11114 *
11115 * <p>The default offset is the scroll offset of this view.</p>
11116 *
11117 * @return the vertical offset of the scrollbar's thumb
11118 *
11119 * @see #computeVerticalScrollRange()
11120 * @see #computeVerticalScrollExtent()
11121 * @see android.widget.ScrollBarDrawable
11122 */
11123 protected int computeVerticalScrollOffset() {
11124 return mScrollY;
11125 }
11126
11127 /**
11128 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11129 * within the vertical range. This value is used to compute the length
11130 * of the thumb within the scrollbar's track.</p>
11131 *
11132 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011133 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 * {@link #computeVerticalScrollOffset()}.</p>
11135 *
11136 * <p>The default extent is the drawing height of this view.</p>
11137 *
11138 * @return the vertical extent of the scrollbar's thumb
11139 *
11140 * @see #computeVerticalScrollRange()
11141 * @see #computeVerticalScrollOffset()
11142 * @see android.widget.ScrollBarDrawable
11143 */
11144 protected int computeVerticalScrollExtent() {
11145 return getHeight();
11146 }
11147
11148 /**
Adam Powell69159442011-06-13 17:53:06 -070011149 * Check if this view can be scrolled horizontally in a certain direction.
11150 *
11151 * @param direction Negative to check scrolling left, positive to check scrolling right.
11152 * @return true if this view can be scrolled in the specified direction, false otherwise.
11153 */
11154 public boolean canScrollHorizontally(int direction) {
11155 final int offset = computeHorizontalScrollOffset();
11156 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11157 if (range == 0) return false;
11158 if (direction < 0) {
11159 return offset > 0;
11160 } else {
11161 return offset < range - 1;
11162 }
11163 }
11164
11165 /**
11166 * Check if this view can be scrolled vertically in a certain direction.
11167 *
11168 * @param direction Negative to check scrolling up, positive to check scrolling down.
11169 * @return true if this view can be scrolled in the specified direction, false otherwise.
11170 */
11171 public boolean canScrollVertically(int direction) {
11172 final int offset = computeVerticalScrollOffset();
11173 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11174 if (range == 0) return false;
11175 if (direction < 0) {
11176 return offset > 0;
11177 } else {
11178 return offset < range - 1;
11179 }
11180 }
11181
11182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11184 * scrollbars are painted only if they have been awakened first.</p>
11185 *
11186 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011187 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011188 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011190 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011191 // scrollbars are drawn only when the animation is running
11192 final ScrollabilityCache cache = mScrollCache;
11193 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011194
Mike Cleronf116bf82009-09-27 19:14:12 -070011195 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011196
Mike Cleronf116bf82009-09-27 19:14:12 -070011197 if (state == ScrollabilityCache.OFF) {
11198 return;
11199 }
Joe Malin32736f02011-01-19 16:14:20 -080011200
Mike Cleronf116bf82009-09-27 19:14:12 -070011201 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011202
Mike Cleronf116bf82009-09-27 19:14:12 -070011203 if (state == ScrollabilityCache.FADING) {
11204 // We're fading -- get our fade interpolation
11205 if (cache.interpolatorValues == null) {
11206 cache.interpolatorValues = new float[1];
11207 }
Joe Malin32736f02011-01-19 16:14:20 -080011208
Mike Cleronf116bf82009-09-27 19:14:12 -070011209 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011210
Mike Cleronf116bf82009-09-27 19:14:12 -070011211 // Stops the animation if we're done
11212 if (cache.scrollBarInterpolator.timeToValues(values) ==
11213 Interpolator.Result.FREEZE_END) {
11214 cache.state = ScrollabilityCache.OFF;
11215 } else {
11216 cache.scrollBar.setAlpha(Math.round(values[0]));
11217 }
Joe Malin32736f02011-01-19 16:14:20 -080011218
11219 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011220 // drawing. We only want this when we're fading so that
11221 // we prevent excessive redraws
11222 invalidate = true;
11223 } else {
11224 // We're just on -- but we may have been fading before so
11225 // reset alpha
11226 cache.scrollBar.setAlpha(255);
11227 }
11228
Joe Malin32736f02011-01-19 16:14:20 -080011229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230 final int viewFlags = mViewFlags;
11231
11232 final boolean drawHorizontalScrollBar =
11233 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11234 final boolean drawVerticalScrollBar =
11235 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11236 && !isVerticalScrollBarHidden();
11237
11238 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11239 final int width = mRight - mLeft;
11240 final int height = mBottom - mTop;
11241
11242 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243
Mike Reede8853fc2009-09-04 14:01:48 -040011244 final int scrollX = mScrollX;
11245 final int scrollY = mScrollY;
11246 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11247
Mike Cleronf116bf82009-09-27 19:14:12 -070011248 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011250 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011251 int size = scrollBar.getSize(false);
11252 if (size <= 0) {
11253 size = cache.scrollBarSize;
11254 }
11255
Mike Cleronf116bf82009-09-27 19:14:12 -070011256 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011257 computeHorizontalScrollOffset(),
11258 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011259 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011260 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011261 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011262 left = scrollX + (mPaddingLeft & inside);
11263 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11264 bottom = top + size;
11265 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11266 if (invalidate) {
11267 invalidate(left, top, right, bottom);
11268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 }
11270
11271 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011272 int size = scrollBar.getSize(true);
11273 if (size <= 0) {
11274 size = cache.scrollBarSize;
11275 }
11276
Mike Reede8853fc2009-09-04 14:01:48 -040011277 scrollBar.setParameters(computeVerticalScrollRange(),
11278 computeVerticalScrollOffset(),
11279 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011280 switch (mVerticalScrollbarPosition) {
11281 default:
11282 case SCROLLBAR_POSITION_DEFAULT:
11283 case SCROLLBAR_POSITION_RIGHT:
11284 left = scrollX + width - size - (mUserPaddingRight & inside);
11285 break;
11286 case SCROLLBAR_POSITION_LEFT:
11287 left = scrollX + (mUserPaddingLeft & inside);
11288 break;
11289 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011290 top = scrollY + (mPaddingTop & inside);
11291 right = left + size;
11292 bottom = scrollY + height - (mUserPaddingBottom & inside);
11293 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11294 if (invalidate) {
11295 invalidate(left, top, right, bottom);
11296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 }
11298 }
11299 }
11300 }
Romain Guy8506ab42009-06-11 17:35:47 -070011301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011302 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011303 * 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 -080011304 * FastScroller is visible.
11305 * @return whether to temporarily hide the vertical scrollbar
11306 * @hide
11307 */
11308 protected boolean isVerticalScrollBarHidden() {
11309 return false;
11310 }
11311
11312 /**
11313 * <p>Draw the horizontal scrollbar if
11314 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 * @param canvas the canvas on which to draw the scrollbar
11317 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011318 *
11319 * @see #isHorizontalScrollBarEnabled()
11320 * @see #computeHorizontalScrollRange()
11321 * @see #computeHorizontalScrollExtent()
11322 * @see #computeHorizontalScrollOffset()
11323 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011324 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011325 */
Romain Guy8fb95422010-08-17 18:38:51 -070011326 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11327 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011328 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011329 scrollBar.draw(canvas);
11330 }
Mike Reede8853fc2009-09-04 14:01:48 -040011331
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011332 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11334 * returns true.</p>
11335 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011336 * @param canvas the canvas on which to draw the scrollbar
11337 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011338 *
11339 * @see #isVerticalScrollBarEnabled()
11340 * @see #computeVerticalScrollRange()
11341 * @see #computeVerticalScrollExtent()
11342 * @see #computeVerticalScrollOffset()
11343 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011344 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 */
Romain Guy8fb95422010-08-17 18:38:51 -070011346 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11347 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011348 scrollBar.setBounds(l, t, r, b);
11349 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011350 }
11351
11352 /**
11353 * Implement this to do your drawing.
11354 *
11355 * @param canvas the canvas on which the background will be drawn
11356 */
11357 protected void onDraw(Canvas canvas) {
11358 }
11359
11360 /*
11361 * Caller is responsible for calling requestLayout if necessary.
11362 * (This allows addViewInLayout to not request a new layout.)
11363 */
11364 void assignParent(ViewParent parent) {
11365 if (mParent == null) {
11366 mParent = parent;
11367 } else if (parent == null) {
11368 mParent = null;
11369 } else {
11370 throw new RuntimeException("view " + this + " being added, but"
11371 + " it already has a parent");
11372 }
11373 }
11374
11375 /**
11376 * This is called when the view is attached to a window. At this point it
11377 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011378 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11379 * however it may be called any time before the first onDraw -- including
11380 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 *
11382 * @see #onDetachedFromWindow()
11383 */
11384 protected void onAttachedToWindow() {
11385 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11386 mParent.requestTransparentRegion(this);
11387 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011388
Adam Powell8568c3a2010-04-19 14:26:11 -070011389 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11390 initialAwakenScrollBars();
11391 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11392 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011393
Chet Haasea9b61ac2010-12-20 07:40:25 -080011394 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011395
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011396 // Order is important here: LayoutDirection MUST be resolved before Padding
11397 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011398 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011399 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011400 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011401 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011402
Svetoslav Ganov42138042012-03-20 11:51:39 -070011403 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011404 if (isFocused()) {
11405 InputMethodManager imm = InputMethodManager.peekInstance();
11406 imm.focusIn(this);
11407 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011408
11409 if (mAttachInfo != null && mDisplayList != null) {
11410 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11411 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011412 }
Cibu Johny86666632010-02-22 13:01:02 -080011413
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011414 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011415 * @see #onScreenStateChanged(int)
11416 */
11417 void dispatchScreenStateChanged(int screenState) {
11418 onScreenStateChanged(screenState);
11419 }
11420
11421 /**
11422 * This method is called whenever the state of the screen this view is
11423 * attached to changes. A state change will usually occurs when the screen
11424 * turns on or off (whether it happens automatically or the user does it
11425 * manually.)
11426 *
11427 * @param screenState The new state of the screen. Can be either
11428 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11429 */
11430 public void onScreenStateChanged(int screenState) {
11431 }
11432
11433 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011434 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11435 */
11436 private boolean hasRtlSupport() {
11437 return mContext.getApplicationInfo().hasRtlSupport();
11438 }
11439
11440 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011441 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11442 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011443 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011444 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011445 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011446 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011447 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011448 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011449
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011450 if (hasRtlSupport()) {
11451 // Set resolved depending on layout direction
11452 switch (getLayoutDirection()) {
11453 case LAYOUT_DIRECTION_INHERIT:
11454 // If this is root view, no need to look at parent's layout dir.
11455 if (canResolveLayoutDirection()) {
11456 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011457
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011458 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11459 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11460 }
11461 } else {
11462 // Nothing to do, LTR by default
11463 }
11464 break;
11465 case LAYOUT_DIRECTION_RTL:
11466 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11467 break;
11468 case LAYOUT_DIRECTION_LOCALE:
11469 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011470 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11471 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011472 break;
11473 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011474 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011475 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011476 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011477
11478 // Set to resolved
11479 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011480 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011481 // Resolve padding
11482 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011483 }
11484
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011485 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011486 * Called when layout direction has been resolved.
11487 *
11488 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011489 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011490 */
11491 public void onResolvedLayoutDirectionChanged() {
11492 }
11493
11494 /**
11495 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011496 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011497 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011498 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011499 // If the user specified the absolute padding (either with android:padding or
11500 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11501 // use the default padding or the padding from the background drawable
11502 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011503 int resolvedLayoutDirection = getResolvedLayoutDirection();
11504 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011505 case LAYOUT_DIRECTION_RTL:
11506 // Start user padding override Right user padding. Otherwise, if Right user
11507 // padding is not defined, use the default Right padding. If Right user padding
11508 // is defined, just use it.
11509 if (mUserPaddingStart >= 0) {
11510 mUserPaddingRight = mUserPaddingStart;
11511 } else if (mUserPaddingRight < 0) {
11512 mUserPaddingRight = mPaddingRight;
11513 }
11514 if (mUserPaddingEnd >= 0) {
11515 mUserPaddingLeft = mUserPaddingEnd;
11516 } else if (mUserPaddingLeft < 0) {
11517 mUserPaddingLeft = mPaddingLeft;
11518 }
11519 break;
11520 case LAYOUT_DIRECTION_LTR:
11521 default:
11522 // Start user padding override Left user padding. Otherwise, if Left user
11523 // padding is not defined, use the default left padding. If Left user padding
11524 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011525 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011526 mUserPaddingLeft = mUserPaddingStart;
11527 } else if (mUserPaddingLeft < 0) {
11528 mUserPaddingLeft = mPaddingLeft;
11529 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011530 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011531 mUserPaddingRight = mUserPaddingEnd;
11532 } else if (mUserPaddingRight < 0) {
11533 mUserPaddingRight = mPaddingRight;
11534 }
11535 }
11536
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011537 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11538
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011539 if(isPaddingRelative()) {
11540 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11541 } else {
11542 recomputePadding();
11543 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011544 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011545 }
11546
11547 /**
11548 * Resolve padding depending on the layout direction. Subclasses that care about
11549 * padding resolution should override this method. The default implementation does
11550 * nothing.
11551 *
11552 * @param layoutDirection the direction of the layout
11553 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011554 * @see {@link #LAYOUT_DIRECTION_LTR}
11555 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011556 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011557 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011558 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011559 }
11560
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011561 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011562 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011563 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011564 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011565 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011566 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011567 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011568 switch (getLayoutDirection()) {
11569 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011570 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011571 default:
11572 return true;
11573 }
11574 }
11575
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011576 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011577 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11578 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011579 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011580 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011581 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011582 // Reset the current resolved bits
11583 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011584 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011585 // Reset also the text direction
11586 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011587 }
11588
11589 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011590 * Called during reset of resolved layout direction.
11591 *
11592 * Subclasses need to override this method to clear cached information that depends on the
11593 * resolved layout direction, or to inform child views that inherit their layout direction.
11594 *
11595 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011596 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011597 */
11598 public void onResolvedLayoutDirectionReset() {
11599 }
11600
11601 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011602 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011603 *
11604 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011605 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011606 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011607 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011608 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011609 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 }
11611
11612 /**
11613 * This is called when the view is detached from a window. At this point it
11614 * no longer has a surface for drawing.
11615 *
11616 * @see #onAttachedToWindow()
11617 */
11618 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011619 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011620
Romain Guya440b002010-02-24 15:57:54 -080011621 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011622 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011623 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011624 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011626 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011627
Romain Guya998dff2012-03-23 18:58:36 -070011628 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011629
11630 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011631 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011632 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011633 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011634 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011635 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011636 // Should never happen
11637 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011638 }
11639
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011640 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011641
11642 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011643 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011644 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645 }
11646
11647 /**
11648 * @return The number of times this view has been attached to a window
11649 */
11650 protected int getWindowAttachCount() {
11651 return mWindowAttachCount;
11652 }
11653
11654 /**
11655 * Retrieve a unique token identifying the window this view is attached to.
11656 * @return Return the window's token for use in
11657 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11658 */
11659 public IBinder getWindowToken() {
11660 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11661 }
11662
11663 /**
11664 * Retrieve a unique token identifying the top-level "real" window of
11665 * the window that this view is attached to. That is, this is like
11666 * {@link #getWindowToken}, except if the window this view in is a panel
11667 * window (attached to another containing window), then the token of
11668 * the containing window is returned instead.
11669 *
11670 * @return Returns the associated window token, either
11671 * {@link #getWindowToken()} or the containing window's token.
11672 */
11673 public IBinder getApplicationWindowToken() {
11674 AttachInfo ai = mAttachInfo;
11675 if (ai != null) {
11676 IBinder appWindowToken = ai.mPanelParentWindowToken;
11677 if (appWindowToken == null) {
11678 appWindowToken = ai.mWindowToken;
11679 }
11680 return appWindowToken;
11681 }
11682 return null;
11683 }
11684
11685 /**
11686 * Retrieve private session object this view hierarchy is using to
11687 * communicate with the window manager.
11688 * @return the session object to communicate with the window manager
11689 */
11690 /*package*/ IWindowSession getWindowSession() {
11691 return mAttachInfo != null ? mAttachInfo.mSession : null;
11692 }
11693
11694 /**
11695 * @param info the {@link android.view.View.AttachInfo} to associated with
11696 * this view
11697 */
11698 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11699 //System.out.println("Attached! " + this);
11700 mAttachInfo = info;
11701 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011702 // We will need to evaluate the drawable state at least once.
11703 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 if (mFloatingTreeObserver != null) {
11705 info.mTreeObserver.merge(mFloatingTreeObserver);
11706 mFloatingTreeObserver = null;
11707 }
11708 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11709 mAttachInfo.mScrollContainers.add(this);
11710 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11711 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011712 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011713 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011714
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011715 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011716 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011717 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011718 if (listeners != null && listeners.size() > 0) {
11719 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11720 // perform the dispatching. The iterator is a safe guard against listeners that
11721 // could mutate the list by calling the various add/remove methods. This prevents
11722 // the array from being modified while we iterate it.
11723 for (OnAttachStateChangeListener listener : listeners) {
11724 listener.onViewAttachedToWindow(this);
11725 }
11726 }
11727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 int vis = info.mWindowVisibility;
11729 if (vis != GONE) {
11730 onWindowVisibilityChanged(vis);
11731 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011732 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11733 // If nobody has evaluated the drawable state yet, then do it now.
11734 refreshDrawableState();
11735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011736 }
11737
11738 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011739 AttachInfo info = mAttachInfo;
11740 if (info != null) {
11741 int vis = info.mWindowVisibility;
11742 if (vis != GONE) {
11743 onWindowVisibilityChanged(GONE);
11744 }
11745 }
11746
11747 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011748
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011749 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011750 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011751 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011752 if (listeners != null && listeners.size() > 0) {
11753 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11754 // perform the dispatching. The iterator is a safe guard against listeners that
11755 // could mutate the list by calling the various add/remove methods. This prevents
11756 // the array from being modified while we iterate it.
11757 for (OnAttachStateChangeListener listener : listeners) {
11758 listener.onViewDetachedFromWindow(this);
11759 }
11760 }
11761
Romain Guy01d5edc2011-01-28 11:28:53 -080011762 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011763 mAttachInfo.mScrollContainers.remove(this);
11764 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11765 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 mAttachInfo = null;
11768 }
11769
11770 /**
11771 * Store this view hierarchy's frozen state into the given container.
11772 *
11773 * @param container The SparseArray in which to save the view's state.
11774 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011775 * @see #restoreHierarchyState(android.util.SparseArray)
11776 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11777 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 */
11779 public void saveHierarchyState(SparseArray<Parcelable> container) {
11780 dispatchSaveInstanceState(container);
11781 }
11782
11783 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011784 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11785 * this view and its children. May be overridden to modify how freezing happens to a
11786 * 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 -080011787 *
11788 * @param container The SparseArray in which to save the view's state.
11789 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011790 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11791 * @see #saveHierarchyState(android.util.SparseArray)
11792 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011793 */
11794 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11795 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11796 mPrivateFlags &= ~SAVE_STATE_CALLED;
11797 Parcelable state = onSaveInstanceState();
11798 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11799 throw new IllegalStateException(
11800 "Derived class did not call super.onSaveInstanceState()");
11801 }
11802 if (state != null) {
11803 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11804 // + ": " + state);
11805 container.put(mID, state);
11806 }
11807 }
11808 }
11809
11810 /**
11811 * Hook allowing a view to generate a representation of its internal state
11812 * that can later be used to create a new instance with that same state.
11813 * This state should only contain information that is not persistent or can
11814 * not be reconstructed later. For example, you will never store your
11815 * current position on screen because that will be computed again when a
11816 * new instance of the view is placed in its view hierarchy.
11817 * <p>
11818 * Some examples of things you may store here: the current cursor position
11819 * in a text view (but usually not the text itself since that is stored in a
11820 * content provider or other persistent storage), the currently selected
11821 * item in a list view.
11822 *
11823 * @return Returns a Parcelable object containing the view's current dynamic
11824 * state, or null if there is nothing interesting to save. The
11825 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011826 * @see #onRestoreInstanceState(android.os.Parcelable)
11827 * @see #saveHierarchyState(android.util.SparseArray)
11828 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011829 * @see #setSaveEnabled(boolean)
11830 */
11831 protected Parcelable onSaveInstanceState() {
11832 mPrivateFlags |= SAVE_STATE_CALLED;
11833 return BaseSavedState.EMPTY_STATE;
11834 }
11835
11836 /**
11837 * Restore this view hierarchy's frozen state from the given container.
11838 *
11839 * @param container The SparseArray which holds previously frozen states.
11840 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011841 * @see #saveHierarchyState(android.util.SparseArray)
11842 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11843 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011844 */
11845 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11846 dispatchRestoreInstanceState(container);
11847 }
11848
11849 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011850 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11851 * state for this view and its children. May be overridden to modify how restoring
11852 * happens to a view's children; for example, some views may want to not store state
11853 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 *
11855 * @param container The SparseArray which holds previously saved state.
11856 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011857 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11858 * @see #restoreHierarchyState(android.util.SparseArray)
11859 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 */
11861 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11862 if (mID != NO_ID) {
11863 Parcelable state = container.get(mID);
11864 if (state != null) {
11865 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11866 // + ": " + state);
11867 mPrivateFlags &= ~SAVE_STATE_CALLED;
11868 onRestoreInstanceState(state);
11869 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11870 throw new IllegalStateException(
11871 "Derived class did not call super.onRestoreInstanceState()");
11872 }
11873 }
11874 }
11875 }
11876
11877 /**
11878 * Hook allowing a view to re-apply a representation of its internal state that had previously
11879 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11880 * null state.
11881 *
11882 * @param state The frozen state that had previously been returned by
11883 * {@link #onSaveInstanceState}.
11884 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011885 * @see #onSaveInstanceState()
11886 * @see #restoreHierarchyState(android.util.SparseArray)
11887 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011888 */
11889 protected void onRestoreInstanceState(Parcelable state) {
11890 mPrivateFlags |= SAVE_STATE_CALLED;
11891 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011892 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11893 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011894 + "when two views of different type have the same id in the same hierarchy. "
11895 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011896 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897 }
11898 }
11899
11900 /**
11901 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11902 *
11903 * @return the drawing start time in milliseconds
11904 */
11905 public long getDrawingTime() {
11906 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11907 }
11908
11909 /**
11910 * <p>Enables or disables the duplication of the parent's state into this view. When
11911 * duplication is enabled, this view gets its drawable state from its parent rather
11912 * than from its own internal properties.</p>
11913 *
11914 * <p>Note: in the current implementation, setting this property to true after the
11915 * view was added to a ViewGroup might have no effect at all. This property should
11916 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11917 *
11918 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11919 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011920 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011921 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11922 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011923 *
11924 * @param enabled True to enable duplication of the parent's drawable state, false
11925 * to disable it.
11926 *
11927 * @see #getDrawableState()
11928 * @see #isDuplicateParentStateEnabled()
11929 */
11930 public void setDuplicateParentStateEnabled(boolean enabled) {
11931 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11932 }
11933
11934 /**
11935 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11936 *
11937 * @return True if this view's drawable state is duplicated from the parent,
11938 * false otherwise
11939 *
11940 * @see #getDrawableState()
11941 * @see #setDuplicateParentStateEnabled(boolean)
11942 */
11943 public boolean isDuplicateParentStateEnabled() {
11944 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11945 }
11946
11947 /**
Romain Guy171c5922011-01-06 10:04:23 -080011948 * <p>Specifies the type of layer backing this view. The layer can be
11949 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11950 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011951 *
Romain Guy171c5922011-01-06 10:04:23 -080011952 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11953 * instance that controls how the layer is composed on screen. The following
11954 * properties of the paint are taken into account when composing the layer:</p>
11955 * <ul>
11956 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11957 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11958 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11959 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011960 *
Romain Guy171c5922011-01-06 10:04:23 -080011961 * <p>If this view has an alpha value set to < 1.0 by calling
11962 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11963 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11964 * equivalent to setting a hardware layer on this view and providing a paint with
11965 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011966 *
Romain Guy171c5922011-01-06 10:04:23 -080011967 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11968 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11969 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011970 *
Romain Guy171c5922011-01-06 10:04:23 -080011971 * @param layerType The ype of layer to use with this view, must be one of
11972 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11973 * {@link #LAYER_TYPE_HARDWARE}
11974 * @param paint The paint used to compose the layer. This argument is optional
11975 * and can be null. It is ignored when the layer type is
11976 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011977 *
11978 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011979 * @see #LAYER_TYPE_NONE
11980 * @see #LAYER_TYPE_SOFTWARE
11981 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011982 * @see #setAlpha(float)
11983 *
Romain Guy171c5922011-01-06 10:04:23 -080011984 * @attr ref android.R.styleable#View_layerType
11985 */
11986 public void setLayerType(int layerType, Paint paint) {
11987 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011988 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011989 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11990 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011991
Romain Guyd6cd5722011-01-17 14:42:41 -080011992 if (layerType == mLayerType) {
11993 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11994 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011995 invalidateParentCaches();
11996 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011997 }
11998 return;
11999 }
Romain Guy171c5922011-01-06 10:04:23 -080012000
12001 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012002 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012003 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012004 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012005 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012006 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012007 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012008 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012009 default:
12010 break;
Romain Guy171c5922011-01-06 10:04:23 -080012011 }
12012
12013 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012014 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12015 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12016 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012017
Romain Guy0fd89bf2011-01-26 15:41:30 -080012018 invalidateParentCaches();
12019 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012020 }
12021
12022 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012023 * Indicates whether this view has a static layer. A view with layer type
12024 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12025 * dynamic.
12026 */
12027 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012028 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012029 }
12030
12031 /**
Romain Guy171c5922011-01-06 10:04:23 -080012032 * Indicates what type of layer is currently associated with this view. By default
12033 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12034 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12035 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012036 *
Romain Guy171c5922011-01-06 10:04:23 -080012037 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12038 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012039 *
12040 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012041 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012042 * @see #LAYER_TYPE_NONE
12043 * @see #LAYER_TYPE_SOFTWARE
12044 * @see #LAYER_TYPE_HARDWARE
12045 */
12046 public int getLayerType() {
12047 return mLayerType;
12048 }
Joe Malin32736f02011-01-19 16:14:20 -080012049
Romain Guy6c319ca2011-01-11 14:29:25 -080012050 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012051 * Forces this view's layer to be created and this view to be rendered
12052 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12053 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012054 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012055 * This method can for instance be used to render a view into its layer before
12056 * starting an animation. If this view is complex, rendering into the layer
12057 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012058 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012059 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012060 *
12061 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012062 */
12063 public void buildLayer() {
12064 if (mLayerType == LAYER_TYPE_NONE) return;
12065
12066 if (mAttachInfo == null) {
12067 throw new IllegalStateException("This view must be attached to a window first");
12068 }
12069
12070 switch (mLayerType) {
12071 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012072 if (mAttachInfo.mHardwareRenderer != null &&
12073 mAttachInfo.mHardwareRenderer.isEnabled() &&
12074 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012075 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012076 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012077 break;
12078 case LAYER_TYPE_SOFTWARE:
12079 buildDrawingCache(true);
12080 break;
12081 }
12082 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012083
Romain Guy9c4b79a2011-11-10 19:23:58 -080012084 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12085 void flushLayer() {
12086 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12087 mHardwareLayer.flush();
12088 }
12089 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012090
12091 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012092 * <p>Returns a hardware layer that can be used to draw this view again
12093 * without executing its draw method.</p>
12094 *
12095 * @return A HardwareLayer ready to render, or null if an error occurred.
12096 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012097 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012098 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12099 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012100 return null;
12101 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012102
Romain Guy9c4b79a2011-11-10 19:23:58 -080012103 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012104
12105 final int width = mRight - mLeft;
12106 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012107
Romain Guy6c319ca2011-01-11 14:29:25 -080012108 if (width == 0 || height == 0) {
12109 return null;
12110 }
12111
12112 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12113 if (mHardwareLayer == null) {
12114 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12115 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012116 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012117 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12118 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012119 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012120 }
12121
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012122 // The layer is not valid if the underlying GPU resources cannot be allocated
12123 if (!mHardwareLayer.isValid()) {
12124 return null;
12125 }
12126
Chet Haasea1cff502012-02-21 13:43:44 -080012127 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012128 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012129 }
12130
12131 return mHardwareLayer;
12132 }
Romain Guy171c5922011-01-06 10:04:23 -080012133
Romain Guy589b0bb2011-10-10 13:57:47 -070012134 /**
12135 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012136 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012137 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012138 *
12139 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012140 * @see #LAYER_TYPE_HARDWARE
12141 */
Romain Guya998dff2012-03-23 18:58:36 -070012142 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012143 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012144 AttachInfo info = mAttachInfo;
12145 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012146 info.mHardwareRenderer.isEnabled() &&
12147 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012148 mHardwareLayer.destroy();
12149 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012150
Romain Guy9c4b79a2011-11-10 19:23:58 -080012151 invalidate(true);
12152 invalidateParentCaches();
12153 }
Romain Guy65b345f2011-07-27 18:51:50 -070012154 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012155 }
Romain Guy65b345f2011-07-27 18:51:50 -070012156 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012157 }
12158
Romain Guy171c5922011-01-06 10:04:23 -080012159 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012160 * Destroys all hardware rendering resources. This method is invoked
12161 * when the system needs to reclaim resources. Upon execution of this
12162 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012163 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012164 * Note: you <strong>must</strong> call
12165 * <code>super.destroyHardwareResources()</code> when overriding
12166 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012167 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012168 * @hide
12169 */
12170 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012171 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012172 }
12173
12174 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012175 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12176 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12177 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12178 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12179 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12180 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012181 *
Romain Guy171c5922011-01-06 10:04:23 -080012182 * <p>Enabling the drawing cache is similar to
12183 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012184 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12185 * drawing cache has no effect on rendering because the system uses a different mechanism
12186 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12187 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12188 * for information on how to enable software and hardware layers.</p>
12189 *
12190 * <p>This API can be used to manually generate
12191 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12192 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 *
12194 * @param enabled true to enable the drawing cache, false otherwise
12195 *
12196 * @see #isDrawingCacheEnabled()
12197 * @see #getDrawingCache()
12198 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012199 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 */
12201 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012202 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012203 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12204 }
12205
12206 /**
12207 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12208 *
12209 * @return true if the drawing cache is enabled
12210 *
12211 * @see #setDrawingCacheEnabled(boolean)
12212 * @see #getDrawingCache()
12213 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012214 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 public boolean isDrawingCacheEnabled() {
12216 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12217 }
12218
12219 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012220 * Debugging utility which recursively outputs the dirty state of a view and its
12221 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012222 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012223 * @hide
12224 */
Romain Guy676b1732011-02-14 14:45:33 -080012225 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012226 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12227 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12228 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12229 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12230 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12231 if (clear) {
12232 mPrivateFlags &= clearMask;
12233 }
12234 if (this instanceof ViewGroup) {
12235 ViewGroup parent = (ViewGroup) this;
12236 final int count = parent.getChildCount();
12237 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012238 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012239 child.outputDirtyFlags(indent + " ", clear, clearMask);
12240 }
12241 }
12242 }
12243
12244 /**
12245 * This method is used by ViewGroup to cause its children to restore or recreate their
12246 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12247 * to recreate its own display list, which would happen if it went through the normal
12248 * draw/dispatchDraw mechanisms.
12249 *
12250 * @hide
12251 */
12252 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012253
12254 /**
12255 * A view that is not attached or hardware accelerated cannot create a display list.
12256 * This method checks these conditions and returns the appropriate result.
12257 *
12258 * @return true if view has the ability to create a display list, false otherwise.
12259 *
12260 * @hide
12261 */
12262 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012263 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012264 }
Joe Malin32736f02011-01-19 16:14:20 -080012265
Chet Haasedaf98e92011-01-10 14:10:36 -080012266 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012267 * @return The HardwareRenderer associated with that view or null if hardware rendering
12268 * is not supported or this this has not been attached to a window.
12269 *
12270 * @hide
12271 */
12272 public HardwareRenderer getHardwareRenderer() {
12273 if (mAttachInfo != null) {
12274 return mAttachInfo.mHardwareRenderer;
12275 }
12276 return null;
12277 }
12278
12279 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012280 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12281 * Otherwise, the same display list will be returned (after having been rendered into
12282 * along the way, depending on the invalidation state of the view).
12283 *
12284 * @param displayList The previous version of this displayList, could be null.
12285 * @param isLayer Whether the requester of the display list is a layer. If so,
12286 * the view will avoid creating a layer inside the resulting display list.
12287 * @return A new or reused DisplayList object.
12288 */
12289 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12290 if (!canHaveDisplayList()) {
12291 return null;
12292 }
12293
12294 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12295 displayList == null || !displayList.isValid() ||
12296 (!isLayer && mRecreateDisplayList))) {
12297 // Don't need to recreate the display list, just need to tell our
12298 // children to restore/recreate theirs
12299 if (displayList != null && displayList.isValid() &&
12300 !isLayer && !mRecreateDisplayList) {
12301 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12302 mPrivateFlags &= ~DIRTY_MASK;
12303 dispatchGetDisplayList();
12304
12305 return displayList;
12306 }
12307
12308 if (!isLayer) {
12309 // If we got here, we're recreating it. Mark it as such to ensure that
12310 // we copy in child display lists into ours in drawChild()
12311 mRecreateDisplayList = true;
12312 }
12313 if (displayList == null) {
12314 final String name = getClass().getSimpleName();
12315 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12316 // If we're creating a new display list, make sure our parent gets invalidated
12317 // since they will need to recreate their display list to account for this
12318 // new child display list.
12319 invalidateParentCaches();
12320 }
12321
12322 boolean caching = false;
12323 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012324 int width = mRight - mLeft;
12325 int height = mBottom - mTop;
12326
12327 try {
12328 canvas.setViewport(width, height);
12329 // The dirty rect should always be null for a display list
12330 canvas.onPreDraw(null);
12331 int layerType = (
12332 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
12333 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070012334 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012335 if (layerType == LAYER_TYPE_HARDWARE) {
12336 final HardwareLayer layer = getHardwareLayer();
12337 if (layer != null && layer.isValid()) {
12338 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12339 } else {
12340 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12341 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12342 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12343 }
12344 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012345 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012346 buildDrawingCache(true);
12347 Bitmap cache = getDrawingCache(true);
12348 if (cache != null) {
12349 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12350 caching = true;
12351 }
Chet Haasea1cff502012-02-21 13:43:44 -080012352 }
Chet Haasea1cff502012-02-21 13:43:44 -080012353 } else {
12354
12355 computeScroll();
12356
Chet Haasea1cff502012-02-21 13:43:44 -080012357 canvas.translate(-mScrollX, -mScrollY);
12358 if (!isLayer) {
12359 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12360 mPrivateFlags &= ~DIRTY_MASK;
12361 }
12362
12363 // Fast path for layouts with no backgrounds
12364 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12365 dispatchDraw(canvas);
12366 } else {
12367 draw(canvas);
12368 }
12369 }
12370 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012371 canvas.onPostDraw();
12372
12373 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012374 displayList.setCaching(caching);
12375 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012376 displayList.setLeftTopRightBottom(0, 0, width, height);
12377 } else {
12378 setDisplayListProperties(displayList);
12379 }
12380 }
12381 } else if (!isLayer) {
12382 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12383 mPrivateFlags &= ~DIRTY_MASK;
12384 }
12385
12386 return displayList;
12387 }
12388
12389 /**
12390 * Get the DisplayList for the HardwareLayer
12391 *
12392 * @param layer The HardwareLayer whose DisplayList we want
12393 * @return A DisplayList fopr the specified HardwareLayer
12394 */
12395 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12396 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12397 layer.setDisplayList(displayList);
12398 return displayList;
12399 }
12400
12401
12402 /**
Romain Guyb051e892010-09-28 19:09:36 -070012403 * <p>Returns a display list that can be used to draw this view again
12404 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012405 *
Romain Guyb051e892010-09-28 19:09:36 -070012406 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012407 *
12408 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012409 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012410 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012411 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012412 return mDisplayList;
12413 }
12414
Romain Guy38c2ece2012-05-24 14:20:56 -070012415 private void clearDisplayList() {
12416 if (mDisplayList != null) {
12417 mDisplayList.invalidate();
12418 mDisplayList.clear();
12419 }
12420 }
12421
Romain Guyb051e892010-09-28 19:09:36 -070012422 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012423 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012424 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012425 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012426 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012427 * @see #getDrawingCache(boolean)
12428 */
12429 public Bitmap getDrawingCache() {
12430 return getDrawingCache(false);
12431 }
12432
12433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012434 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12435 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12436 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12437 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12438 * request the drawing cache by calling this method and draw it on screen if the
12439 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012440 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012441 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12442 * this method will create a bitmap of the same size as this view. Because this bitmap
12443 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12444 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12445 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12446 * size than the view. This implies that your application must be able to handle this
12447 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012448 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012449 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12450 * the current density of the screen when the application is in compatibility
12451 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012452 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012453 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012455 * @see #setDrawingCacheEnabled(boolean)
12456 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012457 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012458 * @see #destroyDrawingCache()
12459 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012460 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12462 return null;
12463 }
12464 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012465 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012466 }
Romain Guy02890fd2010-08-06 17:58:44 -070012467 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 }
12469
12470 /**
12471 * <p>Frees the resources used by the drawing cache. If you call
12472 * {@link #buildDrawingCache()} manually without calling
12473 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12474 * should cleanup the cache with this method afterwards.</p>
12475 *
12476 * @see #setDrawingCacheEnabled(boolean)
12477 * @see #buildDrawingCache()
12478 * @see #getDrawingCache()
12479 */
12480 public void destroyDrawingCache() {
12481 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012482 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 mDrawingCache = null;
12484 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012485 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012486 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012487 mUnscaledDrawingCache = null;
12488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489 }
12490
12491 /**
12492 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012493 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012494 * view will always be drawn on top of a solid color.
12495 *
12496 * @param color The background color to use for the drawing cache's bitmap
12497 *
12498 * @see #setDrawingCacheEnabled(boolean)
12499 * @see #buildDrawingCache()
12500 * @see #getDrawingCache()
12501 */
12502 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012503 if (color != mDrawingCacheBackgroundColor) {
12504 mDrawingCacheBackgroundColor = color;
12505 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 }
12508
12509 /**
12510 * @see #setDrawingCacheBackgroundColor(int)
12511 *
12512 * @return The background color to used for the drawing cache's bitmap
12513 */
12514 public int getDrawingCacheBackgroundColor() {
12515 return mDrawingCacheBackgroundColor;
12516 }
12517
12518 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012519 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012520 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012521 * @see #buildDrawingCache(boolean)
12522 */
12523 public void buildDrawingCache() {
12524 buildDrawingCache(false);
12525 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012526
Romain Guyfbd8f692009-06-26 14:51:58 -070012527 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12529 *
12530 * <p>If you call {@link #buildDrawingCache()} manually without calling
12531 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12532 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012533 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012534 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12535 * this method will create a bitmap of the same size as this view. Because this bitmap
12536 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12537 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12538 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12539 * size than the view. This implies that your application must be able to handle this
12540 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012541 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012542 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12543 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012544 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012545 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012546 *
12547 * @see #getDrawingCache()
12548 * @see #destroyDrawingCache()
12549 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012550 public void buildDrawingCache(boolean autoScale) {
12551 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012552 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012553 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012554
Romain Guy8506ab42009-06-11 17:35:47 -070012555 int width = mRight - mLeft;
12556 int height = mBottom - mTop;
12557
12558 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012559 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012560
Romain Guye1123222009-06-29 14:24:56 -070012561 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012562 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12563 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012565
12566 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012567 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012568 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012569
12570 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012571 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012572 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12574 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012575 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012576 return;
12577 }
12578
12579 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012580 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012581
12582 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583 Bitmap.Config quality;
12584 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012585 // Never pick ARGB_4444 because it looks awful
12586 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012587 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12588 case DRAWING_CACHE_QUALITY_AUTO:
12589 quality = Bitmap.Config.ARGB_8888;
12590 break;
12591 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012592 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012593 break;
12594 case DRAWING_CACHE_QUALITY_HIGH:
12595 quality = Bitmap.Config.ARGB_8888;
12596 break;
12597 default:
12598 quality = Bitmap.Config.ARGB_8888;
12599 break;
12600 }
12601 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012602 // Optimization for translucent windows
12603 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012604 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012605 }
12606
12607 // Try to cleanup memory
12608 if (bitmap != null) bitmap.recycle();
12609
12610 try {
12611 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012612 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012613 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012614 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012615 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012616 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012617 }
Adam Powell26153a32010-11-08 15:22:27 -080012618 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619 } catch (OutOfMemoryError e) {
12620 // If there is not enough memory to create the bitmap cache, just
12621 // ignore the issue as bitmap caches are not required to draw the
12622 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012623 if (autoScale) {
12624 mDrawingCache = null;
12625 } else {
12626 mUnscaledDrawingCache = null;
12627 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012628 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012629 return;
12630 }
12631
12632 clear = drawingCacheBackgroundColor != 0;
12633 }
12634
12635 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012636 if (attachInfo != null) {
12637 canvas = attachInfo.mCanvas;
12638 if (canvas == null) {
12639 canvas = new Canvas();
12640 }
12641 canvas.setBitmap(bitmap);
12642 // Temporarily clobber the cached Canvas in case one of our children
12643 // is also using a drawing cache. Without this, the children would
12644 // steal the canvas by attaching their own bitmap to it and bad, bad
12645 // thing would happen (invisible views, corrupted drawings, etc.)
12646 attachInfo.mCanvas = null;
12647 } else {
12648 // This case should hopefully never or seldom happen
12649 canvas = new Canvas(bitmap);
12650 }
12651
12652 if (clear) {
12653 bitmap.eraseColor(drawingCacheBackgroundColor);
12654 }
12655
12656 computeScroll();
12657 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012658
Romain Guye1123222009-06-29 14:24:56 -070012659 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012660 final float scale = attachInfo.mApplicationScale;
12661 canvas.scale(scale, scale);
12662 }
Joe Malin32736f02011-01-19 16:14:20 -080012663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012664 canvas.translate(-mScrollX, -mScrollY);
12665
Romain Guy5bcdff42009-05-14 21:27:18 -070012666 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012667 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12668 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012669 mPrivateFlags |= DRAWING_CACHE_VALID;
12670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012671
12672 // Fast path for layouts with no backgrounds
12673 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012674 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012675 dispatchDraw(canvas);
12676 } else {
12677 draw(canvas);
12678 }
12679
12680 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012681 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012682
12683 if (attachInfo != null) {
12684 // Restore the cached Canvas for our siblings
12685 attachInfo.mCanvas = canvas;
12686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 }
12688 }
12689
12690 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012691 * Create a snapshot of the view into a bitmap. We should probably make
12692 * some form of this public, but should think about the API.
12693 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012694 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012695 int width = mRight - mLeft;
12696 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012697
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012698 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012699 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012700 width = (int) ((width * scale) + 0.5f);
12701 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012702
Romain Guy8c11e312009-09-14 15:15:30 -070012703 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012704 if (bitmap == null) {
12705 throw new OutOfMemoryError();
12706 }
12707
Romain Guyc529d8d2011-09-06 15:01:39 -070012708 Resources resources = getResources();
12709 if (resources != null) {
12710 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12711 }
Joe Malin32736f02011-01-19 16:14:20 -080012712
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012713 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012714 if (attachInfo != null) {
12715 canvas = attachInfo.mCanvas;
12716 if (canvas == null) {
12717 canvas = new Canvas();
12718 }
12719 canvas.setBitmap(bitmap);
12720 // Temporarily clobber the cached Canvas in case one of our children
12721 // is also using a drawing cache. Without this, the children would
12722 // steal the canvas by attaching their own bitmap to it and bad, bad
12723 // things would happen (invisible views, corrupted drawings, etc.)
12724 attachInfo.mCanvas = null;
12725 } else {
12726 // This case should hopefully never or seldom happen
12727 canvas = new Canvas(bitmap);
12728 }
12729
Romain Guy5bcdff42009-05-14 21:27:18 -070012730 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012731 bitmap.eraseColor(backgroundColor);
12732 }
12733
12734 computeScroll();
12735 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012736 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012737 canvas.translate(-mScrollX, -mScrollY);
12738
Romain Guy5bcdff42009-05-14 21:27:18 -070012739 // Temporarily remove the dirty mask
12740 int flags = mPrivateFlags;
12741 mPrivateFlags &= ~DIRTY_MASK;
12742
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012743 // Fast path for layouts with no backgrounds
12744 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12745 dispatchDraw(canvas);
12746 } else {
12747 draw(canvas);
12748 }
12749
Romain Guy5bcdff42009-05-14 21:27:18 -070012750 mPrivateFlags = flags;
12751
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012752 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012753 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012754
12755 if (attachInfo != null) {
12756 // Restore the cached Canvas for our siblings
12757 attachInfo.mCanvas = canvas;
12758 }
Romain Guy8506ab42009-06-11 17:35:47 -070012759
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012760 return bitmap;
12761 }
12762
12763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012764 * Indicates whether this View is currently in edit mode. A View is usually
12765 * in edit mode when displayed within a developer tool. For instance, if
12766 * this View is being drawn by a visual user interface builder, this method
12767 * should return true.
12768 *
12769 * Subclasses should check the return value of this method to provide
12770 * different behaviors if their normal behavior might interfere with the
12771 * host environment. For instance: the class spawns a thread in its
12772 * constructor, the drawing code relies on device-specific features, etc.
12773 *
12774 * This method is usually checked in the drawing code of custom widgets.
12775 *
12776 * @return True if this View is in edit mode, false otherwise.
12777 */
12778 public boolean isInEditMode() {
12779 return false;
12780 }
12781
12782 /**
12783 * If the View draws content inside its padding and enables fading edges,
12784 * it needs to support padding offsets. Padding offsets are added to the
12785 * fading edges to extend the length of the fade so that it covers pixels
12786 * drawn inside the padding.
12787 *
12788 * Subclasses of this class should override this method if they need
12789 * to draw content inside the padding.
12790 *
12791 * @return True if padding offset must be applied, false otherwise.
12792 *
12793 * @see #getLeftPaddingOffset()
12794 * @see #getRightPaddingOffset()
12795 * @see #getTopPaddingOffset()
12796 * @see #getBottomPaddingOffset()
12797 *
12798 * @since CURRENT
12799 */
12800 protected boolean isPaddingOffsetRequired() {
12801 return false;
12802 }
12803
12804 /**
12805 * Amount by which to extend the left fading region. Called only when
12806 * {@link #isPaddingOffsetRequired()} returns true.
12807 *
12808 * @return The left padding offset in pixels.
12809 *
12810 * @see #isPaddingOffsetRequired()
12811 *
12812 * @since CURRENT
12813 */
12814 protected int getLeftPaddingOffset() {
12815 return 0;
12816 }
12817
12818 /**
12819 * Amount by which to extend the right fading region. Called only when
12820 * {@link #isPaddingOffsetRequired()} returns true.
12821 *
12822 * @return The right padding offset in pixels.
12823 *
12824 * @see #isPaddingOffsetRequired()
12825 *
12826 * @since CURRENT
12827 */
12828 protected int getRightPaddingOffset() {
12829 return 0;
12830 }
12831
12832 /**
12833 * Amount by which to extend the top fading region. Called only when
12834 * {@link #isPaddingOffsetRequired()} returns true.
12835 *
12836 * @return The top padding offset in pixels.
12837 *
12838 * @see #isPaddingOffsetRequired()
12839 *
12840 * @since CURRENT
12841 */
12842 protected int getTopPaddingOffset() {
12843 return 0;
12844 }
12845
12846 /**
12847 * Amount by which to extend the bottom fading region. Called only when
12848 * {@link #isPaddingOffsetRequired()} returns true.
12849 *
12850 * @return The bottom padding offset in pixels.
12851 *
12852 * @see #isPaddingOffsetRequired()
12853 *
12854 * @since CURRENT
12855 */
12856 protected int getBottomPaddingOffset() {
12857 return 0;
12858 }
12859
12860 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012861 * @hide
12862 * @param offsetRequired
12863 */
12864 protected int getFadeTop(boolean offsetRequired) {
12865 int top = mPaddingTop;
12866 if (offsetRequired) top += getTopPaddingOffset();
12867 return top;
12868 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012869
Romain Guyf2fc4602011-07-19 15:20:03 -070012870 /**
12871 * @hide
12872 * @param offsetRequired
12873 */
12874 protected int getFadeHeight(boolean offsetRequired) {
12875 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012876 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012877 return mBottom - mTop - mPaddingBottom - padding;
12878 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012879
Romain Guyf2fc4602011-07-19 15:20:03 -070012880 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012881 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012882 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012883 *
Romain Guy2bffd262010-09-12 17:40:02 -070012884 * <p>Even if this method returns true, it does not mean that every call
12885 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12886 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012887 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012888 * window is hardware accelerated,
12889 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12890 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012891 *
Romain Guy2bffd262010-09-12 17:40:02 -070012892 * @return True if the view is attached to a window and the window is
12893 * hardware accelerated; false in any other case.
12894 */
12895 public boolean isHardwareAccelerated() {
12896 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12897 }
Joe Malin32736f02011-01-19 16:14:20 -080012898
Romain Guy2bffd262010-09-12 17:40:02 -070012899 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012900 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12901 * case of an active Animation being run on the view.
12902 */
12903 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12904 Animation a, boolean scalingRequired) {
12905 Transformation invalidationTransform;
12906 final int flags = parent.mGroupFlags;
12907 final boolean initialized = a.isInitialized();
12908 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012909 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012910 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012911 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012912 onAnimationStart();
12913 }
12914
12915 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12916 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12917 if (parent.mInvalidationTransformation == null) {
12918 parent.mInvalidationTransformation = new Transformation();
12919 }
12920 invalidationTransform = parent.mInvalidationTransformation;
12921 a.getTransformation(drawingTime, invalidationTransform, 1f);
12922 } else {
12923 invalidationTransform = parent.mChildTransformation;
12924 }
Romain Guy393a52c2012-05-22 20:21:08 -070012925
Chet Haasebcca79a2012-02-14 08:45:14 -080012926 if (more) {
12927 if (!a.willChangeBounds()) {
12928 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12929 parent.FLAG_OPTIMIZE_INVALIDATE) {
12930 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12931 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
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 parent.invalidate(mLeft, mTop, mRight, mBottom);
12936 }
12937 } else {
12938 if (parent.mInvalidateRegion == null) {
12939 parent.mInvalidateRegion = new RectF();
12940 }
12941 final RectF region = parent.mInvalidateRegion;
12942 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12943 invalidationTransform);
12944
12945 // The child need to draw an animation, potentially offscreen, so
12946 // make sure we do not cancel invalidate requests
12947 parent.mPrivateFlags |= DRAW_ANIMATION;
12948
12949 final int left = mLeft + (int) region.left;
12950 final int top = mTop + (int) region.top;
12951 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12952 top + (int) (region.height() + .5f));
12953 }
12954 }
12955 return more;
12956 }
12957
Chet Haasea1cff502012-02-21 13:43:44 -080012958 /**
12959 * This method is called by getDisplayList() when a display list is created or re-rendered.
12960 * It sets or resets the current value of all properties on that display list (resetting is
12961 * necessary when a display list is being re-created, because we need to make sure that
12962 * previously-set transform values
12963 */
12964 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012965 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012966 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012967 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012968 if (mParent instanceof ViewGroup) {
12969 displayList.setClipChildren(
12970 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12971 }
Chet Haase9420abd2012-03-29 16:28:32 -070012972 float alpha = 1;
12973 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12974 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12975 ViewGroup parentVG = (ViewGroup) mParent;
12976 final boolean hasTransform =
12977 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12978 if (hasTransform) {
12979 Transformation transform = parentVG.mChildTransformation;
12980 final int transformType = parentVG.mChildTransformation.getTransformationType();
12981 if (transformType != Transformation.TYPE_IDENTITY) {
12982 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12983 alpha = transform.getAlpha();
12984 }
12985 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12986 displayList.setStaticMatrix(transform.getMatrix());
12987 }
12988 }
12989 }
Chet Haasea1cff502012-02-21 13:43:44 -080012990 }
12991 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012992 alpha *= mTransformationInfo.mAlpha;
12993 if (alpha < 1) {
12994 final int multipliedAlpha = (int) (255 * alpha);
12995 if (onSetAlpha(multipliedAlpha)) {
12996 alpha = 1;
12997 }
12998 }
12999 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013000 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13001 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13002 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13003 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013004 if (mTransformationInfo.mCamera == null) {
13005 mTransformationInfo.mCamera = new Camera();
13006 mTransformationInfo.matrix3D = new Matrix();
13007 }
13008 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080013009 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
13010 displayList.setPivotX(getPivotX());
13011 displayList.setPivotY(getPivotY());
13012 }
Chet Haase9420abd2012-03-29 16:28:32 -070013013 } else if (alpha < 1) {
13014 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013015 }
13016 }
13017 }
13018
Chet Haasebcca79a2012-02-14 08:45:14 -080013019 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013020 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13021 * This draw() method is an implementation detail and is not intended to be overridden or
13022 * to be called from anywhere else other than ViewGroup.drawChild().
13023 */
13024 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013025 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013026 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013027 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013028 final int flags = parent.mGroupFlags;
13029
Chet Haasea1cff502012-02-21 13:43:44 -080013030 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013031 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013032 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013033 }
13034
13035 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013036 boolean concatMatrix = false;
13037
13038 boolean scalingRequired = false;
13039 boolean caching;
13040 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
13041
13042 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013043 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13044 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013045 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013046 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013047 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13048 } else {
13049 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13050 }
13051
Chet Haasebcca79a2012-02-14 08:45:14 -080013052 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013053 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013054 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013055 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013056 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070013057 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013058 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013059 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013060 } else {
Chet Haase21433372012-06-05 07:54:09 -070013061 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013062 mDisplayList != null) {
13063 // No longer animating: clear out old animation matrix
13064 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070013065 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013066 }
13067 if (!useDisplayListProperties &&
13068 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13069 final boolean hasTransform =
13070 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13071 if (hasTransform) {
13072 final int transformType = parent.mChildTransformation.getTransformationType();
13073 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13074 parent.mChildTransformation : null;
13075 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13076 }
Chet Haase64a48c12012-02-13 16:33:29 -080013077 }
13078 }
13079
13080 concatMatrix |= !childHasIdentityMatrix;
13081
13082 // Sets the flag as early as possible to allow draw() implementations
13083 // to call invalidate() successfully when doing animations
13084 mPrivateFlags |= DRAWN;
13085
Chet Haasebcca79a2012-02-14 08:45:14 -080013086 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013087 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013088 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013089 return more;
13090 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013091 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013092
13093 if (hardwareAccelerated) {
13094 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13095 // retain the flag's value temporarily in the mRecreateDisplayList flag
13096 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13097 mPrivateFlags &= ~INVALIDATED;
13098 }
13099
13100 computeScroll();
13101
13102 final int sx = mScrollX;
13103 final int sy = mScrollY;
13104
13105 DisplayList displayList = null;
13106 Bitmap cache = null;
13107 boolean hasDisplayList = false;
13108 if (caching) {
13109 if (!hardwareAccelerated) {
13110 if (layerType != LAYER_TYPE_NONE) {
13111 layerType = LAYER_TYPE_SOFTWARE;
13112 buildDrawingCache(true);
13113 }
13114 cache = getDrawingCache(true);
13115 } else {
13116 switch (layerType) {
13117 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013118 if (useDisplayListProperties) {
13119 hasDisplayList = canHaveDisplayList();
13120 } else {
13121 buildDrawingCache(true);
13122 cache = getDrawingCache(true);
13123 }
Chet Haase64a48c12012-02-13 16:33:29 -080013124 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013125 case LAYER_TYPE_HARDWARE:
13126 if (useDisplayListProperties) {
13127 hasDisplayList = canHaveDisplayList();
13128 }
13129 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013130 case LAYER_TYPE_NONE:
13131 // Delay getting the display list until animation-driven alpha values are
13132 // set up and possibly passed on to the view
13133 hasDisplayList = canHaveDisplayList();
13134 break;
13135 }
13136 }
13137 }
Chet Haasea1cff502012-02-21 13:43:44 -080013138 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013139 if (useDisplayListProperties) {
13140 displayList = getDisplayList();
13141 if (!displayList.isValid()) {
13142 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13143 // to getDisplayList(), the display list will be marked invalid and we should not
13144 // try to use it again.
13145 displayList = null;
13146 hasDisplayList = false;
13147 useDisplayListProperties = false;
13148 }
13149 }
Chet Haase64a48c12012-02-13 16:33:29 -080013150
13151 final boolean hasNoCache = cache == null || hasDisplayList;
13152 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13153 layerType != LAYER_TYPE_HARDWARE;
13154
Chet Haasea1cff502012-02-21 13:43:44 -080013155 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013156 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013157 restoreTo = canvas.save();
13158 }
Chet Haase64a48c12012-02-13 16:33:29 -080013159 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013160 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013161 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013162 if (!useDisplayListProperties) {
13163 canvas.translate(mLeft, mTop);
13164 }
Chet Haase64a48c12012-02-13 16:33:29 -080013165 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013166 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013167 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013168 restoreTo = canvas.save();
13169 }
Chet Haase64a48c12012-02-13 16:33:29 -080013170 // mAttachInfo cannot be null, otherwise scalingRequired == false
13171 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13172 canvas.scale(scale, scale);
13173 }
13174 }
13175
Chet Haasea1cff502012-02-21 13:43:44 -080013176 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013177 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13178 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013179 if (transformToApply != null || !childHasIdentityMatrix) {
13180 int transX = 0;
13181 int transY = 0;
13182
13183 if (offsetForScroll) {
13184 transX = -sx;
13185 transY = -sy;
13186 }
13187
13188 if (transformToApply != null) {
13189 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013190 if (useDisplayListProperties) {
13191 displayList.setAnimationMatrix(transformToApply.getMatrix());
13192 } else {
13193 // Undo the scroll translation, apply the transformation matrix,
13194 // then redo the scroll translate to get the correct result.
13195 canvas.translate(-transX, -transY);
13196 canvas.concat(transformToApply.getMatrix());
13197 canvas.translate(transX, transY);
13198 }
Chet Haasea1cff502012-02-21 13:43:44 -080013199 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013200 }
13201
13202 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013203 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013204 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013205 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013206 }
13207 }
13208
Chet Haasea1cff502012-02-21 13:43:44 -080013209 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013210 canvas.translate(-transX, -transY);
13211 canvas.concat(getMatrix());
13212 canvas.translate(transX, transY);
13213 }
13214 }
13215
Chet Haase21433372012-06-05 07:54:09 -070013216 // Deal with alpha if it is or used to be <1
13217 if (alpha < 1 ||
13218 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13219 if (alpha < 1) {
13220 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13221 } else {
13222 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13223 }
Chet Haasea1cff502012-02-21 13:43:44 -080013224 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013225 if (hasNoCache) {
13226 final int multipliedAlpha = (int) (255 * alpha);
13227 if (!onSetAlpha(multipliedAlpha)) {
13228 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013229 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013230 layerType != LAYER_TYPE_NONE) {
13231 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13232 }
Chet Haase9420abd2012-03-29 16:28:32 -070013233 if (useDisplayListProperties) {
13234 displayList.setAlpha(alpha * getAlpha());
13235 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013236 final int scrollX = hasDisplayList ? 0 : sx;
13237 final int scrollY = hasDisplayList ? 0 : sy;
13238 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13239 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013240 }
13241 } else {
13242 // Alpha is handled by the child directly, clobber the layer's alpha
13243 mPrivateFlags |= ALPHA_SET;
13244 }
13245 }
13246 }
13247 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13248 onSetAlpha(255);
13249 mPrivateFlags &= ~ALPHA_SET;
13250 }
13251
Chet Haasea1cff502012-02-21 13:43:44 -080013252 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13253 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013254 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013255 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013256 } else {
13257 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013258 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013259 } else {
13260 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13261 }
13262 }
13263 }
13264
Chet Haase9420abd2012-03-29 16:28:32 -070013265 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013266 displayList = getDisplayList();
13267 if (!displayList.isValid()) {
13268 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13269 // to getDisplayList(), the display list will be marked invalid and we should not
13270 // try to use it again.
13271 displayList = null;
13272 hasDisplayList = false;
13273 }
13274 }
13275
13276 if (hasNoCache) {
13277 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013278 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013279 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013280 if (layer != null && layer.isValid()) {
13281 mLayerPaint.setAlpha((int) (alpha * 255));
13282 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13283 layerRendered = true;
13284 } else {
13285 final int scrollX = hasDisplayList ? 0 : sx;
13286 final int scrollY = hasDisplayList ? 0 : sy;
13287 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013288 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013289 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13290 }
13291 }
13292
13293 if (!layerRendered) {
13294 if (!hasDisplayList) {
13295 // Fast path for layouts with no backgrounds
13296 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013297 mPrivateFlags &= ~DIRTY_MASK;
13298 dispatchDraw(canvas);
13299 } else {
13300 draw(canvas);
13301 }
13302 } else {
13303 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013304 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013305 }
13306 }
13307 } else if (cache != null) {
13308 mPrivateFlags &= ~DIRTY_MASK;
13309 Paint cachePaint;
13310
13311 if (layerType == LAYER_TYPE_NONE) {
13312 cachePaint = parent.mCachePaint;
13313 if (cachePaint == null) {
13314 cachePaint = new Paint();
13315 cachePaint.setDither(false);
13316 parent.mCachePaint = cachePaint;
13317 }
Chet Haase9420abd2012-03-29 16:28:32 -070013318 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013319 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013320 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13321 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013322 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013323 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013324 }
13325 } else {
13326 cachePaint = mLayerPaint;
13327 cachePaint.setAlpha((int) (alpha * 255));
13328 }
13329 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13330 }
13331
Chet Haasea1cff502012-02-21 13:43:44 -080013332 if (restoreTo >= 0) {
13333 canvas.restoreToCount(restoreTo);
13334 }
Chet Haase64a48c12012-02-13 16:33:29 -080013335
13336 if (a != null && !more) {
13337 if (!hardwareAccelerated && !a.getFillAfter()) {
13338 onSetAlpha(255);
13339 }
13340 parent.finishAnimatingView(this, a);
13341 }
13342
13343 if (more && hardwareAccelerated) {
13344 // invalidation is the trigger to recreate display lists, so if we're using
13345 // display lists to render, force an invalidate to allow the animation to
13346 // continue drawing another frame
13347 parent.invalidate(true);
13348 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13349 // alpha animations should cause the child to recreate its display list
13350 invalidate(true);
13351 }
13352 }
13353
13354 mRecreateDisplayList = false;
13355
13356 return more;
13357 }
13358
13359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013360 * Manually render this view (and all of its children) to the given Canvas.
13361 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013362 * called. When implementing a view, implement
13363 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13364 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013365 *
13366 * @param canvas The Canvas to which the View is rendered.
13367 */
13368 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013369 final int privateFlags = mPrivateFlags;
13370 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13371 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13372 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013374 /*
13375 * Draw traversal performs several drawing steps which must be executed
13376 * in the appropriate order:
13377 *
13378 * 1. Draw the background
13379 * 2. If necessary, save the canvas' layers to prepare for fading
13380 * 3. Draw view's content
13381 * 4. Draw children
13382 * 5. If necessary, draw the fading edges and restore layers
13383 * 6. Draw decorations (scrollbars for instance)
13384 */
13385
13386 // Step 1, draw the background, if needed
13387 int saveCount;
13388
Romain Guy24443ea2009-05-11 11:56:30 -070013389 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013390 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013391 if (background != null) {
13392 final int scrollX = mScrollX;
13393 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013394
Romain Guy24443ea2009-05-11 11:56:30 -070013395 if (mBackgroundSizeChanged) {
13396 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13397 mBackgroundSizeChanged = false;
13398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013399
Romain Guy24443ea2009-05-11 11:56:30 -070013400 if ((scrollX | scrollY) == 0) {
13401 background.draw(canvas);
13402 } else {
13403 canvas.translate(scrollX, scrollY);
13404 background.draw(canvas);
13405 canvas.translate(-scrollX, -scrollY);
13406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013407 }
13408 }
13409
13410 // skip step 2 & 5 if possible (common case)
13411 final int viewFlags = mViewFlags;
13412 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13413 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13414 if (!verticalEdges && !horizontalEdges) {
13415 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013416 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013417
13418 // Step 4, draw the children
13419 dispatchDraw(canvas);
13420
13421 // Step 6, draw decorations (scrollbars)
13422 onDrawScrollBars(canvas);
13423
13424 // we're done...
13425 return;
13426 }
13427
13428 /*
13429 * Here we do the full fledged routine...
13430 * (this is an uncommon case where speed matters less,
13431 * this is why we repeat some of the tests that have been
13432 * done above)
13433 */
13434
13435 boolean drawTop = false;
13436 boolean drawBottom = false;
13437 boolean drawLeft = false;
13438 boolean drawRight = false;
13439
13440 float topFadeStrength = 0.0f;
13441 float bottomFadeStrength = 0.0f;
13442 float leftFadeStrength = 0.0f;
13443 float rightFadeStrength = 0.0f;
13444
13445 // Step 2, save the canvas' layers
13446 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013447
13448 final boolean offsetRequired = isPaddingOffsetRequired();
13449 if (offsetRequired) {
13450 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013451 }
13452
13453 int left = mScrollX + paddingLeft;
13454 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013455 int top = mScrollY + getFadeTop(offsetRequired);
13456 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013457
13458 if (offsetRequired) {
13459 right += getRightPaddingOffset();
13460 bottom += getBottomPaddingOffset();
13461 }
13462
13463 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013464 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013465 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013466
13467 // clip the fade length if top and bottom fades overlap
13468 // overlapping fades produce odd-looking artifacts
13469 if (verticalEdges && (top + length > bottom - length)) {
13470 length = (bottom - top) / 2;
13471 }
13472
13473 // also clip horizontal fades if necessary
13474 if (horizontalEdges && (left + length > right - length)) {
13475 length = (right - left) / 2;
13476 }
13477
13478 if (verticalEdges) {
13479 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013480 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013481 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013482 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013483 }
13484
13485 if (horizontalEdges) {
13486 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013487 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013489 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 }
13491
13492 saveCount = canvas.getSaveCount();
13493
13494 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013495 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013496 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13497
13498 if (drawTop) {
13499 canvas.saveLayer(left, top, right, top + length, null, flags);
13500 }
13501
13502 if (drawBottom) {
13503 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13504 }
13505
13506 if (drawLeft) {
13507 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13508 }
13509
13510 if (drawRight) {
13511 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13512 }
13513 } else {
13514 scrollabilityCache.setFadeColor(solidColor);
13515 }
13516
13517 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013518 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519
13520 // Step 4, draw the children
13521 dispatchDraw(canvas);
13522
13523 // Step 5, draw the fade effect and restore layers
13524 final Paint p = scrollabilityCache.paint;
13525 final Matrix matrix = scrollabilityCache.matrix;
13526 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527
13528 if (drawTop) {
13529 matrix.setScale(1, fadeHeight * topFadeStrength);
13530 matrix.postTranslate(left, top);
13531 fade.setLocalMatrix(matrix);
13532 canvas.drawRect(left, top, right, top + length, p);
13533 }
13534
13535 if (drawBottom) {
13536 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13537 matrix.postRotate(180);
13538 matrix.postTranslate(left, bottom);
13539 fade.setLocalMatrix(matrix);
13540 canvas.drawRect(left, bottom - length, right, bottom, p);
13541 }
13542
13543 if (drawLeft) {
13544 matrix.setScale(1, fadeHeight * leftFadeStrength);
13545 matrix.postRotate(-90);
13546 matrix.postTranslate(left, top);
13547 fade.setLocalMatrix(matrix);
13548 canvas.drawRect(left, top, left + length, bottom, p);
13549 }
13550
13551 if (drawRight) {
13552 matrix.setScale(1, fadeHeight * rightFadeStrength);
13553 matrix.postRotate(90);
13554 matrix.postTranslate(right, top);
13555 fade.setLocalMatrix(matrix);
13556 canvas.drawRect(right - length, top, right, bottom, p);
13557 }
13558
13559 canvas.restoreToCount(saveCount);
13560
13561 // Step 6, draw decorations (scrollbars)
13562 onDrawScrollBars(canvas);
13563 }
13564
13565 /**
13566 * Override this if your view is known to always be drawn on top of a solid color background,
13567 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13568 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13569 * should be set to 0xFF.
13570 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013571 * @see #setVerticalFadingEdgeEnabled(boolean)
13572 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013573 *
13574 * @return The known solid color background for this view, or 0 if the color may vary
13575 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013576 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013577 public int getSolidColor() {
13578 return 0;
13579 }
13580
13581 /**
13582 * Build a human readable string representation of the specified view flags.
13583 *
13584 * @param flags the view flags to convert to a string
13585 * @return a String representing the supplied flags
13586 */
13587 private static String printFlags(int flags) {
13588 String output = "";
13589 int numFlags = 0;
13590 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13591 output += "TAKES_FOCUS";
13592 numFlags++;
13593 }
13594
13595 switch (flags & VISIBILITY_MASK) {
13596 case INVISIBLE:
13597 if (numFlags > 0) {
13598 output += " ";
13599 }
13600 output += "INVISIBLE";
13601 // USELESS HERE numFlags++;
13602 break;
13603 case GONE:
13604 if (numFlags > 0) {
13605 output += " ";
13606 }
13607 output += "GONE";
13608 // USELESS HERE numFlags++;
13609 break;
13610 default:
13611 break;
13612 }
13613 return output;
13614 }
13615
13616 /**
13617 * Build a human readable string representation of the specified private
13618 * view flags.
13619 *
13620 * @param privateFlags the private view flags to convert to a string
13621 * @return a String representing the supplied flags
13622 */
13623 private static String printPrivateFlags(int privateFlags) {
13624 String output = "";
13625 int numFlags = 0;
13626
13627 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13628 output += "WANTS_FOCUS";
13629 numFlags++;
13630 }
13631
13632 if ((privateFlags & FOCUSED) == FOCUSED) {
13633 if (numFlags > 0) {
13634 output += " ";
13635 }
13636 output += "FOCUSED";
13637 numFlags++;
13638 }
13639
13640 if ((privateFlags & SELECTED) == SELECTED) {
13641 if (numFlags > 0) {
13642 output += " ";
13643 }
13644 output += "SELECTED";
13645 numFlags++;
13646 }
13647
13648 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13649 if (numFlags > 0) {
13650 output += " ";
13651 }
13652 output += "IS_ROOT_NAMESPACE";
13653 numFlags++;
13654 }
13655
13656 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13657 if (numFlags > 0) {
13658 output += " ";
13659 }
13660 output += "HAS_BOUNDS";
13661 numFlags++;
13662 }
13663
13664 if ((privateFlags & DRAWN) == DRAWN) {
13665 if (numFlags > 0) {
13666 output += " ";
13667 }
13668 output += "DRAWN";
13669 // USELESS HERE numFlags++;
13670 }
13671 return output;
13672 }
13673
13674 /**
13675 * <p>Indicates whether or not this view's layout will be requested during
13676 * the next hierarchy layout pass.</p>
13677 *
13678 * @return true if the layout will be forced during next layout pass
13679 */
13680 public boolean isLayoutRequested() {
13681 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13682 }
13683
13684 /**
13685 * Assign a size and position to a view and all of its
13686 * descendants
13687 *
13688 * <p>This is the second phase of the layout mechanism.
13689 * (The first is measuring). In this phase, each parent calls
13690 * layout on all of its children to position them.
13691 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013692 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013693 *
Chet Haase9c087442011-01-12 16:20:16 -080013694 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013695 * Derived classes with children should override
13696 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013697 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013698 *
13699 * @param l Left position, relative to parent
13700 * @param t Top position, relative to parent
13701 * @param r Right position, relative to parent
13702 * @param b Bottom position, relative to parent
13703 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013704 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013705 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013706 int oldL = mLeft;
13707 int oldT = mTop;
13708 int oldB = mBottom;
13709 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710 boolean changed = setFrame(l, t, r, b);
13711 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013712 onLayout(changed, l, t, r, b);
13713 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013714
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013715 ListenerInfo li = mListenerInfo;
13716 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013717 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013718 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013719 int numListeners = listenersCopy.size();
13720 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013721 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013722 }
13723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013724 }
13725 mPrivateFlags &= ~FORCE_LAYOUT;
13726 }
13727
13728 /**
13729 * Called from layout when this view should
13730 * assign a size and position to each of its children.
13731 *
13732 * Derived classes with children should override
13733 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013734 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 * @param changed This is a new size or position for this view
13736 * @param left Left position, relative to parent
13737 * @param top Top position, relative to parent
13738 * @param right Right position, relative to parent
13739 * @param bottom Bottom position, relative to parent
13740 */
13741 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13742 }
13743
13744 /**
13745 * Assign a size and position to this view.
13746 *
13747 * This is called from layout.
13748 *
13749 * @param left Left position, relative to parent
13750 * @param top Top position, relative to parent
13751 * @param right Right position, relative to parent
13752 * @param bottom Bottom position, relative to parent
13753 * @return true if the new size and position are different than the
13754 * previous ones
13755 * {@hide}
13756 */
13757 protected boolean setFrame(int left, int top, int right, int bottom) {
13758 boolean changed = false;
13759
13760 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013761 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762 + right + "," + bottom + ")");
13763 }
13764
13765 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13766 changed = true;
13767
13768 // Remember our drawn bit
13769 int drawn = mPrivateFlags & DRAWN;
13770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771 int oldWidth = mRight - mLeft;
13772 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013773 int newWidth = right - left;
13774 int newHeight = bottom - top;
13775 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13776
13777 // Invalidate our old position
13778 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013779
13780 mLeft = left;
13781 mTop = top;
13782 mRight = right;
13783 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013784 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013785 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013787
13788 mPrivateFlags |= HAS_BOUNDS;
13789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013790
Chet Haase75755e22011-07-18 17:48:25 -070013791 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013792 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13793 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013794 if (mTransformationInfo != null) {
13795 mTransformationInfo.mMatrixDirty = true;
13796 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013798 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13799 }
13800
13801 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13802 // If we are visible, force the DRAWN bit to on so that
13803 // this invalidate will go through (at least to our parent).
13804 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013805 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806 // the DRAWN bit.
13807 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013808 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013809 // parent display list may need to be recreated based on a change in the bounds
13810 // of any child
13811 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013812 }
13813
13814 // Reset drawn bit to original value (invalidate turns it off)
13815 mPrivateFlags |= drawn;
13816
13817 mBackgroundSizeChanged = true;
13818 }
13819 return changed;
13820 }
13821
13822 /**
13823 * Finalize inflating a view from XML. This is called as the last phase
13824 * of inflation, after all child views have been added.
13825 *
13826 * <p>Even if the subclass overrides onFinishInflate, they should always be
13827 * sure to call the super method, so that we get called.
13828 */
13829 protected void onFinishInflate() {
13830 }
13831
13832 /**
13833 * Returns the resources associated with this view.
13834 *
13835 * @return Resources object.
13836 */
13837 public Resources getResources() {
13838 return mResources;
13839 }
13840
13841 /**
13842 * Invalidates the specified Drawable.
13843 *
13844 * @param drawable the drawable to invalidate
13845 */
13846 public void invalidateDrawable(Drawable drawable) {
13847 if (verifyDrawable(drawable)) {
13848 final Rect dirty = drawable.getBounds();
13849 final int scrollX = mScrollX;
13850 final int scrollY = mScrollY;
13851
13852 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13853 dirty.right + scrollX, dirty.bottom + scrollY);
13854 }
13855 }
13856
13857 /**
13858 * Schedules an action on a drawable to occur at a specified time.
13859 *
13860 * @param who the recipient of the action
13861 * @param what the action to run on the drawable
13862 * @param when the time at which the action must occur. Uses the
13863 * {@link SystemClock#uptimeMillis} timebase.
13864 */
13865 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013866 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013867 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013868 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013869 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13870 Choreographer.CALLBACK_ANIMATION, what, who,
13871 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013872 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013873 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013875 }
13876 }
13877
13878 /**
13879 * Cancels a scheduled action on a drawable.
13880 *
13881 * @param who the recipient of the action
13882 * @param what the action to cancel
13883 */
13884 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013885 if (verifyDrawable(who) && what != null) {
13886 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013887 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13888 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013889 } else {
13890 ViewRootImpl.getRunQueue().removeCallbacks(what);
13891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013892 }
13893 }
13894
13895 /**
13896 * Unschedule any events associated with the given Drawable. This can be
13897 * used when selecting a new Drawable into a view, so that the previous
13898 * one is completely unscheduled.
13899 *
13900 * @param who The Drawable to unschedule.
13901 *
13902 * @see #drawableStateChanged
13903 */
13904 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013905 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013906 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13907 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013908 }
13909 }
13910
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013911 /**
13912 * Return the layout direction of a given Drawable.
13913 *
13914 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013915 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013916 */
13917 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013919 }
13920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 /**
13922 * If your view subclass is displaying its own Drawable objects, it should
13923 * override this function and return true for any Drawable it is
13924 * displaying. This allows animations for those drawables to be
13925 * scheduled.
13926 *
13927 * <p>Be sure to call through to the super class when overriding this
13928 * function.
13929 *
13930 * @param who The Drawable to verify. Return true if it is one you are
13931 * displaying, else return the result of calling through to the
13932 * super class.
13933 *
13934 * @return boolean If true than the Drawable is being displayed in the
13935 * view; else false and it is not allowed to animate.
13936 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013937 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13938 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939 */
13940 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013941 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 }
13943
13944 /**
13945 * This function is called whenever the state of the view changes in such
13946 * a way that it impacts the state of drawables being shown.
13947 *
13948 * <p>Be sure to call through to the superclass when overriding this
13949 * function.
13950 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013951 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013952 */
13953 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013954 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 if (d != null && d.isStateful()) {
13956 d.setState(getDrawableState());
13957 }
13958 }
13959
13960 /**
13961 * Call this to force a view to update its drawable state. This will cause
13962 * drawableStateChanged to be called on this view. Views that are interested
13963 * in the new state should call getDrawableState.
13964 *
13965 * @see #drawableStateChanged
13966 * @see #getDrawableState
13967 */
13968 public void refreshDrawableState() {
13969 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13970 drawableStateChanged();
13971
13972 ViewParent parent = mParent;
13973 if (parent != null) {
13974 parent.childDrawableStateChanged(this);
13975 }
13976 }
13977
13978 /**
13979 * Return an array of resource IDs of the drawable states representing the
13980 * current state of the view.
13981 *
13982 * @return The current drawable state
13983 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013984 * @see Drawable#setState(int[])
13985 * @see #drawableStateChanged()
13986 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013987 */
13988 public final int[] getDrawableState() {
13989 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13990 return mDrawableState;
13991 } else {
13992 mDrawableState = onCreateDrawableState(0);
13993 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13994 return mDrawableState;
13995 }
13996 }
13997
13998 /**
13999 * Generate the new {@link android.graphics.drawable.Drawable} state for
14000 * this view. This is called by the view
14001 * system when the cached Drawable state is determined to be invalid. To
14002 * retrieve the current state, you should use {@link #getDrawableState}.
14003 *
14004 * @param extraSpace if non-zero, this is the number of extra entries you
14005 * would like in the returned array in which you can place your own
14006 * states.
14007 *
14008 * @return Returns an array holding the current {@link Drawable} state of
14009 * the view.
14010 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014011 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 */
14013 protected int[] onCreateDrawableState(int extraSpace) {
14014 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14015 mParent instanceof View) {
14016 return ((View) mParent).onCreateDrawableState(extraSpace);
14017 }
14018
14019 int[] drawableState;
14020
14021 int privateFlags = mPrivateFlags;
14022
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014023 int viewStateIndex = 0;
14024 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14025 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14026 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014027 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014028 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14029 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014030 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14031 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014032 // This is set if HW acceleration is requested, even if the current
14033 // process doesn't allow it. This is just to allow app preview
14034 // windows to better match their app.
14035 viewStateIndex |= VIEW_STATE_ACCELERATED;
14036 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014037 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014038
Christopher Tate3d4bf172011-03-28 16:16:46 -070014039 final int privateFlags2 = mPrivateFlags2;
14040 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14041 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014043 drawableState = VIEW_STATE_SETS[viewStateIndex];
14044
14045 //noinspection ConstantIfStatement
14046 if (false) {
14047 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14048 Log.i("View", toString()
14049 + " pressed=" + ((privateFlags & PRESSED) != 0)
14050 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14051 + " fo=" + hasFocus()
14052 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014053 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014054 + ": " + Arrays.toString(drawableState));
14055 }
14056
14057 if (extraSpace == 0) {
14058 return drawableState;
14059 }
14060
14061 final int[] fullState;
14062 if (drawableState != null) {
14063 fullState = new int[drawableState.length + extraSpace];
14064 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14065 } else {
14066 fullState = new int[extraSpace];
14067 }
14068
14069 return fullState;
14070 }
14071
14072 /**
14073 * Merge your own state values in <var>additionalState</var> into the base
14074 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014075 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014076 *
14077 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014078 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014079 * own additional state values.
14080 *
14081 * @param additionalState The additional state values you would like
14082 * added to <var>baseState</var>; this array is not modified.
14083 *
14084 * @return As a convenience, the <var>baseState</var> array you originally
14085 * passed into the function is returned.
14086 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014087 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014088 */
14089 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14090 final int N = baseState.length;
14091 int i = N - 1;
14092 while (i >= 0 && baseState[i] == 0) {
14093 i--;
14094 }
14095 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14096 return baseState;
14097 }
14098
14099 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014100 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14101 * on all Drawable objects associated with this view.
14102 */
14103 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014104 if (mBackground != null) {
14105 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014106 }
14107 }
14108
14109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014110 * Sets the background color for this view.
14111 * @param color the color of the background
14112 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014113 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014114 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014115 if (mBackground instanceof ColorDrawable) {
14116 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014117 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014118 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014120 }
14121
14122 /**
14123 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014124 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014125 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014127 * @attr ref android.R.styleable#View_background
14128 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014129 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014130 public void setBackgroundResource(int resid) {
14131 if (resid != 0 && resid == mBackgroundResource) {
14132 return;
14133 }
14134
14135 Drawable d= null;
14136 if (resid != 0) {
14137 d = mResources.getDrawable(resid);
14138 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014139 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014140
14141 mBackgroundResource = resid;
14142 }
14143
14144 /**
14145 * Set the background to a given Drawable, or remove the background. If the
14146 * background has padding, this View's padding is set to the background's
14147 * padding. However, when a background is removed, this View's padding isn't
14148 * touched. If setting the padding is desired, please use
14149 * {@link #setPadding(int, int, int, int)}.
14150 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014151 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014152 * background
14153 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014154 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014155 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014156 setBackgroundDrawable(background);
14157 }
14158
14159 /**
14160 * @deprecated use {@link #setBackground(Drawable)} instead
14161 */
14162 @Deprecated
14163 public void setBackgroundDrawable(Drawable background) {
14164 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014165 return;
14166 }
14167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014168 boolean requestLayout = false;
14169
14170 mBackgroundResource = 0;
14171
14172 /*
14173 * Regardless of whether we're setting a new background or not, we want
14174 * to clear the previous drawable.
14175 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014176 if (mBackground != null) {
14177 mBackground.setCallback(null);
14178 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179 }
14180
Philip Milne6c8ea062012-04-03 17:38:43 -070014181 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014182 Rect padding = sThreadLocal.get();
14183 if (padding == null) {
14184 padding = new Rect();
14185 sThreadLocal.set(padding);
14186 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014187 if (background.getPadding(padding)) {
14188 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014189 case LAYOUT_DIRECTION_RTL:
14190 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14191 break;
14192 case LAYOUT_DIRECTION_LTR:
14193 default:
14194 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014196 }
14197
14198 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14199 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014200 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14201 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 requestLayout = true;
14203 }
14204
Philip Milne6c8ea062012-04-03 17:38:43 -070014205 background.setCallback(this);
14206 if (background.isStateful()) {
14207 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014208 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014209 background.setVisible(getVisibility() == VISIBLE, false);
14210 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014211
14212 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14213 mPrivateFlags &= ~SKIP_DRAW;
14214 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14215 requestLayout = true;
14216 }
14217 } else {
14218 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014219 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220
14221 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14222 /*
14223 * This view ONLY drew the background before and we're removing
14224 * the background, so now it won't draw anything
14225 * (hence we SKIP_DRAW)
14226 */
14227 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14228 mPrivateFlags |= SKIP_DRAW;
14229 }
14230
14231 /*
14232 * When the background is set, we try to apply its padding to this
14233 * View. When the background is removed, we don't touch this View's
14234 * padding. This is noted in the Javadocs. Hence, we don't need to
14235 * requestLayout(), the invalidate() below is sufficient.
14236 */
14237
14238 // The old background's minimum size could have affected this
14239 // View's layout, so let's requestLayout
14240 requestLayout = true;
14241 }
14242
Romain Guy8f1344f52009-05-15 16:03:59 -070014243 computeOpaqueFlags();
14244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014245 if (requestLayout) {
14246 requestLayout();
14247 }
14248
14249 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014250 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 }
14252
14253 /**
14254 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014255 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014257 *
14258 * @see #setBackground(Drawable)
14259 *
14260 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014261 */
14262 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014263 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014264 }
14265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 /**
14267 * Sets the padding. The view may add on the space required to display
14268 * the scrollbars, depending on the style and visibility of the scrollbars.
14269 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14270 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14271 * from the values set in this call.
14272 *
14273 * @attr ref android.R.styleable#View_padding
14274 * @attr ref android.R.styleable#View_paddingBottom
14275 * @attr ref android.R.styleable#View_paddingLeft
14276 * @attr ref android.R.styleable#View_paddingRight
14277 * @attr ref android.R.styleable#View_paddingTop
14278 * @param left the left padding in pixels
14279 * @param top the top padding in pixels
14280 * @param right the right padding in pixels
14281 * @param bottom the bottom padding in pixels
14282 */
14283 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014284 mUserPaddingStart = -1;
14285 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014286 mUserPaddingRelative = false;
14287
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014288 internalSetPadding(left, top, right, bottom);
14289 }
14290
14291 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014292 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014293 mUserPaddingRight = right;
14294 mUserPaddingBottom = bottom;
14295
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014296 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014297 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014298
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014299 // Common case is there are no scroll bars.
14300 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014301 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014302 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014303 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014304 switch (mVerticalScrollbarPosition) {
14305 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014306 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14307 left += offset;
14308 } else {
14309 right += offset;
14310 }
14311 break;
Adam Powell20232d02010-12-08 21:08:53 -080014312 case SCROLLBAR_POSITION_RIGHT:
14313 right += offset;
14314 break;
14315 case SCROLLBAR_POSITION_LEFT:
14316 left += offset;
14317 break;
14318 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014319 }
Adam Powell20232d02010-12-08 21:08:53 -080014320 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014321 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14322 ? 0 : getHorizontalScrollbarHeight();
14323 }
14324 }
Romain Guy8506ab42009-06-11 17:35:47 -070014325
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014326 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 changed = true;
14328 mPaddingLeft = left;
14329 }
14330 if (mPaddingTop != top) {
14331 changed = true;
14332 mPaddingTop = top;
14333 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014334 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014335 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014336 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014337 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014338 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014339 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014340 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 }
14342
14343 if (changed) {
14344 requestLayout();
14345 }
14346 }
14347
14348 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014349 * Sets the relative padding. The view may add on the space required to display
14350 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014351 * from the values set in this call.
14352 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014353 * @param start the start padding in pixels
14354 * @param top the top padding in pixels
14355 * @param end the end padding in pixels
14356 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014357 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014358 */
14359 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014360 mUserPaddingStart = start;
14361 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014362 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014363
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014364 switch(getResolvedLayoutDirection()) {
14365 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014366 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014367 break;
14368 case LAYOUT_DIRECTION_LTR:
14369 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014370 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014371 }
14372 }
14373
14374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014375 * Returns the top padding of this view.
14376 *
14377 * @return the top padding in pixels
14378 */
14379 public int getPaddingTop() {
14380 return mPaddingTop;
14381 }
14382
14383 /**
14384 * Returns the bottom padding of this view. If there are inset and enabled
14385 * scrollbars, this value may include the space required to display the
14386 * scrollbars as well.
14387 *
14388 * @return the bottom padding in pixels
14389 */
14390 public int getPaddingBottom() {
14391 return mPaddingBottom;
14392 }
14393
14394 /**
14395 * Returns the left padding of this view. If there are inset and enabled
14396 * scrollbars, this value may include the space required to display the
14397 * scrollbars as well.
14398 *
14399 * @return the left padding in pixels
14400 */
14401 public int getPaddingLeft() {
14402 return mPaddingLeft;
14403 }
14404
14405 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014406 * Returns the start padding of this view depending on its resolved layout direction.
14407 * If there are inset and enabled scrollbars, this value may include the space
14408 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014409 *
14410 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014411 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014412 */
14413 public int getPaddingStart() {
14414 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14415 mPaddingRight : mPaddingLeft;
14416 }
14417
14418 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014419 * Returns the right padding of this view. If there are inset and enabled
14420 * scrollbars, this value may include the space required to display the
14421 * scrollbars as well.
14422 *
14423 * @return the right padding in pixels
14424 */
14425 public int getPaddingRight() {
14426 return mPaddingRight;
14427 }
14428
14429 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014430 * Returns the end padding of this view depending on its resolved layout direction.
14431 * If there are inset and enabled scrollbars, this value may include the space
14432 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014433 *
14434 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014435 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014436 */
14437 public int getPaddingEnd() {
14438 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14439 mPaddingLeft : mPaddingRight;
14440 }
14441
14442 /**
14443 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014444 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014445 *
14446 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070014447 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014448 */
14449 public boolean isPaddingRelative() {
14450 return mUserPaddingRelative;
14451 }
14452
14453 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014454 * @hide
14455 */
Philip Milne7a23b492012-04-24 22:12:36 -070014456 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014457 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014458 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014459 }
14460 return mLayoutInsets;
14461 }
14462
14463 /**
14464 * @hide
14465 */
14466 public void setLayoutInsets(Insets layoutInsets) {
14467 mLayoutInsets = layoutInsets;
14468 }
14469
14470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014471 * Changes the selection state of this view. A view can be selected or not.
14472 * Note that selection is not the same as focus. Views are typically
14473 * selected in the context of an AdapterView like ListView or GridView;
14474 * the selected view is the view that is highlighted.
14475 *
14476 * @param selected true if the view must be selected, false otherwise
14477 */
14478 public void setSelected(boolean selected) {
14479 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14480 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014481 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014482 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014483 refreshDrawableState();
14484 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014485 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14486 notifyAccessibilityStateChanged();
14487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014488 }
14489 }
14490
14491 /**
14492 * Dispatch setSelected to all of this View's children.
14493 *
14494 * @see #setSelected(boolean)
14495 *
14496 * @param selected The new selected state
14497 */
14498 protected void dispatchSetSelected(boolean selected) {
14499 }
14500
14501 /**
14502 * Indicates the selection state of this view.
14503 *
14504 * @return true if the view is selected, false otherwise
14505 */
14506 @ViewDebug.ExportedProperty
14507 public boolean isSelected() {
14508 return (mPrivateFlags & SELECTED) != 0;
14509 }
14510
14511 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014512 * Changes the activated state of this view. A view can be activated or not.
14513 * Note that activation is not the same as selection. Selection is
14514 * a transient property, representing the view (hierarchy) the user is
14515 * currently interacting with. Activation is a longer-term state that the
14516 * user can move views in and out of. For example, in a list view with
14517 * single or multiple selection enabled, the views in the current selection
14518 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14519 * here.) The activated state is propagated down to children of the view it
14520 * is set on.
14521 *
14522 * @param activated true if the view must be activated, false otherwise
14523 */
14524 public void setActivated(boolean activated) {
14525 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14526 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014527 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014528 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014529 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014530 }
14531 }
14532
14533 /**
14534 * Dispatch setActivated to all of this View's children.
14535 *
14536 * @see #setActivated(boolean)
14537 *
14538 * @param activated The new activated state
14539 */
14540 protected void dispatchSetActivated(boolean activated) {
14541 }
14542
14543 /**
14544 * Indicates the activation state of this view.
14545 *
14546 * @return true if the view is activated, false otherwise
14547 */
14548 @ViewDebug.ExportedProperty
14549 public boolean isActivated() {
14550 return (mPrivateFlags & ACTIVATED) != 0;
14551 }
14552
14553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014554 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14555 * observer can be used to get notifications when global events, like
14556 * layout, happen.
14557 *
14558 * The returned ViewTreeObserver observer is not guaranteed to remain
14559 * valid for the lifetime of this View. If the caller of this method keeps
14560 * a long-lived reference to ViewTreeObserver, it should always check for
14561 * the return value of {@link ViewTreeObserver#isAlive()}.
14562 *
14563 * @return The ViewTreeObserver for this view's hierarchy.
14564 */
14565 public ViewTreeObserver getViewTreeObserver() {
14566 if (mAttachInfo != null) {
14567 return mAttachInfo.mTreeObserver;
14568 }
14569 if (mFloatingTreeObserver == null) {
14570 mFloatingTreeObserver = new ViewTreeObserver();
14571 }
14572 return mFloatingTreeObserver;
14573 }
14574
14575 /**
14576 * <p>Finds the topmost view in the current view hierarchy.</p>
14577 *
14578 * @return the topmost view containing this view
14579 */
14580 public View getRootView() {
14581 if (mAttachInfo != null) {
14582 final View v = mAttachInfo.mRootView;
14583 if (v != null) {
14584 return v;
14585 }
14586 }
Romain Guy8506ab42009-06-11 17:35:47 -070014587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014588 View parent = this;
14589
14590 while (parent.mParent != null && parent.mParent instanceof View) {
14591 parent = (View) parent.mParent;
14592 }
14593
14594 return parent;
14595 }
14596
14597 /**
14598 * <p>Computes the coordinates of this view on the screen. The argument
14599 * must be an array of two integers. After the method returns, the array
14600 * contains the x and y location in that order.</p>
14601 *
14602 * @param location an array of two integers in which to hold the coordinates
14603 */
14604 public void getLocationOnScreen(int[] location) {
14605 getLocationInWindow(location);
14606
14607 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014608 if (info != null) {
14609 location[0] += info.mWindowLeft;
14610 location[1] += info.mWindowTop;
14611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014612 }
14613
14614 /**
14615 * <p>Computes the coordinates of this view in its window. The argument
14616 * must be an array of two integers. After the method returns, the array
14617 * contains the x and y location in that order.</p>
14618 *
14619 * @param location an array of two integers in which to hold the coordinates
14620 */
14621 public void getLocationInWindow(int[] location) {
14622 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014623 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 }
14625
Gilles Debunne6583ce52011-12-06 18:09:02 -080014626 if (mAttachInfo == null) {
14627 // When the view is not attached to a window, this method does not make sense
14628 location[0] = location[1] = 0;
14629 return;
14630 }
14631
Gilles Debunnecea45132011-11-24 02:19:27 +010014632 float[] position = mAttachInfo.mTmpTransformLocation;
14633 position[0] = position[1] = 0.0f;
14634
14635 if (!hasIdentityMatrix()) {
14636 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014638
Gilles Debunnecea45132011-11-24 02:19:27 +010014639 position[0] += mLeft;
14640 position[1] += mTop;
14641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642 ViewParent viewParent = mParent;
14643 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014644 final View view = (View) viewParent;
14645
14646 position[0] -= view.mScrollX;
14647 position[1] -= view.mScrollY;
14648
14649 if (!view.hasIdentityMatrix()) {
14650 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014651 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014652
14653 position[0] += view.mLeft;
14654 position[1] += view.mTop;
14655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014656 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014657 }
Romain Guy8506ab42009-06-11 17:35:47 -070014658
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014659 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014661 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14662 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014664
14665 location[0] = (int) (position[0] + 0.5f);
14666 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667 }
14668
14669 /**
14670 * {@hide}
14671 * @param id the id of the view to be found
14672 * @return the view of the specified id, null if cannot be found
14673 */
14674 protected View findViewTraversal(int id) {
14675 if (id == mID) {
14676 return this;
14677 }
14678 return null;
14679 }
14680
14681 /**
14682 * {@hide}
14683 * @param tag the tag of the view to be found
14684 * @return the view of specified tag, null if cannot be found
14685 */
14686 protected View findViewWithTagTraversal(Object tag) {
14687 if (tag != null && tag.equals(mTag)) {
14688 return this;
14689 }
14690 return null;
14691 }
14692
14693 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014694 * {@hide}
14695 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014696 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014697 * @return The first view that matches the predicate or null.
14698 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014699 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014700 if (predicate.apply(this)) {
14701 return this;
14702 }
14703 return null;
14704 }
14705
14706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014707 * Look for a child view with the given id. If this view has the given
14708 * id, return this view.
14709 *
14710 * @param id The id to search for.
14711 * @return The view that has the given id in the hierarchy or null
14712 */
14713 public final View findViewById(int id) {
14714 if (id < 0) {
14715 return null;
14716 }
14717 return findViewTraversal(id);
14718 }
14719
14720 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014721 * Finds a view by its unuque and stable accessibility id.
14722 *
14723 * @param accessibilityId The searched accessibility id.
14724 * @return The found view.
14725 */
14726 final View findViewByAccessibilityId(int accessibilityId) {
14727 if (accessibilityId < 0) {
14728 return null;
14729 }
14730 return findViewByAccessibilityIdTraversal(accessibilityId);
14731 }
14732
14733 /**
14734 * Performs the traversal to find a view by its unuque and stable accessibility id.
14735 *
14736 * <strong>Note:</strong>This method does not stop at the root namespace
14737 * boundary since the user can touch the screen at an arbitrary location
14738 * potentially crossing the root namespace bounday which will send an
14739 * accessibility event to accessibility services and they should be able
14740 * to obtain the event source. Also accessibility ids are guaranteed to be
14741 * unique in the window.
14742 *
14743 * @param accessibilityId The accessibility id.
14744 * @return The found view.
14745 */
14746 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14747 if (getAccessibilityViewId() == accessibilityId) {
14748 return this;
14749 }
14750 return null;
14751 }
14752
14753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014754 * Look for a child view with the given tag. If this view has the given
14755 * tag, return this view.
14756 *
14757 * @param tag The tag to search for, using "tag.equals(getTag())".
14758 * @return The View that has the given tag in the hierarchy or null
14759 */
14760 public final View findViewWithTag(Object tag) {
14761 if (tag == null) {
14762 return null;
14763 }
14764 return findViewWithTagTraversal(tag);
14765 }
14766
14767 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014768 * {@hide}
14769 * Look for a child view that matches the specified predicate.
14770 * If this view matches the predicate, return this view.
14771 *
14772 * @param predicate The predicate to evaluate.
14773 * @return The first view that matches the predicate or null.
14774 */
14775 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014776 return findViewByPredicateTraversal(predicate, null);
14777 }
14778
14779 /**
14780 * {@hide}
14781 * Look for a child view that matches the specified predicate,
14782 * starting with the specified view and its descendents and then
14783 * recusively searching the ancestors and siblings of that view
14784 * until this view is reached.
14785 *
14786 * This method is useful in cases where the predicate does not match
14787 * a single unique view (perhaps multiple views use the same id)
14788 * and we are trying to find the view that is "closest" in scope to the
14789 * starting view.
14790 *
14791 * @param start The view to start from.
14792 * @param predicate The predicate to evaluate.
14793 * @return The first view that matches the predicate or null.
14794 */
14795 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14796 View childToSkip = null;
14797 for (;;) {
14798 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14799 if (view != null || start == this) {
14800 return view;
14801 }
14802
14803 ViewParent parent = start.getParent();
14804 if (parent == null || !(parent instanceof View)) {
14805 return null;
14806 }
14807
14808 childToSkip = start;
14809 start = (View) parent;
14810 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014811 }
14812
14813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014814 * Sets the identifier for this view. The identifier does not have to be
14815 * unique in this view's hierarchy. The identifier should be a positive
14816 * number.
14817 *
14818 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014819 * @see #getId()
14820 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014821 *
14822 * @param id a number used to identify the view
14823 *
14824 * @attr ref android.R.styleable#View_id
14825 */
14826 public void setId(int id) {
14827 mID = id;
14828 }
14829
14830 /**
14831 * {@hide}
14832 *
14833 * @param isRoot true if the view belongs to the root namespace, false
14834 * otherwise
14835 */
14836 public void setIsRootNamespace(boolean isRoot) {
14837 if (isRoot) {
14838 mPrivateFlags |= IS_ROOT_NAMESPACE;
14839 } else {
14840 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14841 }
14842 }
14843
14844 /**
14845 * {@hide}
14846 *
14847 * @return true if the view belongs to the root namespace, false otherwise
14848 */
14849 public boolean isRootNamespace() {
14850 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14851 }
14852
14853 /**
14854 * Returns this view's identifier.
14855 *
14856 * @return a positive integer used to identify the view or {@link #NO_ID}
14857 * if the view has no ID
14858 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014859 * @see #setId(int)
14860 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014861 * @attr ref android.R.styleable#View_id
14862 */
14863 @ViewDebug.CapturedViewProperty
14864 public int getId() {
14865 return mID;
14866 }
14867
14868 /**
14869 * Returns this view's tag.
14870 *
14871 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014872 *
14873 * @see #setTag(Object)
14874 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014875 */
14876 @ViewDebug.ExportedProperty
14877 public Object getTag() {
14878 return mTag;
14879 }
14880
14881 /**
14882 * Sets the tag associated with this view. A tag can be used to mark
14883 * a view in its hierarchy and does not have to be unique within the
14884 * hierarchy. Tags can also be used to store data within a view without
14885 * resorting to another data structure.
14886 *
14887 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014888 *
14889 * @see #getTag()
14890 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014891 */
14892 public void setTag(final Object tag) {
14893 mTag = tag;
14894 }
14895
14896 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014897 * Returns the tag associated with this view and the specified key.
14898 *
14899 * @param key The key identifying the tag
14900 *
14901 * @return the Object stored in this view as a tag
14902 *
14903 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014904 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014905 */
14906 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014907 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014908 return null;
14909 }
14910
14911 /**
14912 * Sets a tag associated with this view and a key. A tag can be used
14913 * to mark a view in its hierarchy and does not have to be unique within
14914 * the hierarchy. Tags can also be used to store data within a view
14915 * without resorting to another data structure.
14916 *
14917 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014918 * application to ensure it is unique (see the <a
14919 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14920 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014921 * the Android framework or not associated with any package will cause
14922 * an {@link IllegalArgumentException} to be thrown.
14923 *
14924 * @param key The key identifying the tag
14925 * @param tag An Object to tag the view with
14926 *
14927 * @throws IllegalArgumentException If they specified key is not valid
14928 *
14929 * @see #setTag(Object)
14930 * @see #getTag(int)
14931 */
14932 public void setTag(int key, final Object tag) {
14933 // If the package id is 0x00 or 0x01, it's either an undefined package
14934 // or a framework id
14935 if ((key >>> 24) < 2) {
14936 throw new IllegalArgumentException("The key must be an application-specific "
14937 + "resource id.");
14938 }
14939
Adam Powell2b2f6d62011-09-23 11:15:39 -070014940 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014941 }
14942
14943 /**
14944 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14945 * framework id.
14946 *
14947 * @hide
14948 */
14949 public void setTagInternal(int key, Object tag) {
14950 if ((key >>> 24) != 0x1) {
14951 throw new IllegalArgumentException("The key must be a framework-specific "
14952 + "resource id.");
14953 }
14954
Adam Powell2b2f6d62011-09-23 11:15:39 -070014955 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014956 }
14957
Adam Powell2b2f6d62011-09-23 11:15:39 -070014958 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014959 if (mKeyedTags == null) {
14960 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014961 }
14962
Adam Powell7db82ac2011-09-22 19:44:04 -070014963 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014964 }
14965
14966 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014967 * Prints information about this view in the log output, with the tag
14968 * {@link #VIEW_LOG_TAG}.
14969 *
14970 * @hide
14971 */
14972 public void debug() {
14973 debug(0);
14974 }
14975
14976 /**
14977 * Prints information about this view in the log output, with the tag
14978 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14979 * indentation defined by the <code>depth</code>.
14980 *
14981 * @param depth the indentation level
14982 *
14983 * @hide
14984 */
14985 protected void debug(int depth) {
14986 String output = debugIndent(depth - 1);
14987
14988 output += "+ " + this;
14989 int id = getId();
14990 if (id != -1) {
14991 output += " (id=" + id + ")";
14992 }
14993 Object tag = getTag();
14994 if (tag != null) {
14995 output += " (tag=" + tag + ")";
14996 }
14997 Log.d(VIEW_LOG_TAG, output);
14998
14999 if ((mPrivateFlags & FOCUSED) != 0) {
15000 output = debugIndent(depth) + " FOCUSED";
15001 Log.d(VIEW_LOG_TAG, output);
15002 }
15003
15004 output = debugIndent(depth);
15005 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15006 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15007 + "} ";
15008 Log.d(VIEW_LOG_TAG, output);
15009
15010 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15011 || mPaddingBottom != 0) {
15012 output = debugIndent(depth);
15013 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15014 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15015 Log.d(VIEW_LOG_TAG, output);
15016 }
15017
15018 output = debugIndent(depth);
15019 output += "mMeasureWidth=" + mMeasuredWidth +
15020 " mMeasureHeight=" + mMeasuredHeight;
15021 Log.d(VIEW_LOG_TAG, output);
15022
15023 output = debugIndent(depth);
15024 if (mLayoutParams == null) {
15025 output += "BAD! no layout params";
15026 } else {
15027 output = mLayoutParams.debug(output);
15028 }
15029 Log.d(VIEW_LOG_TAG, output);
15030
15031 output = debugIndent(depth);
15032 output += "flags={";
15033 output += View.printFlags(mViewFlags);
15034 output += "}";
15035 Log.d(VIEW_LOG_TAG, output);
15036
15037 output = debugIndent(depth);
15038 output += "privateFlags={";
15039 output += View.printPrivateFlags(mPrivateFlags);
15040 output += "}";
15041 Log.d(VIEW_LOG_TAG, output);
15042 }
15043
15044 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015045 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015046 *
15047 * @param depth the indentation level
15048 * @return a String containing (depth * 2 + 3) * 2 white spaces
15049 *
15050 * @hide
15051 */
15052 protected static String debugIndent(int depth) {
15053 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15054 for (int i = 0; i < (depth * 2) + 3; i++) {
15055 spaces.append(' ').append(' ');
15056 }
15057 return spaces.toString();
15058 }
15059
15060 /**
15061 * <p>Return the offset of the widget's text baseline from the widget's top
15062 * boundary. If this widget does not support baseline alignment, this
15063 * method returns -1. </p>
15064 *
15065 * @return the offset of the baseline within the widget's bounds or -1
15066 * if baseline alignment is not supported
15067 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015068 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015069 public int getBaseline() {
15070 return -1;
15071 }
15072
15073 /**
15074 * Call this when something has changed which has invalidated the
15075 * layout of this view. This will schedule a layout pass of the view
15076 * tree.
15077 */
15078 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015079 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015080 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015081
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015082 if (mLayoutParams != null) {
15083 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15084 }
15085
15086 if (mParent != null && !mParent.isLayoutRequested()) {
15087 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088 }
15089 }
15090
15091 /**
15092 * Forces this view to be laid out during the next layout pass.
15093 * This method does not call requestLayout() or forceLayout()
15094 * on the parent.
15095 */
15096 public void forceLayout() {
15097 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015098 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099 }
15100
15101 /**
15102 * <p>
15103 * This is called to find out how big a view should be. The parent
15104 * supplies constraint information in the width and height parameters.
15105 * </p>
15106 *
15107 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015108 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015109 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015110 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015111 * </p>
15112 *
15113 *
15114 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15115 * parent
15116 * @param heightMeasureSpec Vertical space requirements as imposed by the
15117 * parent
15118 *
15119 * @see #onMeasure(int, int)
15120 */
15121 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15122 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15123 widthMeasureSpec != mOldWidthMeasureSpec ||
15124 heightMeasureSpec != mOldHeightMeasureSpec) {
15125
15126 // first clears the measured dimension flag
15127 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015129 // measure ourselves, this should set the measured dimension flag back
15130 onMeasure(widthMeasureSpec, heightMeasureSpec);
15131
15132 // flag not set, setMeasuredDimension() was not invoked, we raise
15133 // an exception to warn the developer
15134 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15135 throw new IllegalStateException("onMeasure() did not set the"
15136 + " measured dimension by calling"
15137 + " setMeasuredDimension()");
15138 }
15139
15140 mPrivateFlags |= LAYOUT_REQUIRED;
15141 }
15142
15143 mOldWidthMeasureSpec = widthMeasureSpec;
15144 mOldHeightMeasureSpec = heightMeasureSpec;
15145 }
15146
15147 /**
15148 * <p>
15149 * Measure the view and its content to determine the measured width and the
15150 * measured height. This method is invoked by {@link #measure(int, int)} and
15151 * should be overriden by subclasses to provide accurate and efficient
15152 * measurement of their contents.
15153 * </p>
15154 *
15155 * <p>
15156 * <strong>CONTRACT:</strong> When overriding this method, you
15157 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15158 * measured width and height of this view. Failure to do so will trigger an
15159 * <code>IllegalStateException</code>, thrown by
15160 * {@link #measure(int, int)}. Calling the superclass'
15161 * {@link #onMeasure(int, int)} is a valid use.
15162 * </p>
15163 *
15164 * <p>
15165 * The base class implementation of measure defaults to the background size,
15166 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15167 * override {@link #onMeasure(int, int)} to provide better measurements of
15168 * their content.
15169 * </p>
15170 *
15171 * <p>
15172 * If this method is overridden, it is the subclass's responsibility to make
15173 * sure the measured height and width are at least the view's minimum height
15174 * and width ({@link #getSuggestedMinimumHeight()} and
15175 * {@link #getSuggestedMinimumWidth()}).
15176 * </p>
15177 *
15178 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15179 * The requirements are encoded with
15180 * {@link android.view.View.MeasureSpec}.
15181 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15182 * The requirements are encoded with
15183 * {@link android.view.View.MeasureSpec}.
15184 *
15185 * @see #getMeasuredWidth()
15186 * @see #getMeasuredHeight()
15187 * @see #setMeasuredDimension(int, int)
15188 * @see #getSuggestedMinimumHeight()
15189 * @see #getSuggestedMinimumWidth()
15190 * @see android.view.View.MeasureSpec#getMode(int)
15191 * @see android.view.View.MeasureSpec#getSize(int)
15192 */
15193 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15194 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15195 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15196 }
15197
15198 /**
15199 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15200 * measured width and measured height. Failing to do so will trigger an
15201 * exception at measurement time.</p>
15202 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015203 * @param measuredWidth The measured width of this view. May be a complex
15204 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15205 * {@link #MEASURED_STATE_TOO_SMALL}.
15206 * @param measuredHeight The measured height of this view. May be a complex
15207 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15208 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015209 */
15210 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15211 mMeasuredWidth = measuredWidth;
15212 mMeasuredHeight = measuredHeight;
15213
15214 mPrivateFlags |= MEASURED_DIMENSION_SET;
15215 }
15216
15217 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015218 * Merge two states as returned by {@link #getMeasuredState()}.
15219 * @param curState The current state as returned from a view or the result
15220 * of combining multiple views.
15221 * @param newState The new view state to combine.
15222 * @return Returns a new integer reflecting the combination of the two
15223 * states.
15224 */
15225 public static int combineMeasuredStates(int curState, int newState) {
15226 return curState | newState;
15227 }
15228
15229 /**
15230 * Version of {@link #resolveSizeAndState(int, int, int)}
15231 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15232 */
15233 public static int resolveSize(int size, int measureSpec) {
15234 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15235 }
15236
15237 /**
15238 * Utility to reconcile a desired size and state, with constraints imposed
15239 * by a MeasureSpec. Will take the desired size, unless a different size
15240 * is imposed by the constraints. The returned value is a compound integer,
15241 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15242 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15243 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015244 *
15245 * @param size How big the view wants to be
15246 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015247 * @return Size information bit mask as defined by
15248 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015249 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015250 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015251 int result = size;
15252 int specMode = MeasureSpec.getMode(measureSpec);
15253 int specSize = MeasureSpec.getSize(measureSpec);
15254 switch (specMode) {
15255 case MeasureSpec.UNSPECIFIED:
15256 result = size;
15257 break;
15258 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015259 if (specSize < size) {
15260 result = specSize | MEASURED_STATE_TOO_SMALL;
15261 } else {
15262 result = size;
15263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015264 break;
15265 case MeasureSpec.EXACTLY:
15266 result = specSize;
15267 break;
15268 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015269 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015270 }
15271
15272 /**
15273 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015274 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015275 * by the MeasureSpec.
15276 *
15277 * @param size Default size for this view
15278 * @param measureSpec Constraints imposed by the parent
15279 * @return The size this view should be.
15280 */
15281 public static int getDefaultSize(int size, int measureSpec) {
15282 int result = size;
15283 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015284 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015285
15286 switch (specMode) {
15287 case MeasureSpec.UNSPECIFIED:
15288 result = size;
15289 break;
15290 case MeasureSpec.AT_MOST:
15291 case MeasureSpec.EXACTLY:
15292 result = specSize;
15293 break;
15294 }
15295 return result;
15296 }
15297
15298 /**
15299 * Returns the suggested minimum height that the view should use. This
15300 * returns the maximum of the view's minimum height
15301 * and the background's minimum height
15302 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15303 * <p>
15304 * When being used in {@link #onMeasure(int, int)}, the caller should still
15305 * ensure the returned height is within the requirements of the parent.
15306 *
15307 * @return The suggested minimum height of the view.
15308 */
15309 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015310 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015312 }
15313
15314 /**
15315 * Returns the suggested minimum width that the view should use. This
15316 * returns the maximum of the view's minimum width)
15317 * and the background's minimum width
15318 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15319 * <p>
15320 * When being used in {@link #onMeasure(int, int)}, the caller should still
15321 * ensure the returned width is within the requirements of the parent.
15322 *
15323 * @return The suggested minimum width of the view.
15324 */
15325 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015326 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015328
Philip Milne6c8ea062012-04-03 17:38:43 -070015329 /**
15330 * Returns the minimum height of the view.
15331 *
15332 * @return the minimum height the view will try to be.
15333 *
15334 * @see #setMinimumHeight(int)
15335 *
15336 * @attr ref android.R.styleable#View_minHeight
15337 */
15338 public int getMinimumHeight() {
15339 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015340 }
15341
15342 /**
15343 * Sets the minimum height of the view. It is not guaranteed the view will
15344 * be able to achieve this minimum height (for example, if its parent layout
15345 * constrains it with less available height).
15346 *
15347 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015348 *
15349 * @see #getMinimumHeight()
15350 *
15351 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015352 */
15353 public void setMinimumHeight(int minHeight) {
15354 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015355 requestLayout();
15356 }
15357
15358 /**
15359 * Returns the minimum width of the view.
15360 *
15361 * @return the minimum width the view will try to be.
15362 *
15363 * @see #setMinimumWidth(int)
15364 *
15365 * @attr ref android.R.styleable#View_minWidth
15366 */
15367 public int getMinimumWidth() {
15368 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015369 }
15370
15371 /**
15372 * Sets the minimum width of the view. It is not guaranteed the view will
15373 * be able to achieve this minimum width (for example, if its parent layout
15374 * constrains it with less available width).
15375 *
15376 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015377 *
15378 * @see #getMinimumWidth()
15379 *
15380 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015381 */
15382 public void setMinimumWidth(int minWidth) {
15383 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015384 requestLayout();
15385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015386 }
15387
15388 /**
15389 * Get the animation currently associated with this view.
15390 *
15391 * @return The animation that is currently playing or
15392 * scheduled to play for this view.
15393 */
15394 public Animation getAnimation() {
15395 return mCurrentAnimation;
15396 }
15397
15398 /**
15399 * Start the specified animation now.
15400 *
15401 * @param animation the animation to start now
15402 */
15403 public void startAnimation(Animation animation) {
15404 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15405 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015406 invalidateParentCaches();
15407 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015408 }
15409
15410 /**
15411 * Cancels any animations for this view.
15412 */
15413 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015414 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015415 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015418 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015419 }
15420
15421 /**
15422 * Sets the next animation to play for this view.
15423 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015424 * {@link #startAnimation(android.view.animation.Animation)} instead.
15425 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015426 * control over the start time and invalidation, but you
15427 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015428 * 2) the view's parent (which controls animations on its children)
15429 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015430 * start.
15431 *
15432 * @param animation The next animation, or null.
15433 */
15434 public void setAnimation(Animation animation) {
15435 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015437 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015438 // If the screen is off assume the animation start time is now instead of
15439 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15440 // would cause the animation to start when the screen turns back on
15441 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15442 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15443 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015445 animation.reset();
15446 }
15447 }
15448
15449 /**
15450 * Invoked by a parent ViewGroup to notify the start of the animation
15451 * currently associated with this view. If you override this method,
15452 * always call super.onAnimationStart();
15453 *
15454 * @see #setAnimation(android.view.animation.Animation)
15455 * @see #getAnimation()
15456 */
15457 protected void onAnimationStart() {
15458 mPrivateFlags |= ANIMATION_STARTED;
15459 }
15460
15461 /**
15462 * Invoked by a parent ViewGroup to notify the end of the animation
15463 * currently associated with this view. If you override this method,
15464 * always call super.onAnimationEnd();
15465 *
15466 * @see #setAnimation(android.view.animation.Animation)
15467 * @see #getAnimation()
15468 */
15469 protected void onAnimationEnd() {
15470 mPrivateFlags &= ~ANIMATION_STARTED;
15471 }
15472
15473 /**
15474 * Invoked if there is a Transform that involves alpha. Subclass that can
15475 * draw themselves with the specified alpha should return true, and then
15476 * respect that alpha when their onDraw() is called. If this returns false
15477 * then the view may be redirected to draw into an offscreen buffer to
15478 * fulfill the request, which will look fine, but may be slower than if the
15479 * subclass handles it internally. The default implementation returns false.
15480 *
15481 * @param alpha The alpha (0..255) to apply to the view's drawing
15482 * @return true if the view can draw with the specified alpha.
15483 */
15484 protected boolean onSetAlpha(int alpha) {
15485 return false;
15486 }
15487
15488 /**
15489 * This is used by the RootView to perform an optimization when
15490 * the view hierarchy contains one or several SurfaceView.
15491 * SurfaceView is always considered transparent, but its children are not,
15492 * therefore all View objects remove themselves from the global transparent
15493 * region (passed as a parameter to this function).
15494 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015495 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015496 *
15497 * @return Returns true if the effective visibility of the view at this
15498 * point is opaque, regardless of the transparent region; returns false
15499 * if it is possible for underlying windows to be seen behind the view.
15500 *
15501 * {@hide}
15502 */
15503 public boolean gatherTransparentRegion(Region region) {
15504 final AttachInfo attachInfo = mAttachInfo;
15505 if (region != null && attachInfo != null) {
15506 final int pflags = mPrivateFlags;
15507 if ((pflags & SKIP_DRAW) == 0) {
15508 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15509 // remove it from the transparent region.
15510 final int[] location = attachInfo.mTransparentLocation;
15511 getLocationInWindow(location);
15512 region.op(location[0], location[1], location[0] + mRight - mLeft,
15513 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015514 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15516 // exists, so we remove the background drawable's non-transparent
15517 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015518 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015519 }
15520 }
15521 return true;
15522 }
15523
15524 /**
15525 * Play a sound effect for this view.
15526 *
15527 * <p>The framework will play sound effects for some built in actions, such as
15528 * clicking, but you may wish to play these effects in your widget,
15529 * for instance, for internal navigation.
15530 *
15531 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15532 * {@link #isSoundEffectsEnabled()} is true.
15533 *
15534 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15535 */
15536 public void playSoundEffect(int soundConstant) {
15537 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15538 return;
15539 }
15540 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15541 }
15542
15543 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015544 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015545 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015546 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015547 *
15548 * <p>The framework will provide haptic feedback for some built in actions,
15549 * such as long presses, but you may wish to provide feedback for your
15550 * own widget.
15551 *
15552 * <p>The feedback will only be performed if
15553 * {@link #isHapticFeedbackEnabled()} is true.
15554 *
15555 * @param feedbackConstant One of the constants defined in
15556 * {@link HapticFeedbackConstants}
15557 */
15558 public boolean performHapticFeedback(int feedbackConstant) {
15559 return performHapticFeedback(feedbackConstant, 0);
15560 }
15561
15562 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015563 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015564 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015565 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015566 *
15567 * @param feedbackConstant One of the constants defined in
15568 * {@link HapticFeedbackConstants}
15569 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15570 */
15571 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15572 if (mAttachInfo == null) {
15573 return false;
15574 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015575 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015576 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015577 && !isHapticFeedbackEnabled()) {
15578 return false;
15579 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015580 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15581 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015582 }
15583
15584 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015585 * Request that the visibility of the status bar or other screen/window
15586 * decorations be changed.
15587 *
15588 * <p>This method is used to put the over device UI into temporary modes
15589 * where the user's attention is focused more on the application content,
15590 * by dimming or hiding surrounding system affordances. This is typically
15591 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15592 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15593 * to be placed behind the action bar (and with these flags other system
15594 * affordances) so that smooth transitions between hiding and showing them
15595 * can be done.
15596 *
15597 * <p>Two representative examples of the use of system UI visibility is
15598 * implementing a content browsing application (like a magazine reader)
15599 * and a video playing application.
15600 *
15601 * <p>The first code shows a typical implementation of a View in a content
15602 * browsing application. In this implementation, the application goes
15603 * into a content-oriented mode by hiding the status bar and action bar,
15604 * and putting the navigation elements into lights out mode. The user can
15605 * then interact with content while in this mode. Such an application should
15606 * provide an easy way for the user to toggle out of the mode (such as to
15607 * check information in the status bar or access notifications). In the
15608 * implementation here, this is done simply by tapping on the content.
15609 *
15610 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15611 * content}
15612 *
15613 * <p>This second code sample shows a typical implementation of a View
15614 * in a video playing application. In this situation, while the video is
15615 * playing the application would like to go into a complete full-screen mode,
15616 * to use as much of the display as possible for the video. When in this state
15617 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015618 * touching on the screen to pop the UI out of full screen mode. See
15619 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015620 *
15621 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15622 * content}
15623 *
15624 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15625 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15626 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15627 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015628 */
15629 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015630 if (visibility != mSystemUiVisibility) {
15631 mSystemUiVisibility = visibility;
15632 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15633 mParent.recomputeViewAttributes(this);
15634 }
Joe Onorato664644d2011-01-23 17:53:23 -080015635 }
15636 }
15637
15638 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015639 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15640 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15641 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15642 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15643 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015644 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015645 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015646 return mSystemUiVisibility;
15647 }
15648
Scott Mainec6331b2011-05-24 16:55:56 -070015649 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015650 * Returns the current system UI visibility that is currently set for
15651 * the entire window. This is the combination of the
15652 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15653 * views in the window.
15654 */
15655 public int getWindowSystemUiVisibility() {
15656 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15657 }
15658
15659 /**
15660 * Override to find out when the window's requested system UI visibility
15661 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15662 * This is different from the callbacks recieved through
15663 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15664 * in that this is only telling you about the local request of the window,
15665 * not the actual values applied by the system.
15666 */
15667 public void onWindowSystemUiVisibilityChanged(int visible) {
15668 }
15669
15670 /**
15671 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15672 * the view hierarchy.
15673 */
15674 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15675 onWindowSystemUiVisibilityChanged(visible);
15676 }
15677
15678 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015679 * Set a listener to receive callbacks when the visibility of the system bar changes.
15680 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15681 */
Joe Onorato664644d2011-01-23 17:53:23 -080015682 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015683 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015684 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15685 mParent.recomputeViewAttributes(this);
15686 }
15687 }
15688
15689 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015690 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15691 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015692 */
15693 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015694 ListenerInfo li = mListenerInfo;
15695 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15696 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015697 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015698 }
15699 }
15700
Dianne Hackborncf675782012-05-10 15:07:24 -070015701 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015702 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15703 if (val != mSystemUiVisibility) {
15704 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015705 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015706 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015707 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015708 }
15709
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015710 /** @hide */
15711 public void setDisabledSystemUiVisibility(int flags) {
15712 if (mAttachInfo != null) {
15713 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15714 mAttachInfo.mDisabledSystemUiVisibility = flags;
15715 if (mParent != null) {
15716 mParent.recomputeViewAttributes(this);
15717 }
15718 }
15719 }
15720 }
15721
Joe Onorato664644d2011-01-23 17:53:23 -080015722 /**
Joe Malin32736f02011-01-19 16:14:20 -080015723 * Creates an image that the system displays during the drag and drop
15724 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15725 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15726 * appearance as the given View. The default also positions the center of the drag shadow
15727 * directly under the touch point. If no View is provided (the constructor with no parameters
15728 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15729 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15730 * default is an invisible drag shadow.
15731 * <p>
15732 * You are not required to use the View you provide to the constructor as the basis of the
15733 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15734 * anything you want as the drag shadow.
15735 * </p>
15736 * <p>
15737 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15738 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15739 * size and position of the drag shadow. It uses this data to construct a
15740 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15741 * so that your application can draw the shadow image in the Canvas.
15742 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015743 *
15744 * <div class="special reference">
15745 * <h3>Developer Guides</h3>
15746 * <p>For a guide to implementing drag and drop features, read the
15747 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15748 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015749 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015750 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015751 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015752
15753 /**
Joe Malin32736f02011-01-19 16:14:20 -080015754 * Constructs a shadow image builder based on a View. By default, the resulting drag
15755 * shadow will have the same appearance and dimensions as the View, with the touch point
15756 * over the center of the View.
15757 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015758 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015759 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015760 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015761 }
15762
Christopher Tate17ed60c2011-01-18 12:50:26 -080015763 /**
15764 * Construct a shadow builder object with no associated View. This
15765 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15766 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15767 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015768 * reference to any View object. If they are not overridden, then the result is an
15769 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015770 */
15771 public DragShadowBuilder() {
15772 mView = new WeakReference<View>(null);
15773 }
15774
15775 /**
15776 * Returns the View object that had been passed to the
15777 * {@link #View.DragShadowBuilder(View)}
15778 * constructor. If that View parameter was {@code null} or if the
15779 * {@link #View.DragShadowBuilder()}
15780 * constructor was used to instantiate the builder object, this method will return
15781 * null.
15782 *
15783 * @return The View object associate with this builder object.
15784 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015785 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015786 final public View getView() {
15787 return mView.get();
15788 }
15789
Christopher Tate2c095f32010-10-04 14:13:40 -070015790 /**
Joe Malin32736f02011-01-19 16:14:20 -080015791 * Provides the metrics for the shadow image. These include the dimensions of
15792 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015793 * be centered under the touch location while dragging.
15794 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015795 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015796 * same as the dimensions of the View itself and centers the shadow under
15797 * the touch point.
15798 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015799 *
Joe Malin32736f02011-01-19 16:14:20 -080015800 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15801 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15802 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15803 * image.
15804 *
15805 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15806 * shadow image that should be underneath the touch point during the drag and drop
15807 * operation. Your application must set {@link android.graphics.Point#x} to the
15808 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015809 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015810 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015811 final View view = mView.get();
15812 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015813 shadowSize.set(view.getWidth(), view.getHeight());
15814 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015815 } else {
15816 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15817 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015818 }
15819
15820 /**
Joe Malin32736f02011-01-19 16:14:20 -080015821 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15822 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015823 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015824 *
Joe Malin32736f02011-01-19 16:14:20 -080015825 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015826 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015827 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015828 final View view = mView.get();
15829 if (view != null) {
15830 view.draw(canvas);
15831 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015832 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015833 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015834 }
15835 }
15836
15837 /**
Joe Malin32736f02011-01-19 16:14:20 -080015838 * Starts a drag and drop operation. When your application calls this method, it passes a
15839 * {@link android.view.View.DragShadowBuilder} object to the system. The
15840 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15841 * to get metrics for the drag shadow, and then calls the object's
15842 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15843 * <p>
15844 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15845 * drag events to all the View objects in your application that are currently visible. It does
15846 * this either by calling the View object's drag listener (an implementation of
15847 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15848 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15849 * Both are passed a {@link android.view.DragEvent} object that has a
15850 * {@link android.view.DragEvent#getAction()} value of
15851 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15852 * </p>
15853 * <p>
15854 * Your application can invoke startDrag() on any attached View object. The View object does not
15855 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15856 * be related to the View the user selected for dragging.
15857 * </p>
15858 * @param data A {@link android.content.ClipData} object pointing to the data to be
15859 * transferred by the drag and drop operation.
15860 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15861 * drag shadow.
15862 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15863 * drop operation. This Object is put into every DragEvent object sent by the system during the
15864 * current drag.
15865 * <p>
15866 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15867 * to the target Views. For example, it can contain flags that differentiate between a
15868 * a copy operation and a move operation.
15869 * </p>
15870 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15871 * so the parameter should be set to 0.
15872 * @return {@code true} if the method completes successfully, or
15873 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15874 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015875 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015876 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015877 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015878 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015879 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015880 }
15881 boolean okay = false;
15882
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015883 Point shadowSize = new Point();
15884 Point shadowTouchPoint = new Point();
15885 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015886
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015887 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15888 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15889 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015890 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015891
Chris Tatea32dcf72010-10-14 12:13:50 -070015892 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015893 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15894 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015895 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015896 Surface surface = new Surface();
15897 try {
15898 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015899 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015900 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015901 + " surface=" + surface);
15902 if (token != null) {
15903 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015904 try {
Chris Tate6b391282010-10-14 15:48:59 -070015905 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015906 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015907 } finally {
15908 surface.unlockCanvasAndPost(canvas);
15909 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015910
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015911 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015912
15913 // Cache the local state object for delivery with DragEvents
15914 root.setLocalDragState(myLocalState);
15915
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015916 // repurpose 'shadowSize' for the last touch point
15917 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015918
Christopher Tatea53146c2010-09-07 11:57:52 -070015919 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015920 shadowSize.x, shadowSize.y,
15921 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015922 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015923
15924 // Off and running! Release our local surface instance; the drag
15925 // shadow surface is now managed by the system process.
15926 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015927 }
15928 } catch (Exception e) {
15929 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15930 surface.destroy();
15931 }
15932
15933 return okay;
15934 }
15935
Christopher Tatea53146c2010-09-07 11:57:52 -070015936 /**
Joe Malin32736f02011-01-19 16:14:20 -080015937 * Handles drag events sent by the system following a call to
15938 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15939 *<p>
15940 * When the system calls this method, it passes a
15941 * {@link android.view.DragEvent} object. A call to
15942 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15943 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15944 * operation.
15945 * @param event The {@link android.view.DragEvent} sent by the system.
15946 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15947 * in DragEvent, indicating the type of drag event represented by this object.
15948 * @return {@code true} if the method was successful, otherwise {@code false}.
15949 * <p>
15950 * The method should return {@code true} in response to an action type of
15951 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15952 * operation.
15953 * </p>
15954 * <p>
15955 * The method should also return {@code true} in response to an action type of
15956 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15957 * {@code false} if it didn't.
15958 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015959 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015960 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015961 return false;
15962 }
15963
15964 /**
Joe Malin32736f02011-01-19 16:14:20 -080015965 * Detects if this View is enabled and has a drag event listener.
15966 * If both are true, then it calls the drag event listener with the
15967 * {@link android.view.DragEvent} it received. If the drag event listener returns
15968 * {@code true}, then dispatchDragEvent() returns {@code true}.
15969 * <p>
15970 * For all other cases, the method calls the
15971 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15972 * method and returns its result.
15973 * </p>
15974 * <p>
15975 * This ensures that a drag event is always consumed, even if the View does not have a drag
15976 * event listener. However, if the View has a listener and the listener returns true, then
15977 * onDragEvent() is not called.
15978 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015979 */
15980 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015981 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015982 ListenerInfo li = mListenerInfo;
15983 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15984 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015985 return true;
15986 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015987 return onDragEvent(event);
15988 }
15989
Christopher Tate3d4bf172011-03-28 16:16:46 -070015990 boolean canAcceptDrag() {
15991 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15992 }
15993
Christopher Tatea53146c2010-09-07 11:57:52 -070015994 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015995 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15996 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015997 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015998 */
15999 public void onCloseSystemDialogs(String reason) {
16000 }
Joe Malin32736f02011-01-19 16:14:20 -080016001
Dianne Hackbornffa42482009-09-23 22:20:11 -070016002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016003 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016004 * update a Region being computed for
16005 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016006 * that any non-transparent parts of the Drawable are removed from the
16007 * given transparent region.
16008 *
16009 * @param dr The Drawable whose transparency is to be applied to the region.
16010 * @param region A Region holding the current transparency information,
16011 * where any parts of the region that are set are considered to be
16012 * transparent. On return, this region will be modified to have the
16013 * transparency information reduced by the corresponding parts of the
16014 * Drawable that are not transparent.
16015 * {@hide}
16016 */
16017 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16018 if (DBG) {
16019 Log.i("View", "Getting transparent region for: " + this);
16020 }
16021 final Region r = dr.getTransparentRegion();
16022 final Rect db = dr.getBounds();
16023 final AttachInfo attachInfo = mAttachInfo;
16024 if (r != null && attachInfo != null) {
16025 final int w = getRight()-getLeft();
16026 final int h = getBottom()-getTop();
16027 if (db.left > 0) {
16028 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16029 r.op(0, 0, db.left, h, Region.Op.UNION);
16030 }
16031 if (db.right < w) {
16032 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16033 r.op(db.right, 0, w, h, Region.Op.UNION);
16034 }
16035 if (db.top > 0) {
16036 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16037 r.op(0, 0, w, db.top, Region.Op.UNION);
16038 }
16039 if (db.bottom < h) {
16040 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16041 r.op(0, db.bottom, w, h, Region.Op.UNION);
16042 }
16043 final int[] location = attachInfo.mTransparentLocation;
16044 getLocationInWindow(location);
16045 r.translate(location[0], location[1]);
16046 region.op(r, Region.Op.INTERSECT);
16047 } else {
16048 region.op(db, Region.Op.DIFFERENCE);
16049 }
16050 }
16051
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016052 private void checkForLongClick(int delayOffset) {
16053 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16054 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016055
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016056 if (mPendingCheckForLongPress == null) {
16057 mPendingCheckForLongPress = new CheckForLongPress();
16058 }
16059 mPendingCheckForLongPress.rememberWindowAttachCount();
16060 postDelayed(mPendingCheckForLongPress,
16061 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016063 }
16064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016065 /**
16066 * Inflate a view from an XML resource. This convenience method wraps the {@link
16067 * LayoutInflater} class, which provides a full range of options for view inflation.
16068 *
16069 * @param context The Context object for your activity or application.
16070 * @param resource The resource ID to inflate
16071 * @param root A view group that will be the parent. Used to properly inflate the
16072 * layout_* parameters.
16073 * @see LayoutInflater
16074 */
16075 public static View inflate(Context context, int resource, ViewGroup root) {
16076 LayoutInflater factory = LayoutInflater.from(context);
16077 return factory.inflate(resource, root);
16078 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016080 /**
Adam Powell637d3372010-08-25 14:37:03 -070016081 * Scroll the view with standard behavior for scrolling beyond the normal
16082 * content boundaries. Views that call this method should override
16083 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16084 * results of an over-scroll operation.
16085 *
16086 * Views can use this method to handle any touch or fling-based scrolling.
16087 *
16088 * @param deltaX Change in X in pixels
16089 * @param deltaY Change in Y in pixels
16090 * @param scrollX Current X scroll value in pixels before applying deltaX
16091 * @param scrollY Current Y scroll value in pixels before applying deltaY
16092 * @param scrollRangeX Maximum content scroll range along the X axis
16093 * @param scrollRangeY Maximum content scroll range along the Y axis
16094 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16095 * along the X axis.
16096 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16097 * along the Y axis.
16098 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16099 * @return true if scrolling was clamped to an over-scroll boundary along either
16100 * axis, false otherwise.
16101 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016102 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016103 protected boolean overScrollBy(int deltaX, int deltaY,
16104 int scrollX, int scrollY,
16105 int scrollRangeX, int scrollRangeY,
16106 int maxOverScrollX, int maxOverScrollY,
16107 boolean isTouchEvent) {
16108 final int overScrollMode = mOverScrollMode;
16109 final boolean canScrollHorizontal =
16110 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16111 final boolean canScrollVertical =
16112 computeVerticalScrollRange() > computeVerticalScrollExtent();
16113 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16114 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16115 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16116 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16117
16118 int newScrollX = scrollX + deltaX;
16119 if (!overScrollHorizontal) {
16120 maxOverScrollX = 0;
16121 }
16122
16123 int newScrollY = scrollY + deltaY;
16124 if (!overScrollVertical) {
16125 maxOverScrollY = 0;
16126 }
16127
16128 // Clamp values if at the limits and record
16129 final int left = -maxOverScrollX;
16130 final int right = maxOverScrollX + scrollRangeX;
16131 final int top = -maxOverScrollY;
16132 final int bottom = maxOverScrollY + scrollRangeY;
16133
16134 boolean clampedX = false;
16135 if (newScrollX > right) {
16136 newScrollX = right;
16137 clampedX = true;
16138 } else if (newScrollX < left) {
16139 newScrollX = left;
16140 clampedX = true;
16141 }
16142
16143 boolean clampedY = false;
16144 if (newScrollY > bottom) {
16145 newScrollY = bottom;
16146 clampedY = true;
16147 } else if (newScrollY < top) {
16148 newScrollY = top;
16149 clampedY = true;
16150 }
16151
16152 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16153
16154 return clampedX || clampedY;
16155 }
16156
16157 /**
16158 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16159 * respond to the results of an over-scroll operation.
16160 *
16161 * @param scrollX New X scroll value in pixels
16162 * @param scrollY New Y scroll value in pixels
16163 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16164 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16165 */
16166 protected void onOverScrolled(int scrollX, int scrollY,
16167 boolean clampedX, boolean clampedY) {
16168 // Intentionally empty.
16169 }
16170
16171 /**
16172 * Returns the over-scroll mode for this view. The result will be
16173 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16174 * (allow over-scrolling only if the view content is larger than the container),
16175 * or {@link #OVER_SCROLL_NEVER}.
16176 *
16177 * @return This view's over-scroll mode.
16178 */
16179 public int getOverScrollMode() {
16180 return mOverScrollMode;
16181 }
16182
16183 /**
16184 * Set the over-scroll mode for this view. Valid over-scroll modes are
16185 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16186 * (allow over-scrolling only if the view content is larger than the container),
16187 * or {@link #OVER_SCROLL_NEVER}.
16188 *
16189 * Setting the over-scroll mode of a view will have an effect only if the
16190 * view is capable of scrolling.
16191 *
16192 * @param overScrollMode The new over-scroll mode for this view.
16193 */
16194 public void setOverScrollMode(int overScrollMode) {
16195 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16196 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16197 overScrollMode != OVER_SCROLL_NEVER) {
16198 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16199 }
16200 mOverScrollMode = overScrollMode;
16201 }
16202
16203 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016204 * Gets a scale factor that determines the distance the view should scroll
16205 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16206 * @return The vertical scroll scale factor.
16207 * @hide
16208 */
16209 protected float getVerticalScrollFactor() {
16210 if (mVerticalScrollFactor == 0) {
16211 TypedValue outValue = new TypedValue();
16212 if (!mContext.getTheme().resolveAttribute(
16213 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16214 throw new IllegalStateException(
16215 "Expected theme to define listPreferredItemHeight.");
16216 }
16217 mVerticalScrollFactor = outValue.getDimension(
16218 mContext.getResources().getDisplayMetrics());
16219 }
16220 return mVerticalScrollFactor;
16221 }
16222
16223 /**
16224 * Gets a scale factor that determines the distance the view should scroll
16225 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16226 * @return The horizontal scroll scale factor.
16227 * @hide
16228 */
16229 protected float getHorizontalScrollFactor() {
16230 // TODO: Should use something else.
16231 return getVerticalScrollFactor();
16232 }
16233
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016234 /**
16235 * Return the value specifying the text direction or policy that was set with
16236 * {@link #setTextDirection(int)}.
16237 *
16238 * @return the defined text direction. It can be one of:
16239 *
16240 * {@link #TEXT_DIRECTION_INHERIT},
16241 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16242 * {@link #TEXT_DIRECTION_ANY_RTL},
16243 * {@link #TEXT_DIRECTION_LTR},
16244 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016245 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016246 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016247 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016248 @ViewDebug.ExportedProperty(category = "text", mapping = {
16249 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16250 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16251 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16252 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16253 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16254 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16255 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016256 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016257 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016258 }
16259
16260 /**
16261 * Set the text direction.
16262 *
16263 * @param textDirection the direction to set. Should be one of:
16264 *
16265 * {@link #TEXT_DIRECTION_INHERIT},
16266 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16267 * {@link #TEXT_DIRECTION_ANY_RTL},
16268 * {@link #TEXT_DIRECTION_LTR},
16269 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016270 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016271 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016272 */
16273 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016274 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016275 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016276 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016277 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016278 // Set the new text direction
16279 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016280 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016281 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016282 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016283 }
16284 }
16285
16286 /**
16287 * Return the resolved text direction.
16288 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016289 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16290 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16291 * up the parent chain of the view. if there is no parent, then it will return the default
16292 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16293 *
16294 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016295 *
Doug Feltcb3791202011-07-07 11:57:48 -070016296 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16297 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016298 * {@link #TEXT_DIRECTION_LTR},
16299 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016300 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016301 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016302 */
16303 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016304 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016305 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016306 resolveTextDirection();
16307 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016308 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016309 }
16310
16311 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016312 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16313 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016314 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016315 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016316 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016317 // Reset any previous text direction resolution
16318 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16319
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016320 if (hasRtlSupport()) {
16321 // Set resolved text direction flag depending on text direction flag
16322 final int textDirection = getTextDirection();
16323 switch(textDirection) {
16324 case TEXT_DIRECTION_INHERIT:
16325 if (canResolveTextDirection()) {
16326 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016327
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016328 // Set current resolved direction to the same value as the parent's one
16329 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16330 switch (parentResolvedDirection) {
16331 case TEXT_DIRECTION_FIRST_STRONG:
16332 case TEXT_DIRECTION_ANY_RTL:
16333 case TEXT_DIRECTION_LTR:
16334 case TEXT_DIRECTION_RTL:
16335 case TEXT_DIRECTION_LOCALE:
16336 mPrivateFlags2 |=
16337 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16338 break;
16339 default:
16340 // Default resolved direction is "first strong" heuristic
16341 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16342 }
16343 } else {
16344 // We cannot do the resolution if there is no parent, so use the default one
16345 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016346 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016347 break;
16348 case TEXT_DIRECTION_FIRST_STRONG:
16349 case TEXT_DIRECTION_ANY_RTL:
16350 case TEXT_DIRECTION_LTR:
16351 case TEXT_DIRECTION_RTL:
16352 case TEXT_DIRECTION_LOCALE:
16353 // Resolved direction is the same as text direction
16354 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16355 break;
16356 default:
16357 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016358 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016359 }
16360 } else {
16361 // Default resolved direction is "first strong" heuristic
16362 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016363 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016364
16365 // Set to resolved
16366 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016367 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016368 }
16369
16370 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016371 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016372 * resolution should override this method.
16373 *
16374 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016375 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016376 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016377 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016378 }
16379
16380 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016381 * Check if text direction resolution can be done.
16382 *
16383 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016384 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016385 */
16386 public boolean canResolveTextDirection() {
16387 switch (getTextDirection()) {
16388 case TEXT_DIRECTION_INHERIT:
16389 return (mParent != null) && (mParent instanceof ViewGroup);
16390 default:
16391 return true;
16392 }
16393 }
16394
16395 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016396 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016397 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016398 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016399 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016400 */
16401 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016402 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016403 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016404 }
16405
16406 /**
16407 * Called when text direction is reset. Subclasses that care about text direction reset should
16408 * override this method and do a reset of the text direction of their children. The default
16409 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016410 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016411 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016412 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016413 }
16414
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016415 /**
16416 * Return the value specifying the text alignment or policy that was set with
16417 * {@link #setTextAlignment(int)}.
16418 *
16419 * @return the defined text alignment. It can be one of:
16420 *
16421 * {@link #TEXT_ALIGNMENT_INHERIT},
16422 * {@link #TEXT_ALIGNMENT_GRAVITY},
16423 * {@link #TEXT_ALIGNMENT_CENTER},
16424 * {@link #TEXT_ALIGNMENT_TEXT_START},
16425 * {@link #TEXT_ALIGNMENT_TEXT_END},
16426 * {@link #TEXT_ALIGNMENT_VIEW_START},
16427 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016428 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016429 */
16430 @ViewDebug.ExportedProperty(category = "text", mapping = {
16431 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16432 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16433 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16434 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16435 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16436 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16437 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16438 })
16439 public int getTextAlignment() {
16440 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16441 }
16442
16443 /**
16444 * Set the text alignment.
16445 *
16446 * @param textAlignment The text alignment to set. Should be one of
16447 *
16448 * {@link #TEXT_ALIGNMENT_INHERIT},
16449 * {@link #TEXT_ALIGNMENT_GRAVITY},
16450 * {@link #TEXT_ALIGNMENT_CENTER},
16451 * {@link #TEXT_ALIGNMENT_TEXT_START},
16452 * {@link #TEXT_ALIGNMENT_TEXT_END},
16453 * {@link #TEXT_ALIGNMENT_VIEW_START},
16454 * {@link #TEXT_ALIGNMENT_VIEW_END}
16455 *
16456 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016457 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016458 */
16459 public void setTextAlignment(int textAlignment) {
16460 if (textAlignment != getTextAlignment()) {
16461 // Reset the current and resolved text alignment
16462 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16463 resetResolvedTextAlignment();
16464 // Set the new text alignment
16465 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16466 // Refresh
16467 requestLayout();
16468 invalidate(true);
16469 }
16470 }
16471
16472 /**
16473 * Return the resolved text alignment.
16474 *
16475 * The resolved text alignment. This needs resolution if the value is
16476 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16477 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16478 *
16479 * @return the resolved text alignment. Returns one of:
16480 *
16481 * {@link #TEXT_ALIGNMENT_GRAVITY},
16482 * {@link #TEXT_ALIGNMENT_CENTER},
16483 * {@link #TEXT_ALIGNMENT_TEXT_START},
16484 * {@link #TEXT_ALIGNMENT_TEXT_END},
16485 * {@link #TEXT_ALIGNMENT_VIEW_START},
16486 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016487 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016488 */
16489 @ViewDebug.ExportedProperty(category = "text", mapping = {
16490 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16491 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16492 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16493 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16494 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16495 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16496 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16497 })
16498 public int getResolvedTextAlignment() {
16499 // If text alignment is not resolved, then resolve it
16500 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16501 resolveTextAlignment();
16502 }
16503 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16504 }
16505
16506 /**
16507 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16508 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016509 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016510 */
16511 public void resolveTextAlignment() {
16512 // Reset any previous text alignment resolution
16513 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16514
16515 if (hasRtlSupport()) {
16516 // Set resolved text alignment flag depending on text alignment flag
16517 final int textAlignment = getTextAlignment();
16518 switch (textAlignment) {
16519 case TEXT_ALIGNMENT_INHERIT:
16520 // Check if we can resolve the text alignment
16521 if (canResolveLayoutDirection() && mParent instanceof View) {
16522 View view = (View) mParent;
16523
16524 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16525 switch (parentResolvedTextAlignment) {
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 the parent resolved
16533 // text alignment
16534 mPrivateFlags2 |=
16535 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16536 break;
16537 default:
16538 // Use default resolved text alignment
16539 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16540 }
16541 }
16542 else {
16543 // We cannot do the resolution if there is no parent so use the default
16544 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16545 }
16546 break;
16547 case TEXT_ALIGNMENT_GRAVITY:
16548 case TEXT_ALIGNMENT_TEXT_START:
16549 case TEXT_ALIGNMENT_TEXT_END:
16550 case TEXT_ALIGNMENT_CENTER:
16551 case TEXT_ALIGNMENT_VIEW_START:
16552 case TEXT_ALIGNMENT_VIEW_END:
16553 // Resolved text alignment is the same as text alignment
16554 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16555 break;
16556 default:
16557 // Use default resolved text alignment
16558 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16559 }
16560 } else {
16561 // Use default resolved text alignment
16562 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16563 }
16564
16565 // Set the resolved
16566 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16567 onResolvedTextAlignmentChanged();
16568 }
16569
16570 /**
16571 * Check if text alignment resolution can be done.
16572 *
16573 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016574 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016575 */
16576 public boolean canResolveTextAlignment() {
16577 switch (getTextAlignment()) {
16578 case TEXT_DIRECTION_INHERIT:
16579 return (mParent != null);
16580 default:
16581 return true;
16582 }
16583 }
16584
16585 /**
16586 * Called when text alignment has been resolved. Subclasses that care about text alignment
16587 * resolution should override this method.
16588 *
16589 * The default 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 onResolvedTextAlignmentChanged() {
16593 }
16594
16595 /**
16596 * Reset resolved text alignment. Text alignment can be resolved with a call to
16597 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16598 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016599 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016600 */
16601 public void resetResolvedTextAlignment() {
16602 // Reset any previous text alignment resolution
16603 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16604 onResolvedTextAlignmentReset();
16605 }
16606
16607 /**
16608 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16609 * override this method and do a reset of the text alignment of their children. The default
16610 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016611 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016612 */
16613 public void onResolvedTextAlignmentReset() {
16614 }
16615
Chet Haaseb39f0512011-05-24 14:36:40 -070016616 //
16617 // Properties
16618 //
16619 /**
16620 * A Property wrapper around the <code>alpha</code> functionality handled by the
16621 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16622 */
Chet Haased47f1532011-12-16 11:18:52 -080016623 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016624 @Override
16625 public void setValue(View object, float value) {
16626 object.setAlpha(value);
16627 }
16628
16629 @Override
16630 public Float get(View object) {
16631 return object.getAlpha();
16632 }
16633 };
16634
16635 /**
16636 * A Property wrapper around the <code>translationX</code> functionality handled by the
16637 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16638 */
Chet Haased47f1532011-12-16 11:18:52 -080016639 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016640 @Override
16641 public void setValue(View object, float value) {
16642 object.setTranslationX(value);
16643 }
16644
16645 @Override
16646 public Float get(View object) {
16647 return object.getTranslationX();
16648 }
16649 };
16650
16651 /**
16652 * A Property wrapper around the <code>translationY</code> functionality handled by the
16653 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16654 */
Chet Haased47f1532011-12-16 11:18:52 -080016655 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016656 @Override
16657 public void setValue(View object, float value) {
16658 object.setTranslationY(value);
16659 }
16660
16661 @Override
16662 public Float get(View object) {
16663 return object.getTranslationY();
16664 }
16665 };
16666
16667 /**
16668 * A Property wrapper around the <code>x</code> functionality handled by the
16669 * {@link View#setX(float)} and {@link View#getX()} methods.
16670 */
Chet Haased47f1532011-12-16 11:18:52 -080016671 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016672 @Override
16673 public void setValue(View object, float value) {
16674 object.setX(value);
16675 }
16676
16677 @Override
16678 public Float get(View object) {
16679 return object.getX();
16680 }
16681 };
16682
16683 /**
16684 * A Property wrapper around the <code>y</code> functionality handled by the
16685 * {@link View#setY(float)} and {@link View#getY()} methods.
16686 */
Chet Haased47f1532011-12-16 11:18:52 -080016687 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016688 @Override
16689 public void setValue(View object, float value) {
16690 object.setY(value);
16691 }
16692
16693 @Override
16694 public Float get(View object) {
16695 return object.getY();
16696 }
16697 };
16698
16699 /**
16700 * A Property wrapper around the <code>rotation</code> functionality handled by the
16701 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16702 */
Chet Haased47f1532011-12-16 11:18:52 -080016703 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016704 @Override
16705 public void setValue(View object, float value) {
16706 object.setRotation(value);
16707 }
16708
16709 @Override
16710 public Float get(View object) {
16711 return object.getRotation();
16712 }
16713 };
16714
16715 /**
16716 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16717 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16718 */
Chet Haased47f1532011-12-16 11:18:52 -080016719 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016720 @Override
16721 public void setValue(View object, float value) {
16722 object.setRotationX(value);
16723 }
16724
16725 @Override
16726 public Float get(View object) {
16727 return object.getRotationX();
16728 }
16729 };
16730
16731 /**
16732 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16733 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16734 */
Chet Haased47f1532011-12-16 11:18:52 -080016735 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016736 @Override
16737 public void setValue(View object, float value) {
16738 object.setRotationY(value);
16739 }
16740
16741 @Override
16742 public Float get(View object) {
16743 return object.getRotationY();
16744 }
16745 };
16746
16747 /**
16748 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16749 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16750 */
Chet Haased47f1532011-12-16 11:18:52 -080016751 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016752 @Override
16753 public void setValue(View object, float value) {
16754 object.setScaleX(value);
16755 }
16756
16757 @Override
16758 public Float get(View object) {
16759 return object.getScaleX();
16760 }
16761 };
16762
16763 /**
16764 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16765 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16766 */
Chet Haased47f1532011-12-16 11:18:52 -080016767 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016768 @Override
16769 public void setValue(View object, float value) {
16770 object.setScaleY(value);
16771 }
16772
16773 @Override
16774 public Float get(View object) {
16775 return object.getScaleY();
16776 }
16777 };
16778
Jeff Brown33bbfd22011-02-24 20:55:35 -080016779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016780 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16781 * Each MeasureSpec represents a requirement for either the width or the height.
16782 * A MeasureSpec is comprised of a size and a mode. There are three possible
16783 * modes:
16784 * <dl>
16785 * <dt>UNSPECIFIED</dt>
16786 * <dd>
16787 * The parent has not imposed any constraint on the child. It can be whatever size
16788 * it wants.
16789 * </dd>
16790 *
16791 * <dt>EXACTLY</dt>
16792 * <dd>
16793 * The parent has determined an exact size for the child. The child is going to be
16794 * given those bounds regardless of how big it wants to be.
16795 * </dd>
16796 *
16797 * <dt>AT_MOST</dt>
16798 * <dd>
16799 * The child can be as large as it wants up to the specified size.
16800 * </dd>
16801 * </dl>
16802 *
16803 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16804 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16805 */
16806 public static class MeasureSpec {
16807 private static final int MODE_SHIFT = 30;
16808 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16809
16810 /**
16811 * Measure specification mode: The parent has not imposed any constraint
16812 * on the child. It can be whatever size it wants.
16813 */
16814 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16815
16816 /**
16817 * Measure specification mode: The parent has determined an exact size
16818 * for the child. The child is going to be given those bounds regardless
16819 * of how big it wants to be.
16820 */
16821 public static final int EXACTLY = 1 << MODE_SHIFT;
16822
16823 /**
16824 * Measure specification mode: The child can be as large as it wants up
16825 * to the specified size.
16826 */
16827 public static final int AT_MOST = 2 << MODE_SHIFT;
16828
16829 /**
16830 * Creates a measure specification based on the supplied size and mode.
16831 *
16832 * The mode must always be one of the following:
16833 * <ul>
16834 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16835 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16836 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16837 * </ul>
16838 *
16839 * @param size the size of the measure specification
16840 * @param mode the mode of the measure specification
16841 * @return the measure specification based on size and mode
16842 */
16843 public static int makeMeasureSpec(int size, int mode) {
16844 return size + mode;
16845 }
16846
16847 /**
16848 * Extracts the mode from the supplied measure specification.
16849 *
16850 * @param measureSpec the measure specification to extract the mode from
16851 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16852 * {@link android.view.View.MeasureSpec#AT_MOST} or
16853 * {@link android.view.View.MeasureSpec#EXACTLY}
16854 */
16855 public static int getMode(int measureSpec) {
16856 return (measureSpec & MODE_MASK);
16857 }
16858
16859 /**
16860 * Extracts the size from the supplied measure specification.
16861 *
16862 * @param measureSpec the measure specification to extract the size from
16863 * @return the size in pixels defined in the supplied measure specification
16864 */
16865 public static int getSize(int measureSpec) {
16866 return (measureSpec & ~MODE_MASK);
16867 }
16868
16869 /**
16870 * Returns a String representation of the specified measure
16871 * specification.
16872 *
16873 * @param measureSpec the measure specification to convert to a String
16874 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16875 */
16876 public static String toString(int measureSpec) {
16877 int mode = getMode(measureSpec);
16878 int size = getSize(measureSpec);
16879
16880 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16881
16882 if (mode == UNSPECIFIED)
16883 sb.append("UNSPECIFIED ");
16884 else if (mode == EXACTLY)
16885 sb.append("EXACTLY ");
16886 else if (mode == AT_MOST)
16887 sb.append("AT_MOST ");
16888 else
16889 sb.append(mode).append(" ");
16890
16891 sb.append(size);
16892 return sb.toString();
16893 }
16894 }
16895
16896 class CheckForLongPress implements Runnable {
16897
16898 private int mOriginalWindowAttachCount;
16899
16900 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016901 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016902 && mOriginalWindowAttachCount == mWindowAttachCount) {
16903 if (performLongClick()) {
16904 mHasPerformedLongPress = true;
16905 }
16906 }
16907 }
16908
16909 public void rememberWindowAttachCount() {
16910 mOriginalWindowAttachCount = mWindowAttachCount;
16911 }
16912 }
Joe Malin32736f02011-01-19 16:14:20 -080016913
Adam Powelle14579b2009-12-16 18:39:52 -080016914 private final class CheckForTap implements Runnable {
16915 public void run() {
16916 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016917 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016918 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016919 }
16920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016921
Adam Powella35d7682010-03-12 14:48:13 -080016922 private final class PerformClick implements Runnable {
16923 public void run() {
16924 performClick();
16925 }
16926 }
16927
Dianne Hackborn63042d62011-01-26 18:56:29 -080016928 /** @hide */
16929 public void hackTurnOffWindowResizeAnim(boolean off) {
16930 mAttachInfo.mTurnOffWindowResizeAnim = off;
16931 }
Joe Malin32736f02011-01-19 16:14:20 -080016932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016933 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016934 * This method returns a ViewPropertyAnimator object, which can be used to animate
16935 * specific properties on this View.
16936 *
16937 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16938 */
16939 public ViewPropertyAnimator animate() {
16940 if (mAnimator == null) {
16941 mAnimator = new ViewPropertyAnimator(this);
16942 }
16943 return mAnimator;
16944 }
16945
16946 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016947 * Interface definition for a callback to be invoked when a hardware key event is
16948 * dispatched to this view. The callback will be invoked before the key event is
16949 * given to the view. This is only useful for hardware keyboards; a software input
16950 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016951 */
16952 public interface OnKeyListener {
16953 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016954 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016955 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016956 * <p>Key presses in software keyboards will generally NOT trigger this method,
16957 * although some may elect to do so in some situations. Do not assume a
16958 * software input method has to be key-based; even if it is, it may use key presses
16959 * in a different way than you expect, so there is no way to reliably catch soft
16960 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016961 *
16962 * @param v The view the key has been dispatched to.
16963 * @param keyCode The code for the physical key that was pressed
16964 * @param event The KeyEvent object containing full information about
16965 * the event.
16966 * @return True if the listener has consumed the event, false otherwise.
16967 */
16968 boolean onKey(View v, int keyCode, KeyEvent event);
16969 }
16970
16971 /**
16972 * Interface definition for a callback to be invoked when a touch event is
16973 * dispatched to this view. The callback will be invoked before the touch
16974 * event is given to the view.
16975 */
16976 public interface OnTouchListener {
16977 /**
16978 * Called when a touch event is dispatched to a view. This allows listeners to
16979 * get a chance to respond before the target view.
16980 *
16981 * @param v The view the touch event has been dispatched to.
16982 * @param event The MotionEvent object containing full information about
16983 * the event.
16984 * @return True if the listener has consumed the event, false otherwise.
16985 */
16986 boolean onTouch(View v, MotionEvent event);
16987 }
16988
16989 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016990 * Interface definition for a callback to be invoked when a hover event is
16991 * dispatched to this view. The callback will be invoked before the hover
16992 * event is given to the view.
16993 */
16994 public interface OnHoverListener {
16995 /**
16996 * Called when a hover event is dispatched to a view. This allows listeners to
16997 * get a chance to respond before the target view.
16998 *
16999 * @param v The view the hover event has been dispatched to.
17000 * @param event The MotionEvent object containing full information about
17001 * the event.
17002 * @return True if the listener has consumed the event, false otherwise.
17003 */
17004 boolean onHover(View v, MotionEvent event);
17005 }
17006
17007 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017008 * Interface definition for a callback to be invoked when a generic motion event is
17009 * dispatched to this view. The callback will be invoked before the generic motion
17010 * event is given to the view.
17011 */
17012 public interface OnGenericMotionListener {
17013 /**
17014 * Called when a generic motion event is dispatched to a view. This allows listeners to
17015 * get a chance to respond before the target view.
17016 *
17017 * @param v The view the generic motion event has been dispatched to.
17018 * @param event The MotionEvent object containing full information about
17019 * the event.
17020 * @return True if the listener has consumed the event, false otherwise.
17021 */
17022 boolean onGenericMotion(View v, MotionEvent event);
17023 }
17024
17025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017026 * Interface definition for a callback to be invoked when a view has been clicked and held.
17027 */
17028 public interface OnLongClickListener {
17029 /**
17030 * Called when a view has been clicked and held.
17031 *
17032 * @param v The view that was clicked and held.
17033 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017034 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017035 */
17036 boolean onLongClick(View v);
17037 }
17038
17039 /**
Chris Tate32affef2010-10-18 15:29:21 -070017040 * Interface definition for a callback to be invoked when a drag is being dispatched
17041 * to this view. The callback will be invoked before the hosting view's own
17042 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17043 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017044 *
17045 * <div class="special reference">
17046 * <h3>Developer Guides</h3>
17047 * <p>For a guide to implementing drag and drop features, read the
17048 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17049 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017050 */
17051 public interface OnDragListener {
17052 /**
17053 * Called when a drag event is dispatched to a view. This allows listeners
17054 * to get a chance to override base View behavior.
17055 *
Joe Malin32736f02011-01-19 16:14:20 -080017056 * @param v The View that received the drag event.
17057 * @param event The {@link android.view.DragEvent} object for the drag event.
17058 * @return {@code true} if the drag event was handled successfully, or {@code false}
17059 * if the drag event was not handled. Note that {@code false} will trigger the View
17060 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017061 */
17062 boolean onDrag(View v, DragEvent event);
17063 }
17064
17065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017066 * Interface definition for a callback to be invoked when the focus state of
17067 * a view changed.
17068 */
17069 public interface OnFocusChangeListener {
17070 /**
17071 * Called when the focus state of a view has changed.
17072 *
17073 * @param v The view whose state has changed.
17074 * @param hasFocus The new focus state of v.
17075 */
17076 void onFocusChange(View v, boolean hasFocus);
17077 }
17078
17079 /**
17080 * Interface definition for a callback to be invoked when a view is clicked.
17081 */
17082 public interface OnClickListener {
17083 /**
17084 * Called when a view has been clicked.
17085 *
17086 * @param v The view that was clicked.
17087 */
17088 void onClick(View v);
17089 }
17090
17091 /**
17092 * Interface definition for a callback to be invoked when the context menu
17093 * for this view is being built.
17094 */
17095 public interface OnCreateContextMenuListener {
17096 /**
17097 * Called when the context menu for this view is being built. It is not
17098 * safe to hold onto the menu after this method returns.
17099 *
17100 * @param menu The context menu that is being built
17101 * @param v The view for which the context menu is being built
17102 * @param menuInfo Extra information about the item for which the
17103 * context menu should be shown. This information will vary
17104 * depending on the class of v.
17105 */
17106 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17107 }
17108
Joe Onorato664644d2011-01-23 17:53:23 -080017109 /**
17110 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017111 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017112 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017113 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017114 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017115 */
17116 public interface OnSystemUiVisibilityChangeListener {
17117 /**
17118 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017119 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017120 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017121 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17122 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17123 * This tells you the <strong>global</strong> state of these UI visibility
17124 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017125 */
17126 public void onSystemUiVisibilityChange(int visibility);
17127 }
17128
Adam Powell4afd62b2011-02-18 15:02:18 -080017129 /**
17130 * Interface definition for a callback to be invoked when this view is attached
17131 * or detached from its window.
17132 */
17133 public interface OnAttachStateChangeListener {
17134 /**
17135 * Called when the view is attached to a window.
17136 * @param v The view that was attached
17137 */
17138 public void onViewAttachedToWindow(View v);
17139 /**
17140 * Called when the view is detached from a window.
17141 * @param v The view that was detached
17142 */
17143 public void onViewDetachedFromWindow(View v);
17144 }
17145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017146 private final class UnsetPressedState implements Runnable {
17147 public void run() {
17148 setPressed(false);
17149 }
17150 }
17151
17152 /**
17153 * Base class for derived classes that want to save and restore their own
17154 * state in {@link android.view.View#onSaveInstanceState()}.
17155 */
17156 public static class BaseSavedState extends AbsSavedState {
17157 /**
17158 * Constructor used when reading from a parcel. Reads the state of the superclass.
17159 *
17160 * @param source
17161 */
17162 public BaseSavedState(Parcel source) {
17163 super(source);
17164 }
17165
17166 /**
17167 * Constructor called by derived classes when creating their SavedState objects
17168 *
17169 * @param superState The state of the superclass of this view
17170 */
17171 public BaseSavedState(Parcelable superState) {
17172 super(superState);
17173 }
17174
17175 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17176 new Parcelable.Creator<BaseSavedState>() {
17177 public BaseSavedState createFromParcel(Parcel in) {
17178 return new BaseSavedState(in);
17179 }
17180
17181 public BaseSavedState[] newArray(int size) {
17182 return new BaseSavedState[size];
17183 }
17184 };
17185 }
17186
17187 /**
17188 * A set of information given to a view when it is attached to its parent
17189 * window.
17190 */
17191 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017192 interface Callbacks {
17193 void playSoundEffect(int effectId);
17194 boolean performHapticFeedback(int effectId, boolean always);
17195 }
17196
17197 /**
17198 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17199 * to a Handler. This class contains the target (View) to invalidate and
17200 * the coordinates of the dirty rectangle.
17201 *
17202 * For performance purposes, this class also implements a pool of up to
17203 * POOL_LIMIT objects that get reused. This reduces memory allocations
17204 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017205 */
Romain Guyd928d682009-03-31 17:52:16 -070017206 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017207 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017208 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17209 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017210 public InvalidateInfo newInstance() {
17211 return new InvalidateInfo();
17212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017213
Romain Guyd928d682009-03-31 17:52:16 -070017214 public void onAcquired(InvalidateInfo element) {
17215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017216
Romain Guyd928d682009-03-31 17:52:16 -070017217 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017218 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017219 }
17220 }, POOL_LIMIT)
17221 );
17222
17223 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017224 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017225
17226 View target;
17227
17228 int left;
17229 int top;
17230 int right;
17231 int bottom;
17232
Romain Guyd928d682009-03-31 17:52:16 -070017233 public void setNextPoolable(InvalidateInfo element) {
17234 mNext = element;
17235 }
17236
17237 public InvalidateInfo getNextPoolable() {
17238 return mNext;
17239 }
17240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017241 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017242 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017243 }
17244
17245 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017246 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017247 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017248
17249 public boolean isPooled() {
17250 return mIsPooled;
17251 }
17252
17253 public void setPooled(boolean isPooled) {
17254 mIsPooled = isPooled;
17255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017256 }
17257
17258 final IWindowSession mSession;
17259
17260 final IWindow mWindow;
17261
17262 final IBinder mWindowToken;
17263
17264 final Callbacks mRootCallbacks;
17265
Romain Guy59a12ca2011-06-09 17:48:21 -070017266 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268 /**
17269 * The top view of the hierarchy.
17270 */
17271 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017273 IBinder mPanelParentWindowToken;
17274 Surface mSurface;
17275
Romain Guyb051e892010-09-28 19:09:36 -070017276 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017277 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017278 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017279
Romain Guy7e4e5612012-03-05 14:37:29 -080017280 boolean mScreenOn;
17281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017282 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017283 * Scale factor used by the compatibility mode
17284 */
17285 float mApplicationScale;
17286
17287 /**
17288 * Indicates whether the application is in compatibility mode
17289 */
17290 boolean mScalingRequired;
17291
17292 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017293 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017294 */
17295 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017296
Dianne Hackborn63042d62011-01-26 18:56:29 -080017297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017298 * Left position of this view's window
17299 */
17300 int mWindowLeft;
17301
17302 /**
17303 * Top position of this view's window
17304 */
17305 int mWindowTop;
17306
17307 /**
Adam Powell26153a32010-11-08 15:22:27 -080017308 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017309 */
Adam Powell26153a32010-11-08 15:22:27 -080017310 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017311
17312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017313 * For windows that are full-screen but using insets to layout inside
17314 * of the screen decorations, these are the current insets for the
17315 * content of the window.
17316 */
17317 final Rect mContentInsets = new Rect();
17318
17319 /**
17320 * For windows that are full-screen but using insets to layout inside
17321 * of the screen decorations, these are the current insets for the
17322 * actual visible parts of the window.
17323 */
17324 final Rect mVisibleInsets = new Rect();
17325
17326 /**
17327 * The internal insets given by this window. This value is
17328 * supplied by the client (through
17329 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17330 * be given to the window manager when changed to be used in laying
17331 * out windows behind it.
17332 */
17333 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17334 = new ViewTreeObserver.InternalInsetsInfo();
17335
17336 /**
17337 * All views in the window's hierarchy that serve as scroll containers,
17338 * used to determine if the window can be resized or must be panned
17339 * to adjust for a soft input area.
17340 */
17341 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17342
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017343 final KeyEvent.DispatcherState mKeyDispatchState
17344 = new KeyEvent.DispatcherState();
17345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017346 /**
17347 * Indicates whether the view's window currently has the focus.
17348 */
17349 boolean mHasWindowFocus;
17350
17351 /**
17352 * The current visibility of the window.
17353 */
17354 int mWindowVisibility;
17355
17356 /**
17357 * Indicates the time at which drawing started to occur.
17358 */
17359 long mDrawingTime;
17360
17361 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017362 * Indicates whether or not ignoring the DIRTY_MASK flags.
17363 */
17364 boolean mIgnoreDirtyState;
17365
17366 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017367 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17368 * to avoid clearing that flag prematurely.
17369 */
17370 boolean mSetIgnoreDirtyState = false;
17371
17372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017373 * Indicates whether the view's window is currently in touch mode.
17374 */
17375 boolean mInTouchMode;
17376
17377 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017378 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017379 * the next time it performs a traversal
17380 */
17381 boolean mRecomputeGlobalAttributes;
17382
17383 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017384 * Always report new attributes at next traversal.
17385 */
17386 boolean mForceReportNewAttributes;
17387
17388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017389 * Set during a traveral if any views want to keep the screen on.
17390 */
17391 boolean mKeepScreenOn;
17392
17393 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017394 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17395 */
17396 int mSystemUiVisibility;
17397
17398 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017399 * Hack to force certain system UI visibility flags to be cleared.
17400 */
17401 int mDisabledSystemUiVisibility;
17402
17403 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017404 * Last global system UI visibility reported by the window manager.
17405 */
17406 int mGlobalSystemUiVisibility;
17407
17408 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017409 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17410 * attached.
17411 */
17412 boolean mHasSystemUiListeners;
17413
17414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017415 * Set if the visibility of any views has changed.
17416 */
17417 boolean mViewVisibilityChanged;
17418
17419 /**
17420 * Set to true if a view has been scrolled.
17421 */
17422 boolean mViewScrollChanged;
17423
17424 /**
17425 * Global to the view hierarchy used as a temporary for dealing with
17426 * x/y points in the transparent region computations.
17427 */
17428 final int[] mTransparentLocation = new int[2];
17429
17430 /**
17431 * Global to the view hierarchy used as a temporary for dealing with
17432 * x/y points in the ViewGroup.invalidateChild implementation.
17433 */
17434 final int[] mInvalidateChildLocation = new int[2];
17435
Chet Haasec3aa3612010-06-17 08:50:37 -070017436
17437 /**
17438 * Global to the view hierarchy used as a temporary for dealing with
17439 * x/y location when view is transformed.
17440 */
17441 final float[] mTmpTransformLocation = new float[2];
17442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017443 /**
17444 * The view tree observer used to dispatch global events like
17445 * layout, pre-draw, touch mode change, etc.
17446 */
17447 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17448
17449 /**
17450 * A Canvas used by the view hierarchy to perform bitmap caching.
17451 */
17452 Canvas mCanvas;
17453
17454 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017455 * The view root impl.
17456 */
17457 final ViewRootImpl mViewRootImpl;
17458
17459 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017460 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017461 * handler can be used to pump events in the UI events queue.
17462 */
17463 final Handler mHandler;
17464
17465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017466 * Temporary for use in computing invalidate rectangles while
17467 * calling up the hierarchy.
17468 */
17469 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017470
17471 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017472 * Temporary for use in computing hit areas with transformed views
17473 */
17474 final RectF mTmpTransformRect = new RectF();
17475
17476 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017477 * Temporary list for use in collecting focusable descendents of a view.
17478 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017479 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017481 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017482 * The id of the window for accessibility purposes.
17483 */
17484 int mAccessibilityWindowId = View.NO_ID;
17485
17486 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017487 * Whether to ingore not exposed for accessibility Views when
17488 * reporting the view tree to accessibility services.
17489 */
17490 boolean mIncludeNotImportantViews;
17491
17492 /**
17493 * The drawable for highlighting accessibility focus.
17494 */
17495 Drawable mAccessibilityFocusDrawable;
17496
17497 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017498 * Show where the margins, bounds and layout bounds are for each view.
17499 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017500 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017501
17502 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017503 * Point used to compute visible regions.
17504 */
17505 final Point mPoint = new Point();
17506
17507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017508 * Creates a new set of attachment information with the specified
17509 * events handler and thread.
17510 *
17511 * @param handler the events handler the view must use
17512 */
17513 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017514 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017515 mSession = session;
17516 mWindow = window;
17517 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017518 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017519 mHandler = handler;
17520 mRootCallbacks = effectPlayer;
17521 }
17522 }
17523
17524 /**
17525 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17526 * is supported. This avoids keeping too many unused fields in most
17527 * instances of View.</p>
17528 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017529 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017530
Mike Cleronf116bf82009-09-27 19:14:12 -070017531 /**
17532 * Scrollbars are not visible
17533 */
17534 public static final int OFF = 0;
17535
17536 /**
17537 * Scrollbars are visible
17538 */
17539 public static final int ON = 1;
17540
17541 /**
17542 * Scrollbars are fading away
17543 */
17544 public static final int FADING = 2;
17545
17546 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017548 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017549 public int scrollBarDefaultDelayBeforeFade;
17550 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017551
17552 public int scrollBarSize;
17553 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017554 public float[] interpolatorValues;
17555 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017556
17557 public final Paint paint;
17558 public final Matrix matrix;
17559 public Shader shader;
17560
Mike Cleronf116bf82009-09-27 19:14:12 -070017561 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17562
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017563 private static final float[] OPAQUE = { 255 };
17564 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017565
Mike Cleronf116bf82009-09-27 19:14:12 -070017566 /**
17567 * When fading should start. This time moves into the future every time
17568 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17569 */
17570 public long fadeStartTime;
17571
17572
17573 /**
17574 * The current state of the scrollbars: ON, OFF, or FADING
17575 */
17576 public int state = OFF;
17577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017578 private int mLastColor;
17579
Mike Cleronf116bf82009-09-27 19:14:12 -070017580 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017581 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17582 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017583 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17584 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017585
17586 paint = new Paint();
17587 matrix = new Matrix();
17588 // use use a height of 1, and then wack the matrix each time we
17589 // actually use it.
17590 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017592 paint.setShader(shader);
17593 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017594 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017595 }
Romain Guy8506ab42009-06-11 17:35:47 -070017596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017597 public void setFadeColor(int color) {
17598 if (color != 0 && color != mLastColor) {
17599 mLastColor = color;
17600 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017601
Romain Guye55e1a72009-08-27 10:42:26 -070017602 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17603 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017605 paint.setShader(shader);
17606 // Restore the default transfer mode (src_over)
17607 paint.setXfermode(null);
17608 }
17609 }
Joe Malin32736f02011-01-19 16:14:20 -080017610
Mike Cleronf116bf82009-09-27 19:14:12 -070017611 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017612 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017613 if (now >= fadeStartTime) {
17614
17615 // the animation fades the scrollbars out by changing
17616 // the opacity (alpha) from fully opaque to fully
17617 // transparent
17618 int nextFrame = (int) now;
17619 int framesCount = 0;
17620
17621 Interpolator interpolator = scrollBarInterpolator;
17622
17623 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017624 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017625
17626 // End transparent
17627 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017628 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017629
17630 state = FADING;
17631
17632 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017633 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017634 }
17635 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017636 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017637
Svetoslav Ganova0156172011-06-26 17:55:44 -070017638 /**
17639 * Resuable callback for sending
17640 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17641 */
17642 private class SendViewScrolledAccessibilityEvent implements Runnable {
17643 public volatile boolean mIsPending;
17644
17645 public void run() {
17646 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17647 mIsPending = false;
17648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017649 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017650
17651 /**
17652 * <p>
17653 * This class represents a delegate that can be registered in a {@link View}
17654 * to enhance accessibility support via composition rather via inheritance.
17655 * It is specifically targeted to widget developers that extend basic View
17656 * classes i.e. classes in package android.view, that would like their
17657 * applications to be backwards compatible.
17658 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017659 * <div class="special reference">
17660 * <h3>Developer Guides</h3>
17661 * <p>For more information about making applications accessible, read the
17662 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17663 * developer guide.</p>
17664 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017665 * <p>
17666 * A scenario in which a developer would like to use an accessibility delegate
17667 * is overriding a method introduced in a later API version then the minimal API
17668 * version supported by the application. For example, the method
17669 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17670 * in API version 4 when the accessibility APIs were first introduced. If a
17671 * developer would like his application to run on API version 4 devices (assuming
17672 * all other APIs used by the application are version 4 or lower) and take advantage
17673 * of this method, instead of overriding the method which would break the application's
17674 * backwards compatibility, he can override the corresponding method in this
17675 * delegate and register the delegate in the target View if the API version of
17676 * the system is high enough i.e. the API version is same or higher to the API
17677 * version that introduced
17678 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17679 * </p>
17680 * <p>
17681 * Here is an example implementation:
17682 * </p>
17683 * <code><pre><p>
17684 * if (Build.VERSION.SDK_INT >= 14) {
17685 * // If the API version is equal of higher than the version in
17686 * // which onInitializeAccessibilityNodeInfo was introduced we
17687 * // register a delegate with a customized implementation.
17688 * View view = findViewById(R.id.view_id);
17689 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17690 * public void onInitializeAccessibilityNodeInfo(View host,
17691 * AccessibilityNodeInfo info) {
17692 * // Let the default implementation populate the info.
17693 * super.onInitializeAccessibilityNodeInfo(host, info);
17694 * // Set some other information.
17695 * info.setEnabled(host.isEnabled());
17696 * }
17697 * });
17698 * }
17699 * </code></pre></p>
17700 * <p>
17701 * This delegate contains methods that correspond to the accessibility methods
17702 * in View. If a delegate has been specified the implementation in View hands
17703 * off handling to the corresponding method in this delegate. The default
17704 * implementation the delegate methods behaves exactly as the corresponding
17705 * method in View for the case of no accessibility delegate been set. Hence,
17706 * to customize the behavior of a View method, clients can override only the
17707 * corresponding delegate method without altering the behavior of the rest
17708 * accessibility related methods of the host view.
17709 * </p>
17710 */
17711 public static class AccessibilityDelegate {
17712
17713 /**
17714 * Sends an accessibility event of the given type. If accessibility is not
17715 * enabled this method has no effect.
17716 * <p>
17717 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17718 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17719 * been set.
17720 * </p>
17721 *
17722 * @param host The View hosting the delegate.
17723 * @param eventType The type of the event to send.
17724 *
17725 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17726 */
17727 public void sendAccessibilityEvent(View host, int eventType) {
17728 host.sendAccessibilityEventInternal(eventType);
17729 }
17730
17731 /**
alanv8eeefef2012-05-07 16:57:53 -070017732 * Performs the specified accessibility action on the view. For
17733 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17734 * <p>
17735 * The default implementation behaves as
17736 * {@link View#performAccessibilityAction(int, Bundle)
17737 * View#performAccessibilityAction(int, Bundle)} for the case of
17738 * no accessibility delegate been set.
17739 * </p>
17740 *
17741 * @param action The action to perform.
17742 * @return Whether the action was performed.
17743 *
17744 * @see View#performAccessibilityAction(int, Bundle)
17745 * View#performAccessibilityAction(int, Bundle)
17746 */
17747 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17748 return host.performAccessibilityActionInternal(action, args);
17749 }
17750
17751 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017752 * Sends an accessibility event. This method behaves exactly as
17753 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17754 * empty {@link AccessibilityEvent} and does not perform a check whether
17755 * accessibility is enabled.
17756 * <p>
17757 * The default implementation behaves as
17758 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17759 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17760 * the case of no accessibility delegate been set.
17761 * </p>
17762 *
17763 * @param host The View hosting the delegate.
17764 * @param event The event to send.
17765 *
17766 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17767 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17768 */
17769 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17770 host.sendAccessibilityEventUncheckedInternal(event);
17771 }
17772
17773 /**
17774 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17775 * to its children for adding their text content to the event.
17776 * <p>
17777 * The default implementation behaves as
17778 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17779 * View#dispatchPopulateAccessibilityEvent(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 event.
17785 * @return True if the event population was completed.
17786 *
17787 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17788 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17789 */
17790 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17791 return host.dispatchPopulateAccessibilityEventInternal(event);
17792 }
17793
17794 /**
17795 * Gives a chance to the host View to populate the accessibility event with its
17796 * text content.
17797 * <p>
17798 * The default implementation behaves as
17799 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17800 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17801 * the case of no accessibility delegate been set.
17802 * </p>
17803 *
17804 * @param host The View hosting the delegate.
17805 * @param event The accessibility event which to populate.
17806 *
17807 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17808 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17809 */
17810 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17811 host.onPopulateAccessibilityEventInternal(event);
17812 }
17813
17814 /**
17815 * Initializes an {@link AccessibilityEvent} with information about the
17816 * the host View which is the event source.
17817 * <p>
17818 * The default implementation behaves as
17819 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17820 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17821 * the case of no accessibility delegate been set.
17822 * </p>
17823 *
17824 * @param host The View hosting the delegate.
17825 * @param event The event to initialize.
17826 *
17827 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17828 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17829 */
17830 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17831 host.onInitializeAccessibilityEventInternal(event);
17832 }
17833
17834 /**
17835 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17836 * <p>
17837 * The default implementation behaves as
17838 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17839 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17840 * the case of no accessibility delegate been set.
17841 * </p>
17842 *
17843 * @param host The View hosting the delegate.
17844 * @param info The instance to initialize.
17845 *
17846 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17847 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17848 */
17849 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17850 host.onInitializeAccessibilityNodeInfoInternal(info);
17851 }
17852
17853 /**
17854 * Called when a child of the host View has requested sending an
17855 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17856 * to augment the event.
17857 * <p>
17858 * The default implementation behaves as
17859 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17860 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17861 * the case of no accessibility delegate been set.
17862 * </p>
17863 *
17864 * @param host The View hosting the delegate.
17865 * @param child The child which requests sending the event.
17866 * @param event The event to be sent.
17867 * @return True if the event should be sent
17868 *
17869 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17870 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17871 */
17872 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17873 AccessibilityEvent event) {
17874 return host.onRequestSendAccessibilityEventInternal(child, event);
17875 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017876
17877 /**
17878 * Gets the provider for managing a virtual view hierarchy rooted at this View
17879 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17880 * that explore the window content.
17881 * <p>
17882 * The default implementation behaves as
17883 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17884 * the case of no accessibility delegate been set.
17885 * </p>
17886 *
17887 * @return The provider.
17888 *
17889 * @see AccessibilityNodeProvider
17890 */
17891 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17892 return null;
17893 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017895}