blob: 414bb501d33efca67916bca877b6aae24fe076a6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070051import android.util.FloatProperty;
52import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070058import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070062import android.view.AccessibilityIterators.TextSegmentIterator;
63import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
64import android.view.AccessibilityIterators.WordTextSegmentIterator;
65import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.accessibility.AccessibilityEvent;
67import android.view.accessibility.AccessibilityEventSource;
68import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070069import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070070import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070072import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080073import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070074import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.inputmethod.InputConnection;
76import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ScrollBarDrawable;
78
Romain Guy1ef3fdb2011-09-09 15:30:30 -070079import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070080import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070081
Doug Feltcb3791202011-07-07 11:57:48 -070082import com.android.internal.R;
83import com.android.internal.util.Predicate;
84import com.android.internal.view.menu.MenuBuilder;
85
Christopher Tatea0374192010-10-05 13:06:41 -070086import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import java.lang.reflect.InvocationTargetException;
88import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
90import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070091import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080092import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94/**
95 * <p>
96 * This class represents the basic building block for user interface components. A View
97 * occupies a rectangular area on the screen and is responsible for drawing and
98 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070099 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
101 * are invisible containers that hold other Views (or other ViewGroups) and define
102 * their layout properties.
103 * </p>
104 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700105 * <div class="special reference">
106 * <h3>Developer Guides</h3>
107 * <p>For information about using this class to develop your application's user interface,
108 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700136 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900207 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900212 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700346 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
347 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
348 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
349 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 * </p>
351 *
352 * <p>
353 * Even though a view can define a padding, it does not provide any support for
354 * margins. However, view groups provide such a support. Refer to
355 * {@link android.view.ViewGroup} and
356 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
357 * </p>
358 *
359 * <a name="Layout"></a>
360 * <h3>Layout</h3>
361 * <p>
362 * Layout is a two pass process: a measure pass and a layout pass. The measuring
363 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
364 * of the view tree. Each view pushes dimension specifications down the tree
365 * during the recursion. At the end of the measure pass, every view has stored
366 * its measurements. The second pass happens in
367 * {@link #layout(int,int,int,int)} and is also top-down. During
368 * this pass each parent is responsible for positioning all of its children
369 * using the sizes computed in the measure pass.
370 * </p>
371 *
372 * <p>
373 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
374 * {@link #getMeasuredHeight()} values must be set, along with those for all of
375 * that view's descendants. A view's measured width and measured height values
376 * must respect the constraints imposed by the view's parents. This guarantees
377 * that at the end of the measure pass, all parents accept all of their
378 * children's measurements. A parent view may call measure() more than once on
379 * its children. For example, the parent may measure each child once with
380 * unspecified dimensions to find out how big they want to be, then call
381 * measure() on them again with actual numbers if the sum of all the children's
382 * unconstrained sizes is too big or too small.
383 * </p>
384 *
385 * <p>
386 * The measure pass uses two classes to communicate dimensions. The
387 * {@link MeasureSpec} class is used by views to tell their parents how they
388 * want to be measured and positioned. The base LayoutParams class just
389 * describes how big the view wants to be for both width and height. For each
390 * dimension, it can specify one of:
391 * <ul>
392 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800393 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 * (minus padding)
395 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
396 * enclose its content (plus padding).
397 * </ul>
398 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
399 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
400 * an X and Y value.
401 * </p>
402 *
403 * <p>
404 * MeasureSpecs are used to push requirements down the tree from parent to
405 * child. A MeasureSpec can be in one of three modes:
406 * <ul>
407 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
408 * of a child view. For example, a LinearLayout may call measure() on its child
409 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
410 * tall the child view wants to be given a width of 240 pixels.
411 * <li>EXACTLY: This is used by the parent to impose an exact size on the
412 * child. The child must use this size, and guarantee that all of its
413 * descendants will fit within this size.
414 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
415 * child. The child must gurantee that it and all of its descendants will fit
416 * within this size.
417 * </ul>
418 * </p>
419 *
420 * <p>
421 * To intiate a layout, call {@link #requestLayout}. This method is typically
422 * called by a view on itself when it believes that is can no longer fit within
423 * its current bounds.
424 * </p>
425 *
426 * <a name="Drawing"></a>
427 * <h3>Drawing</h3>
428 * <p>
429 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700430 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * this means that parents will draw before (i.e., behind) their children, with
432 * siblings drawn in the order they appear in the tree.
433 * If you set a background drawable for a View, then the View will draw it for you
434 * before calling back to its <code>onDraw()</code> method.
435 * </p>
436 *
437 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700438 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * </p>
440 *
441 * <p>
442 * To force a view to draw, call {@link #invalidate()}.
443 * </p>
444 *
445 * <a name="EventHandlingThreading"></a>
446 * <h3>Event Handling and Threading</h3>
447 * <p>
448 * The basic cycle of a view is as follows:
449 * <ol>
450 * <li>An event comes in and is dispatched to the appropriate view. The view
451 * handles the event and notifies any listeners.</li>
452 * <li>If in the course of processing the event, the view's bounds may need
453 * to be changed, the view will call {@link #requestLayout()}.</li>
454 * <li>Similarly, if in the course of processing the event the view's appearance
455 * may need to be changed, the view will call {@link #invalidate()}.</li>
456 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
457 * the framework will take care of measuring, laying out, and drawing the tree
458 * as appropriate.</li>
459 * </ol>
460 * </p>
461 *
462 * <p><em>Note: The entire view tree is single threaded. You must always be on
463 * the UI thread when calling any method on any view.</em>
464 * If you are doing work on other threads and want to update the state of a view
465 * from that thread, you should use a {@link Handler}.
466 * </p>
467 *
468 * <a name="FocusHandling"></a>
469 * <h3>Focus Handling</h3>
470 * <p>
471 * The framework will handle routine focus movement in response to user input.
472 * This includes changing the focus as views are removed or hidden, or as new
473 * views become available. Views indicate their willingness to take focus
474 * through the {@link #isFocusable} method. To change whether a view can take
475 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
476 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
477 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
478 * </p>
479 * <p>
480 * Focus movement is based on an algorithm which finds the nearest neighbor in a
481 * given direction. In rare cases, the default algorithm may not match the
482 * intended behavior of the developer. In these situations, you can provide
483 * explicit overrides by using these XML attributes in the layout file:
484 * <pre>
485 * nextFocusDown
486 * nextFocusLeft
487 * nextFocusRight
488 * nextFocusUp
489 * </pre>
490 * </p>
491 *
492 *
493 * <p>
494 * To get a particular view to take focus, call {@link #requestFocus()}.
495 * </p>
496 *
497 * <a name="TouchMode"></a>
498 * <h3>Touch Mode</h3>
499 * <p>
500 * When a user is navigating a user interface via directional keys such as a D-pad, it is
501 * necessary to give focus to actionable items such as buttons so the user can see
502 * what will take input. If the device has touch capabilities, however, and the user
503 * begins interacting with the interface by touching it, it is no longer necessary to
504 * always highlight, or give focus to, a particular view. This motivates a mode
505 * for interaction named 'touch mode'.
506 * </p>
507 * <p>
508 * For a touch capable device, once the user touches the screen, the device
509 * will enter touch mode. From this point onward, only views for which
510 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
511 * Other views that are touchable, like buttons, will not take focus when touched; they will
512 * only fire the on click listeners.
513 * </p>
514 * <p>
515 * Any time a user hits a directional key, such as a D-pad direction, the view device will
516 * exit touch mode, and find a view to take focus, so that the user may resume interacting
517 * with the user interface without touching the screen again.
518 * </p>
519 * <p>
520 * The touch mode state is maintained across {@link android.app.Activity}s. Call
521 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
522 * </p>
523 *
524 * <a name="Scrolling"></a>
525 * <h3>Scrolling</h3>
526 * <p>
527 * The framework provides basic support for views that wish to internally
528 * scroll their content. This includes keeping track of the X and Y scroll
529 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800530 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700531 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * </p>
533 *
534 * <a name="Tags"></a>
535 * <h3>Tags</h3>
536 * <p>
537 * Unlike IDs, tags are not used to identify views. Tags are essentially an
538 * extra piece of information that can be associated with a view. They are most
539 * often used as a convenience to store data related to views in the views
540 * themselves rather than by putting them in a separate structure.
541 * </p>
542 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700543 * <a name="Properties"></a>
544 * <h3>Properties</h3>
545 * <p>
546 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
547 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
548 * available both in the {@link Property} form as well as in similarly-named setter/getter
549 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
550 * be used to set persistent state associated with these rendering-related properties on the view.
551 * The properties and methods can also be used in conjunction with
552 * {@link android.animation.Animator Animator}-based animations, described more in the
553 * <a href="#Animation">Animation</a> section.
554 * </p>
555 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 * <a name="Animation"></a>
557 * <h3>Animation</h3>
558 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700559 * Starting with Android 3.0, the preferred way of animating views is to use the
560 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
561 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
562 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
563 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
564 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
565 * makes animating these View properties particularly easy and efficient.
566 * </p>
567 * <p>
568 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 * You can attach an {@link Animation} object to a view using
570 * {@link #setAnimation(Animation)} or
571 * {@link #startAnimation(Animation)}. The animation can alter the scale,
572 * rotation, translation and alpha of a view over time. If the animation is
573 * attached to a view that has children, the animation will affect the entire
574 * subtree rooted by that node. When an animation is started, the framework will
575 * take care of redrawing the appropriate views until the animation completes.
576 * </p>
577 *
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * <a name="Security"></a>
579 * <h3>Security</h3>
580 * <p>
581 * Sometimes it is essential that an application be able to verify that an action
582 * is being performed with the full knowledge and consent of the user, such as
583 * granting a permission request, making a purchase or clicking on an advertisement.
584 * Unfortunately, a malicious application could try to spoof the user into
585 * performing these actions, unaware, by concealing the intended purpose of the view.
586 * As a remedy, the framework offers a touch filtering mechanism that can be used to
587 * improve the security of views that provide access to sensitive functionality.
588 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700589 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800590 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700591 * will discard touches that are received whenever the view's window is obscured by
592 * another visible window. As a result, the view will not receive touches whenever a
593 * toast, dialog or other window appears above the view's window.
594 * </p><p>
595 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700596 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
597 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700598 * </p>
599 *
Romain Guy171c5922011-01-06 10:04:23 -0800600 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700601 * @attr ref android.R.styleable#View_background
602 * @attr ref android.R.styleable#View_clickable
603 * @attr ref android.R.styleable#View_contentDescription
604 * @attr ref android.R.styleable#View_drawingCacheQuality
605 * @attr ref android.R.styleable#View_duplicateParentState
606 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700607 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700608 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700610 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_isScrollContainer
613 * @attr ref android.R.styleable#View_focusable
614 * @attr ref android.R.styleable#View_focusableInTouchMode
615 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
616 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800617 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_longClickable
619 * @attr ref android.R.styleable#View_minHeight
620 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_nextFocusDown
622 * @attr ref android.R.styleable#View_nextFocusLeft
623 * @attr ref android.R.styleable#View_nextFocusRight
624 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_onClick
626 * @attr ref android.R.styleable#View_padding
627 * @attr ref android.R.styleable#View_paddingBottom
628 * @attr ref android.R.styleable#View_paddingLeft
629 * @attr ref android.R.styleable#View_paddingRight
630 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800631 * @attr ref android.R.styleable#View_paddingStart
632 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800634 * @attr ref android.R.styleable#View_rotation
635 * @attr ref android.R.styleable#View_rotationX
636 * @attr ref android.R.styleable#View_rotationY
637 * @attr ref android.R.styleable#View_scaleX
638 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 * @attr ref android.R.styleable#View_scrollX
640 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_scrollbarSize
642 * @attr ref android.R.styleable#View_scrollbarStyle
643 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700644 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
645 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
647 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * @attr ref android.R.styleable#View_scrollbarThumbVertical
649 * @attr ref android.R.styleable#View_scrollbarTrackVertical
650 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
651 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700652 * @attr ref android.R.styleable#View_soundEffectsEnabled
653 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700654 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800655 * @attr ref android.R.styleable#View_transformPivotX
656 * @attr ref android.R.styleable#View_transformPivotY
657 * @attr ref android.R.styleable#View_translationX
658 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700659 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 *
661 * @see android.view.ViewGroup
662 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700663public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700664 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 private static final boolean DBG = false;
666
667 /**
668 * The logging tag used by this class with android.util.Log.
669 */
670 protected static final String VIEW_LOG_TAG = "View";
671
672 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700673 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700674 *
675 * @hide
676 */
677 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
678
679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * Used to mark a View that has no ID.
681 */
682 public static final int NO_ID = -1;
683
684 /**
685 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
686 * calling setFlags.
687 */
688 private static final int NOT_FOCUSABLE = 0x00000000;
689
690 /**
691 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
692 * setFlags.
693 */
694 private static final int FOCUSABLE = 0x00000001;
695
696 /**
697 * Mask for use with setFlags indicating bits used for focus.
698 */
699 private static final int FOCUSABLE_MASK = 0x00000001;
700
701 /**
702 * This view will adjust its padding to fit sytem windows (e.g. status bar)
703 */
704 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
705
706 /**
Scott Main812634c22011-07-27 13:22:35 -0700707 * This view is visible.
708 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
709 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public static final int VISIBLE = 0x00000000;
712
713 /**
714 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700715 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
716 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public static final int INVISIBLE = 0x00000004;
719
720 /**
721 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700722 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
723 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 */
725 public static final int GONE = 0x00000008;
726
727 /**
728 * Mask for use with setFlags indicating bits used for visibility.
729 * {@hide}
730 */
731 static final int VISIBILITY_MASK = 0x0000000C;
732
733 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
734
735 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700736 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * Use with ENABLED_MASK when calling setFlags.
738 * {@hide}
739 */
740 static final int ENABLED = 0x00000000;
741
742 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700743 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 * Use with ENABLED_MASK when calling setFlags.
745 * {@hide}
746 */
747 static final int DISABLED = 0x00000020;
748
749 /**
750 * Mask for use with setFlags indicating bits used for indicating whether
751 * this view is enabled
752 * {@hide}
753 */
754 static final int ENABLED_MASK = 0x00000020;
755
756 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700757 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
758 * called and further optimizations will be performed. It is okay to have
759 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 * {@hide}
761 */
762 static final int WILL_NOT_DRAW = 0x00000080;
763
764 /**
765 * Mask for use with setFlags indicating bits used for indicating whether
766 * this view is will draw
767 * {@hide}
768 */
769 static final int DRAW_MASK = 0x00000080;
770
771 /**
772 * <p>This view doesn't show scrollbars.</p>
773 * {@hide}
774 */
775 static final int SCROLLBARS_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal scrollbars.</p>
779 * {@hide}
780 */
781 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
782
783 /**
784 * <p>This view shows vertical scrollbars.</p>
785 * {@hide}
786 */
787 static final int SCROLLBARS_VERTICAL = 0x00000200;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * scrollbars are enabled.</p>
792 * {@hide}
793 */
794 static final int SCROLLBARS_MASK = 0x00000300;
795
Jeff Brown85a31762010-09-01 17:01:00 -0700796 /**
797 * Indicates that the view should filter touches when its window is obscured.
798 * Refer to the class comments for more information about this security feature.
799 * {@hide}
800 */
801 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
802
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700803 /**
804 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
805 * that they are optional and should be skipped if the window has
806 * requested system UI flags that ignore those insets for layout.
807 */
808 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
810 /**
811 * <p>This view doesn't show fading edges.</p>
812 * {@hide}
813 */
814 static final int FADING_EDGE_NONE = 0x00000000;
815
816 /**
817 * <p>This view shows horizontal fading edges.</p>
818 * {@hide}
819 */
820 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
821
822 /**
823 * <p>This view shows vertical fading edges.</p>
824 * {@hide}
825 */
826 static final int FADING_EDGE_VERTICAL = 0x00002000;
827
828 /**
829 * <p>Mask for use with setFlags indicating bits used for indicating which
830 * fading edges are enabled.</p>
831 * {@hide}
832 */
833 static final int FADING_EDGE_MASK = 0x00003000;
834
835 /**
836 * <p>Indicates this view can be clicked. When clickable, a View reacts
837 * to clicks by notifying the OnClickListener.<p>
838 * {@hide}
839 */
840 static final int CLICKABLE = 0x00004000;
841
842 /**
843 * <p>Indicates this view is caching its drawing into a bitmap.</p>
844 * {@hide}
845 */
846 static final int DRAWING_CACHE_ENABLED = 0x00008000;
847
848 /**
849 * <p>Indicates that no icicle should be saved for this view.<p>
850 * {@hide}
851 */
852 static final int SAVE_DISABLED = 0x000010000;
853
854 /**
855 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
856 * property.</p>
857 * {@hide}
858 */
859 static final int SAVE_DISABLED_MASK = 0x000010000;
860
861 /**
862 * <p>Indicates that no drawing cache should ever be created for this view.<p>
863 * {@hide}
864 */
865 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
866
867 /**
868 * <p>Indicates this view can take / keep focus when int touch mode.</p>
869 * {@hide}
870 */
871 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
872
873 /**
874 * <p>Enables low quality mode for the drawing cache.</p>
875 */
876 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
877
878 /**
879 * <p>Enables high quality mode for the drawing cache.</p>
880 */
881 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
882
883 /**
884 * <p>Enables automatic quality mode for the drawing cache.</p>
885 */
886 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
887
888 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
889 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
890 };
891
892 /**
893 * <p>Mask for use with setFlags indicating bits used for the cache
894 * quality property.</p>
895 * {@hide}
896 */
897 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
898
899 /**
900 * <p>
901 * Indicates this view can be long clicked. When long clickable, a View
902 * reacts to long clicks by notifying the OnLongClickListener or showing a
903 * context menu.
904 * </p>
905 * {@hide}
906 */
907 static final int LONG_CLICKABLE = 0x00200000;
908
909 /**
910 * <p>Indicates that this view gets its drawable states from its direct parent
911 * and ignores its original internal states.</p>
912 *
913 * @hide
914 */
915 static final int DUPLICATE_PARENT_STATE = 0x00400000;
916
917 /**
918 * The scrollbar style to display the scrollbars inside the content area,
919 * without increasing the padding. The scrollbars will be overlaid with
920 * translucency on the view's content.
921 */
922 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
923
924 /**
925 * The scrollbar style to display the scrollbars inside the padded area,
926 * increasing the padding of the view. The scrollbars will not overlap the
927 * content area of the view.
928 */
929 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
930
931 /**
932 * The scrollbar style to display the scrollbars at the edge of the view,
933 * without increasing the padding. The scrollbars will be overlaid with
934 * translucency.
935 */
936 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
937
938 /**
939 * The scrollbar style to display the scrollbars at the edge of the view,
940 * increasing the padding of the view. The scrollbars will only overlap the
941 * background, if any.
942 */
943 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
944
945 /**
946 * Mask to check if the scrollbar style is overlay or inset.
947 * {@hide}
948 */
949 static final int SCROLLBARS_INSET_MASK = 0x01000000;
950
951 /**
952 * Mask to check if the scrollbar style is inside or outside.
953 * {@hide}
954 */
955 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
956
957 /**
958 * Mask for scrollbar style.
959 * {@hide}
960 */
961 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
962
963 /**
964 * View flag indicating that the screen should remain on while the
965 * window containing this view is visible to the user. This effectively
966 * takes care of automatically setting the WindowManager's
967 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
968 */
969 public static final int KEEP_SCREEN_ON = 0x04000000;
970
971 /**
972 * View flag indicating whether this view should have sound effects enabled
973 * for events such as clicking and touching.
974 */
975 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
976
977 /**
978 * View flag indicating whether this view should have haptic feedback
979 * enabled for events such as long presses.
980 */
981 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
982
983 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700984 * <p>Indicates that the view hierarchy should stop saving state when
985 * it reaches this view. If state saving is initiated immediately at
986 * the view, it will be allowed.
987 * {@hide}
988 */
989 static final int PARENT_SAVE_DISABLED = 0x20000000;
990
991 /**
992 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
993 * {@hide}
994 */
995 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
996
997 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700998 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
999 * should add all focusable Views regardless if they are focusable in touch mode.
1000 */
1001 public static final int FOCUSABLES_ALL = 0x00000000;
1002
1003 /**
1004 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1005 * should add only Views focusable in touch mode.
1006 */
1007 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1008
1009 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001010 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1011 * should add only accessibility focusable Views.
1012 *
1013 * @hide
1014 */
1015 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_BACKWARD = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_FORWARD = 0x00000002;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_LEFT = 0x00000011;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_UP = 0x00000021;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_RIGHT = 0x00000042;
1043
1044 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001045 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final int FOCUS_DOWN = 0x00000082;
1048
Svetoslav Ganov42138042012-03-20 11:51:39 -07001049 // Accessibility focus directions.
1050
1051 /**
1052 * The accessibility focus which is the current user position when
1053 * interacting with the accessibility framework.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001054 *
1055 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001056 */
1057 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1058
1059 /**
1060 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001061 *
1062 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001063 */
1064 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1065
1066 /**
1067 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001068 *
1069 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001070 */
1071 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1072
1073 /**
1074 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001075 *
1076 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001077 */
1078 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1079
1080 /**
1081 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001082 *
1083 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001084 */
1085 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1086
1087 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001088 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001089 *
1090 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001091 */
1092 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1093
1094 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001095 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07001096 *
1097 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07001098 */
1099 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1100
1101 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001102 * Bits of {@link #getMeasuredWidthAndState()} and
1103 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1104 */
1105 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1106
1107 /**
1108 * Bits of {@link #getMeasuredWidthAndState()} and
1109 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1110 */
1111 public static final int MEASURED_STATE_MASK = 0xff000000;
1112
1113 /**
1114 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1115 * for functions that combine both width and height into a single int,
1116 * such as {@link #getMeasuredState()} and the childState argument of
1117 * {@link #resolveSizeAndState(int, int, int)}.
1118 */
1119 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1120
1121 /**
1122 * Bit of {@link #getMeasuredWidthAndState()} and
1123 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1124 * is smaller that the space the view would like to have.
1125 */
1126 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1127
1128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 * Base View state sets
1130 */
1131 // Singles
1132 /**
1133 * Indicates the view has no states set. 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[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is enabled. 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[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused. 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 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is selected. States are used with
1161 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1162 * view depending on its state.
1163 *
1164 * @see android.graphics.drawable.Drawable
1165 * @see #getDrawableState()
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is pressed. States are used with
1170 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1171 * view depending on its state.
1172 *
1173 * @see android.graphics.drawable.Drawable
1174 * @see #getDrawableState()
1175 * @hide
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 /**
1179 * Indicates the view's window has focus. States are used with
1180 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1181 * view depending on its state.
1182 *
1183 * @see android.graphics.drawable.Drawable
1184 * @see #getDrawableState()
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 // Doubles
1188 /**
1189 * Indicates the view is enabled and has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is enabled and selected.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is enabled and that its window has focus.
1204 *
1205 * @see #ENABLED_STATE_SET
1206 * @see #WINDOW_FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 /**
1210 * Indicates the view is focused and selected.
1211 *
1212 * @see #FOCUSED_STATE_SET
1213 * @see #SELECTED_STATE_SET
1214 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001215 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 /**
1217 * Indicates the view has the focus and that its window has the focus.
1218 *
1219 * @see #FOCUSED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is selected and that its window has the focus.
1225 *
1226 * @see #SELECTED_STATE_SET
1227 * @see #WINDOW_FOCUSED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 // Triples
1231 /**
1232 * Indicates the view is enabled, focused and selected.
1233 *
1234 * @see #ENABLED_STATE_SET
1235 * @see #FOCUSED_STATE_SET
1236 * @see #SELECTED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is enabled, focused and its window has the focus.
1241 *
1242 * @see #ENABLED_STATE_SET
1243 * @see #FOCUSED_STATE_SET
1244 * @see #WINDOW_FOCUSED_STATE_SET
1245 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001246 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * Indicates the view is enabled, selected and its window has the focus.
1249 *
1250 * @see #ENABLED_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_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is focused, selected and its window has the focus.
1257 *
1258 * @see #FOCUSED_STATE_SET
1259 * @see #SELECTED_STATE_SET
1260 * @see #WINDOW_FOCUSED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is enabled, focused, selected and its window
1265 * has the focus.
1266 *
1267 * @see #ENABLED_STATE_SET
1268 * @see #FOCUSED_STATE_SET
1269 * @see #SELECTED_STATE_SET
1270 * @see #WINDOW_FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed and its window has the focus.
1275 *
1276 * @see #PRESSED_STATE_SET
1277 * @see #WINDOW_FOCUSED_STATE_SET
1278 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001279 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 /**
1281 * Indicates the view is pressed and selected.
1282 *
1283 * @see #PRESSED_STATE_SET
1284 * @see #SELECTED_STATE_SET
1285 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001286 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * Indicates the view is pressed, selected and its window has the focus.
1289 *
1290 * @see #PRESSED_STATE_SET
1291 * @see #SELECTED_STATE_SET
1292 * @see #WINDOW_FOCUSED_STATE_SET
1293 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001294 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 /**
1296 * Indicates the view is pressed and focused.
1297 *
1298 * @see #PRESSED_STATE_SET
1299 * @see #FOCUSED_STATE_SET
1300 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001301 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 /**
1303 * Indicates the view is pressed, focused and its window has the focus.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #FOCUSED_STATE_SET
1307 * @see #WINDOW_FOCUSED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, focused and selected.
1312 *
1313 * @see #PRESSED_STATE_SET
1314 * @see #SELECTED_STATE_SET
1315 * @see #FOCUSED_STATE_SET
1316 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 /**
1319 * Indicates the view is pressed, focused, selected and its window has the focus.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #FOCUSED_STATE_SET
1323 * @see #SELECTED_STATE_SET
1324 * @see #WINDOW_FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed and enabled.
1329 *
1330 * @see #PRESSED_STATE_SET
1331 * @see #ENABLED_STATE_SET
1332 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 /**
1335 * Indicates the view is pressed, enabled and its window has the focus.
1336 *
1337 * @see #PRESSED_STATE_SET
1338 * @see #ENABLED_STATE_SET
1339 * @see #WINDOW_FOCUSED_STATE_SET
1340 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
1343 * Indicates the view is pressed, enabled and selected.
1344 *
1345 * @see #PRESSED_STATE_SET
1346 * @see #ENABLED_STATE_SET
1347 * @see #SELECTED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * Indicates the view is pressed, enabled, selected and its window has the
1352 * focus.
1353 *
1354 * @see #PRESSED_STATE_SET
1355 * @see #ENABLED_STATE_SET
1356 * @see #SELECTED_STATE_SET
1357 * @see #WINDOW_FOCUSED_STATE_SET
1358 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001359 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 /**
1361 * Indicates the view is pressed, enabled and focused.
1362 *
1363 * @see #PRESSED_STATE_SET
1364 * @see #ENABLED_STATE_SET
1365 * @see #FOCUSED_STATE_SET
1366 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001367 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 /**
1369 * Indicates the view is pressed, enabled, focused and its window has the
1370 * focus.
1371 *
1372 * @see #PRESSED_STATE_SET
1373 * @see #ENABLED_STATE_SET
1374 * @see #FOCUSED_STATE_SET
1375 * @see #WINDOW_FOCUSED_STATE_SET
1376 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 /**
1379 * Indicates the view is pressed, enabled, focused and selected.
1380 *
1381 * @see #PRESSED_STATE_SET
1382 * @see #ENABLED_STATE_SET
1383 * @see #SELECTED_STATE_SET
1384 * @see #FOCUSED_STATE_SET
1385 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 /**
1388 * Indicates the view is pressed, enabled, focused, selected and its window
1389 * has the focus.
1390 *
1391 * @see #PRESSED_STATE_SET
1392 * @see #ENABLED_STATE_SET
1393 * @see #SELECTED_STATE_SET
1394 * @see #FOCUSED_STATE_SET
1395 * @see #WINDOW_FOCUSED_STATE_SET
1396 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398
1399 /**
1400 * The order here is very important to {@link #getDrawableState()}
1401 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001402 private static final int[][] VIEW_STATE_SETS;
1403
Romain Guyb051e892010-09-28 19:09:36 -07001404 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1405 static final int VIEW_STATE_SELECTED = 1 << 1;
1406 static final int VIEW_STATE_FOCUSED = 1 << 2;
1407 static final int VIEW_STATE_ENABLED = 1 << 3;
1408 static final int VIEW_STATE_PRESSED = 1 << 4;
1409 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001410 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001411 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001412 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1413 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001414
1415 static final int[] VIEW_STATE_IDS = new int[] {
1416 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1417 R.attr.state_selected, VIEW_STATE_SELECTED,
1418 R.attr.state_focused, VIEW_STATE_FOCUSED,
1419 R.attr.state_enabled, VIEW_STATE_ENABLED,
1420 R.attr.state_pressed, VIEW_STATE_PRESSED,
1421 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001422 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001423 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001424 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001425 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 };
1427
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001428 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001429 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1430 throw new IllegalStateException(
1431 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1432 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001433 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001434 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001435 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001436 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001437 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001438 orderedIds[i * 2] = viewState;
1439 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001440 }
1441 }
1442 }
Romain Guyb051e892010-09-28 19:09:36 -07001443 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1444 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1445 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001446 int numBits = Integer.bitCount(i);
1447 int[] set = new int[numBits];
1448 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001449 for (int j = 0; j < orderedIds.length; j += 2) {
1450 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001451 set[pos++] = orderedIds[j];
1452 }
1453 }
1454 VIEW_STATE_SETS[i] = set;
1455 }
1456
1457 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1458 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1459 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1460 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1462 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1463 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1465 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1467 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1468 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1469 | VIEW_STATE_FOCUSED];
1470 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1471 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1473 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1474 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1475 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1477 | VIEW_STATE_ENABLED];
1478 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1480 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1482 | VIEW_STATE_ENABLED];
1483 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1484 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1485 | VIEW_STATE_ENABLED];
1486 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1487 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1488 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1489
1490 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1491 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1492 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1493 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1495 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1496 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1497 | VIEW_STATE_PRESSED];
1498 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1499 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1500 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1501 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1502 | VIEW_STATE_PRESSED];
1503 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1504 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1505 | VIEW_STATE_PRESSED];
1506 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1507 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1508 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1509 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1510 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1511 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1512 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1513 | VIEW_STATE_PRESSED];
1514 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1515 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1516 | VIEW_STATE_PRESSED];
1517 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1518 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1519 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1520 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1521 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1522 | VIEW_STATE_PRESSED];
1523 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1524 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1525 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1526 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1527 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1528 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1529 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1530 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1531 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1532 | VIEW_STATE_PRESSED];
1533 }
1534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001536 * Accessibility event types that are dispatched for text population.
1537 */
1538 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1539 AccessibilityEvent.TYPE_VIEW_CLICKED
1540 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1541 | AccessibilityEvent.TYPE_VIEW_SELECTED
1542 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1543 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1544 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001545 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001546 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001547 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001548 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1549 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001550
1551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 * Temporary Rect currently for use in setBackground(). This will probably
1553 * be extended in the future to hold our own class with more than just
1554 * a Rect. :)
1555 */
1556 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001557
1558 /**
1559 * Map used to store views' tags.
1560 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001561 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001564 * The next available accessiiblity id.
1565 */
1566 private static int sNextAccessibilityViewId;
1567
1568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 * The animation currently associated with this view.
1570 * @hide
1571 */
1572 protected Animation mCurrentAnimation = null;
1573
1574 /**
1575 * Width as measured during measure pass.
1576 * {@hide}
1577 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001578 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001579 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580
1581 /**
1582 * Height as measured during measure pass.
1583 * {@hide}
1584 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001585 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001586 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587
1588 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001589 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1590 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1591 * its display list. This flag, used only when hw accelerated, allows us to clear the
1592 * flag while retaining this information until it's needed (at getDisplayList() time and
1593 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1594 *
1595 * {@hide}
1596 */
1597 boolean mRecreateDisplayList = false;
1598
1599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 * The view's identifier.
1601 * {@hide}
1602 *
1603 * @see #setId(int)
1604 * @see #getId()
1605 */
1606 @ViewDebug.ExportedProperty(resolveId = true)
1607 int mID = NO_ID;
1608
1609 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001610 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001611 */
1612 int mAccessibilityViewId = NO_ID;
1613
1614 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001615 * @hide
1616 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001617 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001618
1619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 * The view's tag.
1621 * {@hide}
1622 *
1623 * @see #setTag(Object)
1624 * @see #getTag()
1625 */
1626 protected Object mTag;
1627
1628 // for mPrivateFlags:
1629 /** {@hide} */
1630 static final int WANTS_FOCUS = 0x00000001;
1631 /** {@hide} */
1632 static final int FOCUSED = 0x00000002;
1633 /** {@hide} */
1634 static final int SELECTED = 0x00000004;
1635 /** {@hide} */
1636 static final int IS_ROOT_NAMESPACE = 0x00000008;
1637 /** {@hide} */
1638 static final int HAS_BOUNDS = 0x00000010;
1639 /** {@hide} */
1640 static final int DRAWN = 0x00000020;
1641 /**
1642 * When this flag is set, this view is running an animation on behalf of its
1643 * children and should therefore not cancel invalidate requests, even if they
1644 * lie outside of this view's bounds.
1645 *
1646 * {@hide}
1647 */
1648 static final int DRAW_ANIMATION = 0x00000040;
1649 /** {@hide} */
1650 static final int SKIP_DRAW = 0x00000080;
1651 /** {@hide} */
1652 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1653 /** {@hide} */
1654 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1655 /** {@hide} */
1656 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1657 /** {@hide} */
1658 static final int MEASURED_DIMENSION_SET = 0x00000800;
1659 /** {@hide} */
1660 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001661 /** {@hide} */
1662 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663
1664 private static final int PRESSED = 0x00004000;
1665
1666 /** {@hide} */
1667 static final int DRAWING_CACHE_VALID = 0x00008000;
1668 /**
1669 * Flag used to indicate that this view should be drawn once more (and only once
1670 * more) after its animation has completed.
1671 * {@hide}
1672 */
1673 static final int ANIMATION_STARTED = 0x00010000;
1674
1675 private static final int SAVE_STATE_CALLED = 0x00020000;
1676
1677 /**
1678 * Indicates that the View returned true when onSetAlpha() was called and that
1679 * the alpha must be restored.
1680 * {@hide}
1681 */
1682 static final int ALPHA_SET = 0x00040000;
1683
1684 /**
1685 * Set by {@link #setScrollContainer(boolean)}.
1686 */
1687 static final int SCROLL_CONTAINER = 0x00080000;
1688
1689 /**
1690 * Set by {@link #setScrollContainer(boolean)}.
1691 */
1692 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1693
1694 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001695 * View flag indicating whether this view was invalidated (fully or partially.)
1696 *
1697 * @hide
1698 */
1699 static final int DIRTY = 0x00200000;
1700
1701 /**
1702 * View flag indicating whether this view was invalidated by an opaque
1703 * invalidate request.
1704 *
1705 * @hide
1706 */
1707 static final int DIRTY_OPAQUE = 0x00400000;
1708
1709 /**
1710 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1711 *
1712 * @hide
1713 */
1714 static final int DIRTY_MASK = 0x00600000;
1715
1716 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001717 * Indicates whether the background is opaque.
1718 *
1719 * @hide
1720 */
1721 static final int OPAQUE_BACKGROUND = 0x00800000;
1722
1723 /**
1724 * Indicates whether the scrollbars are opaque.
1725 *
1726 * @hide
1727 */
1728 static final int OPAQUE_SCROLLBARS = 0x01000000;
1729
1730 /**
1731 * Indicates whether the view is opaque.
1732 *
1733 * @hide
1734 */
1735 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001736
Adam Powelle14579b2009-12-16 18:39:52 -08001737 /**
1738 * Indicates a prepressed state;
1739 * the short time between ACTION_DOWN and recognizing
1740 * a 'real' press. Prepressed is used to recognize quick taps
1741 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001742 *
Adam Powelle14579b2009-12-16 18:39:52 -08001743 * @hide
1744 */
1745 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001746
Adam Powellc9fbaab2010-02-16 17:16:19 -08001747 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001748 * Indicates whether the view is temporarily detached.
1749 *
1750 * @hide
1751 */
1752 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001753
Adam Powell8568c3a2010-04-19 14:26:11 -07001754 /**
1755 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001756 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001757 * @hide
1758 */
1759 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001760
1761 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001762 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1763 * @hide
1764 */
1765 private static final int HOVERED = 0x10000000;
1766
1767 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001768 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1769 * for transform operations
1770 *
1771 * @hide
1772 */
Adam Powellf37df072010-09-17 16:22:49 -07001773 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001774
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001775 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001776 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001777
Chet Haasefd2b0022010-08-06 13:08:56 -07001778 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001779 * Indicates that this view was specifically invalidated, not just dirtied because some
1780 * child view was invalidated. The flag is used to determine when we need to recreate
1781 * a view's display list (as opposed to just returning a reference to its existing
1782 * display list).
1783 *
1784 * @hide
1785 */
1786 static final int INVALIDATED = 0x80000000;
1787
Christopher Tate3d4bf172011-03-28 16:16:46 -07001788 /* Masks for mPrivateFlags2 */
1789
1790 /**
1791 * Indicates that this view has reported that it can accept the current drag's content.
1792 * Cleared when the drag operation concludes.
1793 * @hide
1794 */
1795 static final int DRAG_CAN_ACCEPT = 0x00000001;
1796
1797 /**
1798 * Indicates that this view is currently directly under the drag location in a
1799 * drag-and-drop operation involving content that it can accept. Cleared when
1800 * the drag exits the view, or when the drag operation concludes.
1801 * @hide
1802 */
1803 static final int DRAG_HOVERED = 0x00000002;
1804
Cibu Johny86666632010-02-22 13:01:02 -08001805 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001806 * Horizontal layout direction of this view is from Left to Right.
1807 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001808 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001809 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001810
1811 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001812 * Horizontal layout direction of this view is from Right to Left.
1813 * Use with {@link #setLayoutDirection}.
1814 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001815 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001816
1817 /**
1818 * Horizontal layout direction of this view is inherited from its parent.
1819 * Use with {@link #setLayoutDirection}.
1820 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001821 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001822
1823 /**
1824 * Horizontal layout direction of this view is from deduced from the default language
1825 * script for the locale. Use with {@link #setLayoutDirection}.
1826 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001827 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001828
1829 /**
1830 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001831 * @hide
1832 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001833 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1834
1835 /**
1836 * Mask for use with private flags indicating bits used for horizontal layout direction.
1837 * @hide
1838 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001839 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001840
1841 /**
1842 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1843 * right-to-left direction.
1844 * @hide
1845 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001846 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001847
1848 /**
1849 * Indicates whether the view horizontal layout direction has been resolved.
1850 * @hide
1851 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001852 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001853
1854 /**
1855 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1856 * @hide
1857 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001858 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001859
1860 /*
1861 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1862 * flag value.
1863 * @hide
1864 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001865 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1866 LAYOUT_DIRECTION_LTR,
1867 LAYOUT_DIRECTION_RTL,
1868 LAYOUT_DIRECTION_INHERIT,
1869 LAYOUT_DIRECTION_LOCALE
1870 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001871
1872 /**
1873 * Default horizontal layout direction.
1874 * @hide
1875 */
1876 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001877
Adam Powell539ee872012-02-03 19:00:49 -08001878 /**
1879 * Indicates that the view is tracking some sort of transient state
1880 * that the app should not need to be aware of, but that the framework
1881 * should take special care to preserve.
1882 *
1883 * @hide
1884 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001885 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001886
1887
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001888 /**
1889 * Text direction is inherited thru {@link ViewGroup}
1890 */
1891 public static final int TEXT_DIRECTION_INHERIT = 0;
1892
1893 /**
1894 * Text direction is using "first strong algorithm". The first strong directional character
1895 * determines the paragraph direction. If there is no strong directional character, the
1896 * paragraph direction is the view's resolved layout direction.
1897 */
1898 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1899
1900 /**
1901 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1902 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1903 * If there are neither, the paragraph direction is the view's resolved layout direction.
1904 */
1905 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1906
1907 /**
1908 * Text direction is forced to LTR.
1909 */
1910 public static final int TEXT_DIRECTION_LTR = 3;
1911
1912 /**
1913 * Text direction is forced to RTL.
1914 */
1915 public static final int TEXT_DIRECTION_RTL = 4;
1916
1917 /**
1918 * Text direction is coming from the system Locale.
1919 */
1920 public static final int TEXT_DIRECTION_LOCALE = 5;
1921
1922 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001923 * Default text direction is inherited
1924 */
1925 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1926
1927 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001928 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1929 * @hide
1930 */
1931 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1932
1933 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001934 * Mask for use with private flags indicating bits used for text direction.
1935 * @hide
1936 */
1937 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1938
1939 /**
1940 * Array of text direction flags for mapping attribute "textDirection" to correct
1941 * flag value.
1942 * @hide
1943 */
1944 private static final int[] TEXT_DIRECTION_FLAGS = {
1945 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1946 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1947 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1948 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1949 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1950 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1951 };
1952
1953 /**
1954 * Indicates whether the view text direction has been resolved.
1955 * @hide
1956 */
1957 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1958
1959 /**
1960 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1961 * @hide
1962 */
1963 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1964
1965 /**
1966 * Mask for use with private flags indicating bits used for resolved text direction.
1967 * @hide
1968 */
1969 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1970
1971 /**
1972 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1973 * @hide
1974 */
1975 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1976 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1977
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001978 /*
1979 * Default text alignment. The text alignment of this View is inherited from its parent.
1980 * Use with {@link #setTextAlignment(int)}
1981 */
1982 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1983
1984 /**
1985 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1986 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1987 *
1988 * Use with {@link #setTextAlignment(int)}
1989 */
1990 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1991
1992 /**
1993 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1994 *
1995 * Use with {@link #setTextAlignment(int)}
1996 */
1997 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1998
1999 /**
2000 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
2001 *
2002 * Use with {@link #setTextAlignment(int)}
2003 */
2004 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2005
2006 /**
2007 * Center the paragraph, e.g. ALIGN_CENTER.
2008 *
2009 * Use with {@link #setTextAlignment(int)}
2010 */
2011 public static final int TEXT_ALIGNMENT_CENTER = 4;
2012
2013 /**
2014 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2015 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2016 *
2017 * Use with {@link #setTextAlignment(int)}
2018 */
2019 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2020
2021 /**
2022 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2023 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2024 *
2025 * Use with {@link #setTextAlignment(int)}
2026 */
2027 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2028
2029 /**
2030 * Default text alignment is inherited
2031 */
2032 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2033
2034 /**
2035 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2036 * @hide
2037 */
2038 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2039
2040 /**
2041 * Mask for use with private flags indicating bits used for text alignment.
2042 * @hide
2043 */
2044 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2045
2046 /**
2047 * Array of text direction flags for mapping attribute "textAlignment" to correct
2048 * flag value.
2049 * @hide
2050 */
2051 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2052 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2053 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2054 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2055 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2056 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2057 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2058 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2059 };
2060
2061 /**
2062 * Indicates whether the view text alignment has been resolved.
2063 * @hide
2064 */
2065 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2066
2067 /**
2068 * Bit shift to get the resolved text alignment.
2069 * @hide
2070 */
2071 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2072
2073 /**
2074 * Mask for use with private flags indicating bits used for text alignment.
2075 * @hide
2076 */
2077 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2078
2079 /**
2080 * Indicates whether if the view text alignment has been resolved to gravity
2081 */
2082 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2083 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2084
Svetoslav Ganov42138042012-03-20 11:51:39 -07002085 // Accessiblity constants for mPrivateFlags2
2086
2087 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002088 * Shift for the bits in {@link #mPrivateFlags2} related to the
2089 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002090 */
2091 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2092
2093 /**
2094 * Automatically determine whether a view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2097
2098 /**
2099 * The view is important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2102
2103 /**
2104 * The view is not important for accessibility.
2105 */
2106 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2107
2108 /**
2109 * The default whether the view is important for accessiblity.
2110 */
2111 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2112
2113 /**
2114 * Mask for obtainig the bits which specify how to determine
2115 * whether a view is important for accessibility.
2116 */
2117 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2118 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2119 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2120
2121 /**
2122 * Flag indicating whether a view has accessibility focus.
2123 */
2124 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2125
2126 /**
2127 * Flag indicating whether a view state for accessibility has changed.
2128 */
2129 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002130
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002131 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002132 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2133 * is used to check whether later changes to the view's transform should invalidate the
2134 * view to force the quickReject test to run again.
2135 */
Chet Haase21433372012-06-05 07:54:09 -07002136 static final int VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002137
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002138 // Accessiblity constants for mPrivateFlags2
2139
2140 /**
2141 * Shift for the bits in {@link #mPrivateFlags2} related to the
2142 * "accessibilityFocusable" attribute.
2143 */
Chet Haase21433372012-06-05 07:54:09 -07002144 static final int ACCESSIBILITY_FOCUSABLE_SHIFT = 29;
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002145
2146 /**
2147 * The system determines whether the view can take accessibility focus - default (recommended).
2148 * <p>
2149 * Such a view is consideted by the focus search if it is:
2150 * <ul>
2151 * <li>
2152 * Important for accessibility and actionable (clickable, long clickable, focusable)
2153 * </li>
2154 * <li>
2155 * Important for accessibility, not actionable (clickable, long clickable, focusable),
2156 * and does not have an actionable predecessor.
2157 * </li>
2158 * </ul>
2159 * An accessibility srvice can request putting accessibility focus on such a view.
2160 * </p>
2161 *
2162 * @hide
2163 */
2164 public static final int ACCESSIBILITY_FOCUSABLE_AUTO = 0x00000000;
2165
2166 /**
2167 * The view can take accessibility focus.
2168 * <p>
2169 * A view that can take accessibility focus is always considered during focus
2170 * search and an accessibility service can request putting accessibility focus
2171 * on it.
2172 * </p>
2173 *
2174 * @hide
2175 */
2176 public static final int ACCESSIBILITY_FOCUSABLE_YES = 0x00000001;
2177
2178 /**
2179 * The view can not take accessibility focus.
2180 * <p>
2181 * A view that can not take accessibility focus is never considered during focus
2182 * search and an accessibility service can not request putting accessibility focus
2183 * on it.
2184 * </p>
2185 *
2186 * @hide
2187 */
2188 public static final int ACCESSIBILITY_FOCUSABLE_NO = 0x00000002;
2189
2190 /**
2191 * The default whether the view is accessiblity focusable.
2192 */
2193 static final int ACCESSIBILITY_FOCUSABLE_DEFAULT = ACCESSIBILITY_FOCUSABLE_AUTO;
2194
2195 /**
2196 * Mask for obtainig the bits which specifies how to determine
2197 * whether a view is accessibility focusable.
2198 */
2199 static final int ACCESSIBILITY_FOCUSABLE_MASK = (ACCESSIBILITY_FOCUSABLE_AUTO
2200 | ACCESSIBILITY_FOCUSABLE_YES | ACCESSIBILITY_FOCUSABLE_NO)
2201 << ACCESSIBILITY_FOCUSABLE_SHIFT;
2202
2203
Christopher Tate3d4bf172011-03-28 16:16:46 -07002204 /* End of masks for mPrivateFlags2 */
2205
Chet Haase21433372012-06-05 07:54:09 -07002206 /* Masks for mPrivateFlags3 */
2207
2208 /**
2209 * Flag indicating that view has a transform animation set on it. This is used to track whether
2210 * an animation is cleared between successive frames, in order to tell the associated
2211 * DisplayList to clear its animation matrix.
2212 */
2213 static final int VIEW_IS_ANIMATING_TRANSFORM = 0x1;
2214
2215 /**
2216 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2217 * animation is cleared between successive frames, in order to tell the associated
2218 * DisplayList to restore its alpha value.
2219 */
2220 static final int VIEW_IS_ANIMATING_ALPHA = 0x2;
2221
2222
2223 /* End of masks for mPrivateFlags3 */
2224
Christopher Tate3d4bf172011-03-28 16:16:46 -07002225 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2226
Chet Haasedaf98e92011-01-10 14:10:36 -08002227 /**
Adam Powell637d3372010-08-25 14:37:03 -07002228 * Always allow a user to over-scroll this view, provided it is a
2229 * view that can scroll.
2230 *
2231 * @see #getOverScrollMode()
2232 * @see #setOverScrollMode(int)
2233 */
2234 public static final int OVER_SCROLL_ALWAYS = 0;
2235
2236 /**
2237 * Allow a user to over-scroll this view only if the content is large
2238 * enough to meaningfully scroll, provided it is a view that can scroll.
2239 *
2240 * @see #getOverScrollMode()
2241 * @see #setOverScrollMode(int)
2242 */
2243 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2244
2245 /**
2246 * Never allow a user to over-scroll this view.
2247 *
2248 * @see #getOverScrollMode()
2249 * @see #setOverScrollMode(int)
2250 */
2251 public static final int OVER_SCROLL_NEVER = 2;
2252
2253 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002254 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2255 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002256 *
Joe Malin32736f02011-01-19 16:14:20 -08002257 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002258 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002259 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002260
2261 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002262 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2263 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002264 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002265 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002266 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002267 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002268 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002269 *
Joe Malin32736f02011-01-19 16:14:20 -08002270 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002271 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002272 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2273
2274 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002275 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2276 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002277 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002278 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002279 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2280 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2281 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002282 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002283 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2284 * window flags) for displaying content using every last pixel on the display.
2285 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002286 * <p>There is a limitation: because navigation controls are so important, the least user
2287 * interaction will cause them to reappear immediately. When this happens, both
2288 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2289 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002290 *
2291 * @see #setSystemUiVisibility(int)
2292 */
2293 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2294
2295 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002296 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2297 * into the normal fullscreen mode so that its content can take over the screen
2298 * while still allowing the user to interact with the application.
2299 *
2300 * <p>This has the same visual effect as
2301 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2302 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2303 * meaning that non-critical screen decorations (such as the status bar) will be
2304 * hidden while the user is in the View's window, focusing the experience on
2305 * that content. Unlike the window flag, if you are using ActionBar in
2306 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2307 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2308 * hide the action bar.
2309 *
2310 * <p>This approach to going fullscreen is best used over the window flag when
2311 * it is a transient state -- that is, the application does this at certain
2312 * points in its user interaction where it wants to allow the user to focus
2313 * on content, but not as a continuous state. For situations where the application
2314 * would like to simply stay full screen the entire time (such as a game that
2315 * wants to take over the screen), the
2316 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2317 * is usually a better approach. The state set here will be removed by the system
2318 * in various situations (such as the user moving to another application) like
2319 * the other system UI states.
2320 *
2321 * <p>When using this flag, the application should provide some easy facility
2322 * for the user to go out of it. A common example would be in an e-book
2323 * reader, where tapping on the screen brings back whatever screen and UI
2324 * decorations that had been hidden while the user was immersed in reading
2325 * the book.
2326 *
2327 * @see #setSystemUiVisibility(int)
2328 */
2329 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2330
2331 /**
2332 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2333 * flags, we would like a stable view of the content insets given to
2334 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2335 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002336 * as a continuous state. In the stock Android UI this is the space for
2337 * the system bar, nav bar, and status bar, but not more transient elements
2338 * such as an input method.
2339 *
2340 * The stable layout your UI sees is based on the system UI modes you can
2341 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2342 * then you will get a stable layout for changes of the
2343 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2344 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2345 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2346 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2347 * with a stable layout. (Note that you should avoid using
2348 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2349 *
2350 * If you have set the window flag {@ WindowManager.LayoutParams#FLAG_FULLSCREEN}
2351 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2352 * then a hidden status bar will be considered a "stable" state for purposes
2353 * here. This allows your UI to continually hide the status bar, while still
2354 * using the system UI flags to hide the action bar while still retaining
2355 * a stable layout. Note that changing the window fullscreen flag will never
2356 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002357 *
2358 * <p>If you are using ActionBar in
2359 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2360 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2361 * insets it adds to those given to the application.
2362 */
2363 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2364
2365 /**
2366 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2367 * to be layed out as if it has requested
2368 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2369 * allows it to avoid artifacts when switching in and out of that mode, at
2370 * the expense that some of its user interface may be covered by screen
2371 * decorations when they are shown. You can perform layout of your inner
2372 * UI elements to account for the navagation system UI through the
2373 * {@link #fitSystemWindows(Rect)} method.
2374 */
2375 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2376
2377 /**
2378 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2379 * to be layed out as if it has requested
2380 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2381 * allows it to avoid artifacts when switching in and out of that mode, at
2382 * the expense that some of its user interface may be covered by screen
2383 * decorations when they are shown. You can perform layout of your inner
2384 * UI elements to account for non-fullscreen system UI through the
2385 * {@link #fitSystemWindows(Rect)} method.
2386 */
2387 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2388
2389 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002390 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2391 */
2392 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2393
2394 /**
2395 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2396 */
2397 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002398
2399 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002400 * @hide
2401 *
2402 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2403 * out of the public fields to keep the undefined bits out of the developer's way.
2404 *
2405 * Flag to make the status bar not expandable. Unless you also
2406 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2407 */
2408 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2409
2410 /**
2411 * @hide
2412 *
2413 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2414 * out of the public fields to keep the undefined bits out of the developer's way.
2415 *
2416 * Flag to hide notification icons and scrolling ticker text.
2417 */
2418 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2419
2420 /**
2421 * @hide
2422 *
2423 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2424 * out of the public fields to keep the undefined bits out of the developer's way.
2425 *
2426 * Flag to disable incoming notification alerts. This will not block
2427 * icons, but it will block sound, vibrating and other visual or aural notifications.
2428 */
2429 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2430
2431 /**
2432 * @hide
2433 *
2434 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2435 * out of the public fields to keep the undefined bits out of the developer's way.
2436 *
2437 * Flag to hide only the scrolling ticker. Note that
2438 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2439 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2440 */
2441 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2442
2443 /**
2444 * @hide
2445 *
2446 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2447 * out of the public fields to keep the undefined bits out of the developer's way.
2448 *
2449 * Flag to hide the center system info area.
2450 */
2451 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2452
2453 /**
2454 * @hide
2455 *
2456 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2457 * out of the public fields to keep the undefined bits out of the developer's way.
2458 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002459 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002460 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2461 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002462 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002463
2464 /**
2465 * @hide
2466 *
2467 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2468 * out of the public fields to keep the undefined bits out of the developer's way.
2469 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002470 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002471 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2472 */
2473 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2474
2475 /**
2476 * @hide
2477 *
2478 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2479 * out of the public fields to keep the undefined bits out of the developer's way.
2480 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002481 * Flag to hide only the clock. You might use this if your activity has
2482 * its own clock making the status bar's clock redundant.
2483 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002484 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2485
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002486 /**
2487 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002488 *
2489 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2490 * out of the public fields to keep the undefined bits out of the developer's way.
2491 *
2492 * Flag to hide only the recent apps button. Don't use this
2493 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2494 */
2495 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2496
2497 /**
2498 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002499 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002500 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002501
2502 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002503 * These are the system UI flags that can be cleared by events outside
2504 * of an application. Currently this is just the ability to tap on the
2505 * screen while hiding the navigation bar to have it return.
2506 * @hide
2507 */
2508 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002509 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2510 | SYSTEM_UI_FLAG_FULLSCREEN;
2511
2512 /**
2513 * Flags that can impact the layout in relation to system UI.
2514 */
2515 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2516 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2517 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002518
2519 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002520 * Find views that render the specified text.
2521 *
2522 * @see #findViewsWithText(ArrayList, CharSequence, int)
2523 */
2524 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2525
2526 /**
2527 * Find find views that contain the specified content description.
2528 *
2529 * @see #findViewsWithText(ArrayList, CharSequence, int)
2530 */
2531 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2532
2533 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002534 * Find views that contain {@link AccessibilityNodeProvider}. Such
2535 * a View is a root of virtual view hierarchy and may contain the searched
2536 * text. If this flag is set Views with providers are automatically
2537 * added and it is a responsibility of the client to call the APIs of
2538 * the provider to determine whether the virtual tree rooted at this View
2539 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2540 * represeting the virtual views with this text.
2541 *
2542 * @see #findViewsWithText(ArrayList, CharSequence, int)
2543 *
2544 * @hide
2545 */
2546 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2547
2548 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002549 * The undefined cursor position.
2550 */
2551 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2552
2553 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002554 * Indicates that the screen has changed state and is now off.
2555 *
2556 * @see #onScreenStateChanged(int)
2557 */
2558 public static final int SCREEN_STATE_OFF = 0x0;
2559
2560 /**
2561 * Indicates that the screen has changed state and is now on.
2562 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002563 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002564 */
2565 public static final int SCREEN_STATE_ON = 0x1;
2566
2567 /**
Adam Powell637d3372010-08-25 14:37:03 -07002568 * Controls the over-scroll mode for this view.
2569 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2570 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2571 * and {@link #OVER_SCROLL_NEVER}.
2572 */
2573 private int mOverScrollMode;
2574
2575 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 * The parent this view is attached to.
2577 * {@hide}
2578 *
2579 * @see #getParent()
2580 */
2581 protected ViewParent mParent;
2582
2583 /**
2584 * {@hide}
2585 */
2586 AttachInfo mAttachInfo;
2587
2588 /**
2589 * {@hide}
2590 */
Romain Guy809a7f62009-05-14 15:44:42 -07002591 @ViewDebug.ExportedProperty(flagMapping = {
2592 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2593 name = "FORCE_LAYOUT"),
2594 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2595 name = "LAYOUT_REQUIRED"),
2596 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002597 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002598 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2599 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2600 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2601 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2602 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002604 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002605 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606
2607 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002608 * This view's request for the visibility of the status bar.
2609 * @hide
2610 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002611 @ViewDebug.ExportedProperty(flagMapping = {
2612 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2613 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2614 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2615 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2616 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2617 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2618 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2619 equals = SYSTEM_UI_FLAG_VISIBLE,
2620 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2621 })
Joe Onorato664644d2011-01-23 17:53:23 -08002622 int mSystemUiVisibility;
2623
2624 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002625 * Reference count for transient state.
2626 * @see #setHasTransientState(boolean)
2627 */
2628 int mTransientStateCount = 0;
2629
2630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 * Count of how many windows this view has been attached to.
2632 */
2633 int mWindowAttachCount;
2634
2635 /**
2636 * The layout parameters associated with this view and used by the parent
2637 * {@link android.view.ViewGroup} to determine how this view should be
2638 * laid out.
2639 * {@hide}
2640 */
2641 protected ViewGroup.LayoutParams mLayoutParams;
2642
2643 /**
2644 * The view flags hold various views states.
2645 * {@hide}
2646 */
2647 @ViewDebug.ExportedProperty
2648 int mViewFlags;
2649
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002650 static class TransformationInfo {
2651 /**
2652 * The transform matrix for the View. This transform is calculated internally
2653 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2654 * is used by default. Do *not* use this variable directly; instead call
2655 * getMatrix(), which will automatically recalculate the matrix if necessary
2656 * to get the correct matrix based on the latest rotation and scale properties.
2657 */
2658 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002659
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002660 /**
2661 * The transform matrix for the View. This transform is calculated internally
2662 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2663 * is used by default. Do *not* use this variable directly; instead call
2664 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2665 * to get the correct matrix based on the latest rotation and scale properties.
2666 */
2667 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002668
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002669 /**
2670 * An internal variable that tracks whether we need to recalculate the
2671 * transform matrix, based on whether the rotation or scaleX/Y properties
2672 * have changed since the matrix was last calculated.
2673 */
2674 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002675
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002676 /**
2677 * An internal variable that tracks whether we need to recalculate the
2678 * transform matrix, based on whether the rotation or scaleX/Y properties
2679 * have changed since the matrix was last calculated.
2680 */
2681 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002682
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002683 /**
2684 * A variable that tracks whether we need to recalculate the
2685 * transform matrix, based on whether the rotation or scaleX/Y properties
2686 * have changed since the matrix was last calculated. This variable
2687 * is only valid after a call to updateMatrix() or to a function that
2688 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2689 */
2690 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002691
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002692 /**
2693 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2694 */
2695 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002696
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002697 /**
2698 * This matrix is used when computing the matrix for 3D rotations.
2699 */
2700 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002701
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002702 /**
2703 * These prev values are used to recalculate a centered pivot point when necessary. The
2704 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2705 * set), so thes values are only used then as well.
2706 */
2707 private int mPrevWidth = -1;
2708 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002709
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002710 /**
2711 * The degrees rotation around the vertical axis through the pivot point.
2712 */
2713 @ViewDebug.ExportedProperty
2714 float mRotationY = 0f;
2715
2716 /**
2717 * The degrees rotation around the horizontal axis through the pivot point.
2718 */
2719 @ViewDebug.ExportedProperty
2720 float mRotationX = 0f;
2721
2722 /**
2723 * The degrees rotation around the pivot point.
2724 */
2725 @ViewDebug.ExportedProperty
2726 float mRotation = 0f;
2727
2728 /**
2729 * The amount of translation of the object away from its left property (post-layout).
2730 */
2731 @ViewDebug.ExportedProperty
2732 float mTranslationX = 0f;
2733
2734 /**
2735 * The amount of translation of the object away from its top property (post-layout).
2736 */
2737 @ViewDebug.ExportedProperty
2738 float mTranslationY = 0f;
2739
2740 /**
2741 * The amount of scale in the x direction around the pivot point. A
2742 * value of 1 means no scaling is applied.
2743 */
2744 @ViewDebug.ExportedProperty
2745 float mScaleX = 1f;
2746
2747 /**
2748 * The amount of scale in the y direction around the pivot point. A
2749 * value of 1 means no scaling is applied.
2750 */
2751 @ViewDebug.ExportedProperty
2752 float mScaleY = 1f;
2753
2754 /**
Chet Haasea33de552012-02-03 16:28:24 -08002755 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002756 */
2757 @ViewDebug.ExportedProperty
2758 float mPivotX = 0f;
2759
2760 /**
Chet Haasea33de552012-02-03 16:28:24 -08002761 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002762 */
2763 @ViewDebug.ExportedProperty
2764 float mPivotY = 0f;
2765
2766 /**
2767 * The opacity of the View. This is a value from 0 to 1, where 0 means
2768 * completely transparent and 1 means completely opaque.
2769 */
2770 @ViewDebug.ExportedProperty
2771 float mAlpha = 1f;
2772 }
2773
2774 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002775
Joe Malin32736f02011-01-19 16:14:20 -08002776 private boolean mLastIsOpaque;
2777
Chet Haasefd2b0022010-08-06 13:08:56 -07002778 /**
2779 * Convenience value to check for float values that are close enough to zero to be considered
2780 * zero.
2781 */
Romain Guy2542d192010-08-18 11:47:12 -07002782 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002783
2784 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 * The distance in pixels from the left edge of this view's parent
2786 * to the left edge of this view.
2787 * {@hide}
2788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 protected int mLeft;
2791 /**
2792 * The distance in pixels from the left edge of this view's parent
2793 * to the right edge of this view.
2794 * {@hide}
2795 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002796 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 protected int mRight;
2798 /**
2799 * The distance in pixels from the top edge of this view's parent
2800 * to the top edge of this view.
2801 * {@hide}
2802 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002803 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 protected int mTop;
2805 /**
2806 * The distance in pixels from the top edge of this view's parent
2807 * to the bottom edge of this view.
2808 * {@hide}
2809 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002810 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 protected int mBottom;
2812
2813 /**
2814 * The offset, in pixels, by which the content of this view is scrolled
2815 * horizontally.
2816 * {@hide}
2817 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002818 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 protected int mScrollX;
2820 /**
2821 * The offset, in pixels, by which the content of this view is scrolled
2822 * vertically.
2823 * {@hide}
2824 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002825 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 protected int mScrollY;
2827
2828 /**
2829 * The left padding in pixels, that is the distance in pixels between the
2830 * left edge of this view and the left edge of its content.
2831 * {@hide}
2832 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002833 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 protected int mPaddingLeft;
2835 /**
2836 * The right padding in pixels, that is the distance in pixels between the
2837 * right edge of this view and the right edge of its content.
2838 * {@hide}
2839 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002840 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 protected int mPaddingRight;
2842 /**
2843 * The top padding in pixels, that is the distance in pixels between the
2844 * top edge of this view and the top edge of its content.
2845 * {@hide}
2846 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002847 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 protected int mPaddingTop;
2849 /**
2850 * The bottom padding in pixels, that is the distance in pixels between the
2851 * bottom edge of this view and the bottom edge of its content.
2852 * {@hide}
2853 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002854 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 protected int mPaddingBottom;
2856
2857 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002858 * The layout insets in pixels, that is the distance in pixels between the
2859 * visible edges of this view its bounds.
2860 */
2861 private Insets mLayoutInsets;
2862
2863 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002864 * Briefly describes the view and is primarily used for accessibility support.
2865 */
2866 private CharSequence mContentDescription;
2867
2868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002870 *
2871 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002873 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002874 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
2876 /**
2877 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002878 *
2879 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002881 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002882 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002884 /**
Adam Powell20232d02010-12-08 21:08:53 -08002885 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002886 *
2887 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002888 */
2889 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002890 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002891
2892 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002893 * Cache if the user padding is relative.
2894 *
2895 */
2896 @ViewDebug.ExportedProperty(category = "padding")
2897 boolean mUserPaddingRelative;
2898
2899 /**
2900 * Cache the paddingStart set by the user to append to the scrollbar's size.
2901 *
2902 */
2903 @ViewDebug.ExportedProperty(category = "padding")
2904 int mUserPaddingStart;
2905
2906 /**
2907 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2908 *
2909 */
2910 @ViewDebug.ExportedProperty(category = "padding")
2911 int mUserPaddingEnd;
2912
2913 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002914 * @hide
2915 */
2916 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2917 /**
2918 * @hide
2919 */
2920 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921
Philip Milne6c8ea062012-04-03 17:38:43 -07002922 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923
2924 private int mBackgroundResource;
2925 private boolean mBackgroundSizeChanged;
2926
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002927 static class ListenerInfo {
2928 /**
2929 * Listener used to dispatch focus change events.
2930 * This field should be made private, so it is hidden from the SDK.
2931 * {@hide}
2932 */
2933 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002935 /**
2936 * Listeners for layout change events.
2937 */
2938 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002939
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002940 /**
2941 * Listeners for attach events.
2942 */
2943 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002944
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002945 /**
2946 * Listener used to dispatch click events.
2947 * This field should be made private, so it is hidden from the SDK.
2948 * {@hide}
2949 */
2950 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002952 /**
2953 * Listener used to dispatch long click events.
2954 * This field should be made private, so it is hidden from the SDK.
2955 * {@hide}
2956 */
2957 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002959 /**
2960 * Listener used to build the context menu.
2961 * This field should be made private, so it is hidden from the SDK.
2962 * {@hide}
2963 */
2964 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002966 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002968 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002970 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002971
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002972 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002973
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002974 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002975
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002976 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2977 }
2978
2979 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 /**
2982 * The application environment this view lives in.
2983 * This field should be made private, so it is hidden from the SDK.
2984 * {@hide}
2985 */
2986 protected Context mContext;
2987
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002988 private final Resources mResources;
2989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 private ScrollabilityCache mScrollCache;
2991
2992 private int[] mDrawableState = null;
2993
Romain Guy0211a0a2011-02-14 16:34:59 -08002994 /**
2995 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002996 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002997 * @hide
2998 */
2999 public boolean mCachingFailed;
3000
Romain Guy02890fd2010-08-06 17:58:44 -07003001 private Bitmap mDrawingCache;
3002 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08003003 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003004 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005
3006 /**
3007 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3008 * the user may specify which view to go to next.
3009 */
3010 private int mNextFocusLeftId = View.NO_ID;
3011
3012 /**
3013 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3014 * the user may specify which view to go to next.
3015 */
3016 private int mNextFocusRightId = View.NO_ID;
3017
3018 /**
3019 * When this view has focus and the next focus is {@link #FOCUS_UP},
3020 * the user may specify which view to go to next.
3021 */
3022 private int mNextFocusUpId = View.NO_ID;
3023
3024 /**
3025 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3026 * the user may specify which view to go to next.
3027 */
3028 private int mNextFocusDownId = View.NO_ID;
3029
Jeff Brown4e6319b2010-12-13 10:36:51 -08003030 /**
3031 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3032 * the user may specify which view to go to next.
3033 */
3034 int mNextFocusForwardId = View.NO_ID;
3035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003037 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003038 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003039 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 private UnsetPressedState mUnsetPressedState;
3042
3043 /**
3044 * Whether the long press's action has been invoked. The tap's action is invoked on the
3045 * up event while a long press is invoked as soon as the long press duration is reached, so
3046 * a long press could be performed before the tap is checked, in which case the tap's action
3047 * should not be invoked.
3048 */
3049 private boolean mHasPerformedLongPress;
3050
3051 /**
3052 * The minimum height of the view. We'll try our best to have the height
3053 * of this view to at least this amount.
3054 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003055 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 private int mMinHeight;
3057
3058 /**
3059 * The minimum width of the view. We'll try our best to have the width
3060 * of this view to at least this amount.
3061 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003062 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 private int mMinWidth;
3064
3065 /**
3066 * The delegate to handle touch events that are physically in this view
3067 * but should be handled by another view.
3068 */
3069 private TouchDelegate mTouchDelegate = null;
3070
3071 /**
3072 * Solid color to use as a background when creating the drawing cache. Enables
3073 * the cache to use 16 bit bitmaps instead of 32 bit.
3074 */
3075 private int mDrawingCacheBackgroundColor = 0;
3076
3077 /**
3078 * Special tree observer used when mAttachInfo is null.
3079 */
3080 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003081
Adam Powelle14579b2009-12-16 18:39:52 -08003082 /**
3083 * Cache the touch slop from the context that created the view.
3084 */
3085 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003088 * Object that handles automatic animation of view properties.
3089 */
3090 private ViewPropertyAnimator mAnimator = null;
3091
3092 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003093 * Flag indicating that a drag can cross window boundaries. When
3094 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3095 * with this flag set, all visible applications will be able to participate
3096 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003097 *
3098 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003099 */
3100 public static final int DRAG_FLAG_GLOBAL = 1;
3101
3102 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003103 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3104 */
3105 private float mVerticalScrollFactor;
3106
3107 /**
Adam Powell20232d02010-12-08 21:08:53 -08003108 * Position of the vertical scroll bar.
3109 */
3110 private int mVerticalScrollbarPosition;
3111
3112 /**
3113 * Position the scroll bar at the default position as determined by the system.
3114 */
3115 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3116
3117 /**
3118 * Position the scroll bar along the left edge.
3119 */
3120 public static final int SCROLLBAR_POSITION_LEFT = 1;
3121
3122 /**
3123 * Position the scroll bar along the right edge.
3124 */
3125 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3126
3127 /**
Romain Guy171c5922011-01-06 10:04:23 -08003128 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003129 *
3130 * @see #getLayerType()
3131 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003132 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003133 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003134 */
3135 public static final int LAYER_TYPE_NONE = 0;
3136
3137 /**
3138 * <p>Indicates that the view has a software layer. A software layer is backed
3139 * by a bitmap and causes the view to be rendered using Android's software
3140 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003141 *
Romain Guy171c5922011-01-06 10:04:23 -08003142 * <p>Software layers have various usages:</p>
3143 * <p>When the application is not using hardware acceleration, a software layer
3144 * is useful to apply a specific color filter and/or blending mode and/or
3145 * translucency to a view and all its children.</p>
3146 * <p>When the application is using hardware acceleration, a software layer
3147 * is useful to render drawing primitives not supported by the hardware
3148 * accelerated pipeline. It can also be used to cache a complex view tree
3149 * into a texture and reduce the complexity of drawing operations. For instance,
3150 * when animating a complex view tree with a translation, a software layer can
3151 * be used to render the view tree only once.</p>
3152 * <p>Software layers should be avoided when the affected view tree updates
3153 * often. Every update will require to re-render the software layer, which can
3154 * potentially be slow (particularly when hardware acceleration is turned on
3155 * since the layer will have to be uploaded into a hardware texture after every
3156 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003157 *
3158 * @see #getLayerType()
3159 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003160 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003161 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003162 */
3163 public static final int LAYER_TYPE_SOFTWARE = 1;
3164
3165 /**
3166 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3167 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3168 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3169 * rendering pipeline, but only if hardware acceleration is turned on for the
3170 * view hierarchy. When hardware acceleration is turned off, hardware layers
3171 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003172 *
Romain Guy171c5922011-01-06 10:04:23 -08003173 * <p>A hardware layer is useful to apply a specific color filter and/or
3174 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003175 * <p>A hardware layer can be used to cache a complex view tree into a
3176 * texture and reduce the complexity of drawing operations. For instance,
3177 * when animating a complex view tree with a translation, a hardware layer can
3178 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003179 * <p>A hardware layer can also be used to increase the rendering quality when
3180 * rotation transformations are applied on a view. It can also be used to
3181 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003182 *
3183 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003184 * @see #setLayerType(int, android.graphics.Paint)
3185 * @see #LAYER_TYPE_NONE
3186 * @see #LAYER_TYPE_SOFTWARE
3187 */
3188 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003189
Romain Guy3aaff3a2011-01-12 14:18:47 -08003190 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3191 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3192 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3193 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3194 })
Romain Guy171c5922011-01-06 10:04:23 -08003195 int mLayerType = LAYER_TYPE_NONE;
3196 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003197 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003198
3199 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003200 * Set to true when the view is sending hover accessibility events because it
3201 * is the innermost hovered view.
3202 */
3203 private boolean mSendingHoverAccessibilityEvents;
3204
3205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 * Simple constructor to use when creating a view from code.
3207 *
3208 * @param context The Context the view is running in, through which it can
3209 * access the current theme, resources, etc.
3210 */
3211 public View(Context context) {
3212 mContext = context;
3213 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003214 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003215 // Set layout and text direction defaults
3216 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003217 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003218 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07003219 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT) |
3220 (ACCESSIBILITY_FOCUSABLE_DEFAULT << ACCESSIBILITY_FOCUSABLE_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003221 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003222 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003223 mUserPaddingStart = -1;
3224 mUserPaddingEnd = -1;
3225 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 }
3227
3228 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003229 * Delegate for injecting accessiblity functionality.
3230 */
3231 AccessibilityDelegate mAccessibilityDelegate;
3232
3233 /**
3234 * Consistency verifier for debugging purposes.
3235 * @hide
3236 */
3237 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3238 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3239 new InputEventConsistencyVerifier(this, 0) : null;
3240
3241 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 * Constructor that is called when inflating a view from XML. This is called
3243 * when a view is being constructed from an XML file, supplying attributes
3244 * that were specified in the XML file. This version uses a default style of
3245 * 0, so the only attribute values applied are those in the Context's Theme
3246 * and the given AttributeSet.
3247 *
3248 * <p>
3249 * The method onFinishInflate() will be called after all children have been
3250 * added.
3251 *
3252 * @param context The Context the view is running in, through which it can
3253 * access the current theme, resources, etc.
3254 * @param attrs The attributes of the XML tag that is inflating the view.
3255 * @see #View(Context, AttributeSet, int)
3256 */
3257 public View(Context context, AttributeSet attrs) {
3258 this(context, attrs, 0);
3259 }
3260
3261 /**
3262 * Perform inflation from XML and apply a class-specific base style. This
3263 * constructor of View allows subclasses to use their own base style when
3264 * they are inflating. For example, a Button class's constructor would call
3265 * this version of the super class constructor and supply
3266 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3267 * the theme's button style to modify all of the base view attributes (in
3268 * particular its background) as well as the Button class's attributes.
3269 *
3270 * @param context The Context the view is running in, through which it can
3271 * access the current theme, resources, etc.
3272 * @param attrs The attributes of the XML tag that is inflating the view.
3273 * @param defStyle The default style to apply to this view. If 0, no style
3274 * will be applied (beyond what is included in the theme). This may
3275 * either be an attribute resource, whose value will be retrieved
3276 * from the current theme, or an explicit style resource.
3277 * @see #View(Context, AttributeSet)
3278 */
3279 public View(Context context, AttributeSet attrs, int defStyle) {
3280 this(context);
3281
3282 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3283 defStyle, 0);
3284
3285 Drawable background = null;
3286
3287 int leftPadding = -1;
3288 int topPadding = -1;
3289 int rightPadding = -1;
3290 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003291 int startPadding = -1;
3292 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293
3294 int padding = -1;
3295
3296 int viewFlagValues = 0;
3297 int viewFlagMasks = 0;
3298
3299 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 int x = 0;
3302 int y = 0;
3303
Chet Haase73066682010-11-29 15:55:32 -08003304 float tx = 0;
3305 float ty = 0;
3306 float rotation = 0;
3307 float rotationX = 0;
3308 float rotationY = 0;
3309 float sx = 1f;
3310 float sy = 1f;
3311 boolean transformSet = false;
3312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3314
Adam Powell637d3372010-08-25 14:37:03 -07003315 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 final int N = a.getIndexCount();
3317 for (int i = 0; i < N; i++) {
3318 int attr = a.getIndex(i);
3319 switch (attr) {
3320 case com.android.internal.R.styleable.View_background:
3321 background = a.getDrawable(attr);
3322 break;
3323 case com.android.internal.R.styleable.View_padding:
3324 padding = a.getDimensionPixelSize(attr, -1);
3325 break;
3326 case com.android.internal.R.styleable.View_paddingLeft:
3327 leftPadding = a.getDimensionPixelSize(attr, -1);
3328 break;
3329 case com.android.internal.R.styleable.View_paddingTop:
3330 topPadding = a.getDimensionPixelSize(attr, -1);
3331 break;
3332 case com.android.internal.R.styleable.View_paddingRight:
3333 rightPadding = a.getDimensionPixelSize(attr, -1);
3334 break;
3335 case com.android.internal.R.styleable.View_paddingBottom:
3336 bottomPadding = a.getDimensionPixelSize(attr, -1);
3337 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003338 case com.android.internal.R.styleable.View_paddingStart:
3339 startPadding = a.getDimensionPixelSize(attr, -1);
3340 break;
3341 case com.android.internal.R.styleable.View_paddingEnd:
3342 endPadding = a.getDimensionPixelSize(attr, -1);
3343 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 case com.android.internal.R.styleable.View_scrollX:
3345 x = a.getDimensionPixelOffset(attr, 0);
3346 break;
3347 case com.android.internal.R.styleable.View_scrollY:
3348 y = a.getDimensionPixelOffset(attr, 0);
3349 break;
Chet Haase73066682010-11-29 15:55:32 -08003350 case com.android.internal.R.styleable.View_alpha:
3351 setAlpha(a.getFloat(attr, 1f));
3352 break;
3353 case com.android.internal.R.styleable.View_transformPivotX:
3354 setPivotX(a.getDimensionPixelOffset(attr, 0));
3355 break;
3356 case com.android.internal.R.styleable.View_transformPivotY:
3357 setPivotY(a.getDimensionPixelOffset(attr, 0));
3358 break;
3359 case com.android.internal.R.styleable.View_translationX:
3360 tx = a.getDimensionPixelOffset(attr, 0);
3361 transformSet = true;
3362 break;
3363 case com.android.internal.R.styleable.View_translationY:
3364 ty = a.getDimensionPixelOffset(attr, 0);
3365 transformSet = true;
3366 break;
3367 case com.android.internal.R.styleable.View_rotation:
3368 rotation = a.getFloat(attr, 0);
3369 transformSet = true;
3370 break;
3371 case com.android.internal.R.styleable.View_rotationX:
3372 rotationX = a.getFloat(attr, 0);
3373 transformSet = true;
3374 break;
3375 case com.android.internal.R.styleable.View_rotationY:
3376 rotationY = a.getFloat(attr, 0);
3377 transformSet = true;
3378 break;
3379 case com.android.internal.R.styleable.View_scaleX:
3380 sx = a.getFloat(attr, 1f);
3381 transformSet = true;
3382 break;
3383 case com.android.internal.R.styleable.View_scaleY:
3384 sy = a.getFloat(attr, 1f);
3385 transformSet = true;
3386 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 case com.android.internal.R.styleable.View_id:
3388 mID = a.getResourceId(attr, NO_ID);
3389 break;
3390 case com.android.internal.R.styleable.View_tag:
3391 mTag = a.getText(attr);
3392 break;
3393 case com.android.internal.R.styleable.View_fitsSystemWindows:
3394 if (a.getBoolean(attr, false)) {
3395 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3396 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3397 }
3398 break;
3399 case com.android.internal.R.styleable.View_focusable:
3400 if (a.getBoolean(attr, false)) {
3401 viewFlagValues |= FOCUSABLE;
3402 viewFlagMasks |= FOCUSABLE_MASK;
3403 }
3404 break;
3405 case com.android.internal.R.styleable.View_focusableInTouchMode:
3406 if (a.getBoolean(attr, false)) {
3407 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3408 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3409 }
3410 break;
3411 case com.android.internal.R.styleable.View_clickable:
3412 if (a.getBoolean(attr, false)) {
3413 viewFlagValues |= CLICKABLE;
3414 viewFlagMasks |= CLICKABLE;
3415 }
3416 break;
3417 case com.android.internal.R.styleable.View_longClickable:
3418 if (a.getBoolean(attr, false)) {
3419 viewFlagValues |= LONG_CLICKABLE;
3420 viewFlagMasks |= LONG_CLICKABLE;
3421 }
3422 break;
3423 case com.android.internal.R.styleable.View_saveEnabled:
3424 if (!a.getBoolean(attr, true)) {
3425 viewFlagValues |= SAVE_DISABLED;
3426 viewFlagMasks |= SAVE_DISABLED_MASK;
3427 }
3428 break;
3429 case com.android.internal.R.styleable.View_duplicateParentState:
3430 if (a.getBoolean(attr, false)) {
3431 viewFlagValues |= DUPLICATE_PARENT_STATE;
3432 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3433 }
3434 break;
3435 case com.android.internal.R.styleable.View_visibility:
3436 final int visibility = a.getInt(attr, 0);
3437 if (visibility != 0) {
3438 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3439 viewFlagMasks |= VISIBILITY_MASK;
3440 }
3441 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003442 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003443 // Clear any layout direction flags (included resolved bits) already set
3444 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3445 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003446 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003447 final int value = (layoutDirection != -1) ?
3448 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3449 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003450 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 case com.android.internal.R.styleable.View_drawingCacheQuality:
3452 final int cacheQuality = a.getInt(attr, 0);
3453 if (cacheQuality != 0) {
3454 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3455 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3456 }
3457 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003458 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003459 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003460 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3462 if (!a.getBoolean(attr, true)) {
3463 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3464 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3465 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003466 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3468 if (!a.getBoolean(attr, true)) {
3469 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3470 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3471 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003472 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 case R.styleable.View_scrollbars:
3474 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3475 if (scrollbars != SCROLLBARS_NONE) {
3476 viewFlagValues |= scrollbars;
3477 viewFlagMasks |= SCROLLBARS_MASK;
3478 initializeScrollbars(a);
3479 }
3480 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003481 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003483 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3484 // Ignore the attribute starting with ICS
3485 break;
3486 }
3487 // With builds < ICS, fall through and apply fading edges
3488 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3490 if (fadingEdge != FADING_EDGE_NONE) {
3491 viewFlagValues |= fadingEdge;
3492 viewFlagMasks |= FADING_EDGE_MASK;
3493 initializeFadingEdge(a);
3494 }
3495 break;
3496 case R.styleable.View_scrollbarStyle:
3497 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3498 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3499 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3500 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3501 }
3502 break;
3503 case R.styleable.View_isScrollContainer:
3504 setScrollContainer = true;
3505 if (a.getBoolean(attr, false)) {
3506 setScrollContainer(true);
3507 }
3508 break;
3509 case com.android.internal.R.styleable.View_keepScreenOn:
3510 if (a.getBoolean(attr, false)) {
3511 viewFlagValues |= KEEP_SCREEN_ON;
3512 viewFlagMasks |= KEEP_SCREEN_ON;
3513 }
3514 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003515 case R.styleable.View_filterTouchesWhenObscured:
3516 if (a.getBoolean(attr, false)) {
3517 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3518 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3519 }
3520 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 case R.styleable.View_nextFocusLeft:
3522 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3523 break;
3524 case R.styleable.View_nextFocusRight:
3525 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3526 break;
3527 case R.styleable.View_nextFocusUp:
3528 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3529 break;
3530 case R.styleable.View_nextFocusDown:
3531 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3532 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003533 case R.styleable.View_nextFocusForward:
3534 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3535 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 case R.styleable.View_minWidth:
3537 mMinWidth = a.getDimensionPixelSize(attr, 0);
3538 break;
3539 case R.styleable.View_minHeight:
3540 mMinHeight = a.getDimensionPixelSize(attr, 0);
3541 break;
Romain Guy9a817362009-05-01 10:57:14 -07003542 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003543 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003544 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003545 + "be used within a restricted context");
3546 }
3547
Romain Guy9a817362009-05-01 10:57:14 -07003548 final String handlerName = a.getString(attr);
3549 if (handlerName != null) {
3550 setOnClickListener(new OnClickListener() {
3551 private Method mHandler;
3552
3553 public void onClick(View v) {
3554 if (mHandler == null) {
3555 try {
3556 mHandler = getContext().getClass().getMethod(handlerName,
3557 View.class);
3558 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003559 int id = getId();
3560 String idText = id == NO_ID ? "" : " with id '"
3561 + getContext().getResources().getResourceEntryName(
3562 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003563 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003564 handlerName + "(View) in the activity "
3565 + getContext().getClass() + " for onClick handler"
3566 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003567 }
3568 }
3569
3570 try {
3571 mHandler.invoke(getContext(), View.this);
3572 } catch (IllegalAccessException e) {
3573 throw new IllegalStateException("Could not execute non "
3574 + "public method of the activity", e);
3575 } catch (InvocationTargetException e) {
3576 throw new IllegalStateException("Could not execute "
3577 + "method of the activity", e);
3578 }
3579 }
3580 });
3581 }
3582 break;
Adam Powell637d3372010-08-25 14:37:03 -07003583 case R.styleable.View_overScrollMode:
3584 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3585 break;
Adam Powell20232d02010-12-08 21:08:53 -08003586 case R.styleable.View_verticalScrollbarPosition:
3587 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3588 break;
Romain Guy171c5922011-01-06 10:04:23 -08003589 case R.styleable.View_layerType:
3590 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3591 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003592 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003593 // Clear any text direction flag already set
3594 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3595 // Set the text direction flags depending on the value of the attribute
3596 final int textDirection = a.getInt(attr, -1);
3597 if (textDirection != -1) {
3598 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3599 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003600 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003601 case R.styleable.View_textAlignment:
3602 // Clear any text alignment flag already set
3603 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3604 // Set the text alignment flag depending on the value of the attribute
3605 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3606 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3607 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003608 case R.styleable.View_importantForAccessibility:
3609 setImportantForAccessibility(a.getInt(attr,
3610 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003611 break;
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
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003869 // Apply layout direction to the new Drawables if needed
3870 final int layoutDirection = getResolvedLayoutDirection();
3871 if (track != null) {
3872 track.setLayoutDirection(layoutDirection);
3873 }
3874 if (thumb != null) {
3875 thumb.setLayoutDirection(layoutDirection);
3876 }
3877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003879 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003880 }
3881
3882 /**
3883 * <p>
3884 * Initalizes the scrollability cache if necessary.
3885 * </p>
3886 */
3887 private void initScrollCache() {
3888 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003889 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 }
3891 }
3892
Philip Milne6c8ea062012-04-03 17:38:43 -07003893 private ScrollabilityCache getScrollCache() {
3894 initScrollCache();
3895 return mScrollCache;
3896 }
3897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 /**
Adam Powell20232d02010-12-08 21:08:53 -08003899 * Set the position of the vertical scroll bar. Should be one of
3900 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3901 * {@link #SCROLLBAR_POSITION_RIGHT}.
3902 *
3903 * @param position Where the vertical scroll bar should be positioned.
3904 */
3905 public void setVerticalScrollbarPosition(int position) {
3906 if (mVerticalScrollbarPosition != position) {
3907 mVerticalScrollbarPosition = position;
3908 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003909 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003910 }
3911 }
3912
3913 /**
3914 * @return The position where the vertical scroll bar will show, if applicable.
3915 * @see #setVerticalScrollbarPosition(int)
3916 */
3917 public int getVerticalScrollbarPosition() {
3918 return mVerticalScrollbarPosition;
3919 }
3920
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003921 ListenerInfo getListenerInfo() {
3922 if (mListenerInfo != null) {
3923 return mListenerInfo;
3924 }
3925 mListenerInfo = new ListenerInfo();
3926 return mListenerInfo;
3927 }
3928
Adam Powell20232d02010-12-08 21:08:53 -08003929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 * Register a callback to be invoked when focus of this view changed.
3931 *
3932 * @param l The callback that will run.
3933 */
3934 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003935 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937
3938 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003939 * Add a listener that will be called when the bounds of the view change due to
3940 * layout processing.
3941 *
3942 * @param listener The listener that will be called when layout bounds change.
3943 */
3944 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003945 ListenerInfo li = getListenerInfo();
3946 if (li.mOnLayoutChangeListeners == null) {
3947 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003948 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003949 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3950 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003951 }
Chet Haase21cd1382010-09-01 17:42:29 -07003952 }
3953
3954 /**
3955 * Remove a listener for layout changes.
3956 *
3957 * @param listener The listener for layout bounds change.
3958 */
3959 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003960 ListenerInfo li = mListenerInfo;
3961 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003962 return;
3963 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003964 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003965 }
3966
3967 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003968 * Add a listener for attach state changes.
3969 *
3970 * This listener will be called whenever this view is attached or detached
3971 * from a window. Remove the listener using
3972 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3973 *
3974 * @param listener Listener to attach
3975 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3976 */
3977 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003978 ListenerInfo li = getListenerInfo();
3979 if (li.mOnAttachStateChangeListeners == null) {
3980 li.mOnAttachStateChangeListeners
3981 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003982 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003983 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003984 }
3985
3986 /**
3987 * Remove a listener for attach state changes. The listener will receive no further
3988 * notification of window attach/detach events.
3989 *
3990 * @param listener Listener to remove
3991 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3992 */
3993 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003994 ListenerInfo li = mListenerInfo;
3995 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003996 return;
3997 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003998 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003999 }
4000
4001 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 * Returns the focus-change callback registered for this view.
4003 *
4004 * @return The callback, or null if one is not registered.
4005 */
4006 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004007 ListenerInfo li = mListenerInfo;
4008 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 }
4010
4011 /**
4012 * Register a callback to be invoked when this view is clicked. If this view is not
4013 * clickable, it becomes clickable.
4014 *
4015 * @param l The callback that will run
4016 *
4017 * @see #setClickable(boolean)
4018 */
4019 public void setOnClickListener(OnClickListener l) {
4020 if (!isClickable()) {
4021 setClickable(true);
4022 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004023 getListenerInfo().mOnClickListener = l;
4024 }
4025
4026 /**
4027 * Return whether this view has an attached OnClickListener. Returns
4028 * true if there is a listener, false if there is none.
4029 */
4030 public boolean hasOnClickListeners() {
4031 ListenerInfo li = mListenerInfo;
4032 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 }
4034
4035 /**
4036 * Register a callback to be invoked when this view is clicked and held. If this view is not
4037 * long clickable, it becomes long clickable.
4038 *
4039 * @param l The callback that will run
4040 *
4041 * @see #setLongClickable(boolean)
4042 */
4043 public void setOnLongClickListener(OnLongClickListener l) {
4044 if (!isLongClickable()) {
4045 setLongClickable(true);
4046 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004047 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049
4050 /**
4051 * Register a callback to be invoked when the context menu for this view is
4052 * being built. If this view is not long clickable, it becomes long clickable.
4053 *
4054 * @param l The callback that will run
4055 *
4056 */
4057 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4058 if (!isLongClickable()) {
4059 setLongClickable(true);
4060 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063
4064 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004065 * Call this view's OnClickListener, if it is defined. Performs all normal
4066 * actions associated with clicking: reporting accessibility event, playing
4067 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 *
4069 * @return True there was an assigned OnClickListener that was called, false
4070 * otherwise is returned.
4071 */
4072 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004073 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4074
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004075 ListenerInfo li = mListenerInfo;
4076 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004078 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 return true;
4080 }
4081
4082 return false;
4083 }
4084
4085 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004086 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4087 * this only calls the listener, and does not do any associated clicking
4088 * actions like reporting an accessibility event.
4089 *
4090 * @return True there was an assigned OnClickListener that was called, false
4091 * otherwise is returned.
4092 */
4093 public boolean callOnClick() {
4094 ListenerInfo li = mListenerInfo;
4095 if (li != null && li.mOnClickListener != null) {
4096 li.mOnClickListener.onClick(this);
4097 return true;
4098 }
4099 return false;
4100 }
4101
4102 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004103 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4104 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004106 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 */
4108 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004109 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004112 ListenerInfo li = mListenerInfo;
4113 if (li != null && li.mOnLongClickListener != null) {
4114 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 }
4116 if (!handled) {
4117 handled = showContextMenu();
4118 }
4119 if (handled) {
4120 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4121 }
4122 return handled;
4123 }
4124
4125 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004126 * Performs button-related actions during a touch down event.
4127 *
4128 * @param event The event.
4129 * @return True if the down was consumed.
4130 *
4131 * @hide
4132 */
4133 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4134 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4135 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4136 return true;
4137 }
4138 }
4139 return false;
4140 }
4141
4142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 * Bring up the context menu for this view.
4144 *
4145 * @return Whether a context menu was displayed.
4146 */
4147 public boolean showContextMenu() {
4148 return getParent().showContextMenuForChild(this);
4149 }
4150
4151 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004152 * Bring up the context menu for this view, referring to the item under the specified point.
4153 *
4154 * @param x The referenced x coordinate.
4155 * @param y The referenced y coordinate.
4156 * @param metaState The keyboard modifiers that were pressed.
4157 * @return Whether a context menu was displayed.
4158 *
4159 * @hide
4160 */
4161 public boolean showContextMenu(float x, float y, int metaState) {
4162 return showContextMenu();
4163 }
4164
4165 /**
Adam Powell6e346362010-07-23 10:18:23 -07004166 * Start an action mode.
4167 *
4168 * @param callback Callback that will control the lifecycle of the action mode
4169 * @return The new action mode if it is started, null otherwise
4170 *
4171 * @see ActionMode
4172 */
4173 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004174 ViewParent parent = getParent();
4175 if (parent == null) return null;
4176 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004177 }
4178
4179 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004180 * Register a callback to be invoked when a hardware key is pressed in this view.
4181 * Key presses in software input methods will generally not trigger the methods of
4182 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004183 * @param l the key listener to attach to this view
4184 */
4185 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004186 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 }
4188
4189 /**
4190 * Register a callback to be invoked when a touch event is sent to this view.
4191 * @param l the touch listener to attach to this view
4192 */
4193 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004194 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196
4197 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004198 * Register a callback to be invoked when a generic motion event is sent to this view.
4199 * @param l the generic motion listener to attach to this view
4200 */
4201 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004202 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004203 }
4204
4205 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004206 * Register a callback to be invoked when a hover event is sent to this view.
4207 * @param l the hover listener to attach to this view
4208 */
4209 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004210 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004211 }
4212
4213 /**
Joe Malin32736f02011-01-19 16:14:20 -08004214 * Register a drag event listener callback object for this View. The parameter is
4215 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4216 * View, the system calls the
4217 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4218 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004219 */
4220 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004221 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004222 }
4223
4224 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004225 * Give this view focus. This will cause
4226 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 *
4228 * Note: this does not check whether this {@link View} should get focus, it just
4229 * gives it focus no matter what. It should only be called internally by framework
4230 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4231 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004232 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4233 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 * focus moved when requestFocus() is called. It may not always
4235 * apply, in which case use the default View.FOCUS_DOWN.
4236 * @param previouslyFocusedRect The rectangle of the view that had focus
4237 * prior in this View's coordinate system.
4238 */
4239 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4240 if (DBG) {
4241 System.out.println(this + " requestFocus()");
4242 }
4243
4244 if ((mPrivateFlags & FOCUSED) == 0) {
4245 mPrivateFlags |= FOCUSED;
4246
4247 if (mParent != null) {
4248 mParent.requestChildFocus(this, this);
4249 }
4250
4251 onFocusChanged(true, direction, previouslyFocusedRect);
4252 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004253
4254 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4255 notifyAccessibilityStateChanged();
4256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
4258 }
4259
4260 /**
4261 * Request that a rectangle of this view be visible on the screen,
4262 * scrolling if necessary just enough.
4263 *
4264 * <p>A View should call this if it maintains some notion of which part
4265 * of its content is interesting. For example, a text editing view
4266 * should call this when its cursor moves.
4267 *
4268 * @param rectangle The rectangle.
4269 * @return Whether any parent scrolled.
4270 */
4271 public boolean requestRectangleOnScreen(Rect rectangle) {
4272 return requestRectangleOnScreen(rectangle, false);
4273 }
4274
4275 /**
4276 * Request that a rectangle of this view be visible on the screen,
4277 * scrolling if necessary just enough.
4278 *
4279 * <p>A View should call this if it maintains some notion of which part
4280 * of its content is interesting. For example, a text editing view
4281 * should call this when its cursor moves.
4282 *
4283 * <p>When <code>immediate</code> is set to true, scrolling will not be
4284 * animated.
4285 *
4286 * @param rectangle The rectangle.
4287 * @param immediate True to forbid animated scrolling, false otherwise
4288 * @return Whether any parent scrolled.
4289 */
4290 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4291 View child = this;
4292 ViewParent parent = mParent;
4293 boolean scrolled = false;
4294 while (parent != null) {
4295 scrolled |= parent.requestChildRectangleOnScreen(child,
4296 rectangle, immediate);
4297
4298 // offset rect so next call has the rectangle in the
4299 // coordinate system of its direct child.
4300 rectangle.offset(child.getLeft(), child.getTop());
4301 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4302
4303 if (!(parent instanceof View)) {
4304 break;
4305 }
Romain Guy8506ab42009-06-11 17:35:47 -07004306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 child = (View) parent;
4308 parent = child.getParent();
4309 }
4310 return scrolled;
4311 }
4312
4313 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004314 * Called when this view wants to give up focus. If focus is cleared
4315 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4316 * <p>
4317 * <strong>Note:</strong> When a View clears focus the framework is trying
4318 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004319 * View is the first from the top that can take focus, then all callbacks
4320 * related to clearing focus will be invoked after wich the framework will
4321 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004322 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 */
4324 public void clearFocus() {
4325 if (DBG) {
4326 System.out.println(this + " clearFocus()");
4327 }
4328
4329 if ((mPrivateFlags & FOCUSED) != 0) {
4330 mPrivateFlags &= ~FOCUSED;
4331
4332 if (mParent != null) {
4333 mParent.clearChildFocus(this);
4334 }
4335
4336 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004339
4340 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004341
4342 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4343 notifyAccessibilityStateChanged();
4344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 }
4346 }
4347
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004348 void ensureInputFocusOnFirstFocusable() {
4349 View root = getRootView();
4350 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004351 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 }
4353 }
4354
4355 /**
4356 * Called internally by the view system when a new view is getting focus.
4357 * This is what clears the old focus.
4358 */
4359 void unFocus() {
4360 if (DBG) {
4361 System.out.println(this + " unFocus()");
4362 }
4363
4364 if ((mPrivateFlags & FOCUSED) != 0) {
4365 mPrivateFlags &= ~FOCUSED;
4366
4367 onFocusChanged(false, 0, null);
4368 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004369
4370 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4371 notifyAccessibilityStateChanged();
4372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373 }
4374 }
4375
4376 /**
4377 * Returns true if this view has focus iteself, or is the ancestor of the
4378 * view that has focus.
4379 *
4380 * @return True if this view has or contains focus, false otherwise.
4381 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004382 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 public boolean hasFocus() {
4384 return (mPrivateFlags & FOCUSED) != 0;
4385 }
4386
4387 /**
4388 * Returns true if this view is focusable or if it contains a reachable View
4389 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4390 * is a View whose parents do not block descendants focus.
4391 *
4392 * Only {@link #VISIBLE} views are considered focusable.
4393 *
4394 * @return True if the view is focusable or if the view contains a focusable
4395 * View, false otherwise.
4396 *
4397 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4398 */
4399 public boolean hasFocusable() {
4400 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4401 }
4402
4403 /**
4404 * Called by the view system when the focus state of this view changes.
4405 * When the focus change event is caused by directional navigation, direction
4406 * and previouslyFocusedRect provide insight into where the focus is coming from.
4407 * When overriding, be sure to call up through to the super class so that
4408 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 * @param gainFocus True if the View has focus; false otherwise.
4411 * @param direction The direction focus has moved when requestFocus()
4412 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004413 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4414 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4415 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4417 * system, of the previously focused view. If applicable, this will be
4418 * passed in as finer grained information about where the focus is coming
4419 * from (in addition to direction). Will be <code>null</code> otherwise.
4420 */
4421 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004422 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004423 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4424 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004425 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004426 }
4427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 InputMethodManager imm = InputMethodManager.peekInstance();
4429 if (!gainFocus) {
4430 if (isPressed()) {
4431 setPressed(false);
4432 }
4433 if (imm != null && mAttachInfo != null
4434 && mAttachInfo.mHasWindowFocus) {
4435 imm.focusOut(this);
4436 }
Romain Guya2431d02009-04-30 16:30:00 -07004437 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 } else if (imm != null && mAttachInfo != null
4439 && mAttachInfo.mHasWindowFocus) {
4440 imm.focusIn(this);
4441 }
Romain Guy8506ab42009-06-11 17:35:47 -07004442
Romain Guy0fd89bf2011-01-26 15:41:30 -08004443 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004444 ListenerInfo li = mListenerInfo;
4445 if (li != null && li.mOnFocusChangeListener != null) {
4446 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 }
Joe Malin32736f02011-01-19 16:14:20 -08004448
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004449 if (mAttachInfo != null) {
4450 mAttachInfo.mKeyDispatchState.reset(this);
4451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 }
4453
4454 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004455 * Sends an accessibility event of the given type. If accessiiblity is
4456 * not enabled this method has no effect. The default implementation calls
4457 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4458 * to populate information about the event source (this View), then calls
4459 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4460 * populate the text content of the event source including its descendants,
4461 * and last calls
4462 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4463 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004464 * <p>
4465 * If an {@link AccessibilityDelegate} has been specified via calling
4466 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4467 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4468 * responsible for handling this call.
4469 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004470 *
Scott Mainb303d832011-10-12 16:45:18 -07004471 * @param eventType The type of the event to send, as defined by several types from
4472 * {@link android.view.accessibility.AccessibilityEvent}, such as
4473 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4474 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004475 *
4476 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4477 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4478 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004479 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004480 */
4481 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004482 if (mAccessibilityDelegate != null) {
4483 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4484 } else {
4485 sendAccessibilityEventInternal(eventType);
4486 }
4487 }
4488
4489 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004490 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4491 * {@link AccessibilityEvent} to make an announcement which is related to some
4492 * sort of a context change for which none of the events representing UI transitions
4493 * is a good fit. For example, announcing a new page in a book. If accessibility
4494 * is not enabled this method does nothing.
4495 *
4496 * @param text The announcement text.
4497 */
4498 public void announceForAccessibility(CharSequence text) {
4499 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4500 AccessibilityEvent event = AccessibilityEvent.obtain(
4501 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4502 event.getText().add(text);
4503 sendAccessibilityEventUnchecked(event);
4504 }
4505 }
4506
4507 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004508 * @see #sendAccessibilityEvent(int)
4509 *
4510 * Note: Called from the default {@link AccessibilityDelegate}.
4511 */
4512 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004513 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4514 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4515 }
4516 }
4517
4518 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004519 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4520 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004521 * perform a check whether accessibility is enabled.
4522 * <p>
4523 * If an {@link AccessibilityDelegate} has been specified via calling
4524 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4525 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4526 * is responsible for handling this call.
4527 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004528 *
4529 * @param event The event to send.
4530 *
4531 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004532 */
4533 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004534 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004535 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004536 } else {
4537 sendAccessibilityEventUncheckedInternal(event);
4538 }
4539 }
4540
4541 /**
4542 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4543 *
4544 * Note: Called from the default {@link AccessibilityDelegate}.
4545 */
4546 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004547 if (!isShown()) {
4548 return;
4549 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004550 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004551 // Only a subset of accessibility events populates text content.
4552 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4553 dispatchPopulateAccessibilityEvent(event);
4554 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004555 // In the beginning we called #isShown(), so we know that getParent() is not null.
4556 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004557 }
4558
4559 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004560 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4561 * to its children for adding their text content to the event. Note that the
4562 * event text is populated in a separate dispatch path since we add to the
4563 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004564 * A typical implementation will call
4565 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4566 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4567 * on each child. Override this method if custom population of the event text
4568 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004569 * <p>
4570 * If an {@link AccessibilityDelegate} has been specified via calling
4571 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4572 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4573 * is responsible for handling this call.
4574 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004575 * <p>
4576 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4577 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4578 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004579 *
4580 * @param event The event.
4581 *
4582 * @return True if the event population was completed.
4583 */
4584 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004585 if (mAccessibilityDelegate != null) {
4586 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4587 } else {
4588 return dispatchPopulateAccessibilityEventInternal(event);
4589 }
4590 }
4591
4592 /**
4593 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4594 *
4595 * Note: Called from the default {@link AccessibilityDelegate}.
4596 */
4597 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004598 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004599 return false;
4600 }
4601
4602 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004603 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004604 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004605 * text content. While this method is free to modify event
4606 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004607 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4608 * <p>
4609 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004610 * to the text added by the super implementation:
4611 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004612 * super.onPopulateAccessibilityEvent(event);
4613 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4614 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4615 * mCurrentDate.getTimeInMillis(), flags);
4616 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004617 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 * <p>
4619 * If an {@link AccessibilityDelegate} has been specified via calling
4620 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4621 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4622 * is responsible for handling this call.
4623 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004624 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4625 * information to the event, in case the default implementation has basic information to add.
4626 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004627 *
4628 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004629 *
4630 * @see #sendAccessibilityEvent(int)
4631 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004632 */
4633 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004634 if (mAccessibilityDelegate != null) {
4635 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4636 } else {
4637 onPopulateAccessibilityEventInternal(event);
4638 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004639 }
4640
4641 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004642 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4643 *
4644 * Note: Called from the default {@link AccessibilityDelegate}.
4645 */
4646 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4647
4648 }
4649
4650 /**
4651 * Initializes an {@link AccessibilityEvent} with information about
4652 * this View which is the event source. In other words, the source of
4653 * an accessibility event is the view whose state change triggered firing
4654 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004655 * <p>
4656 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004657 * to properties set by the super implementation:
4658 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4659 * super.onInitializeAccessibilityEvent(event);
4660 * event.setPassword(true);
4661 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004662 * <p>
4663 * If an {@link AccessibilityDelegate} has been specified via calling
4664 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4665 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4666 * is responsible for handling this call.
4667 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004668 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4669 * information to the event, in case the default implementation has basic information to add.
4670 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004671 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004672 *
4673 * @see #sendAccessibilityEvent(int)
4674 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4675 */
4676 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004677 if (mAccessibilityDelegate != null) {
4678 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4679 } else {
4680 onInitializeAccessibilityEventInternal(event);
4681 }
4682 }
4683
4684 /**
4685 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4686 *
4687 * Note: Called from the default {@link AccessibilityDelegate}.
4688 */
4689 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004690 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004691 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004692 event.setPackageName(getContext().getPackageName());
4693 event.setEnabled(isEnabled());
4694 event.setContentDescription(mContentDescription);
4695
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004696 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004697 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004698 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4699 FOCUSABLES_ALL);
4700 event.setItemCount(focusablesTempList.size());
4701 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4702 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004703 }
4704 }
4705
4706 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004707 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4708 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4709 * This method is responsible for obtaining an accessibility node info from a
4710 * pool of reusable instances and calling
4711 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4712 * initialize the former.
4713 * <p>
4714 * Note: The client is responsible for recycling the obtained instance by calling
4715 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4716 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004717 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004718 * @return A populated {@link AccessibilityNodeInfo}.
4719 *
4720 * @see AccessibilityNodeInfo
4721 */
4722 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004723 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4724 if (provider != null) {
4725 return provider.createAccessibilityNodeInfo(View.NO_ID);
4726 } else {
4727 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4728 onInitializeAccessibilityNodeInfo(info);
4729 return info;
4730 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004731 }
4732
4733 /**
4734 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4735 * The base implementation sets:
4736 * <ul>
4737 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004738 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4739 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004740 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4741 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4742 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4743 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4744 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4745 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4746 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4747 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4748 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4749 * </ul>
4750 * <p>
4751 * Subclasses should override this method, call the super implementation,
4752 * and set additional attributes.
4753 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004754 * <p>
4755 * If an {@link AccessibilityDelegate} has been specified via calling
4756 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4757 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4758 * is responsible for handling this call.
4759 * </p>
4760 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004761 * @param info The instance to initialize.
4762 */
4763 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004764 if (mAccessibilityDelegate != null) {
4765 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4766 } else {
4767 onInitializeAccessibilityNodeInfoInternal(info);
4768 }
4769 }
4770
4771 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004772 * Gets the location of this view in screen coordintates.
4773 *
4774 * @param outRect The output location
4775 */
4776 private void getBoundsOnScreen(Rect outRect) {
4777 if (mAttachInfo == null) {
4778 return;
4779 }
4780
4781 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004782 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004783
4784 if (!hasIdentityMatrix()) {
4785 getMatrix().mapRect(position);
4786 }
4787
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004788 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004789
4790 ViewParent parent = mParent;
4791 while (parent instanceof View) {
4792 View parentView = (View) parent;
4793
4794 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4795
4796 if (!parentView.hasIdentityMatrix()) {
4797 parentView.getMatrix().mapRect(position);
4798 }
4799
4800 position.offset(parentView.mLeft, parentView.mTop);
4801
4802 parent = parentView.mParent;
4803 }
4804
4805 if (parent instanceof ViewRootImpl) {
4806 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4807 position.offset(0, -viewRootImpl.mCurScrollY);
4808 }
4809
4810 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4811
4812 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4813 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4814 }
4815
4816 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004817 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4818 *
4819 * Note: Called from the default {@link AccessibilityDelegate}.
4820 */
4821 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004822 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004823 final float applicationScale = mAttachInfo.mApplicationScale;
4824
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004825 getDrawingRect(bounds);
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004826 bounds.scale(applicationScale);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004827 info.setBoundsInParent(bounds);
4828
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004829 getBoundsOnScreen(bounds);
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004830 bounds.scale(applicationScale);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004831 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004832
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004833 ViewParent parent = getParentForAccessibility();
4834 if (parent instanceof View) {
4835 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004836 }
4837
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004838 info.setVisibleToUser(isVisibleToUser());
4839
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004840 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004841 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004842 info.setContentDescription(getContentDescription());
4843
4844 info.setEnabled(isEnabled());
4845 info.setClickable(isClickable());
4846 info.setFocusable(isFocusable());
4847 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004848 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004849 info.setSelected(isSelected());
4850 info.setLongClickable(isLongClickable());
4851
4852 // TODO: These make sense only if we are in an AdapterView but all
4853 // views can be selected. Maybe from accessiiblity perspective
4854 // we should report as selectable view in an AdapterView.
4855 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4856 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4857
4858 if (isFocusable()) {
4859 if (isFocused()) {
4860 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4861 } else {
4862 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4863 }
4864 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004865
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004866 if (!isAccessibilityFocused()) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07004867 final int mode = getAccessibilityFocusable();
4868 if (mode == ACCESSIBILITY_FOCUSABLE_YES || mode == ACCESSIBILITY_FOCUSABLE_AUTO) {
4869 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4870 }
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004871 } else {
4872 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4873 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004874
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004875 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004876 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4877 }
4878
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004879 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004880 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4881 }
4882
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004883 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004884 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4885 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4886 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004887 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4888 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004889 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004890 }
4891
4892 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -07004893 * Returns the delta between the actual and last reported window left.
4894 *
4895 * @hide
4896 */
4897 public int getActualAndReportedWindowLeftDelta() {
4898 if (mAttachInfo != null) {
4899 return mAttachInfo.mActualWindowLeft - mAttachInfo.mWindowLeft;
4900 }
4901 return 0;
4902 }
4903
4904 /**
4905 * Returns the delta between the actual and last reported window top.
4906 *
4907 * @hide
4908 */
4909 public int getActualAndReportedWindowTopDelta() {
4910 if (mAttachInfo != null) {
4911 return mAttachInfo.mActualWindowTop - mAttachInfo.mWindowTop;
4912 }
4913 return 0;
4914 }
4915
4916 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004917 * Computes whether this view is visible to the user. Such a view is
4918 * attached, visible, all its predecessors are visible, it is not clipped
4919 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004920 *
4921 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004922 *
4923 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004924 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004925 protected boolean isVisibleToUser() {
4926 return isVisibleToUser(null);
4927 }
4928
4929 /**
4930 * Computes whether the given portion of this view is visible to the user. Such a view is
4931 * attached, visible, all its predecessors are visible, has an alpha greater than zero, and
4932 * the specified portion is not clipped entirely by its predecessors.
4933 *
4934 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4935 * <code>null</code>, and the entire view will be tested in this case.
4936 * When <code>true</code> is returned by the function, the actual visible
4937 * region will be stored in this parameter; that is, if boundInView is fully
4938 * contained within the view, no modification will be made, otherwise regions
4939 * outside of the visible area of the view will be clipped.
4940 *
4941 * @return Whether the specified portion of the view is visible on the screen.
4942 *
4943 * @hide
4944 */
4945 protected boolean isVisibleToUser(Rect boundInView) {
4946 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4947 Point offset = mAttachInfo.mPoint;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004948 // The first two checks are made also made by isShown() which
4949 // however traverses the tree up to the parent to catch that.
4950 // Therefore, we do some fail fast check to minimize the up
4951 // tree traversal.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004952 boolean isVisible = mAttachInfo != null
4953 && mAttachInfo.mWindowVisibility == View.VISIBLE
4954 && getAlpha() > 0
4955 && isShown()
4956 && getGlobalVisibleRect(visibleRect, offset);
4957 if (isVisible && boundInView != null) {
4958 visibleRect.offset(-offset.x, -offset.y);
4959 isVisible &= boundInView.intersect(visibleRect);
4960 }
4961 return isVisible;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004962 }
4963
4964 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004965 * Sets a delegate for implementing accessibility support via compositon as
4966 * opposed to inheritance. The delegate's primary use is for implementing
4967 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4968 *
4969 * @param delegate The delegate instance.
4970 *
4971 * @see AccessibilityDelegate
4972 */
4973 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4974 mAccessibilityDelegate = delegate;
4975 }
4976
4977 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004978 * Gets the provider for managing a virtual view hierarchy rooted at this View
4979 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4980 * that explore the window content.
4981 * <p>
4982 * If this method returns an instance, this instance is responsible for managing
4983 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4984 * View including the one representing the View itself. Similarly the returned
4985 * instance is responsible for performing accessibility actions on any virtual
4986 * view or the root view itself.
4987 * </p>
4988 * <p>
4989 * If an {@link AccessibilityDelegate} has been specified via calling
4990 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4991 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4992 * is responsible for handling this call.
4993 * </p>
4994 *
4995 * @return The provider.
4996 *
4997 * @see AccessibilityNodeProvider
4998 */
4999 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5000 if (mAccessibilityDelegate != null) {
5001 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5002 } else {
5003 return null;
5004 }
5005 }
5006
5007 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005008 * Gets the unique identifier of this view on the screen for accessibility purposes.
5009 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5010 *
5011 * @return The view accessibility id.
5012 *
5013 * @hide
5014 */
5015 public int getAccessibilityViewId() {
5016 if (mAccessibilityViewId == NO_ID) {
5017 mAccessibilityViewId = sNextAccessibilityViewId++;
5018 }
5019 return mAccessibilityViewId;
5020 }
5021
5022 /**
5023 * Gets the unique identifier of the window in which this View reseides.
5024 *
5025 * @return The window accessibility id.
5026 *
5027 * @hide
5028 */
5029 public int getAccessibilityWindowId() {
5030 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5031 }
5032
5033 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005034 * Gets the {@link View} description. It briefly describes the view and is
5035 * primarily used for accessibility support. Set this property to enable
5036 * better accessibility support for your application. This is especially
5037 * true for views that do not have textual representation (For example,
5038 * ImageButton).
5039 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005040 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005041 *
5042 * @attr ref android.R.styleable#View_contentDescription
5043 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005044 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005045 public CharSequence getContentDescription() {
5046 return mContentDescription;
5047 }
5048
5049 /**
5050 * Sets the {@link View} description. It briefly describes the view and is
5051 * primarily used for accessibility support. Set this property to enable
5052 * better accessibility support for your application. This is especially
5053 * true for views that do not have textual representation (For example,
5054 * ImageButton).
5055 *
5056 * @param contentDescription The content description.
5057 *
5058 * @attr ref android.R.styleable#View_contentDescription
5059 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005060 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005061 public void setContentDescription(CharSequence contentDescription) {
5062 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005063 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5064 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5065 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5066 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005067 }
5068
5069 /**
Romain Guya2431d02009-04-30 16:30:00 -07005070 * Invoked whenever this view loses focus, either by losing window focus or by losing
5071 * focus within its window. This method can be used to clear any state tied to the
5072 * focus. For instance, if a button is held pressed with the trackball and the window
5073 * loses focus, this method can be used to cancel the press.
5074 *
5075 * Subclasses of View overriding this method should always call super.onFocusLost().
5076 *
5077 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005078 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005079 *
5080 * @hide pending API council approval
5081 */
5082 protected void onFocusLost() {
5083 resetPressedState();
5084 }
5085
5086 private void resetPressedState() {
5087 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5088 return;
5089 }
5090
5091 if (isPressed()) {
5092 setPressed(false);
5093
5094 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005095 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005096 }
5097 }
5098 }
5099
5100 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 * Returns true if this view has focus
5102 *
5103 * @return True if this view has focus, false otherwise.
5104 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005105 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 public boolean isFocused() {
5107 return (mPrivateFlags & FOCUSED) != 0;
5108 }
5109
5110 /**
5111 * Find the view in the hierarchy rooted at this view that currently has
5112 * focus.
5113 *
5114 * @return The view that currently has focus, or null if no focused view can
5115 * be found.
5116 */
5117 public View findFocus() {
5118 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
5119 }
5120
5121 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005122 * Indicates whether this view is one of the set of scrollable containers in
5123 * its window.
5124 *
5125 * @return whether this view is one of the set of scrollable containers in
5126 * its window
5127 *
5128 * @attr ref android.R.styleable#View_isScrollContainer
5129 */
5130 public boolean isScrollContainer() {
5131 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
5132 }
5133
5134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 * Change whether this view is one of the set of scrollable containers in
5136 * its window. This will be used to determine whether the window can
5137 * resize or must pan when a soft input area is open -- scrollable
5138 * containers allow the window to use resize mode since the container
5139 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005140 *
5141 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 */
5143 public void setScrollContainer(boolean isScrollContainer) {
5144 if (isScrollContainer) {
5145 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
5146 mAttachInfo.mScrollContainers.add(this);
5147 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5148 }
5149 mPrivateFlags |= SCROLL_CONTAINER;
5150 } else {
5151 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5152 mAttachInfo.mScrollContainers.remove(this);
5153 }
5154 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
5155 }
5156 }
5157
5158 /**
5159 * Returns the quality of the drawing cache.
5160 *
5161 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5162 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5163 *
5164 * @see #setDrawingCacheQuality(int)
5165 * @see #setDrawingCacheEnabled(boolean)
5166 * @see #isDrawingCacheEnabled()
5167 *
5168 * @attr ref android.R.styleable#View_drawingCacheQuality
5169 */
5170 public int getDrawingCacheQuality() {
5171 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5172 }
5173
5174 /**
5175 * Set the drawing cache quality of this view. This value is used only when the
5176 * drawing cache is enabled
5177 *
5178 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5179 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5180 *
5181 * @see #getDrawingCacheQuality()
5182 * @see #setDrawingCacheEnabled(boolean)
5183 * @see #isDrawingCacheEnabled()
5184 *
5185 * @attr ref android.R.styleable#View_drawingCacheQuality
5186 */
5187 public void setDrawingCacheQuality(int quality) {
5188 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5189 }
5190
5191 /**
5192 * Returns whether the screen should remain on, corresponding to the current
5193 * value of {@link #KEEP_SCREEN_ON}.
5194 *
5195 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5196 *
5197 * @see #setKeepScreenOn(boolean)
5198 *
5199 * @attr ref android.R.styleable#View_keepScreenOn
5200 */
5201 public boolean getKeepScreenOn() {
5202 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5203 }
5204
5205 /**
5206 * Controls whether the screen should remain on, modifying the
5207 * value of {@link #KEEP_SCREEN_ON}.
5208 *
5209 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5210 *
5211 * @see #getKeepScreenOn()
5212 *
5213 * @attr ref android.R.styleable#View_keepScreenOn
5214 */
5215 public void setKeepScreenOn(boolean keepScreenOn) {
5216 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5217 }
5218
5219 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005220 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5221 * @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 -08005222 *
5223 * @attr ref android.R.styleable#View_nextFocusLeft
5224 */
5225 public int getNextFocusLeftId() {
5226 return mNextFocusLeftId;
5227 }
5228
5229 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005230 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5231 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5232 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 *
5234 * @attr ref android.R.styleable#View_nextFocusLeft
5235 */
5236 public void setNextFocusLeftId(int nextFocusLeftId) {
5237 mNextFocusLeftId = nextFocusLeftId;
5238 }
5239
5240 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005241 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5242 * @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 -08005243 *
5244 * @attr ref android.R.styleable#View_nextFocusRight
5245 */
5246 public int getNextFocusRightId() {
5247 return mNextFocusRightId;
5248 }
5249
5250 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005251 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5252 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5253 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 *
5255 * @attr ref android.R.styleable#View_nextFocusRight
5256 */
5257 public void setNextFocusRightId(int nextFocusRightId) {
5258 mNextFocusRightId = nextFocusRightId;
5259 }
5260
5261 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005262 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5263 * @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 -08005264 *
5265 * @attr ref android.R.styleable#View_nextFocusUp
5266 */
5267 public int getNextFocusUpId() {
5268 return mNextFocusUpId;
5269 }
5270
5271 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005272 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5273 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5274 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 *
5276 * @attr ref android.R.styleable#View_nextFocusUp
5277 */
5278 public void setNextFocusUpId(int nextFocusUpId) {
5279 mNextFocusUpId = nextFocusUpId;
5280 }
5281
5282 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005283 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5284 * @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 -08005285 *
5286 * @attr ref android.R.styleable#View_nextFocusDown
5287 */
5288 public int getNextFocusDownId() {
5289 return mNextFocusDownId;
5290 }
5291
5292 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005293 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5294 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5295 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 *
5297 * @attr ref android.R.styleable#View_nextFocusDown
5298 */
5299 public void setNextFocusDownId(int nextFocusDownId) {
5300 mNextFocusDownId = nextFocusDownId;
5301 }
5302
5303 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005304 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5305 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5306 *
5307 * @attr ref android.R.styleable#View_nextFocusForward
5308 */
5309 public int getNextFocusForwardId() {
5310 return mNextFocusForwardId;
5311 }
5312
5313 /**
5314 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5315 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5316 * decide automatically.
5317 *
5318 * @attr ref android.R.styleable#View_nextFocusForward
5319 */
5320 public void setNextFocusForwardId(int nextFocusForwardId) {
5321 mNextFocusForwardId = nextFocusForwardId;
5322 }
5323
5324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 * Returns the visibility of this view and all of its ancestors
5326 *
5327 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5328 */
5329 public boolean isShown() {
5330 View current = this;
5331 //noinspection ConstantConditions
5332 do {
5333 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5334 return false;
5335 }
5336 ViewParent parent = current.mParent;
5337 if (parent == null) {
5338 return false; // We are not attached to the view root
5339 }
5340 if (!(parent instanceof View)) {
5341 return true;
5342 }
5343 current = (View) parent;
5344 } while (current != null);
5345
5346 return false;
5347 }
5348
5349 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005350 * Called by the view hierarchy when the content insets for a window have
5351 * changed, to allow it to adjust its content to fit within those windows.
5352 * The content insets tell you the space that the status bar, input method,
5353 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005355 * <p>You do not normally need to deal with this function, since the default
5356 * window decoration given to applications takes care of applying it to the
5357 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5358 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5359 * and your content can be placed under those system elements. You can then
5360 * use this method within your view hierarchy if you have parts of your UI
5361 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005363 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005364 * inset's to the view's padding, consuming that content (modifying the
5365 * insets to be 0), and returning true. This behavior is off by default, but can
5366 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5367 *
5368 * <p>This function's traversal down the hierarchy is depth-first. The same content
5369 * insets object is propagated down the hierarchy, so any changes made to it will
5370 * be seen by all following views (including potentially ones above in
5371 * the hierarchy since this is a depth-first traversal). The first view
5372 * that returns true will abort the entire traversal.
5373 *
5374 * <p>The default implementation works well for a situation where it is
5375 * used with a container that covers the entire window, allowing it to
5376 * apply the appropriate insets to its content on all edges. If you need
5377 * a more complicated layout (such as two different views fitting system
5378 * windows, one on the top of the window, and one on the bottom),
5379 * you can override the method and handle the insets however you would like.
5380 * Note that the insets provided by the framework are always relative to the
5381 * far edges of the window, not accounting for the location of the called view
5382 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005383 * where the layout will place the view, as it is done before layout happens.)
5384 *
5385 * <p>Note: unlike many View methods, there is no dispatch phase to this
5386 * call. If you are overriding it in a ViewGroup and want to allow the
5387 * call to continue to your children, you must be sure to call the super
5388 * implementation.
5389 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005390 * <p>Here is a sample layout that makes use of fitting system windows
5391 * to have controls for a video view placed inside of the window decorations
5392 * that it hides and shows. This can be used with code like the second
5393 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5394 *
5395 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5396 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005397 * @param insets Current content insets of the window. Prior to
5398 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5399 * the insets or else you and Android will be unhappy.
5400 *
5401 * @return Return true if this view applied the insets and it should not
5402 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005403 * @see #getFitsSystemWindows()
5404 * @see #setFitsSystemWindows()
5405 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 */
5407 protected boolean fitSystemWindows(Rect insets) {
5408 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005409 mUserPaddingStart = -1;
5410 mUserPaddingEnd = -1;
5411 mUserPaddingRelative = false;
5412 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5413 || mAttachInfo == null
5414 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5415 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5416 return true;
5417 } else {
5418 internalSetPadding(0, 0, 0, 0);
5419 return false;
5420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 }
5422 return false;
5423 }
5424
5425 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005426 * Sets whether or not this view should account for system screen decorations
5427 * such as the status bar and inset its content; that is, controlling whether
5428 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5429 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005430 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005431 * <p>Note that if you are providing your own implementation of
5432 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5433 * flag to true -- your implementation will be overriding the default
5434 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005435 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005436 * @param fitSystemWindows If true, then the default implementation of
5437 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005438 *
5439 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005440 * @see #getFitsSystemWindows()
5441 * @see #fitSystemWindows(Rect)
5442 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005443 */
5444 public void setFitsSystemWindows(boolean fitSystemWindows) {
5445 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5446 }
5447
5448 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005449 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005450 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5451 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005452 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005453 * @return Returns true if the default implementation of
5454 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005455 *
5456 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005457 * @see #setFitsSystemWindows()
5458 * @see #fitSystemWindows(Rect)
5459 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005460 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005461 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005462 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5463 }
5464
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005465 /** @hide */
5466 public boolean fitsSystemWindows() {
5467 return getFitsSystemWindows();
5468 }
5469
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005470 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005471 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5472 */
5473 public void requestFitSystemWindows() {
5474 if (mParent != null) {
5475 mParent.requestFitSystemWindows();
5476 }
5477 }
5478
5479 /**
5480 * For use by PhoneWindow to make its own system window fitting optional.
5481 * @hide
5482 */
5483 public void makeOptionalFitsSystemWindows() {
5484 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5485 }
5486
5487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 * Returns the visibility status for this view.
5489 *
5490 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5491 * @attr ref android.R.styleable#View_visibility
5492 */
5493 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005494 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5495 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5496 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 })
5498 public int getVisibility() {
5499 return mViewFlags & VISIBILITY_MASK;
5500 }
5501
5502 /**
5503 * Set the enabled state of this view.
5504 *
5505 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5506 * @attr ref android.R.styleable#View_visibility
5507 */
5508 @RemotableViewMethod
5509 public void setVisibility(int visibility) {
5510 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005511 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 }
5513
5514 /**
5515 * Returns the enabled status for this view. The interpretation of the
5516 * enabled state varies by subclass.
5517 *
5518 * @return True if this view is enabled, false otherwise.
5519 */
5520 @ViewDebug.ExportedProperty
5521 public boolean isEnabled() {
5522 return (mViewFlags & ENABLED_MASK) == ENABLED;
5523 }
5524
5525 /**
5526 * Set the enabled state of this view. The interpretation of the enabled
5527 * state varies by subclass.
5528 *
5529 * @param enabled True if this view is enabled, false otherwise.
5530 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005531 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005533 if (enabled == isEnabled()) return;
5534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005535 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5536
5537 /*
5538 * The View most likely has to change its appearance, so refresh
5539 * the drawable state.
5540 */
5541 refreshDrawableState();
5542
5543 // Invalidate too, since the default behavior for views is to be
5544 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005545 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 }
5547
5548 /**
5549 * Set whether this view can receive the focus.
5550 *
5551 * Setting this to false will also ensure that this view is not focusable
5552 * in touch mode.
5553 *
5554 * @param focusable If true, this view can receive the focus.
5555 *
5556 * @see #setFocusableInTouchMode(boolean)
5557 * @attr ref android.R.styleable#View_focusable
5558 */
5559 public void setFocusable(boolean focusable) {
5560 if (!focusable) {
5561 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5562 }
5563 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5564 }
5565
5566 /**
5567 * Set whether this view can receive focus while in touch mode.
5568 *
5569 * Setting this to true will also ensure that this view is focusable.
5570 *
5571 * @param focusableInTouchMode If true, this view can receive the focus while
5572 * in touch mode.
5573 *
5574 * @see #setFocusable(boolean)
5575 * @attr ref android.R.styleable#View_focusableInTouchMode
5576 */
5577 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5578 // Focusable in touch mode should always be set before the focusable flag
5579 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5580 // which, in touch mode, will not successfully request focus on this view
5581 // because the focusable in touch mode flag is not set
5582 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5583 if (focusableInTouchMode) {
5584 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5585 }
5586 }
5587
5588 /**
5589 * Set whether this view should have sound effects enabled for events such as
5590 * clicking and touching.
5591 *
5592 * <p>You may wish to disable sound effects for a view if you already play sounds,
5593 * for instance, a dial key that plays dtmf tones.
5594 *
5595 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5596 * @see #isSoundEffectsEnabled()
5597 * @see #playSoundEffect(int)
5598 * @attr ref android.R.styleable#View_soundEffectsEnabled
5599 */
5600 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5601 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5602 }
5603
5604 /**
5605 * @return whether this view should have sound effects enabled for events such as
5606 * clicking and touching.
5607 *
5608 * @see #setSoundEffectsEnabled(boolean)
5609 * @see #playSoundEffect(int)
5610 * @attr ref android.R.styleable#View_soundEffectsEnabled
5611 */
5612 @ViewDebug.ExportedProperty
5613 public boolean isSoundEffectsEnabled() {
5614 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5615 }
5616
5617 /**
5618 * Set whether this view should have haptic feedback for events such as
5619 * long presses.
5620 *
5621 * <p>You may wish to disable haptic feedback if your view already controls
5622 * its own haptic feedback.
5623 *
5624 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5625 * @see #isHapticFeedbackEnabled()
5626 * @see #performHapticFeedback(int)
5627 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5628 */
5629 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5630 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5631 }
5632
5633 /**
5634 * @return whether this view should have haptic feedback enabled for events
5635 * long presses.
5636 *
5637 * @see #setHapticFeedbackEnabled(boolean)
5638 * @see #performHapticFeedback(int)
5639 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5640 */
5641 @ViewDebug.ExportedProperty
5642 public boolean isHapticFeedbackEnabled() {
5643 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5644 }
5645
5646 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005647 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005648 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005649 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5650 * {@link #LAYOUT_DIRECTION_RTL},
5651 * {@link #LAYOUT_DIRECTION_INHERIT} or
5652 * {@link #LAYOUT_DIRECTION_LOCALE}.
5653 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005654 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005655 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005656 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5657 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5658 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5659 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005660 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005661 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005662 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005663 }
5664
5665 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005666 * Set the layout direction for this view. This will propagate a reset of layout direction
5667 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005668 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005669 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5670 * {@link #LAYOUT_DIRECTION_RTL},
5671 * {@link #LAYOUT_DIRECTION_INHERIT} or
5672 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005673 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005674 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005675 */
5676 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005677 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005678 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005679 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005680 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005681 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005682 // Set the new layout direction (filtered) and ask for a layout pass
5683 mPrivateFlags2 |=
5684 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5685 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005686 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005687 }
5688
5689 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005690 * Returns the resolved layout direction for this view.
5691 *
5692 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005693 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005694 */
5695 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005696 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5697 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005698 })
5699 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005700 // The layout diretion will be resolved only if needed
5701 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5702 resolveLayoutDirection();
5703 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005704 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005705 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5706 }
5707
5708 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005709 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5710 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005711 *
5712 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005713 */
5714 @ViewDebug.ExportedProperty(category = "layout")
5715 public boolean isLayoutRtl() {
5716 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5717 }
5718
5719 /**
Adam Powell539ee872012-02-03 19:00:49 -08005720 * Indicates whether the view is currently tracking transient state that the
5721 * app should not need to concern itself with saving and restoring, but that
5722 * the framework should take special note to preserve when possible.
5723 *
Adam Powell785c4472012-05-02 21:25:39 -07005724 * <p>A view with transient state cannot be trivially rebound from an external
5725 * data source, such as an adapter binding item views in a list. This may be
5726 * because the view is performing an animation, tracking user selection
5727 * of content, or similar.</p>
5728 *
Adam Powell539ee872012-02-03 19:00:49 -08005729 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005730 */
5731 @ViewDebug.ExportedProperty(category = "layout")
5732 public boolean hasTransientState() {
5733 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5734 }
5735
5736 /**
5737 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005738 * framework should attempt to preserve when possible. This flag is reference counted,
5739 * so every call to setHasTransientState(true) should be paired with a later call
5740 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005741 *
Adam Powell785c4472012-05-02 21:25:39 -07005742 * <p>A view with transient state cannot be trivially rebound from an external
5743 * data source, such as an adapter binding item views in a list. This may be
5744 * because the view is performing an animation, tracking user selection
5745 * of content, or similar.</p>
5746 *
Adam Powell539ee872012-02-03 19:00:49 -08005747 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005748 */
5749 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005750 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5751 mTransientStateCount - 1;
5752 if (mTransientStateCount < 0) {
5753 mTransientStateCount = 0;
5754 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5755 "unmatched pair of setHasTransientState calls");
5756 }
5757 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005758 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005759 // update flag if we've just incremented up from 0 or decremented down to 0
5760 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5761 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5762 if (mParent != null) {
5763 try {
5764 mParent.childHasTransientStateChanged(this, hasTransientState);
5765 } catch (AbstractMethodError e) {
5766 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5767 " does not fully implement ViewParent", e);
5768 }
Adam Powell539ee872012-02-03 19:00:49 -08005769 }
5770 }
5771 }
5772
5773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 * If this view doesn't do any drawing on its own, set this flag to
5775 * allow further optimizations. By default, this flag is not set on
5776 * View, but could be set on some View subclasses such as ViewGroup.
5777 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005778 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5779 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 *
5781 * @param willNotDraw whether or not this View draw on its own
5782 */
5783 public void setWillNotDraw(boolean willNotDraw) {
5784 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5785 }
5786
5787 /**
5788 * Returns whether or not this View draws on its own.
5789 *
5790 * @return true if this view has nothing to draw, false otherwise
5791 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005792 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 public boolean willNotDraw() {
5794 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5795 }
5796
5797 /**
5798 * When a View's drawing cache is enabled, drawing is redirected to an
5799 * offscreen bitmap. Some views, like an ImageView, must be able to
5800 * bypass this mechanism if they already draw a single bitmap, to avoid
5801 * unnecessary usage of the memory.
5802 *
5803 * @param willNotCacheDrawing true if this view does not cache its
5804 * drawing, false otherwise
5805 */
5806 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5807 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5808 }
5809
5810 /**
5811 * Returns whether or not this View can cache its drawing or not.
5812 *
5813 * @return true if this view does not cache its drawing, false otherwise
5814 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005815 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816 public boolean willNotCacheDrawing() {
5817 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5818 }
5819
5820 /**
5821 * Indicates whether this view reacts to click events or not.
5822 *
5823 * @return true if the view is clickable, false otherwise
5824 *
5825 * @see #setClickable(boolean)
5826 * @attr ref android.R.styleable#View_clickable
5827 */
5828 @ViewDebug.ExportedProperty
5829 public boolean isClickable() {
5830 return (mViewFlags & CLICKABLE) == CLICKABLE;
5831 }
5832
5833 /**
5834 * Enables or disables click events for this view. When a view
5835 * is clickable it will change its state to "pressed" on every click.
5836 * Subclasses should set the view clickable to visually react to
5837 * user's clicks.
5838 *
5839 * @param clickable true to make the view clickable, false otherwise
5840 *
5841 * @see #isClickable()
5842 * @attr ref android.R.styleable#View_clickable
5843 */
5844 public void setClickable(boolean clickable) {
5845 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5846 }
5847
5848 /**
5849 * Indicates whether this view reacts to long click events or not.
5850 *
5851 * @return true if the view is long clickable, false otherwise
5852 *
5853 * @see #setLongClickable(boolean)
5854 * @attr ref android.R.styleable#View_longClickable
5855 */
5856 public boolean isLongClickable() {
5857 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5858 }
5859
5860 /**
5861 * Enables or disables long click events for this view. When a view is long
5862 * clickable it reacts to the user holding down the button for a longer
5863 * duration than a tap. This event can either launch the listener or a
5864 * context menu.
5865 *
5866 * @param longClickable true to make the view long clickable, false otherwise
5867 * @see #isLongClickable()
5868 * @attr ref android.R.styleable#View_longClickable
5869 */
5870 public void setLongClickable(boolean longClickable) {
5871 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5872 }
5873
5874 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005875 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 *
5877 * @see #isClickable()
5878 * @see #setClickable(boolean)
5879 *
5880 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5881 * the View's internal state from a previously set "pressed" state.
5882 */
5883 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005884 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 if (pressed) {
5887 mPrivateFlags |= PRESSED;
5888 } else {
5889 mPrivateFlags &= ~PRESSED;
5890 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005891
5892 if (needsRefresh) {
5893 refreshDrawableState();
5894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 dispatchSetPressed(pressed);
5896 }
5897
5898 /**
5899 * Dispatch setPressed to all of this View's children.
5900 *
5901 * @see #setPressed(boolean)
5902 *
5903 * @param pressed The new pressed state
5904 */
5905 protected void dispatchSetPressed(boolean pressed) {
5906 }
5907
5908 /**
5909 * Indicates whether the view is currently in pressed state. Unless
5910 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5911 * the pressed state.
5912 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005913 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005914 * @see #isClickable()
5915 * @see #setClickable(boolean)
5916 *
5917 * @return true if the view is currently pressed, false otherwise
5918 */
5919 public boolean isPressed() {
5920 return (mPrivateFlags & PRESSED) == PRESSED;
5921 }
5922
5923 /**
5924 * Indicates whether this view will save its state (that is,
5925 * whether its {@link #onSaveInstanceState} method will be called).
5926 *
5927 * @return Returns true if the view state saving is enabled, else false.
5928 *
5929 * @see #setSaveEnabled(boolean)
5930 * @attr ref android.R.styleable#View_saveEnabled
5931 */
5932 public boolean isSaveEnabled() {
5933 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5934 }
5935
5936 /**
5937 * Controls whether the saving of this view's state is
5938 * enabled (that is, whether its {@link #onSaveInstanceState} method
5939 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005940 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 * for its state to be saved. This flag can only disable the
5942 * saving of this view; any child views may still have their state saved.
5943 *
5944 * @param enabled Set to false to <em>disable</em> state saving, or true
5945 * (the default) to allow it.
5946 *
5947 * @see #isSaveEnabled()
5948 * @see #setId(int)
5949 * @see #onSaveInstanceState()
5950 * @attr ref android.R.styleable#View_saveEnabled
5951 */
5952 public void setSaveEnabled(boolean enabled) {
5953 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5954 }
5955
Jeff Brown85a31762010-09-01 17:01:00 -07005956 /**
5957 * Gets whether the framework should discard touches when the view's
5958 * window is obscured by another visible window.
5959 * Refer to the {@link View} security documentation for more details.
5960 *
5961 * @return True if touch filtering is enabled.
5962 *
5963 * @see #setFilterTouchesWhenObscured(boolean)
5964 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5965 */
5966 @ViewDebug.ExportedProperty
5967 public boolean getFilterTouchesWhenObscured() {
5968 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5969 }
5970
5971 /**
5972 * Sets whether the framework should discard touches when the view's
5973 * window is obscured by another visible window.
5974 * Refer to the {@link View} security documentation for more details.
5975 *
5976 * @param enabled True if touch filtering should be enabled.
5977 *
5978 * @see #getFilterTouchesWhenObscured
5979 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5980 */
5981 public void setFilterTouchesWhenObscured(boolean enabled) {
5982 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5983 FILTER_TOUCHES_WHEN_OBSCURED);
5984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985
5986 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005987 * Indicates whether the entire hierarchy under this view will save its
5988 * state when a state saving traversal occurs from its parent. The default
5989 * is true; if false, these views will not be saved unless
5990 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5991 *
5992 * @return Returns true if the view state saving from parent is enabled, else false.
5993 *
5994 * @see #setSaveFromParentEnabled(boolean)
5995 */
5996 public boolean isSaveFromParentEnabled() {
5997 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5998 }
5999
6000 /**
6001 * Controls whether the entire hierarchy under this view will save its
6002 * state when a state saving traversal occurs from its parent. The default
6003 * is true; if false, these views will not be saved unless
6004 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6005 *
6006 * @param enabled Set to false to <em>disable</em> state saving, or true
6007 * (the default) to allow it.
6008 *
6009 * @see #isSaveFromParentEnabled()
6010 * @see #setId(int)
6011 * @see #onSaveInstanceState()
6012 */
6013 public void setSaveFromParentEnabled(boolean enabled) {
6014 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6015 }
6016
6017
6018 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 * Returns whether this View is able to take focus.
6020 *
6021 * @return True if this view can take focus, or false otherwise.
6022 * @attr ref android.R.styleable#View_focusable
6023 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006024 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 public final boolean isFocusable() {
6026 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6027 }
6028
6029 /**
6030 * When a view is focusable, it may not want to take focus when in touch mode.
6031 * For example, a button would like focus when the user is navigating via a D-pad
6032 * so that the user can click on it, but once the user starts touching the screen,
6033 * the button shouldn't take focus
6034 * @return Whether the view is focusable in touch mode.
6035 * @attr ref android.R.styleable#View_focusableInTouchMode
6036 */
6037 @ViewDebug.ExportedProperty
6038 public final boolean isFocusableInTouchMode() {
6039 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6040 }
6041
6042 /**
6043 * Find the nearest view in the specified direction that can take focus.
6044 * This does not actually give focus to that view.
6045 *
6046 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6047 *
6048 * @return The nearest focusable in the specified direction, or null if none
6049 * can be found.
6050 */
6051 public View focusSearch(int direction) {
6052 if (mParent != null) {
6053 return mParent.focusSearch(this, direction);
6054 } else {
6055 return null;
6056 }
6057 }
6058
6059 /**
6060 * This method is the last chance for the focused view and its ancestors to
6061 * respond to an arrow key. This is called when the focused view did not
6062 * consume the key internally, nor could the view system find a new view in
6063 * the requested direction to give focus to.
6064 *
6065 * @param focused The currently focused view.
6066 * @param direction The direction focus wants to move. One of FOCUS_UP,
6067 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6068 * @return True if the this view consumed this unhandled move.
6069 */
6070 public boolean dispatchUnhandledMove(View focused, int direction) {
6071 return false;
6072 }
6073
6074 /**
6075 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006076 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006078 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6079 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 * @return The user specified next view, or null if there is none.
6081 */
6082 View findUserSetNextFocus(View root, int direction) {
6083 switch (direction) {
6084 case FOCUS_LEFT:
6085 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006086 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 case FOCUS_RIGHT:
6088 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006089 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 case FOCUS_UP:
6091 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006092 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 case FOCUS_DOWN:
6094 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006095 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006096 case FOCUS_FORWARD:
6097 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006098 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006099 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006100 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006101 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006102 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006103 @Override
6104 public boolean apply(View t) {
6105 return t.mNextFocusForwardId == id;
6106 }
6107 });
6108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 }
6110 return null;
6111 }
6112
Jeff Brown4dfbec22011-08-15 14:55:37 -07006113 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6114 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6115 @Override
6116 public boolean apply(View t) {
6117 return t.mID == childViewId;
6118 }
6119 });
6120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 if (result == null) {
6122 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6123 + "by user for id " + childViewId);
6124 }
6125 return result;
6126 }
6127
6128 /**
6129 * Find and return all focusable views that are descendants of this view,
6130 * possibly including this view if it is focusable itself.
6131 *
6132 * @param direction The direction of the focus
6133 * @return A list of focusable views
6134 */
6135 public ArrayList<View> getFocusables(int direction) {
6136 ArrayList<View> result = new ArrayList<View>(24);
6137 addFocusables(result, direction);
6138 return result;
6139 }
6140
6141 /**
6142 * Add any focusable views that are descendants of this view (possibly
6143 * including this view if it is focusable itself) to views. If we are in touch mode,
6144 * only add views that are also focusable in touch mode.
6145 *
6146 * @param views Focusable views found so far
6147 * @param direction The direction of the focus
6148 */
6149 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006150 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152
svetoslavganov75986cf2009-05-14 22:28:01 -07006153 /**
6154 * Adds any focusable views that are descendants of this view (possibly
6155 * including this view if it is focusable itself) to views. This method
6156 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006157 * only views focusable in touch mode if we are in touch mode or
6158 * only views that can take accessibility focus if accessibility is enabeld
6159 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006160 *
6161 * @param views Focusable views found so far or null if all we are interested is
6162 * the number of focusables.
6163 * @param direction The direction of the focus.
6164 * @param focusableMode The type of focusables to be added.
6165 *
6166 * @see #FOCUSABLES_ALL
6167 * @see #FOCUSABLES_TOUCH_MODE
6168 */
6169 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006170 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006171 return;
6172 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006173 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006174 if (isAccessibilityFocusable()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006175 views.add(this);
6176 return;
6177 }
6178 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006179 if (!isFocusable()) {
6180 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006181 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006182 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6183 && isInTouchMode() && !isFocusableInTouchMode()) {
6184 return;
6185 }
6186 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 }
6188
6189 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006190 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006191 * The search is performed by either the text that the View renders or the content
6192 * description that describes the view for accessibility purposes and the view does
6193 * not render or both. Clients can specify how the search is to be performed via
6194 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6195 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006196 *
6197 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006198 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006199 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006200 * @see #FIND_VIEWS_WITH_TEXT
6201 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6202 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006203 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006204 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006205 if (getAccessibilityNodeProvider() != null) {
6206 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6207 outViews.add(this);
6208 }
6209 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006210 && (searched != null && searched.length() > 0)
6211 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006212 String searchedLowerCase = searched.toString().toLowerCase();
6213 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6214 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6215 outViews.add(this);
6216 }
6217 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006218 }
6219
6220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006221 * Find and return all touchable views that are descendants of this view,
6222 * possibly including this view if it is touchable itself.
6223 *
6224 * @return A list of touchable views
6225 */
6226 public ArrayList<View> getTouchables() {
6227 ArrayList<View> result = new ArrayList<View>();
6228 addTouchables(result);
6229 return result;
6230 }
6231
6232 /**
6233 * Add any touchable views that are descendants of this view (possibly
6234 * including this view if it is touchable itself) to views.
6235 *
6236 * @param views Touchable views found so far
6237 */
6238 public void addTouchables(ArrayList<View> views) {
6239 final int viewFlags = mViewFlags;
6240
6241 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6242 && (viewFlags & ENABLED_MASK) == ENABLED) {
6243 views.add(this);
6244 }
6245 }
6246
6247 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006248 * Returns whether this View is accessibility focused.
6249 *
6250 * @return True if this View is accessibility focused.
6251 */
6252 boolean isAccessibilityFocused() {
6253 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6254 }
6255
6256 /**
6257 * Call this to try to give accessibility focus to this view.
6258 *
6259 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6260 * returns false or the view is no visible or the view already has accessibility
6261 * focus.
6262 *
6263 * See also {@link #focusSearch(int)}, which is what you call to say that you
6264 * have focus, and you want your parent to look for the next one.
6265 *
6266 * @return Whether this view actually took accessibility focus.
6267 *
6268 * @hide
6269 */
6270 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006271 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6272 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006273 return false;
6274 }
6275 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6276 return false;
6277 }
6278 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6279 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6280 ViewRootImpl viewRootImpl = getViewRootImpl();
6281 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006282 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006283 }
6284 invalidate();
6285 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6286 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006287 return true;
6288 }
6289 return false;
6290 }
6291
6292 /**
6293 * Call this to try to clear accessibility focus of this view.
6294 *
6295 * See also {@link #focusSearch(int)}, which is what you call to say that you
6296 * have focus, and you want your parent to look for the next one.
6297 *
6298 * @hide
6299 */
6300 public void clearAccessibilityFocus() {
Svetoslav Ganov791fd312012-05-14 15:12:30 -07006301 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6302 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006303 invalidate();
6304 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6305 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006306 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006307 // Clear the global reference of accessibility focus if this
6308 // view or any of its descendants had accessibility focus.
6309 ViewRootImpl viewRootImpl = getViewRootImpl();
6310 if (viewRootImpl != null) {
6311 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6312 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006313 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006314 }
6315 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006316 }
6317
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006318 private void sendAccessibilityHoverEvent(int eventType) {
6319 // Since we are not delivering to a client accessibility events from not
6320 // important views (unless the clinet request that) we need to fire the
6321 // event from the deepest view exposed to the client. As a consequence if
6322 // the user crosses a not exposed view the client will see enter and exit
6323 // of the exposed predecessor followed by and enter and exit of that same
6324 // predecessor when entering and exiting the not exposed descendant. This
6325 // is fine since the client has a clear idea which view is hovered at the
6326 // price of a couple more events being sent. This is a simple and
6327 // working solution.
6328 View source = this;
6329 while (true) {
6330 if (source.includeForAccessibility()) {
6331 source.sendAccessibilityEvent(eventType);
6332 return;
6333 }
6334 ViewParent parent = source.getParent();
6335 if (parent instanceof View) {
6336 source = (View) parent;
6337 } else {
6338 return;
6339 }
6340 }
6341 }
6342
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006343 private void requestAccessibilityFocusFromHover() {
6344 if (includeForAccessibility() && isActionableForAccessibility()) {
6345 requestAccessibilityFocus();
6346 } else {
6347 if (mParent != null) {
6348 View nextFocus = mParent.findViewToTakeAccessibilityFocusFromHover(this, this);
6349 if (nextFocus != null) {
6350 nextFocus.requestAccessibilityFocus();
6351 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006352 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006353 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006354 }
6355
Svetoslav Ganova90e4512012-06-01 19:02:32 -07006356 private boolean canTakeAccessibilityFocusFromHover() {
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006357 if (includeForAccessibility() && isActionableForAccessibility()) {
6358 return true;
6359 }
6360 if (mParent != null) {
6361 return (mParent.findViewToTakeAccessibilityFocusFromHover(this, this) == this);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006362 }
6363 return false;
6364 }
6365
6366 /**
6367 * Clears accessibility focus without calling any callback methods
6368 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6369 * is used for clearing accessibility focus when giving this focus to
6370 * another view.
6371 */
6372 void clearAccessibilityFocusNoCallbacks() {
6373 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6374 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6375 invalidate();
6376 }
6377 }
6378
6379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 * Call this to try to give focus to a specific view or to one of its
6381 * descendants.
6382 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006383 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6384 * false), or if it is focusable and it is not focusable in touch mode
6385 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006387 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 * have focus, and you want your parent to look for the next one.
6389 *
6390 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6391 * {@link #FOCUS_DOWN} and <code>null</code>.
6392 *
6393 * @return Whether this view or one of its descendants actually took focus.
6394 */
6395 public final boolean requestFocus() {
6396 return requestFocus(View.FOCUS_DOWN);
6397 }
6398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006399 /**
6400 * Call this to try to give focus to a specific view or to one of its
6401 * descendants and give it a hint about what direction focus is heading.
6402 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006403 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6404 * false), or if it is focusable and it is not focusable in touch mode
6405 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006407 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006408 * have focus, and you want your parent to look for the next one.
6409 *
6410 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6411 * <code>null</code> set for the previously focused rectangle.
6412 *
6413 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6414 * @return Whether this view or one of its descendants actually took focus.
6415 */
6416 public final boolean requestFocus(int direction) {
6417 return requestFocus(direction, null);
6418 }
6419
6420 /**
6421 * Call this to try to give focus to a specific view or to one of its descendants
6422 * and give it hints about the direction and a specific rectangle that the focus
6423 * is coming from. The rectangle can help give larger views a finer grained hint
6424 * about where focus is coming from, and therefore, where to show selection, or
6425 * forward focus change internally.
6426 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006427 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6428 * false), or if it is focusable and it is not focusable in touch mode
6429 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006430 *
6431 * A View will not take focus if it is not visible.
6432 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006433 * A View will not take focus if one of its parents has
6434 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6435 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006437 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006438 * have focus, and you want your parent to look for the next one.
6439 *
6440 * You may wish to override this method if your custom {@link View} has an internal
6441 * {@link View} that it wishes to forward the request to.
6442 *
6443 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6444 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6445 * to give a finer grained hint about where focus is coming from. May be null
6446 * if there is no hint.
6447 * @return Whether this view or one of its descendants actually took focus.
6448 */
6449 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006450 return requestFocusNoSearch(direction, previouslyFocusedRect);
6451 }
6452
6453 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 // need to be focusable
6455 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6456 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6457 return false;
6458 }
6459
6460 // need to be focusable in touch mode if in touch mode
6461 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006462 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6463 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 }
6465
6466 // need to not have any parents blocking us
6467 if (hasAncestorThatBlocksDescendantFocus()) {
6468 return false;
6469 }
6470
6471 handleFocusGainInternal(direction, previouslyFocusedRect);
6472 return true;
6473 }
6474
6475 /**
6476 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6477 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6478 * touch mode to request focus when they are touched.
6479 *
6480 * @return Whether this view or one of its descendants actually took focus.
6481 *
6482 * @see #isInTouchMode()
6483 *
6484 */
6485 public final boolean requestFocusFromTouch() {
6486 // Leave touch mode if we need to
6487 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006488 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006489 if (viewRoot != null) {
6490 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 }
6492 }
6493 return requestFocus(View.FOCUS_DOWN);
6494 }
6495
6496 /**
6497 * @return Whether any ancestor of this view blocks descendant focus.
6498 */
6499 private boolean hasAncestorThatBlocksDescendantFocus() {
6500 ViewParent ancestor = mParent;
6501 while (ancestor instanceof ViewGroup) {
6502 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6503 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6504 return true;
6505 } else {
6506 ancestor = vgAncestor.getParent();
6507 }
6508 }
6509 return false;
6510 }
6511
6512 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006513 * Gets the mode for determining whether this View is important for accessibility
6514 * which is if it fires accessibility events and if it is reported to
6515 * accessibility services that query the screen.
6516 *
6517 * @return The mode for determining whether a View is important for accessibility.
6518 *
6519 * @attr ref android.R.styleable#View_importantForAccessibility
6520 *
6521 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6522 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6523 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6524 */
6525 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006526 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6527 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6528 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006529 })
6530 public int getImportantForAccessibility() {
6531 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6532 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6533 }
6534
6535 /**
6536 * Sets how to determine whether this view is important for accessibility
6537 * which is if it fires accessibility events and if it is reported to
6538 * accessibility services that query the screen.
6539 *
6540 * @param mode How to determine whether this view is important for accessibility.
6541 *
6542 * @attr ref android.R.styleable#View_importantForAccessibility
6543 *
6544 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6545 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6546 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6547 */
6548 public void setImportantForAccessibility(int mode) {
6549 if (mode != getImportantForAccessibility()) {
6550 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6551 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6552 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6553 notifyAccessibilityStateChanged();
6554 }
6555 }
6556
6557 /**
6558 * Gets whether this view should be exposed for accessibility.
6559 *
6560 * @return Whether the view is exposed for accessibility.
6561 *
6562 * @hide
6563 */
6564 public boolean isImportantForAccessibility() {
6565 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6566 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6567 switch (mode) {
6568 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6569 return true;
6570 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6571 return false;
6572 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6573 return isActionableForAccessibility() || hasListenersForAccessibility();
6574 default:
6575 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6576 + mode);
6577 }
6578 }
6579
6580 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006581 * Gets the mode for determining whether this View can take accessibility focus.
6582 *
6583 * @return The mode for determining whether a View can take accessibility focus.
6584 *
6585 * @attr ref android.R.styleable#View_accessibilityFocusable
6586 *
6587 * @see #ACCESSIBILITY_FOCUSABLE_YES
6588 * @see #ACCESSIBILITY_FOCUSABLE_NO
6589 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6590 *
6591 * @hide
6592 */
6593 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6594 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_AUTO, to = "auto"),
6595 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_YES, to = "yes"),
6596 @ViewDebug.IntToString(from = ACCESSIBILITY_FOCUSABLE_NO, to = "no")
6597 })
6598 public int getAccessibilityFocusable() {
6599 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK) >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6600 }
6601
6602 /**
6603 * Sets how to determine whether this view can take accessibility focus.
6604 *
6605 * @param mode How to determine whether this view can take accessibility focus.
6606 *
6607 * @attr ref android.R.styleable#View_accessibilityFocusable
6608 *
6609 * @see #ACCESSIBILITY_FOCUSABLE_YES
6610 * @see #ACCESSIBILITY_FOCUSABLE_NO
6611 * @see #ACCESSIBILITY_FOCUSABLE_AUTO
6612 *
6613 * @hide
6614 */
6615 public void setAccessibilityFocusable(int mode) {
6616 if (mode != getAccessibilityFocusable()) {
6617 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSABLE_MASK;
6618 mPrivateFlags2 |= (mode << ACCESSIBILITY_FOCUSABLE_SHIFT)
6619 & ACCESSIBILITY_FOCUSABLE_MASK;
6620 notifyAccessibilityStateChanged();
6621 }
6622 }
6623
6624 /**
6625 * Gets whether this view can take accessibility focus.
6626 *
6627 * @return Whether the view can take accessibility focus.
6628 *
6629 * @hide
6630 */
6631 public boolean isAccessibilityFocusable() {
6632 final int mode = (mPrivateFlags2 & ACCESSIBILITY_FOCUSABLE_MASK)
6633 >>> ACCESSIBILITY_FOCUSABLE_SHIFT;
6634 switch (mode) {
6635 case ACCESSIBILITY_FOCUSABLE_YES:
6636 return true;
6637 case ACCESSIBILITY_FOCUSABLE_NO:
6638 return false;
6639 case ACCESSIBILITY_FOCUSABLE_AUTO:
6640 return canTakeAccessibilityFocusFromHover()
6641 || getAccessibilityNodeProvider() != null;
6642 default:
6643 throw new IllegalArgumentException("Unknow accessibility focusable mode: " + mode);
6644 }
6645 }
6646
6647 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006648 * Gets the parent for accessibility purposes. Note that the parent for
6649 * accessibility is not necessary the immediate parent. It is the first
6650 * predecessor that is important for accessibility.
6651 *
6652 * @return The parent for accessibility purposes.
6653 */
6654 public ViewParent getParentForAccessibility() {
6655 if (mParent instanceof View) {
6656 View parentView = (View) mParent;
6657 if (parentView.includeForAccessibility()) {
6658 return mParent;
6659 } else {
6660 return mParent.getParentForAccessibility();
6661 }
6662 }
6663 return null;
6664 }
6665
6666 /**
6667 * Adds the children of a given View for accessibility. Since some Views are
6668 * not important for accessibility the children for accessibility are not
6669 * necessarily direct children of the riew, rather they are the first level of
6670 * descendants important for accessibility.
6671 *
6672 * @param children The list of children for accessibility.
6673 */
6674 public void addChildrenForAccessibility(ArrayList<View> children) {
6675 if (includeForAccessibility()) {
6676 children.add(this);
6677 }
6678 }
6679
6680 /**
6681 * Whether to regard this view for accessibility. A view is regarded for
6682 * accessibility if it is important for accessibility or the querying
6683 * accessibility service has explicitly requested that view not
6684 * important for accessibility are regarded.
6685 *
6686 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006687 *
6688 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006689 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006690 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006691 if (mAttachInfo != null) {
6692 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006693 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006694 }
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006695 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006696 }
6697 return false;
6698 }
6699
6700 /**
6701 * Returns whether the View is considered actionable from
6702 * accessibility perspective. Such view are important for
6703 * accessiiblity.
6704 *
6705 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006706 *
6707 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006708 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006709 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006710 return (isClickable() || isLongClickable() || isFocusable());
6711 }
6712
6713 /**
6714 * Returns whether the View has registered callbacks wich makes it
6715 * important for accessiiblity.
6716 *
6717 * @return True if the view is actionable for accessibility.
6718 */
6719 private boolean hasListenersForAccessibility() {
6720 ListenerInfo info = getListenerInfo();
6721 return mTouchDelegate != null || info.mOnKeyListener != null
6722 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6723 || info.mOnHoverListener != null || info.mOnDragListener != null;
6724 }
6725
6726 /**
6727 * Notifies accessibility services that some view's important for
6728 * accessibility state has changed. Note that such notifications
6729 * are made at most once every
6730 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6731 * to avoid unnecessary load to the system. Also once a view has
6732 * made a notifucation this method is a NOP until the notification has
6733 * been sent to clients.
6734 *
6735 * @hide
6736 *
6737 * TODO: Makse sure this method is called for any view state change
6738 * that is interesting for accessilility purposes.
6739 */
6740 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006741 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6742 return;
6743 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6745 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6746 if (mParent != null) {
6747 mParent.childAccessibilityStateChanged(this);
6748 }
6749 }
6750 }
6751
6752 /**
6753 * Reset the state indicating the this view has requested clients
6754 * interested in its accessiblity state to be notified.
6755 *
6756 * @hide
6757 */
6758 public void resetAccessibilityStateChanged() {
6759 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6760 }
6761
6762 /**
6763 * Performs the specified accessibility action on the view. For
6764 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006765 * <p>
6766 * If an {@link AccessibilityDelegate} has been specified via calling
6767 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6768 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6769 * is responsible for handling this call.
6770 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006771 *
6772 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006773 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006774 * @return Whether the action was performed.
6775 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006776 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006777 if (mAccessibilityDelegate != null) {
6778 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6779 } else {
6780 return performAccessibilityActionInternal(action, arguments);
6781 }
6782 }
6783
6784 /**
6785 * @see #performAccessibilityAction(int, Bundle)
6786 *
6787 * Note: Called from the default {@link AccessibilityDelegate}.
6788 */
6789 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006790 switch (action) {
6791 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006792 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006793 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006794 }
6795 } break;
6796 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6797 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006798 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006799 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006800 } break;
6801 case AccessibilityNodeInfo.ACTION_FOCUS: {
6802 if (!hasFocus()) {
6803 // Get out of touch mode since accessibility
6804 // wants to move focus around.
6805 getViewRootImpl().ensureTouchMode(false);
6806 return requestFocus();
6807 }
6808 } break;
6809 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6810 if (hasFocus()) {
6811 clearFocus();
6812 return !isFocused();
6813 }
6814 } break;
6815 case AccessibilityNodeInfo.ACTION_SELECT: {
6816 if (!isSelected()) {
6817 setSelected(true);
6818 return isSelected();
6819 }
6820 } break;
6821 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6822 if (isSelected()) {
6823 setSelected(false);
6824 return !isSelected();
6825 }
6826 } break;
6827 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006828 final int mode = getAccessibilityFocusable();
6829 if (!isAccessibilityFocused()
6830 && (mode == ACCESSIBILITY_FOCUSABLE_YES
6831 || mode == ACCESSIBILITY_FOCUSABLE_AUTO)) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006832 return requestAccessibilityFocus();
6833 }
6834 } break;
6835 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6836 if (isAccessibilityFocused()) {
6837 clearAccessibilityFocus();
6838 return true;
6839 }
6840 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006841 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6842 if (arguments != null) {
6843 final int granularity = arguments.getInt(
6844 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6845 return nextAtGranularity(granularity);
6846 }
6847 } break;
6848 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6849 if (arguments != null) {
6850 final int granularity = arguments.getInt(
6851 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6852 return previousAtGranularity(granularity);
6853 }
6854 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006855 }
6856 return false;
6857 }
6858
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006859 private boolean nextAtGranularity(int granularity) {
6860 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006861 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006862 return false;
6863 }
6864 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6865 if (iterator == null) {
6866 return false;
6867 }
6868 final int current = getAccessibilityCursorPosition();
6869 final int[] range = iterator.following(current);
6870 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006871 return false;
6872 }
6873 final int start = range[0];
6874 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006875 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006876 sendViewTextTraversedAtGranularityEvent(
6877 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6878 granularity, start, end);
6879 return true;
6880 }
6881
6882 private boolean previousAtGranularity(int granularity) {
6883 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006884 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006885 return false;
6886 }
6887 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6888 if (iterator == null) {
6889 return false;
6890 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006891 int current = getAccessibilityCursorPosition();
6892 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6893 current = text.length();
6894 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6895 // When traversing by character we always put the cursor after the character
6896 // to ease edit and have to compensate before asking the for previous segment.
6897 current--;
6898 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006899 final int[] range = iterator.preceding(current);
6900 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006901 return false;
6902 }
6903 final int start = range[0];
6904 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006905 // Always put the cursor after the character to ease edit.
6906 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6907 setAccessibilityCursorPosition(end);
6908 } else {
6909 setAccessibilityCursorPosition(start);
6910 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006911 sendViewTextTraversedAtGranularityEvent(
6912 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6913 granularity, start, end);
6914 return true;
6915 }
6916
6917 /**
6918 * Gets the text reported for accessibility purposes.
6919 *
6920 * @return The accessibility text.
6921 *
6922 * @hide
6923 */
6924 public CharSequence getIterableTextForAccessibility() {
6925 return mContentDescription;
6926 }
6927
6928 /**
6929 * @hide
6930 */
6931 public int getAccessibilityCursorPosition() {
6932 return mAccessibilityCursorPosition;
6933 }
6934
6935 /**
6936 * @hide
6937 */
6938 public void setAccessibilityCursorPosition(int position) {
6939 mAccessibilityCursorPosition = position;
6940 }
6941
6942 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6943 int fromIndex, int toIndex) {
6944 if (mParent == null) {
6945 return;
6946 }
6947 AccessibilityEvent event = AccessibilityEvent.obtain(
6948 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6949 onInitializeAccessibilityEvent(event);
6950 onPopulateAccessibilityEvent(event);
6951 event.setFromIndex(fromIndex);
6952 event.setToIndex(toIndex);
6953 event.setAction(action);
6954 event.setMovementGranularity(granularity);
6955 mParent.requestSendAccessibilityEvent(this, event);
6956 }
6957
6958 /**
6959 * @hide
6960 */
6961 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6962 switch (granularity) {
6963 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6964 CharSequence text = getIterableTextForAccessibility();
6965 if (text != null && text.length() > 0) {
6966 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006967 CharacterTextSegmentIterator.getInstance(
6968 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006969 iterator.initialize(text.toString());
6970 return iterator;
6971 }
6972 } break;
6973 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6974 CharSequence text = getIterableTextForAccessibility();
6975 if (text != null && text.length() > 0) {
6976 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006977 WordTextSegmentIterator.getInstance(
6978 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006979 iterator.initialize(text.toString());
6980 return iterator;
6981 }
6982 } break;
6983 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6984 CharSequence text = getIterableTextForAccessibility();
6985 if (text != null && text.length() > 0) {
6986 ParagraphTextSegmentIterator iterator =
6987 ParagraphTextSegmentIterator.getInstance();
6988 iterator.initialize(text.toString());
6989 return iterator;
6990 }
6991 } break;
6992 }
6993 return null;
6994 }
6995
Svetoslav Ganov42138042012-03-20 11:51:39 -07006996 /**
Romain Guya440b002010-02-24 15:57:54 -08006997 * @hide
6998 */
6999 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007000 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007001 clearDisplayList();
7002
Romain Guya440b002010-02-24 15:57:54 -08007003 onStartTemporaryDetach();
7004 }
7005
7006 /**
7007 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7009 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007010 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007011 */
7012 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007013 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08007014 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007015 }
7016
7017 /**
7018 * @hide
7019 */
7020 public void dispatchFinishTemporaryDetach() {
7021 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 }
Romain Guy8506ab42009-06-11 17:35:47 -07007023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 /**
7025 * Called after {@link #onStartTemporaryDetach} when the container is done
7026 * changing the view.
7027 */
7028 public void onFinishTemporaryDetach() {
7029 }
Romain Guy8506ab42009-06-11 17:35:47 -07007030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007032 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7033 * for this view's window. Returns null if the view is not currently attached
7034 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007035 * just use the standard high-level event callbacks like
7036 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007037 */
7038 public KeyEvent.DispatcherState getKeyDispatcherState() {
7039 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7040 }
Joe Malin32736f02011-01-19 16:14:20 -08007041
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007042 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 * Dispatch a key event before it is processed by any input method
7044 * associated with the view hierarchy. This can be used to intercept
7045 * key events in special situations before the IME consumes them; a
7046 * typical example would be handling the BACK key to update the application's
7047 * UI instead of allowing the IME to see it and close itself.
7048 *
7049 * @param event The key event to be dispatched.
7050 * @return True if the event was handled, false otherwise.
7051 */
7052 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7053 return onKeyPreIme(event.getKeyCode(), event);
7054 }
7055
7056 /**
7057 * Dispatch a key event to the next view on the focus path. This path runs
7058 * from the top of the view tree down to the currently focused view. If this
7059 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7060 * the next node down the focus path. This method also fires any key
7061 * listeners.
7062 *
7063 * @param event The key event to be dispatched.
7064 * @return True if the event was handled, false otherwise.
7065 */
7066 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007067 if (mInputEventConsistencyVerifier != null) {
7068 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070
Jeff Brown21bc5c92011-02-28 18:27:14 -08007071 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007072 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007073 ListenerInfo li = mListenerInfo;
7074 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7075 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007076 return true;
7077 }
7078
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007079 if (event.dispatch(this, mAttachInfo != null
7080 ? mAttachInfo.mKeyDispatchState : null, this)) {
7081 return true;
7082 }
7083
7084 if (mInputEventConsistencyVerifier != null) {
7085 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7086 }
7087 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 }
7089
7090 /**
7091 * Dispatches a key shortcut event.
7092 *
7093 * @param event The key event to be dispatched.
7094 * @return True if the event was handled by the view, false otherwise.
7095 */
7096 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7097 return onKeyShortcut(event.getKeyCode(), event);
7098 }
7099
7100 /**
7101 * Pass the touch screen motion event down to the target view, or this
7102 * view if it is the target.
7103 *
7104 * @param event The motion event to be dispatched.
7105 * @return True if the event was handled by the view, false otherwise.
7106 */
7107 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007108 if (mInputEventConsistencyVerifier != null) {
7109 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7110 }
7111
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007112 if (onFilterTouchEventForSecurity(event)) {
7113 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007114 ListenerInfo li = mListenerInfo;
7115 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7116 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007117 return true;
7118 }
7119
7120 if (onTouchEvent(event)) {
7121 return true;
7122 }
Jeff Brown85a31762010-09-01 17:01:00 -07007123 }
7124
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007125 if (mInputEventConsistencyVerifier != null) {
7126 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007128 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 }
7130
7131 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007132 * Filter the touch event to apply security policies.
7133 *
7134 * @param event The motion event to be filtered.
7135 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007136 *
Jeff Brown85a31762010-09-01 17:01:00 -07007137 * @see #getFilterTouchesWhenObscured
7138 */
7139 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007140 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007141 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7142 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7143 // Window is obscured, drop this touch.
7144 return false;
7145 }
7146 return true;
7147 }
7148
7149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150 * Pass a trackball motion event down to the focused view.
7151 *
7152 * @param event The motion event to be dispatched.
7153 * @return True if the event was handled by the view, false otherwise.
7154 */
7155 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007156 if (mInputEventConsistencyVerifier != null) {
7157 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7158 }
7159
Romain Guy02ccac62011-06-24 13:20:23 -07007160 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 }
7162
7163 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007164 * Dispatch a generic motion event.
7165 * <p>
7166 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7167 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007168 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007169 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007170 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007171 *
7172 * @param event The motion event to be dispatched.
7173 * @return True if the event was handled by the view, false otherwise.
7174 */
7175 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007176 if (mInputEventConsistencyVerifier != null) {
7177 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7178 }
7179
Jeff Browna032cc02011-03-07 16:56:21 -08007180 final int source = event.getSource();
7181 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7182 final int action = event.getAction();
7183 if (action == MotionEvent.ACTION_HOVER_ENTER
7184 || action == MotionEvent.ACTION_HOVER_MOVE
7185 || action == MotionEvent.ACTION_HOVER_EXIT) {
7186 if (dispatchHoverEvent(event)) {
7187 return true;
7188 }
7189 } else if (dispatchGenericPointerEvent(event)) {
7190 return true;
7191 }
7192 } else if (dispatchGenericFocusedEvent(event)) {
7193 return true;
7194 }
7195
Jeff Brown10b62902011-06-20 16:40:37 -07007196 if (dispatchGenericMotionEventInternal(event)) {
7197 return true;
7198 }
7199
7200 if (mInputEventConsistencyVerifier != null) {
7201 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7202 }
7203 return false;
7204 }
7205
7206 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007207 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007208 ListenerInfo li = mListenerInfo;
7209 if (li != null && li.mOnGenericMotionListener != null
7210 && (mViewFlags & ENABLED_MASK) == ENABLED
7211 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007212 return true;
7213 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007214
7215 if (onGenericMotionEvent(event)) {
7216 return true;
7217 }
7218
7219 if (mInputEventConsistencyVerifier != null) {
7220 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7221 }
7222 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007223 }
7224
7225 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007226 * Dispatch a hover event.
7227 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007228 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007229 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007230 * </p>
7231 *
7232 * @param event The motion event to be dispatched.
7233 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007234 */
7235 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007236 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007237 ListenerInfo li = mListenerInfo;
7238 if (li != null && li.mOnHoverListener != null
7239 && (mViewFlags & ENABLED_MASK) == ENABLED
7240 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007241 return true;
7242 }
7243
Jeff Browna032cc02011-03-07 16:56:21 -08007244 return onHoverEvent(event);
7245 }
7246
7247 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007248 * Returns true if the view has a child to which it has recently sent
7249 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7250 * it does not have a hovered child, then it must be the innermost hovered view.
7251 * @hide
7252 */
7253 protected boolean hasHoveredChild() {
7254 return false;
7255 }
7256
7257 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007258 * Dispatch a generic motion event to the view under the first pointer.
7259 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007260 * Do not call this method directly.
7261 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007262 * </p>
7263 *
7264 * @param event The motion event to be dispatched.
7265 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007266 */
7267 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7268 return false;
7269 }
7270
7271 /**
7272 * Dispatch a generic motion event to the currently focused view.
7273 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007274 * Do not call this method directly.
7275 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007276 * </p>
7277 *
7278 * @param event The motion event to be dispatched.
7279 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007280 */
7281 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7282 return false;
7283 }
7284
7285 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007286 * Dispatch a pointer event.
7287 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007288 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7289 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7290 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007291 * and should not be expected to handle other pointing device features.
7292 * </p>
7293 *
7294 * @param event The motion event to be dispatched.
7295 * @return True if the event was handled by the view, false otherwise.
7296 * @hide
7297 */
7298 public final boolean dispatchPointerEvent(MotionEvent event) {
7299 if (event.isTouchEvent()) {
7300 return dispatchTouchEvent(event);
7301 } else {
7302 return dispatchGenericMotionEvent(event);
7303 }
7304 }
7305
7306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 * Called when the window containing this view gains or loses window focus.
7308 * ViewGroups should override to route to their children.
7309 *
7310 * @param hasFocus True if the window containing this view now has focus,
7311 * false otherwise.
7312 */
7313 public void dispatchWindowFocusChanged(boolean hasFocus) {
7314 onWindowFocusChanged(hasFocus);
7315 }
7316
7317 /**
7318 * Called when the window containing this view gains or loses focus. Note
7319 * that this is separate from view focus: to receive key events, both
7320 * your view and its window must have focus. If a window is displayed
7321 * on top of yours that takes input focus, then your own window will lose
7322 * focus but the view focus will remain unchanged.
7323 *
7324 * @param hasWindowFocus True if the window containing this view now has
7325 * focus, false otherwise.
7326 */
7327 public void onWindowFocusChanged(boolean hasWindowFocus) {
7328 InputMethodManager imm = InputMethodManager.peekInstance();
7329 if (!hasWindowFocus) {
7330 if (isPressed()) {
7331 setPressed(false);
7332 }
7333 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7334 imm.focusOut(this);
7335 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007336 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007337 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007338 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
7340 imm.focusIn(this);
7341 }
7342 refreshDrawableState();
7343 }
7344
7345 /**
7346 * Returns true if this view is in a window that currently has window focus.
7347 * Note that this is not the same as the view itself having focus.
7348 *
7349 * @return True if this view is in a window that currently has window focus.
7350 */
7351 public boolean hasWindowFocus() {
7352 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7353 }
7354
7355 /**
Adam Powell326d8082009-12-09 15:10:07 -08007356 * Dispatch a view visibility change down the view hierarchy.
7357 * ViewGroups should override to route to their children.
7358 * @param changedView The view whose visibility changed. Could be 'this' or
7359 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007360 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7361 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007362 */
7363 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7364 onVisibilityChanged(changedView, visibility);
7365 }
7366
7367 /**
7368 * Called when the visibility of the view or an ancestor of the view is changed.
7369 * @param changedView The view whose visibility changed. Could be 'this' or
7370 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007371 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7372 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007373 */
7374 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007375 if (visibility == VISIBLE) {
7376 if (mAttachInfo != null) {
7377 initialAwakenScrollBars();
7378 } else {
7379 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
7380 }
7381 }
Adam Powell326d8082009-12-09 15:10:07 -08007382 }
7383
7384 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007385 * Dispatch a hint about whether this view is displayed. For instance, when
7386 * a View moves out of the screen, it might receives a display hint indicating
7387 * the view is not displayed. Applications should not <em>rely</em> on this hint
7388 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007389 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007390 * @param hint A hint about whether or not this view is displayed:
7391 * {@link #VISIBLE} or {@link #INVISIBLE}.
7392 */
7393 public void dispatchDisplayHint(int hint) {
7394 onDisplayHint(hint);
7395 }
7396
7397 /**
7398 * Gives this view a hint about whether is displayed or not. For instance, when
7399 * a View moves out of the screen, it might receives a display hint indicating
7400 * the view is not displayed. Applications should not <em>rely</em> on this hint
7401 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007402 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007403 * @param hint A hint about whether or not this view is displayed:
7404 * {@link #VISIBLE} or {@link #INVISIBLE}.
7405 */
7406 protected void onDisplayHint(int hint) {
7407 }
7408
7409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 * Dispatch a window visibility change down the view hierarchy.
7411 * ViewGroups should override to route to their children.
7412 *
7413 * @param visibility The new visibility of the window.
7414 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007415 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 */
7417 public void dispatchWindowVisibilityChanged(int visibility) {
7418 onWindowVisibilityChanged(visibility);
7419 }
7420
7421 /**
7422 * Called when the window containing has change its visibility
7423 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7424 * that this tells you whether or not your window is being made visible
7425 * to the window manager; this does <em>not</em> tell you whether or not
7426 * your window is obscured by other windows on the screen, even if it
7427 * is itself visible.
7428 *
7429 * @param visibility The new visibility of the window.
7430 */
7431 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007432 if (visibility == VISIBLE) {
7433 initialAwakenScrollBars();
7434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007435 }
7436
7437 /**
7438 * Returns the current visibility of the window this view is attached to
7439 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7440 *
7441 * @return Returns the current visibility of the view's window.
7442 */
7443 public int getWindowVisibility() {
7444 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7445 }
7446
7447 /**
7448 * Retrieve the overall visible display size in which the window this view is
7449 * attached to has been positioned in. This takes into account screen
7450 * decorations above the window, for both cases where the window itself
7451 * is being position inside of them or the window is being placed under
7452 * then and covered insets are used for the window to position its content
7453 * inside. In effect, this tells you the available area where content can
7454 * be placed and remain visible to users.
7455 *
7456 * <p>This function requires an IPC back to the window manager to retrieve
7457 * the requested information, so should not be used in performance critical
7458 * code like drawing.
7459 *
7460 * @param outRect Filled in with the visible display frame. If the view
7461 * is not attached to a window, this is simply the raw display size.
7462 */
7463 public void getWindowVisibleDisplayFrame(Rect outRect) {
7464 if (mAttachInfo != null) {
7465 try {
7466 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7467 } catch (RemoteException e) {
7468 return;
7469 }
7470 // XXX This is really broken, and probably all needs to be done
7471 // in the window manager, and we need to know more about whether
7472 // we want the area behind or in front of the IME.
7473 final Rect insets = mAttachInfo.mVisibleInsets;
7474 outRect.left += insets.left;
7475 outRect.top += insets.top;
7476 outRect.right -= insets.right;
7477 outRect.bottom -= insets.bottom;
7478 return;
7479 }
7480 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007481 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007482 }
7483
7484 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007485 * Dispatch a notification about a resource configuration change down
7486 * the view hierarchy.
7487 * ViewGroups should override to route to their children.
7488 *
7489 * @param newConfig The new resource configuration.
7490 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007491 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007492 */
7493 public void dispatchConfigurationChanged(Configuration newConfig) {
7494 onConfigurationChanged(newConfig);
7495 }
7496
7497 /**
7498 * Called when the current configuration of the resources being used
7499 * by the application have changed. You can use this to decide when
7500 * to reload resources that can changed based on orientation and other
7501 * configuration characterstics. You only need to use this if you are
7502 * not relying on the normal {@link android.app.Activity} mechanism of
7503 * recreating the activity instance upon a configuration change.
7504 *
7505 * @param newConfig The new resource configuration.
7506 */
7507 protected void onConfigurationChanged(Configuration newConfig) {
7508 }
7509
7510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 * Private function to aggregate all per-view attributes in to the view
7512 * root.
7513 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007514 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7515 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 }
7517
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007518 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7519 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007520 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007521 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007522 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007523 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007524 ListenerInfo li = mListenerInfo;
7525 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007526 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007528 }
7529 }
7530
7531 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007532 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007534 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7535 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007536 ai.mRecomputeGlobalAttributes = true;
7537 }
7538 }
7539 }
7540
7541 /**
7542 * Returns whether the device is currently in touch mode. Touch mode is entered
7543 * once the user begins interacting with the device by touch, and affects various
7544 * things like whether focus is always visible to the user.
7545 *
7546 * @return Whether the device is in touch mode.
7547 */
7548 @ViewDebug.ExportedProperty
7549 public boolean isInTouchMode() {
7550 if (mAttachInfo != null) {
7551 return mAttachInfo.mInTouchMode;
7552 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007553 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007554 }
7555 }
7556
7557 /**
7558 * Returns the context the view is running in, through which it can
7559 * access the current theme, resources, etc.
7560 *
7561 * @return The view's Context.
7562 */
7563 @ViewDebug.CapturedViewProperty
7564 public final Context getContext() {
7565 return mContext;
7566 }
7567
7568 /**
7569 * Handle a key event before it is processed by any input method
7570 * associated with the view hierarchy. This can be used to intercept
7571 * key events in special situations before the IME consumes them; a
7572 * typical example would be handling the BACK key to update the application's
7573 * UI instead of allowing the IME to see it and close itself.
7574 *
7575 * @param keyCode The value in event.getKeyCode().
7576 * @param event Description of the key event.
7577 * @return If you handled the event, return true. If you want to allow the
7578 * event to be handled by the next receiver, return false.
7579 */
7580 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7581 return false;
7582 }
7583
7584 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007585 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7586 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7588 * is released, if the view is enabled and clickable.
7589 *
Jean Chalard405bc512012-05-29 19:12:34 +09007590 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7591 * although some may elect to do so in some situations. Do not rely on this to
7592 * catch software key presses.
7593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 * @param keyCode A key code that represents the button pressed, from
7595 * {@link android.view.KeyEvent}.
7596 * @param event The KeyEvent object that defines the button action.
7597 */
7598 public boolean onKeyDown(int keyCode, KeyEvent event) {
7599 boolean result = false;
7600
7601 switch (keyCode) {
7602 case KeyEvent.KEYCODE_DPAD_CENTER:
7603 case KeyEvent.KEYCODE_ENTER: {
7604 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7605 return true;
7606 }
7607 // Long clickable items don't necessarily have to be clickable
7608 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7609 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7610 (event.getRepeatCount() == 0)) {
7611 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007612 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007613 return true;
7614 }
7615 break;
7616 }
7617 }
7618 return result;
7619 }
7620
7621 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007622 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7623 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7624 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007625 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7626 * although some may elect to do so in some situations. Do not rely on this to
7627 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007628 */
7629 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7630 return false;
7631 }
7632
7633 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007634 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7635 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7637 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007638 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7639 * although some may elect to do so in some situations. Do not rely on this to
7640 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 *
7642 * @param keyCode A key code that represents the button pressed, from
7643 * {@link android.view.KeyEvent}.
7644 * @param event The KeyEvent object that defines the button action.
7645 */
7646 public boolean onKeyUp(int keyCode, KeyEvent event) {
7647 boolean result = false;
7648
7649 switch (keyCode) {
7650 case KeyEvent.KEYCODE_DPAD_CENTER:
7651 case KeyEvent.KEYCODE_ENTER: {
7652 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7653 return true;
7654 }
7655 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7656 setPressed(false);
7657
7658 if (!mHasPerformedLongPress) {
7659 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007660 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661
7662 result = performClick();
7663 }
7664 }
7665 break;
7666 }
7667 }
7668 return result;
7669 }
7670
7671 /**
7672 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7673 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7674 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007675 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7676 * although some may elect to do so in some situations. Do not rely on this to
7677 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 *
7679 * @param keyCode A key code that represents the button pressed, from
7680 * {@link android.view.KeyEvent}.
7681 * @param repeatCount The number of times the action was made.
7682 * @param event The KeyEvent object that defines the button action.
7683 */
7684 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7685 return false;
7686 }
7687
7688 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007689 * Called on the focused view when a key shortcut event is not handled.
7690 * Override this method to implement local key shortcuts for the View.
7691 * Key shortcuts can also be implemented by setting the
7692 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 *
7694 * @param keyCode The value in event.getKeyCode().
7695 * @param event Description of the key event.
7696 * @return If you handled the event, return true. If you want to allow the
7697 * event to be handled by the next receiver, return false.
7698 */
7699 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7700 return false;
7701 }
7702
7703 /**
7704 * Check whether the called view is a text editor, in which case it
7705 * would make sense to automatically display a soft input window for
7706 * it. Subclasses should override this if they implement
7707 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007708 * a call on that method would return a non-null InputConnection, and
7709 * they are really a first-class editor that the user would normally
7710 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007711 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007712 * <p>The default implementation always returns false. This does
7713 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7714 * will not be called or the user can not otherwise perform edits on your
7715 * view; it is just a hint to the system that this is not the primary
7716 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007717 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 * @return Returns true if this view is a text editor, else false.
7719 */
7720 public boolean onCheckIsTextEditor() {
7721 return false;
7722 }
Romain Guy8506ab42009-06-11 17:35:47 -07007723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 /**
7725 * Create a new InputConnection for an InputMethod to interact
7726 * with the view. The default implementation returns null, since it doesn't
7727 * support input methods. You can override this to implement such support.
7728 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007729 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 * <p>When implementing this, you probably also want to implement
7731 * {@link #onCheckIsTextEditor()} to indicate you will return a
7732 * non-null InputConnection.
7733 *
7734 * @param outAttrs Fill in with attribute information about the connection.
7735 */
7736 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7737 return null;
7738 }
7739
7740 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007741 * Called by the {@link android.view.inputmethod.InputMethodManager}
7742 * when a view who is not the current
7743 * input connection target is trying to make a call on the manager. The
7744 * default implementation returns false; you can override this to return
7745 * true for certain views if you are performing InputConnection proxying
7746 * to them.
7747 * @param view The View that is making the InputMethodManager call.
7748 * @return Return true to allow the call, false to reject.
7749 */
7750 public boolean checkInputConnectionProxy(View view) {
7751 return false;
7752 }
Romain Guy8506ab42009-06-11 17:35:47 -07007753
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 * Show the context menu for this view. It is not safe to hold on to the
7756 * menu after returning from this method.
7757 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007758 * You should normally not overload this method. Overload
7759 * {@link #onCreateContextMenu(ContextMenu)} or define an
7760 * {@link OnCreateContextMenuListener} to add items to the context menu.
7761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 * @param menu The context menu to populate
7763 */
7764 public void createContextMenu(ContextMenu menu) {
7765 ContextMenuInfo menuInfo = getContextMenuInfo();
7766
7767 // Sets the current menu info so all items added to menu will have
7768 // my extra info set.
7769 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7770
7771 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007772 ListenerInfo li = mListenerInfo;
7773 if (li != null && li.mOnCreateContextMenuListener != null) {
7774 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 }
7776
7777 // Clear the extra information so subsequent items that aren't mine don't
7778 // have my extra info.
7779 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7780
7781 if (mParent != null) {
7782 mParent.createContextMenu(menu);
7783 }
7784 }
7785
7786 /**
7787 * Views should implement this if they have extra information to associate
7788 * with the context menu. The return result is supplied as a parameter to
7789 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7790 * callback.
7791 *
7792 * @return Extra information about the item for which the context menu
7793 * should be shown. This information will vary across different
7794 * subclasses of View.
7795 */
7796 protected ContextMenuInfo getContextMenuInfo() {
7797 return null;
7798 }
7799
7800 /**
7801 * Views should implement this if the view itself is going to add items to
7802 * the context menu.
7803 *
7804 * @param menu the context menu to populate
7805 */
7806 protected void onCreateContextMenu(ContextMenu menu) {
7807 }
7808
7809 /**
7810 * Implement this method to handle trackball motion events. The
7811 * <em>relative</em> movement of the trackball since the last event
7812 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7813 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7814 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7815 * they will often be fractional values, representing the more fine-grained
7816 * movement information available from a trackball).
7817 *
7818 * @param event The motion event.
7819 * @return True if the event was handled, false otherwise.
7820 */
7821 public boolean onTrackballEvent(MotionEvent event) {
7822 return false;
7823 }
7824
7825 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007826 * Implement this method to handle generic motion events.
7827 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007828 * Generic motion events describe joystick movements, mouse hovers, track pad
7829 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007830 * {@link MotionEvent#getSource() source} of the motion event specifies
7831 * the class of input that was received. Implementations of this method
7832 * must examine the bits in the source before processing the event.
7833 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007834 * </p><p>
7835 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7836 * are delivered to the view under the pointer. All other generic motion events are
7837 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007838 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007839 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007840 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007841 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7842 * // process the joystick movement...
7843 * return true;
7844 * }
7845 * }
7846 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7847 * switch (event.getAction()) {
7848 * case MotionEvent.ACTION_HOVER_MOVE:
7849 * // process the mouse hover movement...
7850 * return true;
7851 * case MotionEvent.ACTION_SCROLL:
7852 * // process the scroll wheel movement...
7853 * return true;
7854 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007855 * }
7856 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007857 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007858 *
7859 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007860 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007861 */
7862 public boolean onGenericMotionEvent(MotionEvent event) {
7863 return false;
7864 }
7865
7866 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007867 * Implement this method to handle hover events.
7868 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007869 * This method is called whenever a pointer is hovering into, over, or out of the
7870 * bounds of a view and the view is not currently being touched.
7871 * Hover events are represented as pointer events with action
7872 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7873 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7874 * </p>
7875 * <ul>
7876 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7877 * when the pointer enters the bounds of the view.</li>
7878 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7879 * when the pointer has already entered the bounds of the view and has moved.</li>
7880 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7881 * when the pointer has exited the bounds of the view or when the pointer is
7882 * about to go down due to a button click, tap, or similar user action that
7883 * causes the view to be touched.</li>
7884 * </ul>
7885 * <p>
7886 * The view should implement this method to return true to indicate that it is
7887 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007888 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007889 * The default implementation calls {@link #setHovered} to update the hovered state
7890 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007891 * is enabled and is clickable. The default implementation also sends hover
7892 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007893 * </p>
7894 *
7895 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007896 * @return True if the view handled the hover event.
7897 *
7898 * @see #isHovered
7899 * @see #setHovered
7900 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007901 */
7902 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007903 // The root view may receive hover (or touch) events that are outside the bounds of
7904 // the window. This code ensures that we only send accessibility events for
7905 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007906 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007907 if (!mSendingHoverAccessibilityEvents) {
7908 if ((action == MotionEvent.ACTION_HOVER_ENTER
7909 || action == MotionEvent.ACTION_HOVER_MOVE)
7910 && !hasHoveredChild()
7911 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007912 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007913 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007914 }
7915 } else {
7916 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007917 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007918 && !pointInView(event.getX(), event.getY()))) {
7919 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007920 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007921 // If the window does not have input focus we take away accessibility
7922 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007923 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007924 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007925 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007926 }
Jeff Browna1b24182011-07-28 13:38:24 -07007927 }
7928
Jeff Brown87b7f802011-06-21 18:35:45 -07007929 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007930 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007931 case MotionEvent.ACTION_HOVER_ENTER:
7932 setHovered(true);
7933 break;
7934 case MotionEvent.ACTION_HOVER_EXIT:
7935 setHovered(false);
7936 break;
7937 }
Jeff Browna1b24182011-07-28 13:38:24 -07007938
7939 // Dispatch the event to onGenericMotionEvent before returning true.
7940 // This is to provide compatibility with existing applications that
7941 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7942 // break because of the new default handling for hoverable views
7943 // in onHoverEvent.
7944 // Note that onGenericMotionEvent will be called by default when
7945 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7946 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007947 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007948 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007949
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007950 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007951 }
7952
7953 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007954 * Returns true if the view should handle {@link #onHoverEvent}
7955 * by calling {@link #setHovered} to change its hovered state.
7956 *
7957 * @return True if the view is hoverable.
7958 */
7959 private boolean isHoverable() {
7960 final int viewFlags = mViewFlags;
7961 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7962 return false;
7963 }
7964
7965 return (viewFlags & CLICKABLE) == CLICKABLE
7966 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7967 }
7968
7969 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007970 * Returns true if the view is currently hovered.
7971 *
7972 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007973 *
7974 * @see #setHovered
7975 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007976 */
Jeff Brown10b62902011-06-20 16:40:37 -07007977 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007978 public boolean isHovered() {
7979 return (mPrivateFlags & HOVERED) != 0;
7980 }
7981
7982 /**
7983 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007984 * <p>
7985 * Calling this method also changes the drawable state of the view. This
7986 * enables the view to react to hover by using different drawable resources
7987 * to change its appearance.
7988 * </p><p>
7989 * The {@link #onHoverChanged} method is called when the hovered state changes.
7990 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007991 *
7992 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007993 *
7994 * @see #isHovered
7995 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007996 */
7997 public void setHovered(boolean hovered) {
7998 if (hovered) {
7999 if ((mPrivateFlags & HOVERED) == 0) {
8000 mPrivateFlags |= HOVERED;
8001 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008002 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008003 }
8004 } else {
8005 if ((mPrivateFlags & HOVERED) != 0) {
8006 mPrivateFlags &= ~HOVERED;
8007 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008008 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008009 }
8010 }
8011 }
8012
8013 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008014 * Implement this method to handle hover state changes.
8015 * <p>
8016 * This method is called whenever the hover state changes as a result of a
8017 * call to {@link #setHovered}.
8018 * </p>
8019 *
8020 * @param hovered The current hover state, as returned by {@link #isHovered}.
8021 *
8022 * @see #isHovered
8023 * @see #setHovered
8024 */
8025 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008026 }
8027
8028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 * Implement this method to handle touch screen motion events.
8030 *
8031 * @param event The motion event.
8032 * @return True if the event was handled, false otherwise.
8033 */
8034 public boolean onTouchEvent(MotionEvent event) {
8035 final int viewFlags = mViewFlags;
8036
8037 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008038 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008039 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 // A disabled view that is clickable still consumes the touch
8042 // events, it just doesn't respond to them.
8043 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8044 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8045 }
8046
8047 if (mTouchDelegate != null) {
8048 if (mTouchDelegate.onTouchEvent(event)) {
8049 return true;
8050 }
8051 }
8052
8053 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8054 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8055 switch (event.getAction()) {
8056 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08008057 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
8058 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008059 // take focus if we don't have it already and we should in
8060 // touch mode.
8061 boolean focusTaken = false;
8062 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8063 focusTaken = requestFocus();
8064 }
8065
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008066 if (prepressed) {
8067 // The button is being released before we actually
8068 // showed it as pressed. Make it show the pressed
8069 // state now (before scheduling the click) to ensure
8070 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008071 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008072 }
Joe Malin32736f02011-01-19 16:14:20 -08008073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008074 if (!mHasPerformedLongPress) {
8075 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008076 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008077
8078 // Only perform take click actions if we were in the pressed state
8079 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008080 // Use a Runnable and post this rather than calling
8081 // performClick directly. This lets other visual state
8082 // of the view update before click actions start.
8083 if (mPerformClick == null) {
8084 mPerformClick = new PerformClick();
8085 }
8086 if (!post(mPerformClick)) {
8087 performClick();
8088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
8090 }
8091
8092 if (mUnsetPressedState == null) {
8093 mUnsetPressedState = new UnsetPressedState();
8094 }
8095
Adam Powelle14579b2009-12-16 18:39:52 -08008096 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008097 postDelayed(mUnsetPressedState,
8098 ViewConfiguration.getPressedStateDuration());
8099 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 // If the post failed, unpress right now
8101 mUnsetPressedState.run();
8102 }
Adam Powelle14579b2009-12-16 18:39:52 -08008103 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 }
8105 break;
8106
8107 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008108 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008109
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008110 if (performButtonActionOnTouchDown(event)) {
8111 break;
8112 }
8113
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008114 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008115 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008116
8117 // For views inside a scrolling container, delay the pressed feedback for
8118 // a short period in case this is a scroll.
8119 if (isInScrollingContainer) {
8120 mPrivateFlags |= PREPRESSED;
8121 if (mPendingCheckForTap == null) {
8122 mPendingCheckForTap = new CheckForTap();
8123 }
8124 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8125 } else {
8126 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008127 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008128 checkForLongClick(0);
8129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 break;
8131
8132 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008133 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008134 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 break;
8136
8137 case MotionEvent.ACTION_MOVE:
8138 final int x = (int) event.getX();
8139 final int y = (int) event.getY();
8140
8141 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008142 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008144 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008146 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008147 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148
Adam Powell4d6f0662012-02-21 15:11:11 -08008149 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 }
8152 break;
8153 }
8154 return true;
8155 }
8156
8157 return false;
8158 }
8159
8160 /**
Adam Powell10298662011-08-14 18:26:30 -07008161 * @hide
8162 */
8163 public boolean isInScrollingContainer() {
8164 ViewParent p = getParent();
8165 while (p != null && p instanceof ViewGroup) {
8166 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8167 return true;
8168 }
8169 p = p.getParent();
8170 }
8171 return false;
8172 }
8173
8174 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008175 * Remove the longpress detection timer.
8176 */
8177 private void removeLongPressCallback() {
8178 if (mPendingCheckForLongPress != null) {
8179 removeCallbacks(mPendingCheckForLongPress);
8180 }
8181 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008182
8183 /**
8184 * Remove the pending click action
8185 */
8186 private void removePerformClickCallback() {
8187 if (mPerformClick != null) {
8188 removeCallbacks(mPerformClick);
8189 }
8190 }
8191
Adam Powelle14579b2009-12-16 18:39:52 -08008192 /**
Romain Guya440b002010-02-24 15:57:54 -08008193 * Remove the prepress detection timer.
8194 */
8195 private void removeUnsetPressCallback() {
8196 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
8197 setPressed(false);
8198 removeCallbacks(mUnsetPressedState);
8199 }
8200 }
8201
8202 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008203 * Remove the tap detection timer.
8204 */
8205 private void removeTapCallback() {
8206 if (mPendingCheckForTap != null) {
8207 mPrivateFlags &= ~PREPRESSED;
8208 removeCallbacks(mPendingCheckForTap);
8209 }
8210 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008211
8212 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 * Cancels a pending long press. Your subclass can use this if you
8214 * want the context menu to come up if the user presses and holds
8215 * at the same place, but you don't want it to come up if they press
8216 * and then move around enough to cause scrolling.
8217 */
8218 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008219 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008220
8221 /*
8222 * The prepressed state handled by the tap callback is a display
8223 * construct, but the tap callback will post a long press callback
8224 * less its own timeout. Remove it here.
8225 */
8226 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 }
8228
8229 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008230 * Remove the pending callback for sending a
8231 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8232 */
8233 private void removeSendViewScrolledAccessibilityEventCallback() {
8234 if (mSendViewScrolledAccessibilityEvent != null) {
8235 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008236 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008237 }
8238 }
8239
8240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008241 * Sets the TouchDelegate for this View.
8242 */
8243 public void setTouchDelegate(TouchDelegate delegate) {
8244 mTouchDelegate = delegate;
8245 }
8246
8247 /**
8248 * Gets the TouchDelegate for this View.
8249 */
8250 public TouchDelegate getTouchDelegate() {
8251 return mTouchDelegate;
8252 }
8253
8254 /**
8255 * Set flags controlling behavior of this view.
8256 *
8257 * @param flags Constant indicating the value which should be set
8258 * @param mask Constant indicating the bit range that should be changed
8259 */
8260 void setFlags(int flags, int mask) {
8261 int old = mViewFlags;
8262 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8263
8264 int changed = mViewFlags ^ old;
8265 if (changed == 0) {
8266 return;
8267 }
8268 int privateFlags = mPrivateFlags;
8269
8270 /* Check if the FOCUSABLE bit has changed */
8271 if (((changed & FOCUSABLE_MASK) != 0) &&
8272 ((privateFlags & HAS_BOUNDS) !=0)) {
8273 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
8274 && ((privateFlags & FOCUSED) != 0)) {
8275 /* Give up focus if we are no longer focusable */
8276 clearFocus();
8277 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
8278 && ((privateFlags & FOCUSED) == 0)) {
8279 /*
8280 * Tell the view system that we are now available to take focus
8281 * if no one else already has it.
8282 */
8283 if (mParent != null) mParent.focusableViewAvailable(this);
8284 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008285 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8286 notifyAccessibilityStateChanged();
8287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 }
8289
8290 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8291 if ((changed & VISIBILITY_MASK) != 0) {
8292 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008293 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008294 * it was not visible. Marking it drawn ensures that the invalidation will
8295 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 */
Chet Haaseaceafe62011-08-26 15:44:33 -07008297 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008298 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299
8300 needGlobalAttributesUpdate(true);
8301
8302 // a view becoming visible is worth notifying the parent
8303 // about in case nothing has focus. even if this specific view
8304 // isn't focusable, it may contain something that is, so let
8305 // the root view try to give this focus if nothing else does.
8306 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8307 mParent.focusableViewAvailable(this);
8308 }
8309 }
8310 }
8311
8312 /* Check if the GONE bit has changed */
8313 if ((changed & GONE) != 0) {
8314 needGlobalAttributesUpdate(false);
8315 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008316
Romain Guyecd80ee2009-12-03 17:13:02 -08008317 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8318 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008319 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008320 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008321 if (mParent instanceof View) {
8322 // GONE views noop invalidation, so invalidate the parent
8323 ((View) mParent).invalidate(true);
8324 }
8325 // Mark the view drawn to ensure that it gets invalidated properly the next
8326 // time it is visible and gets invalidated
8327 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 }
8329 if (mAttachInfo != null) {
8330 mAttachInfo.mViewVisibilityChanged = true;
8331 }
8332 }
8333
8334 /* Check if the VISIBLE bit has changed */
8335 if ((changed & INVISIBLE) != 0) {
8336 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008337 /*
8338 * If this view is becoming invisible, set the DRAWN flag so that
8339 * the next invalidate() will not be skipped.
8340 */
8341 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342
8343 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008344 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 if (getRootView() != this) {
8346 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008347 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 }
8349 }
8350 if (mAttachInfo != null) {
8351 mAttachInfo.mViewVisibilityChanged = true;
8352 }
8353 }
8354
Adam Powell326d8082009-12-09 15:10:07 -08008355 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008356 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008357 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8358 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008359 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008360 } else if (mParent != null) {
8361 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008362 }
Adam Powell326d8082009-12-09 15:10:07 -08008363 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8364 }
8365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8367 destroyDrawingCache();
8368 }
8369
8370 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8371 destroyDrawingCache();
8372 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008373 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008374 }
8375
8376 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8377 destroyDrawingCache();
8378 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8379 }
8380
8381 if ((changed & DRAW_MASK) != 0) {
8382 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008383 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 mPrivateFlags &= ~SKIP_DRAW;
8385 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8386 } else {
8387 mPrivateFlags |= SKIP_DRAW;
8388 }
8389 } else {
8390 mPrivateFlags &= ~SKIP_DRAW;
8391 }
8392 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008393 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 }
8395
8396 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008397 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 mParent.recomputeViewAttributes(this);
8399 }
8400 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008401
8402 if (AccessibilityManager.getInstance(mContext).isEnabled()
8403 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8404 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8405 notifyAccessibilityStateChanged();
8406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 }
8408
8409 /**
8410 * Change the view's z order in the tree, so it's on top of other sibling
8411 * views
8412 */
8413 public void bringToFront() {
8414 if (mParent != null) {
8415 mParent.bringChildToFront(this);
8416 }
8417 }
8418
8419 /**
8420 * This is called in response to an internal scroll in this view (i.e., the
8421 * view scrolled its own contents). This is typically as a result of
8422 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8423 * called.
8424 *
8425 * @param l Current horizontal scroll origin.
8426 * @param t Current vertical scroll origin.
8427 * @param oldl Previous horizontal scroll origin.
8428 * @param oldt Previous vertical scroll origin.
8429 */
8430 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008431 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8432 postSendViewScrolledAccessibilityEventCallback();
8433 }
8434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 mBackgroundSizeChanged = true;
8436
8437 final AttachInfo ai = mAttachInfo;
8438 if (ai != null) {
8439 ai.mViewScrollChanged = true;
8440 }
8441 }
8442
8443 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008444 * Interface definition for a callback to be invoked when the layout bounds of a view
8445 * changes due to layout processing.
8446 */
8447 public interface OnLayoutChangeListener {
8448 /**
8449 * Called when the focus state of a view has changed.
8450 *
8451 * @param v The view whose state has changed.
8452 * @param left The new value of the view's left property.
8453 * @param top The new value of the view's top property.
8454 * @param right The new value of the view's right property.
8455 * @param bottom The new value of the view's bottom property.
8456 * @param oldLeft The previous value of the view's left property.
8457 * @param oldTop The previous value of the view's top property.
8458 * @param oldRight The previous value of the view's right property.
8459 * @param oldBottom The previous value of the view's bottom property.
8460 */
8461 void onLayoutChange(View v, int left, int top, int right, int bottom,
8462 int oldLeft, int oldTop, int oldRight, int oldBottom);
8463 }
8464
8465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 * This is called during layout when the size of this view has changed. If
8467 * you were just added to the view hierarchy, you're called with the old
8468 * values of 0.
8469 *
8470 * @param w Current width of this view.
8471 * @param h Current height of this view.
8472 * @param oldw Old width of this view.
8473 * @param oldh Old height of this view.
8474 */
8475 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8476 }
8477
8478 /**
8479 * Called by draw to draw the child views. This may be overridden
8480 * by derived classes to gain control just before its children are drawn
8481 * (but after its own view has been drawn).
8482 * @param canvas the canvas on which to draw the view
8483 */
8484 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 }
8487
8488 /**
8489 * Gets the parent of this view. Note that the parent is a
8490 * ViewParent and not necessarily a View.
8491 *
8492 * @return Parent of this view.
8493 */
8494 public final ViewParent getParent() {
8495 return mParent;
8496 }
8497
8498 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008499 * Set the horizontal scrolled position of your view. This will cause a call to
8500 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8501 * invalidated.
8502 * @param value the x position to scroll to
8503 */
8504 public void setScrollX(int value) {
8505 scrollTo(value, mScrollY);
8506 }
8507
8508 /**
8509 * Set the vertical scrolled position of your view. This will cause a call to
8510 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8511 * invalidated.
8512 * @param value the y position to scroll to
8513 */
8514 public void setScrollY(int value) {
8515 scrollTo(mScrollX, value);
8516 }
8517
8518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 * Return the scrolled left position of this view. This is the left edge of
8520 * the displayed part of your view. You do not need to draw any pixels
8521 * farther left, since those are outside of the frame of your view on
8522 * screen.
8523 *
8524 * @return The left edge of the displayed part of your view, in pixels.
8525 */
8526 public final int getScrollX() {
8527 return mScrollX;
8528 }
8529
8530 /**
8531 * Return the scrolled top position of this view. This is the top edge of
8532 * the displayed part of your view. You do not need to draw any pixels above
8533 * it, since those are outside of the frame of your view on screen.
8534 *
8535 * @return The top edge of the displayed part of your view, in pixels.
8536 */
8537 public final int getScrollY() {
8538 return mScrollY;
8539 }
8540
8541 /**
8542 * Return the width of the your view.
8543 *
8544 * @return The width of your view, in pixels.
8545 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008546 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008547 public final int getWidth() {
8548 return mRight - mLeft;
8549 }
8550
8551 /**
8552 * Return the height of your view.
8553 *
8554 * @return The height of your view, in pixels.
8555 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008556 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 public final int getHeight() {
8558 return mBottom - mTop;
8559 }
8560
8561 /**
8562 * Return the visible drawing bounds of your view. Fills in the output
8563 * rectangle with the values from getScrollX(), getScrollY(),
8564 * getWidth(), and getHeight().
8565 *
8566 * @param outRect The (scrolled) drawing bounds of the view.
8567 */
8568 public void getDrawingRect(Rect outRect) {
8569 outRect.left = mScrollX;
8570 outRect.top = mScrollY;
8571 outRect.right = mScrollX + (mRight - mLeft);
8572 outRect.bottom = mScrollY + (mBottom - mTop);
8573 }
8574
8575 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008576 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8577 * raw width component (that is the result is masked by
8578 * {@link #MEASURED_SIZE_MASK}).
8579 *
8580 * @return The raw measured width of this view.
8581 */
8582 public final int getMeasuredWidth() {
8583 return mMeasuredWidth & MEASURED_SIZE_MASK;
8584 }
8585
8586 /**
8587 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008588 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008589 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008590 * This should be used during measurement and layout calculations only. Use
8591 * {@link #getWidth()} to see how wide a view is after layout.
8592 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008593 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008595 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008596 return mMeasuredWidth;
8597 }
8598
8599 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008600 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8601 * raw width component (that is the result is masked by
8602 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008604 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008605 */
8606 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008607 return mMeasuredHeight & MEASURED_SIZE_MASK;
8608 }
8609
8610 /**
8611 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008612 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008613 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8614 * This should be used during measurement and layout calculations only. Use
8615 * {@link #getHeight()} to see how wide a view is after layout.
8616 *
8617 * @return The measured width of this view as a bit mask.
8618 */
8619 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 return mMeasuredHeight;
8621 }
8622
8623 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008624 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8625 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8626 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8627 * and the height component is at the shifted bits
8628 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8629 */
8630 public final int getMeasuredState() {
8631 return (mMeasuredWidth&MEASURED_STATE_MASK)
8632 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8633 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8634 }
8635
8636 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008637 * The transform matrix of this view, which is calculated based on the current
8638 * roation, scale, and pivot properties.
8639 *
8640 * @see #getRotation()
8641 * @see #getScaleX()
8642 * @see #getScaleY()
8643 * @see #getPivotX()
8644 * @see #getPivotY()
8645 * @return The current transform matrix for the view
8646 */
8647 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008648 if (mTransformationInfo != null) {
8649 updateMatrix();
8650 return mTransformationInfo.mMatrix;
8651 }
8652 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008653 }
8654
8655 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008656 * Utility function to determine if the value is far enough away from zero to be
8657 * considered non-zero.
8658 * @param value A floating point value to check for zero-ness
8659 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8660 */
8661 private static boolean nonzero(float value) {
8662 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8663 }
8664
8665 /**
Jeff Brown86671742010-09-30 20:00:15 -07008666 * Returns true if the transform matrix is the identity matrix.
8667 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008668 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008669 * @return True if the transform matrix is the identity matrix, false otherwise.
8670 */
Jeff Brown86671742010-09-30 20:00:15 -07008671 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 if (mTransformationInfo != null) {
8673 updateMatrix();
8674 return mTransformationInfo.mMatrixIsIdentity;
8675 }
8676 return true;
8677 }
8678
8679 void ensureTransformationInfo() {
8680 if (mTransformationInfo == null) {
8681 mTransformationInfo = new TransformationInfo();
8682 }
Jeff Brown86671742010-09-30 20:00:15 -07008683 }
8684
8685 /**
8686 * Recomputes the transform matrix if necessary.
8687 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008688 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008689 final TransformationInfo info = mTransformationInfo;
8690 if (info == null) {
8691 return;
8692 }
8693 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008694 // transform-related properties have changed since the last time someone
8695 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008696
8697 // Figure out if we need to update the pivot point
8698 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008699 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8700 info.mPrevWidth = mRight - mLeft;
8701 info.mPrevHeight = mBottom - mTop;
8702 info.mPivotX = info.mPrevWidth / 2f;
8703 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008704 }
8705 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008706 info.mMatrix.reset();
8707 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8708 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8709 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8710 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008711 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008712 if (info.mCamera == null) {
8713 info.mCamera = new Camera();
8714 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008715 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008716 info.mCamera.save();
8717 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8718 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8719 info.mCamera.getMatrix(info.matrix3D);
8720 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8721 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8722 info.mPivotY + info.mTranslationY);
8723 info.mMatrix.postConcat(info.matrix3D);
8724 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008725 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008726 info.mMatrixDirty = false;
8727 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8728 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008729 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008730 }
8731
8732 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008733 * When searching for a view to focus this rectangle is used when considering if this view is
8734 * a good candidate for receiving focus.
8735 *
8736 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8737 *
8738 * @param r The rectangle to fill in, in this view's coordinates.
8739 */
8740 public void getFocusRect(Rect r) {
8741 getDrawingRect(r);
8742 }
8743
8744 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008745 * Utility method to retrieve the inverse of the current mMatrix property.
8746 * We cache the matrix to avoid recalculating it when transform properties
8747 * have not changed.
8748 *
8749 * @return The inverse of the current matrix of this view.
8750 */
Jeff Brown86671742010-09-30 20:00:15 -07008751 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 final TransformationInfo info = mTransformationInfo;
8753 if (info != null) {
8754 updateMatrix();
8755 if (info.mInverseMatrixDirty) {
8756 if (info.mInverseMatrix == null) {
8757 info.mInverseMatrix = new Matrix();
8758 }
8759 info.mMatrix.invert(info.mInverseMatrix);
8760 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008761 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008762 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008763 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008764 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 }
8766
8767 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008768 * Gets the distance along the Z axis from the camera to this view.
8769 *
8770 * @see #setCameraDistance(float)
8771 *
8772 * @return The distance along the Z axis.
8773 */
8774 public float getCameraDistance() {
8775 ensureTransformationInfo();
8776 final float dpi = mResources.getDisplayMetrics().densityDpi;
8777 final TransformationInfo info = mTransformationInfo;
8778 if (info.mCamera == null) {
8779 info.mCamera = new Camera();
8780 info.matrix3D = new Matrix();
8781 }
8782 return -(info.mCamera.getLocationZ() * dpi);
8783 }
8784
8785 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008786 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8787 * views are drawn) from the camera to this view. The camera's distance
8788 * affects 3D transformations, for instance rotations around the X and Y
8789 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008790 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008791 * use a camera distance that's greater than the height (X axis rotation) or
8792 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008793 *
Romain Guya5364ee2011-02-24 14:46:04 -08008794 * <p>The distance of the camera from the view plane can have an affect on the
8795 * perspective distortion of the view when it is rotated around the x or y axis.
8796 * For example, a large distance will result in a large viewing angle, and there
8797 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008798 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008799 * also result in some drawing artifacts if the rotated view ends up partially
8800 * behind the camera (which is why the recommendation is to use a distance at
8801 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008802 *
Romain Guya5364ee2011-02-24 14:46:04 -08008803 * <p>The distance is expressed in "depth pixels." The default distance depends
8804 * on the screen density. For instance, on a medium density display, the
8805 * default distance is 1280. On a high density display, the default distance
8806 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008807 *
Romain Guya5364ee2011-02-24 14:46:04 -08008808 * <p>If you want to specify a distance that leads to visually consistent
8809 * results across various densities, use the following formula:</p>
8810 * <pre>
8811 * float scale = context.getResources().getDisplayMetrics().density;
8812 * view.setCameraDistance(distance * scale);
8813 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008814 *
Romain Guya5364ee2011-02-24 14:46:04 -08008815 * <p>The density scale factor of a high density display is 1.5,
8816 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008817 *
Romain Guya5364ee2011-02-24 14:46:04 -08008818 * @param distance The distance in "depth pixels", if negative the opposite
8819 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008820 *
8821 * @see #setRotationX(float)
8822 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008823 */
8824 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008825 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008826
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008827 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008828 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008829 final TransformationInfo info = mTransformationInfo;
8830 if (info.mCamera == null) {
8831 info.mCamera = new Camera();
8832 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008833 }
8834
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008835 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8836 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008837
Chet Haase9d1992d2012-03-13 11:03:25 -07008838 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008839 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008840 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008841 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008842 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8843 // View was rejected last time it was drawn by its parent; this may have changed
8844 invalidateParentIfNeeded();
8845 }
Romain Guya5364ee2011-02-24 14:46:04 -08008846 }
8847
8848 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008849 * The degrees that the view is rotated around the pivot point.
8850 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008851 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008852 * @see #getPivotX()
8853 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008854 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008855 * @return The degrees of rotation.
8856 */
Chet Haasea5531132012-02-02 13:41:44 -08008857 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008858 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008859 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008860 }
8861
8862 /**
Chet Haase897247b2010-09-09 14:54:47 -07008863 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8864 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008865 *
8866 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008867 *
8868 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008869 * @see #getPivotX()
8870 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008871 * @see #setRotationX(float)
8872 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008873 *
8874 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 */
8876 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008877 ensureTransformationInfo();
8878 final TransformationInfo info = mTransformationInfo;
8879 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008880 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008881 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008882 info.mRotation = rotation;
8883 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008884 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008885 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008886 mDisplayList.setRotation(rotation);
8887 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008888 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8889 // View was rejected last time it was drawn by its parent; this may have changed
8890 invalidateParentIfNeeded();
8891 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008892 }
8893 }
8894
8895 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008896 * The degrees that the view is rotated around the vertical axis through the pivot point.
8897 *
8898 * @see #getPivotX()
8899 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008900 * @see #setRotationY(float)
8901 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008902 * @return The degrees of Y rotation.
8903 */
Chet Haasea5531132012-02-02 13:41:44 -08008904 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008905 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008906 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008907 }
8908
8909 /**
Chet Haase897247b2010-09-09 14:54:47 -07008910 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8911 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8912 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008913 *
Romain Guya5364ee2011-02-24 14:46:04 -08008914 * When rotating large views, it is recommended to adjust the camera distance
8915 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008916 *
8917 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008918 *
8919 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008920 * @see #getPivotX()
8921 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008922 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008923 * @see #setRotationX(float)
8924 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008925 *
8926 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008927 */
8928 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008929 ensureTransformationInfo();
8930 final TransformationInfo info = mTransformationInfo;
8931 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008932 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008933 info.mRotationY = rotationY;
8934 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008935 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008936 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008937 mDisplayList.setRotationY(rotationY);
8938 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008939 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8940 // View was rejected last time it was drawn by its parent; this may have changed
8941 invalidateParentIfNeeded();
8942 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008943 }
8944 }
8945
8946 /**
8947 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8948 *
8949 * @see #getPivotX()
8950 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008951 * @see #setRotationX(float)
8952 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008953 * @return The degrees of X rotation.
8954 */
Chet Haasea5531132012-02-02 13:41:44 -08008955 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008956 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008957 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008958 }
8959
8960 /**
Chet Haase897247b2010-09-09 14:54:47 -07008961 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8962 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8963 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008964 *
Romain Guya5364ee2011-02-24 14:46:04 -08008965 * When rotating large views, it is recommended to adjust the camera distance
8966 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008967 *
8968 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008969 *
8970 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008971 * @see #getPivotX()
8972 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008973 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008974 * @see #setRotationY(float)
8975 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008976 *
8977 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008978 */
8979 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 ensureTransformationInfo();
8981 final TransformationInfo info = mTransformationInfo;
8982 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008983 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008984 info.mRotationX = rotationX;
8985 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008986 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008987 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008988 mDisplayList.setRotationX(rotationX);
8989 }
Chet Haase1a3ab172012-05-11 08:41:20 -07008990 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
8991 // View was rejected last time it was drawn by its parent; this may have changed
8992 invalidateParentIfNeeded();
8993 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008994 }
8995 }
8996
8997 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008998 * The amount that the view is scaled in x around the pivot point, as a proportion of
8999 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9000 *
Joe Onorato93162322010-09-16 15:42:01 -04009001 * <p>By default, this is 1.0f.
9002 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009003 * @see #getPivotX()
9004 * @see #getPivotY()
9005 * @return The scaling factor.
9006 */
Chet Haasea5531132012-02-02 13:41:44 -08009007 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009009 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009010 }
9011
9012 /**
9013 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9014 * the view's unscaled width. A value of 1 means that no scaling is applied.
9015 *
9016 * @param scaleX The scaling factor.
9017 * @see #getPivotX()
9018 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009019 *
9020 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009021 */
9022 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 ensureTransformationInfo();
9024 final TransformationInfo info = mTransformationInfo;
9025 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009026 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009027 info.mScaleX = scaleX;
9028 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009029 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009030 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009031 mDisplayList.setScaleX(scaleX);
9032 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009033 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9034 // View was rejected last time it was drawn by its parent; this may have changed
9035 invalidateParentIfNeeded();
9036 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009037 }
9038 }
9039
9040 /**
9041 * The amount that the view is scaled in y around the pivot point, as a proportion of
9042 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9043 *
Joe Onorato93162322010-09-16 15:42:01 -04009044 * <p>By default, this is 1.0f.
9045 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009046 * @see #getPivotX()
9047 * @see #getPivotY()
9048 * @return The scaling factor.
9049 */
Chet Haasea5531132012-02-02 13:41:44 -08009050 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009051 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009052 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009053 }
9054
9055 /**
9056 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9057 * the view's unscaled width. A value of 1 means that no scaling is applied.
9058 *
9059 * @param scaleY The scaling factor.
9060 * @see #getPivotX()
9061 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009062 *
9063 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009064 */
9065 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009066 ensureTransformationInfo();
9067 final TransformationInfo info = mTransformationInfo;
9068 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009069 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009070 info.mScaleY = scaleY;
9071 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009072 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009073 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009074 mDisplayList.setScaleY(scaleY);
9075 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009076 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9077 // View was rejected last time it was drawn by its parent; this may have changed
9078 invalidateParentIfNeeded();
9079 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009080 }
9081 }
9082
9083 /**
9084 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9085 * and {@link #setScaleX(float) scaled}.
9086 *
9087 * @see #getRotation()
9088 * @see #getScaleX()
9089 * @see #getScaleY()
9090 * @see #getPivotY()
9091 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009092 *
9093 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009094 */
Chet Haasea5531132012-02-02 13:41:44 -08009095 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009096 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009097 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009098 }
9099
9100 /**
9101 * Sets the x location of the point around which the view is
9102 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009103 * By default, the pivot point is centered on the object.
9104 * Setting this property disables this behavior and causes the view to use only the
9105 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009106 *
9107 * @param pivotX The x location of the pivot point.
9108 * @see #getRotation()
9109 * @see #getScaleX()
9110 * @see #getScaleY()
9111 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009112 *
9113 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009114 */
9115 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009116 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009117 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009118 final TransformationInfo info = mTransformationInfo;
9119 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009120 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009121 info.mPivotX = pivotX;
9122 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009123 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009124 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009125 mDisplayList.setPivotX(pivotX);
9126 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009127 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9128 // View was rejected last time it was drawn by its parent; this may have changed
9129 invalidateParentIfNeeded();
9130 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009131 }
9132 }
9133
9134 /**
9135 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9136 * and {@link #setScaleY(float) scaled}.
9137 *
9138 * @see #getRotation()
9139 * @see #getScaleX()
9140 * @see #getScaleY()
9141 * @see #getPivotY()
9142 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009143 *
9144 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009145 */
Chet Haasea5531132012-02-02 13:41:44 -08009146 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009147 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009148 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009149 }
9150
9151 /**
9152 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009153 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9154 * Setting this property disables this behavior and causes the view to use only the
9155 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009156 *
9157 * @param pivotY The y location of the pivot point.
9158 * @see #getRotation()
9159 * @see #getScaleX()
9160 * @see #getScaleY()
9161 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009162 *
9163 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009164 */
9165 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009166 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07009167 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009168 final TransformationInfo info = mTransformationInfo;
9169 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009170 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009171 info.mPivotY = pivotY;
9172 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009173 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009174 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009175 mDisplayList.setPivotY(pivotY);
9176 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009177 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9178 // View was rejected last time it was drawn by its parent; this may have changed
9179 invalidateParentIfNeeded();
9180 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009181 }
9182 }
9183
9184 /**
9185 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9186 * completely transparent and 1 means the view is completely opaque.
9187 *
Joe Onorato93162322010-09-16 15:42:01 -04009188 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009189 * @return The opacity of the view.
9190 */
Chet Haasea5531132012-02-02 13:41:44 -08009191 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009192 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009193 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009194 }
9195
9196 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009197 * Returns whether this View has content which overlaps. This function, intended to be
9198 * overridden by specific View types, is an optimization when alpha is set on a view. If
9199 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9200 * and then composited it into place, which can be expensive. If the view has no overlapping
9201 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9202 * An example of overlapping rendering is a TextView with a background image, such as a
9203 * Button. An example of non-overlapping rendering is a TextView with no background, or
9204 * an ImageView with only the foreground image. The default implementation returns true;
9205 * subclasses should override if they have cases which can be optimized.
9206 *
9207 * @return true if the content in this view might overlap, false otherwise.
9208 */
9209 public boolean hasOverlappingRendering() {
9210 return true;
9211 }
9212
9213 /**
Romain Guy171c5922011-01-06 10:04:23 -08009214 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9215 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009216 *
Romain Guy171c5922011-01-06 10:04:23 -08009217 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9218 * responsible for applying the opacity itself. Otherwise, calling this method is
9219 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009220 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009221 *
Chet Haasea5531132012-02-02 13:41:44 -08009222 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9223 * performance implications. It is generally best to use the alpha property sparingly and
9224 * transiently, as in the case of fading animations.</p>
9225 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009226 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009227 *
Joe Malin32736f02011-01-19 16:14:20 -08009228 * @see #setLayerType(int, android.graphics.Paint)
9229 *
Chet Haase73066682010-11-29 15:55:32 -08009230 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009231 */
9232 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009233 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009234 if (mTransformationInfo.mAlpha != alpha) {
9235 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009236 if (onSetAlpha((int) (alpha * 255))) {
9237 mPrivateFlags |= ALPHA_SET;
9238 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009239 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009240 invalidate(true);
9241 } else {
9242 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009243 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009244 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009245 mDisplayList.setAlpha(alpha);
9246 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009247 }
Chet Haaseed032702010-10-01 14:05:54 -07009248 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009249 }
9250
9251 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009252 * Faster version of setAlpha() which performs the same steps except there are
9253 * no calls to invalidate(). The caller of this function should perform proper invalidation
9254 * on the parent and this object. The return value indicates whether the subclass handles
9255 * alpha (the return value for onSetAlpha()).
9256 *
9257 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009258 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9259 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009260 */
9261 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009262 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009263 if (mTransformationInfo.mAlpha != alpha) {
9264 mTransformationInfo.mAlpha = alpha;
9265 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9266 if (subclassHandlesAlpha) {
9267 mPrivateFlags |= ALPHA_SET;
9268 return true;
9269 } else {
9270 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009271 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009272 mDisplayList.setAlpha(alpha);
9273 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009274 }
Chet Haasea00f3862011-02-22 06:34:40 -08009275 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009276 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009277 }
9278
9279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 * Top position of this view relative to its parent.
9281 *
9282 * @return The top of this view, in pixels.
9283 */
9284 @ViewDebug.CapturedViewProperty
9285 public final int getTop() {
9286 return mTop;
9287 }
9288
9289 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009290 * Sets the top position of this view relative to its parent. This method is meant to be called
9291 * by the layout system and should not generally be called otherwise, because the property
9292 * may be changed at any time by the layout.
9293 *
9294 * @param top The top of this view, in pixels.
9295 */
9296 public final void setTop(int top) {
9297 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009298 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009299 final boolean matrixIsIdentity = mTransformationInfo == null
9300 || mTransformationInfo.mMatrixIsIdentity;
9301 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009302 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009303 int minTop;
9304 int yLoc;
9305 if (top < mTop) {
9306 minTop = top;
9307 yLoc = top - mTop;
9308 } else {
9309 minTop = mTop;
9310 yLoc = 0;
9311 }
Chet Haasee9140a72011-02-16 16:23:29 -08009312 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009313 }
9314 } else {
9315 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009316 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009317 }
9318
Chet Haaseed032702010-10-01 14:05:54 -07009319 int width = mRight - mLeft;
9320 int oldHeight = mBottom - mTop;
9321
Chet Haase21cd1382010-09-01 17:42:29 -07009322 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009323 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009324 mDisplayList.setTop(mTop);
9325 }
Chet Haase21cd1382010-09-01 17:42:29 -07009326
Chet Haaseed032702010-10-01 14:05:54 -07009327 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9328
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009329 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009330 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9331 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009332 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009333 }
Chet Haase21cd1382010-09-01 17:42:29 -07009334 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009335 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009336 }
Chet Haase55dbb652010-12-21 20:15:08 -08009337 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009338 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009339 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9340 // View was rejected last time it was drawn by its parent; this may have changed
9341 invalidateParentIfNeeded();
9342 }
Chet Haase21cd1382010-09-01 17:42:29 -07009343 }
9344 }
9345
9346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 * Bottom position of this view relative to its parent.
9348 *
9349 * @return The bottom of this view, in pixels.
9350 */
9351 @ViewDebug.CapturedViewProperty
9352 public final int getBottom() {
9353 return mBottom;
9354 }
9355
9356 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009357 * True if this view has changed since the last time being drawn.
9358 *
9359 * @return The dirty state of this view.
9360 */
9361 public boolean isDirty() {
9362 return (mPrivateFlags & DIRTY_MASK) != 0;
9363 }
9364
9365 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009366 * Sets the bottom position of this view relative to its parent. This method is meant to be
9367 * called by the layout system and should not generally be called otherwise, because the
9368 * property may be changed at any time by the layout.
9369 *
9370 * @param bottom The bottom of this view, in pixels.
9371 */
9372 public final void setBottom(int bottom) {
9373 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009374 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009375 final boolean matrixIsIdentity = mTransformationInfo == null
9376 || mTransformationInfo.mMatrixIsIdentity;
9377 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009378 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009379 int maxBottom;
9380 if (bottom < mBottom) {
9381 maxBottom = mBottom;
9382 } else {
9383 maxBottom = bottom;
9384 }
Chet Haasee9140a72011-02-16 16:23:29 -08009385 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009386 }
9387 } else {
9388 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009389 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009390 }
9391
Chet Haaseed032702010-10-01 14:05:54 -07009392 int width = mRight - mLeft;
9393 int oldHeight = mBottom - mTop;
9394
Chet Haase21cd1382010-09-01 17:42:29 -07009395 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009396 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009397 mDisplayList.setBottom(mBottom);
9398 }
Chet Haase21cd1382010-09-01 17:42:29 -07009399
Chet Haaseed032702010-10-01 14:05:54 -07009400 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9401
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009402 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009403 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9404 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009405 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009406 }
Chet Haase21cd1382010-09-01 17:42:29 -07009407 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009408 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009409 }
Chet Haase55dbb652010-12-21 20:15:08 -08009410 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009411 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009412 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9413 // View was rejected last time it was drawn by its parent; this may have changed
9414 invalidateParentIfNeeded();
9415 }
Chet Haase21cd1382010-09-01 17:42:29 -07009416 }
9417 }
9418
9419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009420 * Left position of this view relative to its parent.
9421 *
9422 * @return The left edge of this view, in pixels.
9423 */
9424 @ViewDebug.CapturedViewProperty
9425 public final int getLeft() {
9426 return mLeft;
9427 }
9428
9429 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009430 * Sets the left position of this view relative to its parent. This method is meant to be called
9431 * by the layout system and should not generally be called otherwise, because the property
9432 * may be changed at any time by the layout.
9433 *
9434 * @param left The bottom of this view, in pixels.
9435 */
9436 public final void setLeft(int left) {
9437 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009438 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009439 final boolean matrixIsIdentity = mTransformationInfo == null
9440 || mTransformationInfo.mMatrixIsIdentity;
9441 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009442 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009443 int minLeft;
9444 int xLoc;
9445 if (left < mLeft) {
9446 minLeft = left;
9447 xLoc = left - mLeft;
9448 } else {
9449 minLeft = mLeft;
9450 xLoc = 0;
9451 }
Chet Haasee9140a72011-02-16 16:23:29 -08009452 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009453 }
9454 } else {
9455 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009456 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009457 }
9458
Chet Haaseed032702010-10-01 14:05:54 -07009459 int oldWidth = mRight - mLeft;
9460 int height = mBottom - mTop;
9461
Chet Haase21cd1382010-09-01 17:42:29 -07009462 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009463 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009464 mDisplayList.setLeft(left);
9465 }
Chet Haase21cd1382010-09-01 17:42:29 -07009466
Chet Haaseed032702010-10-01 14:05:54 -07009467 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9468
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009469 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009470 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9471 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009472 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009473 }
Chet Haase21cd1382010-09-01 17:42:29 -07009474 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009475 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009476 }
Chet Haase55dbb652010-12-21 20:15:08 -08009477 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009478 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009479 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9480 // View was rejected last time it was drawn by its parent; this may have changed
9481 invalidateParentIfNeeded();
9482 }
Chet Haase21cd1382010-09-01 17:42:29 -07009483 }
9484 }
9485
9486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 * Right position of this view relative to its parent.
9488 *
9489 * @return The right edge of this view, in pixels.
9490 */
9491 @ViewDebug.CapturedViewProperty
9492 public final int getRight() {
9493 return mRight;
9494 }
9495
9496 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009497 * Sets the right position of this view relative to its parent. This method is meant to be called
9498 * by the layout system and should not generally be called otherwise, because the property
9499 * may be changed at any time by the layout.
9500 *
9501 * @param right The bottom of this view, in pixels.
9502 */
9503 public final void setRight(int right) {
9504 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009505 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009506 final boolean matrixIsIdentity = mTransformationInfo == null
9507 || mTransformationInfo.mMatrixIsIdentity;
9508 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009509 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009510 int maxRight;
9511 if (right < mRight) {
9512 maxRight = mRight;
9513 } else {
9514 maxRight = right;
9515 }
Chet Haasee9140a72011-02-16 16:23:29 -08009516 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009517 }
9518 } else {
9519 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009520 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009521 }
9522
Chet Haaseed032702010-10-01 14:05:54 -07009523 int oldWidth = mRight - mLeft;
9524 int height = mBottom - mTop;
9525
Chet Haase21cd1382010-09-01 17:42:29 -07009526 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009527 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009528 mDisplayList.setRight(mRight);
9529 }
Chet Haase21cd1382010-09-01 17:42:29 -07009530
Chet Haaseed032702010-10-01 14:05:54 -07009531 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9532
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009533 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009534 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9535 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009536 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009537 }
Chet Haase21cd1382010-09-01 17:42:29 -07009538 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009539 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009540 }
Chet Haase55dbb652010-12-21 20:15:08 -08009541 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009542 invalidateParentIfNeeded();
Chet Haase1a3ab172012-05-11 08:41:20 -07009543 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9544 // View was rejected last time it was drawn by its parent; this may have changed
9545 invalidateParentIfNeeded();
9546 }
Chet Haase21cd1382010-09-01 17:42:29 -07009547 }
9548 }
9549
9550 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009551 * The visual x position of this view, in pixels. This is equivalent to the
9552 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009553 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009554 *
Chet Haasedf030d22010-07-30 17:22:38 -07009555 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009556 */
Chet Haasea5531132012-02-02 13:41:44 -08009557 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009558 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009559 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009560 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009561
Chet Haasedf030d22010-07-30 17:22:38 -07009562 /**
9563 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9564 * {@link #setTranslationX(float) translationX} property to be the difference between
9565 * the x value passed in and the current {@link #getLeft() left} property.
9566 *
9567 * @param x The visual x position of this view, in pixels.
9568 */
9569 public void setX(float x) {
9570 setTranslationX(x - mLeft);
9571 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009572
Chet Haasedf030d22010-07-30 17:22:38 -07009573 /**
9574 * The visual y position of this view, in pixels. This is equivalent to the
9575 * {@link #setTranslationY(float) translationY} property plus the current
9576 * {@link #getTop() top} property.
9577 *
9578 * @return The visual y position of this view, in pixels.
9579 */
Chet Haasea5531132012-02-02 13:41:44 -08009580 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009581 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009582 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009583 }
9584
9585 /**
9586 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9587 * {@link #setTranslationY(float) translationY} property to be the difference between
9588 * the y value passed in and the current {@link #getTop() top} property.
9589 *
9590 * @param y The visual y position of this view, in pixels.
9591 */
9592 public void setY(float y) {
9593 setTranslationY(y - mTop);
9594 }
9595
9596
9597 /**
9598 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9599 * This position is post-layout, in addition to wherever the object's
9600 * layout placed it.
9601 *
9602 * @return The horizontal position of this view relative to its left position, in pixels.
9603 */
Chet Haasea5531132012-02-02 13:41:44 -08009604 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009605 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009606 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009607 }
9608
9609 /**
9610 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9611 * This effectively positions the object post-layout, in addition to wherever the object's
9612 * layout placed it.
9613 *
9614 * @param translationX The horizontal position of this view relative to its left position,
9615 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009616 *
9617 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009618 */
9619 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009620 ensureTransformationInfo();
9621 final TransformationInfo info = mTransformationInfo;
9622 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009623 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009624 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009625 info.mTranslationX = translationX;
9626 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009627 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009628 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009629 mDisplayList.setTranslationX(translationX);
9630 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009631 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9632 // View was rejected last time it was drawn by its parent; this may have changed
9633 invalidateParentIfNeeded();
9634 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009635 }
9636 }
9637
9638 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009639 * The horizontal location of this view relative to its {@link #getTop() top} position.
9640 * This position is post-layout, in addition to wherever the object's
9641 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009642 *
Chet Haasedf030d22010-07-30 17:22:38 -07009643 * @return The vertical position of this view relative to its top position,
9644 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009645 */
Chet Haasea5531132012-02-02 13:41:44 -08009646 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009647 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009648 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009649 }
9650
9651 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009652 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9653 * This effectively positions the object post-layout, in addition to wherever the object's
9654 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009655 *
Chet Haasedf030d22010-07-30 17:22:38 -07009656 * @param translationY The vertical position of this view relative to its top position,
9657 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009658 *
9659 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009660 */
Chet Haasedf030d22010-07-30 17:22:38 -07009661 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009662 ensureTransformationInfo();
9663 final TransformationInfo info = mTransformationInfo;
9664 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009665 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009666 info.mTranslationY = translationY;
9667 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009668 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009669 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009670 mDisplayList.setTranslationY(translationY);
9671 }
Chet Haase1a3ab172012-05-11 08:41:20 -07009672 if ((mPrivateFlags2 & VIEW_QUICK_REJECTED) == VIEW_QUICK_REJECTED) {
9673 // View was rejected last time it was drawn by its parent; this may have changed
9674 invalidateParentIfNeeded();
9675 }
Chet Haasedf030d22010-07-30 17:22:38 -07009676 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009677 }
9678
9679 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 * Hit rectangle in parent's coordinates
9681 *
9682 * @param outRect The hit rectangle of the view.
9683 */
9684 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009685 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009686 final TransformationInfo info = mTransformationInfo;
9687 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009688 outRect.set(mLeft, mTop, mRight, mBottom);
9689 } else {
9690 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009691 tmpRect.set(-info.mPivotX, -info.mPivotY,
9692 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9693 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009694 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9695 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009696 }
9697 }
9698
9699 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009700 * Determines whether the given point, in local coordinates is inside the view.
9701 */
9702 /*package*/ final boolean pointInView(float localX, float localY) {
9703 return localX >= 0 && localX < (mRight - mLeft)
9704 && localY >= 0 && localY < (mBottom - mTop);
9705 }
9706
9707 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009708 * Utility method to determine whether the given point, in local coordinates,
9709 * is inside the view, where the area of the view is expanded by the slop factor.
9710 * This method is called while processing touch-move events to determine if the event
9711 * is still within the view.
9712 */
9713 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009714 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009715 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
9717
9718 /**
9719 * When a view has focus and the user navigates away from it, the next view is searched for
9720 * starting from the rectangle filled in by this method.
9721 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009722 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9723 * of the view. However, if your view maintains some idea of internal selection,
9724 * such as a cursor, or a selected row or column, you should override this method and
9725 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 *
9727 * @param r The rectangle to fill in, in this view's coordinates.
9728 */
9729 public void getFocusedRect(Rect r) {
9730 getDrawingRect(r);
9731 }
9732
9733 /**
9734 * If some part of this view is not clipped by any of its parents, then
9735 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009736 * coordinates (without taking possible View rotations into account), offset
9737 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9738 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 *
9740 * @param r If true is returned, r holds the global coordinates of the
9741 * visible portion of this view.
9742 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9743 * between this view and its root. globalOffet may be null.
9744 * @return true if r is non-empty (i.e. part of the view is visible at the
9745 * root level.
9746 */
9747 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9748 int width = mRight - mLeft;
9749 int height = mBottom - mTop;
9750 if (width > 0 && height > 0) {
9751 r.set(0, 0, width, height);
9752 if (globalOffset != null) {
9753 globalOffset.set(-mScrollX, -mScrollY);
9754 }
9755 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9756 }
9757 return false;
9758 }
9759
9760 public final boolean getGlobalVisibleRect(Rect r) {
9761 return getGlobalVisibleRect(r, null);
9762 }
9763
9764 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009765 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 if (getGlobalVisibleRect(r, offset)) {
9767 r.offset(-offset.x, -offset.y); // make r local
9768 return true;
9769 }
9770 return false;
9771 }
9772
9773 /**
9774 * Offset this view's vertical location by the specified number of pixels.
9775 *
9776 * @param offset the number of pixels to offset the view by
9777 */
9778 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009779 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009780 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009781 final boolean matrixIsIdentity = mTransformationInfo == null
9782 || mTransformationInfo.mMatrixIsIdentity;
9783 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009784 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009785 invalidateViewProperty(false, false);
9786 } else {
9787 final ViewParent p = mParent;
9788 if (p != null && mAttachInfo != null) {
9789 final Rect r = mAttachInfo.mTmpInvalRect;
9790 int minTop;
9791 int maxBottom;
9792 int yLoc;
9793 if (offset < 0) {
9794 minTop = mTop + offset;
9795 maxBottom = mBottom;
9796 yLoc = offset;
9797 } else {
9798 minTop = mTop;
9799 maxBottom = mBottom + offset;
9800 yLoc = 0;
9801 }
9802 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9803 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009804 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009805 }
9806 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009807 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009808 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009809
Chet Haasec3aa3612010-06-17 08:50:37 -07009810 mTop += offset;
9811 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009812 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009813 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009814 invalidateViewProperty(false, false);
9815 } else {
9816 if (!matrixIsIdentity) {
9817 invalidateViewProperty(false, true);
9818 }
9819 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009820 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009822 }
9823
9824 /**
9825 * Offset this view's horizontal location by the specified amount of pixels.
9826 *
9827 * @param offset the numer of pixels to offset the view by
9828 */
9829 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009830 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009831 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009832 final boolean matrixIsIdentity = mTransformationInfo == null
9833 || mTransformationInfo.mMatrixIsIdentity;
9834 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009835 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009836 invalidateViewProperty(false, false);
9837 } else {
9838 final ViewParent p = mParent;
9839 if (p != null && mAttachInfo != null) {
9840 final Rect r = mAttachInfo.mTmpInvalRect;
9841 int minLeft;
9842 int maxRight;
9843 if (offset < 0) {
9844 minLeft = mLeft + offset;
9845 maxRight = mRight;
9846 } else {
9847 minLeft = mLeft;
9848 maxRight = mRight + offset;
9849 }
9850 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9851 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009852 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009853 }
9854 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009855 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009856 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009857
Chet Haasec3aa3612010-06-17 08:50:37 -07009858 mLeft += offset;
9859 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009860 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009861 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009862 invalidateViewProperty(false, false);
9863 } else {
9864 if (!matrixIsIdentity) {
9865 invalidateViewProperty(false, true);
9866 }
9867 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009868 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 }
9871
9872 /**
9873 * Get the LayoutParams associated with this view. All views should have
9874 * layout parameters. These supply parameters to the <i>parent</i> of this
9875 * view specifying how it should be arranged. There are many subclasses of
9876 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9877 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009878 *
9879 * This method may return null if this View is not attached to a parent
9880 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9881 * was not invoked successfully. When a View is attached to a parent
9882 * ViewGroup, this method must not return null.
9883 *
9884 * @return The LayoutParams associated with this view, or null if no
9885 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009887 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 public ViewGroup.LayoutParams getLayoutParams() {
9889 return mLayoutParams;
9890 }
9891
9892 /**
9893 * Set the layout parameters associated with this view. These supply
9894 * parameters to the <i>parent</i> of this view specifying how it should be
9895 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9896 * correspond to the different subclasses of ViewGroup that are responsible
9897 * for arranging their children.
9898 *
Romain Guy01c174b2011-02-22 11:51:06 -08009899 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 */
9901 public void setLayoutParams(ViewGroup.LayoutParams params) {
9902 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009903 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 }
9905 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009906 if (mParent instanceof ViewGroup) {
9907 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009909 requestLayout();
9910 }
9911
9912 /**
9913 * Set the scrolled position of your view. This will cause a call to
9914 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9915 * invalidated.
9916 * @param x the x position to scroll to
9917 * @param y the y position to scroll to
9918 */
9919 public void scrollTo(int x, int y) {
9920 if (mScrollX != x || mScrollY != y) {
9921 int oldX = mScrollX;
9922 int oldY = mScrollY;
9923 mScrollX = x;
9924 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009925 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009927 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009928 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 }
9931 }
9932
9933 /**
9934 * Move the scrolled position of your view. This will cause a call to
9935 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9936 * invalidated.
9937 * @param x the amount of pixels to scroll by horizontally
9938 * @param y the amount of pixels to scroll by vertically
9939 */
9940 public void scrollBy(int x, int y) {
9941 scrollTo(mScrollX + x, mScrollY + y);
9942 }
9943
9944 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009945 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9946 * animation to fade the scrollbars out after a default delay. If a subclass
9947 * provides animated scrolling, the start delay should equal the duration
9948 * of the scrolling animation.</p>
9949 *
9950 * <p>The animation starts only if at least one of the scrollbars is
9951 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9952 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9953 * this method returns true, and false otherwise. If the animation is
9954 * started, this method calls {@link #invalidate()}; in that case the
9955 * caller should not call {@link #invalidate()}.</p>
9956 *
9957 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009958 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009959 *
9960 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9961 * and {@link #scrollTo(int, int)}.</p>
9962 *
9963 * @return true if the animation is played, false otherwise
9964 *
9965 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009966 * @see #scrollBy(int, int)
9967 * @see #scrollTo(int, int)
9968 * @see #isHorizontalScrollBarEnabled()
9969 * @see #isVerticalScrollBarEnabled()
9970 * @see #setHorizontalScrollBarEnabled(boolean)
9971 * @see #setVerticalScrollBarEnabled(boolean)
9972 */
9973 protected boolean awakenScrollBars() {
9974 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009975 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009976 }
9977
9978 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009979 * Trigger the scrollbars to draw.
9980 * This method differs from awakenScrollBars() only in its default duration.
9981 * initialAwakenScrollBars() will show the scroll bars for longer than
9982 * usual to give the user more of a chance to notice them.
9983 *
9984 * @return true if the animation is played, false otherwise.
9985 */
9986 private boolean initialAwakenScrollBars() {
9987 return mScrollCache != null &&
9988 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9989 }
9990
9991 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009992 * <p>
9993 * Trigger the scrollbars to draw. When invoked this method starts an
9994 * animation to fade the scrollbars out after a fixed delay. If a subclass
9995 * provides animated scrolling, the start delay should equal the duration of
9996 * the scrolling animation.
9997 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009998 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009999 * <p>
10000 * The animation starts only if at least one of the scrollbars is enabled,
10001 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10002 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10003 * this method returns true, and false otherwise. If the animation is
10004 * started, this method calls {@link #invalidate()}; in that case the caller
10005 * should not call {@link #invalidate()}.
10006 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010007 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010008 * <p>
10009 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010010 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010011 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010012 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010013 * @param startDelay the delay, in milliseconds, after which the animation
10014 * should start; when the delay is 0, the animation starts
10015 * immediately
10016 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010017 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010018 * @see #scrollBy(int, int)
10019 * @see #scrollTo(int, int)
10020 * @see #isHorizontalScrollBarEnabled()
10021 * @see #isVerticalScrollBarEnabled()
10022 * @see #setHorizontalScrollBarEnabled(boolean)
10023 * @see #setVerticalScrollBarEnabled(boolean)
10024 */
10025 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010026 return awakenScrollBars(startDelay, true);
10027 }
Joe Malin32736f02011-01-19 16:14:20 -080010028
Mike Cleron290947b2009-09-29 18:34:32 -070010029 /**
10030 * <p>
10031 * Trigger the scrollbars to draw. When invoked this method starts an
10032 * animation to fade the scrollbars out after a fixed delay. If a subclass
10033 * provides animated scrolling, the start delay should equal the duration of
10034 * the scrolling animation.
10035 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010036 *
Mike Cleron290947b2009-09-29 18:34:32 -070010037 * <p>
10038 * The animation starts only if at least one of the scrollbars is enabled,
10039 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10040 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10041 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010042 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010043 * is set to true; in that case the caller
10044 * should not call {@link #invalidate()}.
10045 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010046 *
Mike Cleron290947b2009-09-29 18:34:32 -070010047 * <p>
10048 * This method should be invoked everytime a subclass directly updates the
10049 * scroll parameters.
10050 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010051 *
Mike Cleron290947b2009-09-29 18:34:32 -070010052 * @param startDelay the delay, in milliseconds, after which the animation
10053 * should start; when the delay is 0, the animation starts
10054 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010055 *
Mike Cleron290947b2009-09-29 18:34:32 -070010056 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010057 *
Mike Cleron290947b2009-09-29 18:34:32 -070010058 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010059 *
Mike Cleron290947b2009-09-29 18:34:32 -070010060 * @see #scrollBy(int, int)
10061 * @see #scrollTo(int, int)
10062 * @see #isHorizontalScrollBarEnabled()
10063 * @see #isVerticalScrollBarEnabled()
10064 * @see #setHorizontalScrollBarEnabled(boolean)
10065 * @see #setVerticalScrollBarEnabled(boolean)
10066 */
10067 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010068 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010069
Mike Cleronf116bf82009-09-27 19:14:12 -070010070 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10071 return false;
10072 }
10073
10074 if (scrollCache.scrollBar == null) {
10075 scrollCache.scrollBar = new ScrollBarDrawable();
10076 }
10077
10078 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10079
Mike Cleron290947b2009-09-29 18:34:32 -070010080 if (invalidate) {
10081 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010082 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010083 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010084
10085 if (scrollCache.state == ScrollabilityCache.OFF) {
10086 // FIXME: this is copied from WindowManagerService.
10087 // We should get this value from the system when it
10088 // is possible to do so.
10089 final int KEY_REPEAT_FIRST_DELAY = 750;
10090 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10091 }
10092
10093 // Tell mScrollCache when we should start fading. This may
10094 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010095 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010096 scrollCache.fadeStartTime = fadeStartTime;
10097 scrollCache.state = ScrollabilityCache.ON;
10098
10099 // Schedule our fader to run, unscheduling any old ones first
10100 if (mAttachInfo != null) {
10101 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10102 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10103 }
10104
10105 return true;
10106 }
10107
10108 return false;
10109 }
10110
10111 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010112 * Do not invalidate views which are not visible and which are not running an animation. They
10113 * will not get drawn and they should not set dirty flags as if they will be drawn
10114 */
10115 private boolean skipInvalidate() {
10116 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10117 (!(mParent instanceof ViewGroup) ||
10118 !((ViewGroup) mParent).isViewTransitioning(this));
10119 }
10120 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010121 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010122 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10123 * in the future. This must be called from a UI thread. To call from a non-UI
10124 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010125 *
10126 * WARNING: This method is destructive to dirty.
10127 * @param dirty the rectangle representing the bounds of the dirty region
10128 */
10129 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010130 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010131 return;
10132 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010133 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010134 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10135 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010137 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010138 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 final ViewParent p = mParent;
10140 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010141 //noinspection PointlessBooleanExpression,ConstantConditions
10142 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10143 if (p != null && ai != null && ai.mHardwareAccelerated) {
10144 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010145 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010146 p.invalidateChild(this, null);
10147 return;
10148 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 if (p != null && ai != null) {
10151 final int scrollX = mScrollX;
10152 final int scrollY = mScrollY;
10153 final Rect r = ai.mTmpInvalRect;
10154 r.set(dirty.left - scrollX, dirty.top - scrollY,
10155 dirty.right - scrollX, dirty.bottom - scrollY);
10156 mParent.invalidateChild(this, r);
10157 }
10158 }
10159 }
10160
10161 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010162 * 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 -080010163 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010164 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10165 * will be called at some point in the future. This must be called from
10166 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 * @param l the left position of the dirty region
10168 * @param t the top position of the dirty region
10169 * @param r the right position of the dirty region
10170 * @param b the bottom position of the dirty region
10171 */
10172 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010173 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010174 return;
10175 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010176 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -080010177 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
10178 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010179 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -080010180 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -070010181 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 final ViewParent p = mParent;
10183 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010184 //noinspection PointlessBooleanExpression,ConstantConditions
10185 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10186 if (p != null && ai != null && ai.mHardwareAccelerated) {
10187 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010188 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010189 p.invalidateChild(this, null);
10190 return;
10191 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193 if (p != null && ai != null && l < r && t < b) {
10194 final int scrollX = mScrollX;
10195 final int scrollY = mScrollY;
10196 final Rect tmpr = ai.mTmpInvalRect;
10197 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10198 p.invalidateChild(this, tmpr);
10199 }
10200 }
10201 }
10202
10203 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010204 * Invalidate the whole view. If the view is visible,
10205 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10206 * the future. This must be called from a UI thread. To call from a non-UI thread,
10207 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 */
10209 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010210 invalidate(true);
10211 }
Joe Malin32736f02011-01-19 16:14:20 -080010212
Chet Haaseed032702010-10-01 14:05:54 -070010213 /**
10214 * This is where the invalidate() work actually happens. A full invalidate()
10215 * causes the drawing cache to be invalidated, but this function can be called with
10216 * invalidateCache set to false to skip that invalidation step for cases that do not
10217 * need it (for example, a component that remains at the same dimensions with the same
10218 * content).
10219 *
10220 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10221 * well. This is usually true for a full invalidate, but may be set to false if the
10222 * View's contents or dimensions have not changed.
10223 */
Romain Guy849d0a32011-02-01 17:20:48 -080010224 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010225 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010226 return;
10227 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010228 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -080010229 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -080010230 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
10231 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -070010232 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -070010233 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010234 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -080010235 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -070010236 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010239 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010240 //noinspection PointlessBooleanExpression,ConstantConditions
10241 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10242 if (p != null && ai != null && ai.mHardwareAccelerated) {
10243 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010244 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010245 p.invalidateChild(this, null);
10246 return;
10247 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010248 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010250 if (p != null && ai != null) {
10251 final Rect r = ai.mTmpInvalRect;
10252 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10253 // Don't call invalidate -- we don't want to internally scroll
10254 // our own bounds
10255 p.invalidateChild(this, r);
10256 }
10257 }
10258 }
10259
10260 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010261 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10262 * set any flags or handle all of the cases handled by the default invalidation methods.
10263 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10264 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10265 * walk up the hierarchy, transforming the dirty rect as necessary.
10266 *
10267 * The method also handles normal invalidation logic if display list properties are not
10268 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10269 * backup approach, to handle these cases used in the various property-setting methods.
10270 *
10271 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10272 * are not being used in this view
10273 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10274 * list properties are not being used in this view
10275 */
10276 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -070010277 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010278 if (invalidateParent) {
10279 invalidateParentCaches();
10280 }
10281 if (forceRedraw) {
10282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
10283 }
10284 invalidate(false);
10285 } else {
10286 final AttachInfo ai = mAttachInfo;
10287 final ViewParent p = mParent;
10288 if (p != null && ai != null) {
10289 final Rect r = ai.mTmpInvalRect;
10290 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10291 if (mParent instanceof ViewGroup) {
10292 ((ViewGroup) mParent).invalidateChildFast(this, r);
10293 } else {
10294 mParent.invalidateChild(this, r);
10295 }
10296 }
10297 }
10298 }
10299
10300 /**
10301 * Utility method to transform a given Rect by the current matrix of this view.
10302 */
10303 void transformRect(final Rect rect) {
10304 if (!getMatrix().isIdentity()) {
10305 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10306 boundingRect.set(rect);
10307 getMatrix().mapRect(boundingRect);
10308 rect.set((int) (boundingRect.left - 0.5f),
10309 (int) (boundingRect.top - 0.5f),
10310 (int) (boundingRect.right + 0.5f),
10311 (int) (boundingRect.bottom + 0.5f));
10312 }
10313 }
10314
10315 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010316 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010317 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10318 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010319 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10320 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010321 *
10322 * @hide
10323 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010324 protected void invalidateParentCaches() {
10325 if (mParent instanceof View) {
10326 ((View) mParent).mPrivateFlags |= INVALIDATED;
10327 }
10328 }
Joe Malin32736f02011-01-19 16:14:20 -080010329
Romain Guy0fd89bf2011-01-26 15:41:30 -080010330 /**
10331 * Used to indicate that the parent of this view should be invalidated. This functionality
10332 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10333 * which is necessary when various parent-managed properties of the view change, such as
10334 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10335 * an invalidation event to the parent.
10336 *
10337 * @hide
10338 */
10339 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010340 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010341 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010342 }
10343 }
10344
10345 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010346 * Indicates whether this View is opaque. An opaque View guarantees that it will
10347 * draw all the pixels overlapping its bounds using a fully opaque color.
10348 *
10349 * Subclasses of View should override this method whenever possible to indicate
10350 * whether an instance is opaque. Opaque Views are treated in a special way by
10351 * the View hierarchy, possibly allowing it to perform optimizations during
10352 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010353 *
Romain Guy24443ea2009-05-11 11:56:30 -070010354 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010355 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010356 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010357 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -070010358 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -070010359 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
10360 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -070010361 }
10362
Adam Powell20232d02010-12-08 21:08:53 -080010363 /**
10364 * @hide
10365 */
10366 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010367 // Opaque if:
10368 // - Has a background
10369 // - Background is opaque
10370 // - Doesn't have scrollbars or scrollbars are inside overlay
10371
Philip Milne6c8ea062012-04-03 17:38:43 -070010372 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -070010373 mPrivateFlags |= OPAQUE_BACKGROUND;
10374 } else {
10375 mPrivateFlags &= ~OPAQUE_BACKGROUND;
10376 }
10377
10378 final int flags = mViewFlags;
10379 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10380 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
10381 mPrivateFlags |= OPAQUE_SCROLLBARS;
10382 } else {
10383 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
10384 }
10385 }
10386
10387 /**
10388 * @hide
10389 */
10390 protected boolean hasOpaqueScrollbars() {
10391 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010392 }
10393
10394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 * @return A handler associated with the thread running the View. This
10396 * handler can be used to pump events in the UI events queue.
10397 */
10398 public Handler getHandler() {
10399 if (mAttachInfo != null) {
10400 return mAttachInfo.mHandler;
10401 }
10402 return null;
10403 }
10404
10405 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010406 * Gets the view root associated with the View.
10407 * @return The view root, or null if none.
10408 * @hide
10409 */
10410 public ViewRootImpl getViewRootImpl() {
10411 if (mAttachInfo != null) {
10412 return mAttachInfo.mViewRootImpl;
10413 }
10414 return null;
10415 }
10416
10417 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010418 * <p>Causes the Runnable to be added to the message queue.
10419 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010420 *
Romain Guye63a4f32011-08-11 11:33:31 -070010421 * <p>This method can be invoked from outside of the UI thread
10422 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 *
10424 * @param action The Runnable that will be executed.
10425 *
10426 * @return Returns true if the Runnable was successfully placed in to the
10427 * message queue. Returns false on failure, usually because the
10428 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010429 *
10430 * @see #postDelayed
10431 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 */
10433 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010434 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010435 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010436 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010437 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010438 // Assume that post will succeed later
10439 ViewRootImpl.getRunQueue().post(action);
10440 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 }
10442
10443 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010444 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010446 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010447 *
Romain Guye63a4f32011-08-11 11:33:31 -070010448 * <p>This method can be invoked from outside of the UI thread
10449 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010450 *
10451 * @param action The Runnable that will be executed.
10452 * @param delayMillis The delay (in milliseconds) until the Runnable
10453 * will be executed.
10454 *
10455 * @return true if the Runnable was successfully placed in to the
10456 * message queue. Returns false on failure, usually because the
10457 * looper processing the message queue is exiting. Note that a
10458 * result of true does not mean the Runnable will be processed --
10459 * if the looper is quit before the delivery time of the message
10460 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010461 *
10462 * @see #post
10463 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010464 */
10465 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010466 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010467 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010468 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010469 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010470 // Assume that post will succeed later
10471 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10472 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 }
10474
10475 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010476 * <p>Causes the Runnable to execute on the next animation time step.
10477 * The runnable will be run on the user interface thread.</p>
10478 *
10479 * <p>This method can be invoked from outside of the UI thread
10480 * only when this View is attached to a window.</p>
10481 *
10482 * @param action The Runnable that will be executed.
10483 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010484 * @see #postOnAnimationDelayed
10485 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010486 */
10487 public void postOnAnimation(Runnable action) {
10488 final AttachInfo attachInfo = mAttachInfo;
10489 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010490 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10491 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010492 } else {
10493 // Assume that post will succeed later
10494 ViewRootImpl.getRunQueue().post(action);
10495 }
10496 }
10497
10498 /**
10499 * <p>Causes the Runnable to execute on the next animation time step,
10500 * after the specified amount of time elapses.
10501 * The runnable will be run on the user interface thread.</p>
10502 *
10503 * <p>This method can be invoked from outside of the UI thread
10504 * only when this View is attached to a window.</p>
10505 *
10506 * @param action The Runnable that will be executed.
10507 * @param delayMillis The delay (in milliseconds) until the Runnable
10508 * will be executed.
10509 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010510 * @see #postOnAnimation
10511 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010512 */
10513 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10514 final AttachInfo attachInfo = mAttachInfo;
10515 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010516 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10517 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010518 } else {
10519 // Assume that post will succeed later
10520 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10521 }
10522 }
10523
10524 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010525 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010526 *
Romain Guye63a4f32011-08-11 11:33:31 -070010527 * <p>This method can be invoked from outside of the UI thread
10528 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 *
10530 * @param action The Runnable to remove from the message handling queue
10531 *
10532 * @return true if this view could ask the Handler to remove the Runnable,
10533 * false otherwise. When the returned value is true, the Runnable
10534 * may or may not have been actually removed from the message queue
10535 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010536 *
10537 * @see #post
10538 * @see #postDelayed
10539 * @see #postOnAnimation
10540 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 */
10542 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010543 if (action != null) {
10544 final AttachInfo attachInfo = mAttachInfo;
10545 if (attachInfo != null) {
10546 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010547 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10548 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010549 } else {
10550 // Assume that post will succeed later
10551 ViewRootImpl.getRunQueue().removeCallbacks(action);
10552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 return true;
10555 }
10556
10557 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010558 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10559 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010560 *
Romain Guye63a4f32011-08-11 11:33:31 -070010561 * <p>This method can be invoked from outside of the UI thread
10562 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010565 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 */
10567 public void postInvalidate() {
10568 postInvalidateDelayed(0);
10569 }
10570
10571 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010572 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10573 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010574 *
Romain Guye63a4f32011-08-11 11:33:31 -070010575 * <p>This method can be invoked from outside of the UI thread
10576 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010577 *
10578 * @param left The left coordinate of the rectangle to invalidate.
10579 * @param top The top coordinate of the rectangle to invalidate.
10580 * @param right The right coordinate of the rectangle to invalidate.
10581 * @param bottom The bottom coordinate of the rectangle to invalidate.
10582 *
10583 * @see #invalidate(int, int, int, int)
10584 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010585 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 */
10587 public void postInvalidate(int left, int top, int right, int bottom) {
10588 postInvalidateDelayed(0, left, top, right, bottom);
10589 }
10590
10591 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010592 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10593 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010594 *
Romain Guye63a4f32011-08-11 11:33:31 -070010595 * <p>This method can be invoked from outside of the UI thread
10596 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 *
10598 * @param delayMilliseconds the duration in milliseconds to delay the
10599 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010600 *
10601 * @see #invalidate()
10602 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 */
10604 public void postInvalidateDelayed(long delayMilliseconds) {
10605 // We try only with the AttachInfo because there's no point in invalidating
10606 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010607 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010608 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010609 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 }
10611 }
10612
10613 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010614 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10615 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010616 *
Romain Guye63a4f32011-08-11 11:33:31 -070010617 * <p>This method can be invoked from outside of the UI thread
10618 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010619 *
10620 * @param delayMilliseconds the duration in milliseconds to delay the
10621 * invalidation by
10622 * @param left The left coordinate of the rectangle to invalidate.
10623 * @param top The top coordinate of the rectangle to invalidate.
10624 * @param right The right coordinate of the rectangle to invalidate.
10625 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010626 *
10627 * @see #invalidate(int, int, int, int)
10628 * @see #invalidate(Rect)
10629 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 */
10631 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10632 int right, int bottom) {
10633
10634 // We try only with the AttachInfo because there's no point in invalidating
10635 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010636 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010637 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10639 info.target = this;
10640 info.left = left;
10641 info.top = top;
10642 info.right = right;
10643 info.bottom = bottom;
10644
Jeff Browna175a5b2012-02-15 19:18:31 -080010645 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 }
10647 }
10648
10649 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010650 * <p>Cause an invalidate to happen on the next animation time step, typically the
10651 * next display frame.</p>
10652 *
10653 * <p>This method can be invoked from outside of the UI thread
10654 * only when this View is attached to a window.</p>
10655 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010656 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010657 */
10658 public void postInvalidateOnAnimation() {
10659 // We try only with the AttachInfo because there's no point in invalidating
10660 // if we are not attached to our window
10661 final AttachInfo attachInfo = mAttachInfo;
10662 if (attachInfo != null) {
10663 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10664 }
10665 }
10666
10667 /**
10668 * <p>Cause an invalidate of the specified area to happen on the next animation
10669 * time step, typically the next display frame.</p>
10670 *
10671 * <p>This method can be invoked from outside of the UI thread
10672 * only when this View is attached to a window.</p>
10673 *
10674 * @param left The left coordinate of the rectangle to invalidate.
10675 * @param top The top coordinate of the rectangle to invalidate.
10676 * @param right The right coordinate of the rectangle to invalidate.
10677 * @param bottom The bottom coordinate of the rectangle to invalidate.
10678 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010679 * @see #invalidate(int, int, int, int)
10680 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010681 */
10682 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10683 // We try only with the AttachInfo because there's no point in invalidating
10684 // if we are not attached to our window
10685 final AttachInfo attachInfo = mAttachInfo;
10686 if (attachInfo != null) {
10687 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10688 info.target = this;
10689 info.left = left;
10690 info.top = top;
10691 info.right = right;
10692 info.bottom = bottom;
10693
10694 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10695 }
10696 }
10697
10698 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010699 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10700 * This event is sent at most once every
10701 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10702 */
10703 private void postSendViewScrolledAccessibilityEventCallback() {
10704 if (mSendViewScrolledAccessibilityEvent == null) {
10705 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10706 }
10707 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10708 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10709 postDelayed(mSendViewScrolledAccessibilityEvent,
10710 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10711 }
10712 }
10713
10714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 * Called by a parent to request that a child update its values for mScrollX
10716 * and mScrollY if necessary. This will typically be done if the child is
10717 * animating a scroll using a {@link android.widget.Scroller Scroller}
10718 * object.
10719 */
10720 public void computeScroll() {
10721 }
10722
10723 /**
10724 * <p>Indicate whether the horizontal edges are faded when the view is
10725 * scrolled horizontally.</p>
10726 *
10727 * @return true if the horizontal edges should are faded on scroll, false
10728 * otherwise
10729 *
10730 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010731 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010732 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 */
10734 public boolean isHorizontalFadingEdgeEnabled() {
10735 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10736 }
10737
10738 /**
10739 * <p>Define whether the horizontal edges should be faded when this view
10740 * is scrolled horizontally.</p>
10741 *
10742 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10743 * be faded when the view is scrolled
10744 * horizontally
10745 *
10746 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010747 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010748 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010749 */
10750 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10751 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10752 if (horizontalFadingEdgeEnabled) {
10753 initScrollCache();
10754 }
10755
10756 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10757 }
10758 }
10759
10760 /**
10761 * <p>Indicate whether the vertical edges are faded when the view is
10762 * scrolled horizontally.</p>
10763 *
10764 * @return true if the vertical edges should are faded on scroll, false
10765 * otherwise
10766 *
10767 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010768 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010769 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010770 */
10771 public boolean isVerticalFadingEdgeEnabled() {
10772 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10773 }
10774
10775 /**
10776 * <p>Define whether the vertical edges should be faded when this view
10777 * is scrolled vertically.</p>
10778 *
10779 * @param verticalFadingEdgeEnabled true if the vertical edges should
10780 * be faded when the view is scrolled
10781 * vertically
10782 *
10783 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010784 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010785 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010786 */
10787 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10788 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10789 if (verticalFadingEdgeEnabled) {
10790 initScrollCache();
10791 }
10792
10793 mViewFlags ^= FADING_EDGE_VERTICAL;
10794 }
10795 }
10796
10797 /**
10798 * Returns the strength, or intensity, of the top faded edge. The strength is
10799 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10800 * returns 0.0 or 1.0 but no value in between.
10801 *
10802 * Subclasses should override this method to provide a smoother fade transition
10803 * when scrolling occurs.
10804 *
10805 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10806 */
10807 protected float getTopFadingEdgeStrength() {
10808 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10809 }
10810
10811 /**
10812 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10813 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10814 * returns 0.0 or 1.0 but no value in between.
10815 *
10816 * Subclasses should override this method to provide a smoother fade transition
10817 * when scrolling occurs.
10818 *
10819 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10820 */
10821 protected float getBottomFadingEdgeStrength() {
10822 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10823 computeVerticalScrollRange() ? 1.0f : 0.0f;
10824 }
10825
10826 /**
10827 * Returns the strength, or intensity, of the left faded edge. The strength is
10828 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10829 * returns 0.0 or 1.0 but no value in between.
10830 *
10831 * Subclasses should override this method to provide a smoother fade transition
10832 * when scrolling occurs.
10833 *
10834 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10835 */
10836 protected float getLeftFadingEdgeStrength() {
10837 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10838 }
10839
10840 /**
10841 * Returns the strength, or intensity, of the right faded edge. The strength is
10842 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10843 * returns 0.0 or 1.0 but no value in between.
10844 *
10845 * Subclasses should override this method to provide a smoother fade transition
10846 * when scrolling occurs.
10847 *
10848 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10849 */
10850 protected float getRightFadingEdgeStrength() {
10851 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10852 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10853 }
10854
10855 /**
10856 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10857 * scrollbar is not drawn by default.</p>
10858 *
10859 * @return true if the horizontal scrollbar should be painted, false
10860 * otherwise
10861 *
10862 * @see #setHorizontalScrollBarEnabled(boolean)
10863 */
10864 public boolean isHorizontalScrollBarEnabled() {
10865 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10866 }
10867
10868 /**
10869 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10870 * scrollbar is not drawn by default.</p>
10871 *
10872 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10873 * be painted
10874 *
10875 * @see #isHorizontalScrollBarEnabled()
10876 */
10877 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10878 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10879 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010880 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010881 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 }
10883 }
10884
10885 /**
10886 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10887 * scrollbar is not drawn by default.</p>
10888 *
10889 * @return true if the vertical scrollbar should be painted, false
10890 * otherwise
10891 *
10892 * @see #setVerticalScrollBarEnabled(boolean)
10893 */
10894 public boolean isVerticalScrollBarEnabled() {
10895 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10896 }
10897
10898 /**
10899 * <p>Define whether the vertical scrollbar should be drawn or not. The
10900 * scrollbar is not drawn by default.</p>
10901 *
10902 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10903 * be painted
10904 *
10905 * @see #isVerticalScrollBarEnabled()
10906 */
10907 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10908 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10909 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010910 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010911 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 }
10913 }
10914
Adam Powell20232d02010-12-08 21:08:53 -080010915 /**
10916 * @hide
10917 */
10918 protected void recomputePadding() {
10919 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 }
Joe Malin32736f02011-01-19 16:14:20 -080010921
Mike Cleronfe81d382009-09-28 14:22:16 -070010922 /**
10923 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010924 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010925 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010926 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010927 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010928 */
10929 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10930 initScrollCache();
10931 final ScrollabilityCache scrollabilityCache = mScrollCache;
10932 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010933 if (fadeScrollbars) {
10934 scrollabilityCache.state = ScrollabilityCache.OFF;
10935 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010936 scrollabilityCache.state = ScrollabilityCache.ON;
10937 }
10938 }
Joe Malin32736f02011-01-19 16:14:20 -080010939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 /**
Joe Malin32736f02011-01-19 16:14:20 -080010941 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010942 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010943 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010944 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010945 *
10946 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010947 */
10948 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010949 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010950 }
Joe Malin32736f02011-01-19 16:14:20 -080010951
Mike Cleron52f0a642009-09-28 18:21:37 -070010952 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010953 *
10954 * Returns the delay before scrollbars fade.
10955 *
10956 * @return the delay before scrollbars fade
10957 *
10958 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10959 */
10960 public int getScrollBarDefaultDelayBeforeFade() {
10961 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10962 mScrollCache.scrollBarDefaultDelayBeforeFade;
10963 }
10964
10965 /**
10966 * Define the delay before scrollbars fade.
10967 *
10968 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10969 *
10970 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10971 */
10972 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10973 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10974 }
10975
10976 /**
10977 *
10978 * Returns the scrollbar fade duration.
10979 *
10980 * @return the scrollbar fade duration
10981 *
10982 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10983 */
10984 public int getScrollBarFadeDuration() {
10985 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10986 mScrollCache.scrollBarFadeDuration;
10987 }
10988
10989 /**
10990 * Define the scrollbar fade duration.
10991 *
10992 * @param scrollBarFadeDuration - the scrollbar fade duration
10993 *
10994 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10995 */
10996 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10997 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10998 }
10999
11000 /**
11001 *
11002 * Returns the scrollbar size.
11003 *
11004 * @return the scrollbar size
11005 *
11006 * @attr ref android.R.styleable#View_scrollbarSize
11007 */
11008 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011009 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011010 mScrollCache.scrollBarSize;
11011 }
11012
11013 /**
11014 * Define the scrollbar size.
11015 *
11016 * @param scrollBarSize - the scrollbar size
11017 *
11018 * @attr ref android.R.styleable#View_scrollbarSize
11019 */
11020 public void setScrollBarSize(int scrollBarSize) {
11021 getScrollCache().scrollBarSize = scrollBarSize;
11022 }
11023
11024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11026 * inset. When inset, they add to the padding of the view. And the scrollbars
11027 * can be drawn inside the padding area or on the edge of the view. For example,
11028 * if a view has a background drawable and you want to draw the scrollbars
11029 * inside the padding specified by the drawable, you can use
11030 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11031 * appear at the edge of the view, ignoring the padding, then you can use
11032 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11033 * @param style the style of the scrollbars. Should be one of
11034 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11035 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11036 * @see #SCROLLBARS_INSIDE_OVERLAY
11037 * @see #SCROLLBARS_INSIDE_INSET
11038 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11039 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011040 *
11041 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 */
11043 public void setScrollBarStyle(int style) {
11044 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11045 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011046 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011047 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 }
11049 }
11050
11051 /**
11052 * <p>Returns the current scrollbar style.</p>
11053 * @return the current scrollbar style
11054 * @see #SCROLLBARS_INSIDE_OVERLAY
11055 * @see #SCROLLBARS_INSIDE_INSET
11056 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11057 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011058 *
11059 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011061 @ViewDebug.ExportedProperty(mapping = {
11062 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11063 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11064 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11065 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11066 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011067 public int getScrollBarStyle() {
11068 return mViewFlags & SCROLLBARS_STYLE_MASK;
11069 }
11070
11071 /**
11072 * <p>Compute the horizontal range that the horizontal scrollbar
11073 * represents.</p>
11074 *
11075 * <p>The range is expressed in arbitrary units that must be the same as the
11076 * units used by {@link #computeHorizontalScrollExtent()} and
11077 * {@link #computeHorizontalScrollOffset()}.</p>
11078 *
11079 * <p>The default range is the drawing width of this view.</p>
11080 *
11081 * @return the total horizontal range represented by the horizontal
11082 * scrollbar
11083 *
11084 * @see #computeHorizontalScrollExtent()
11085 * @see #computeHorizontalScrollOffset()
11086 * @see android.widget.ScrollBarDrawable
11087 */
11088 protected int computeHorizontalScrollRange() {
11089 return getWidth();
11090 }
11091
11092 /**
11093 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11094 * within the horizontal range. This value is used to compute the position
11095 * of the thumb within the scrollbar's track.</p>
11096 *
11097 * <p>The range is expressed in arbitrary units that must be the same as the
11098 * units used by {@link #computeHorizontalScrollRange()} and
11099 * {@link #computeHorizontalScrollExtent()}.</p>
11100 *
11101 * <p>The default offset is the scroll offset of this view.</p>
11102 *
11103 * @return the horizontal offset of the scrollbar's thumb
11104 *
11105 * @see #computeHorizontalScrollRange()
11106 * @see #computeHorizontalScrollExtent()
11107 * @see android.widget.ScrollBarDrawable
11108 */
11109 protected int computeHorizontalScrollOffset() {
11110 return mScrollX;
11111 }
11112
11113 /**
11114 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11115 * within the horizontal range. This value is used to compute the length
11116 * of the thumb within the scrollbar's track.</p>
11117 *
11118 * <p>The range is expressed in arbitrary units that must be the same as the
11119 * units used by {@link #computeHorizontalScrollRange()} and
11120 * {@link #computeHorizontalScrollOffset()}.</p>
11121 *
11122 * <p>The default extent is the drawing width of this view.</p>
11123 *
11124 * @return the horizontal extent of the scrollbar's thumb
11125 *
11126 * @see #computeHorizontalScrollRange()
11127 * @see #computeHorizontalScrollOffset()
11128 * @see android.widget.ScrollBarDrawable
11129 */
11130 protected int computeHorizontalScrollExtent() {
11131 return getWidth();
11132 }
11133
11134 /**
11135 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11136 *
11137 * <p>The range is expressed in arbitrary units that must be the same as the
11138 * units used by {@link #computeVerticalScrollExtent()} and
11139 * {@link #computeVerticalScrollOffset()}.</p>
11140 *
11141 * @return the total vertical range represented by the vertical scrollbar
11142 *
11143 * <p>The default range is the drawing height of this view.</p>
11144 *
11145 * @see #computeVerticalScrollExtent()
11146 * @see #computeVerticalScrollOffset()
11147 * @see android.widget.ScrollBarDrawable
11148 */
11149 protected int computeVerticalScrollRange() {
11150 return getHeight();
11151 }
11152
11153 /**
11154 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11155 * within the horizontal range. This value is used to compute the position
11156 * of the thumb within the scrollbar's track.</p>
11157 *
11158 * <p>The range is expressed in arbitrary units that must be the same as the
11159 * units used by {@link #computeVerticalScrollRange()} and
11160 * {@link #computeVerticalScrollExtent()}.</p>
11161 *
11162 * <p>The default offset is the scroll offset of this view.</p>
11163 *
11164 * @return the vertical offset of the scrollbar's thumb
11165 *
11166 * @see #computeVerticalScrollRange()
11167 * @see #computeVerticalScrollExtent()
11168 * @see android.widget.ScrollBarDrawable
11169 */
11170 protected int computeVerticalScrollOffset() {
11171 return mScrollY;
11172 }
11173
11174 /**
11175 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11176 * within the vertical range. This value is used to compute the length
11177 * of the thumb within the scrollbar's track.</p>
11178 *
11179 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011180 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 * {@link #computeVerticalScrollOffset()}.</p>
11182 *
11183 * <p>The default extent is the drawing height of this view.</p>
11184 *
11185 * @return the vertical extent of the scrollbar's thumb
11186 *
11187 * @see #computeVerticalScrollRange()
11188 * @see #computeVerticalScrollOffset()
11189 * @see android.widget.ScrollBarDrawable
11190 */
11191 protected int computeVerticalScrollExtent() {
11192 return getHeight();
11193 }
11194
11195 /**
Adam Powell69159442011-06-13 17:53:06 -070011196 * Check if this view can be scrolled horizontally in a certain direction.
11197 *
11198 * @param direction Negative to check scrolling left, positive to check scrolling right.
11199 * @return true if this view can be scrolled in the specified direction, false otherwise.
11200 */
11201 public boolean canScrollHorizontally(int direction) {
11202 final int offset = computeHorizontalScrollOffset();
11203 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11204 if (range == 0) return false;
11205 if (direction < 0) {
11206 return offset > 0;
11207 } else {
11208 return offset < range - 1;
11209 }
11210 }
11211
11212 /**
11213 * Check if this view can be scrolled vertically in a certain direction.
11214 *
11215 * @param direction Negative to check scrolling up, positive to check scrolling down.
11216 * @return true if this view can be scrolled in the specified direction, false otherwise.
11217 */
11218 public boolean canScrollVertically(int direction) {
11219 final int offset = computeVerticalScrollOffset();
11220 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11221 if (range == 0) return false;
11222 if (direction < 0) {
11223 return offset > 0;
11224 } else {
11225 return offset < range - 1;
11226 }
11227 }
11228
11229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11231 * scrollbars are painted only if they have been awakened first.</p>
11232 *
11233 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011234 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011235 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011236 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011237 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011238 // scrollbars are drawn only when the animation is running
11239 final ScrollabilityCache cache = mScrollCache;
11240 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011241
Mike Cleronf116bf82009-09-27 19:14:12 -070011242 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011243
Mike Cleronf116bf82009-09-27 19:14:12 -070011244 if (state == ScrollabilityCache.OFF) {
11245 return;
11246 }
Joe Malin32736f02011-01-19 16:14:20 -080011247
Mike Cleronf116bf82009-09-27 19:14:12 -070011248 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011249
Mike Cleronf116bf82009-09-27 19:14:12 -070011250 if (state == ScrollabilityCache.FADING) {
11251 // We're fading -- get our fade interpolation
11252 if (cache.interpolatorValues == null) {
11253 cache.interpolatorValues = new float[1];
11254 }
Joe Malin32736f02011-01-19 16:14:20 -080011255
Mike Cleronf116bf82009-09-27 19:14:12 -070011256 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011257
Mike Cleronf116bf82009-09-27 19:14:12 -070011258 // Stops the animation if we're done
11259 if (cache.scrollBarInterpolator.timeToValues(values) ==
11260 Interpolator.Result.FREEZE_END) {
11261 cache.state = ScrollabilityCache.OFF;
11262 } else {
11263 cache.scrollBar.setAlpha(Math.round(values[0]));
11264 }
Joe Malin32736f02011-01-19 16:14:20 -080011265
11266 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011267 // drawing. We only want this when we're fading so that
11268 // we prevent excessive redraws
11269 invalidate = true;
11270 } else {
11271 // We're just on -- but we may have been fading before so
11272 // reset alpha
11273 cache.scrollBar.setAlpha(255);
11274 }
11275
Joe Malin32736f02011-01-19 16:14:20 -080011276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 final int viewFlags = mViewFlags;
11278
11279 final boolean drawHorizontalScrollBar =
11280 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11281 final boolean drawVerticalScrollBar =
11282 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11283 && !isVerticalScrollBarHidden();
11284
11285 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11286 final int width = mRight - mLeft;
11287 final int height = mBottom - mTop;
11288
11289 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290
Mike Reede8853fc2009-09-04 14:01:48 -040011291 final int scrollX = mScrollX;
11292 final int scrollY = mScrollY;
11293 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11294
Mike Cleronf116bf82009-09-27 19:14:12 -070011295 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011298 int size = scrollBar.getSize(false);
11299 if (size <= 0) {
11300 size = cache.scrollBarSize;
11301 }
11302
Mike Cleronf116bf82009-09-27 19:14:12 -070011303 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011304 computeHorizontalScrollOffset(),
11305 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011306 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011307 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011308 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011309 left = scrollX + (mPaddingLeft & inside);
11310 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11311 bottom = top + size;
11312 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11313 if (invalidate) {
11314 invalidate(left, top, right, bottom);
11315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 }
11317
11318 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011319 int size = scrollBar.getSize(true);
11320 if (size <= 0) {
11321 size = cache.scrollBarSize;
11322 }
11323
Mike Reede8853fc2009-09-04 14:01:48 -040011324 scrollBar.setParameters(computeVerticalScrollRange(),
11325 computeVerticalScrollOffset(),
11326 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080011327 switch (mVerticalScrollbarPosition) {
11328 default:
11329 case SCROLLBAR_POSITION_DEFAULT:
11330 case SCROLLBAR_POSITION_RIGHT:
11331 left = scrollX + width - size - (mUserPaddingRight & inside);
11332 break;
11333 case SCROLLBAR_POSITION_LEFT:
11334 left = scrollX + (mUserPaddingLeft & inside);
11335 break;
11336 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011337 top = scrollY + (mPaddingTop & inside);
11338 right = left + size;
11339 bottom = scrollY + height - (mUserPaddingBottom & inside);
11340 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11341 if (invalidate) {
11342 invalidate(left, top, right, bottom);
11343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011344 }
11345 }
11346 }
11347 }
Romain Guy8506ab42009-06-11 17:35:47 -070011348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011350 * 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 -080011351 * FastScroller is visible.
11352 * @return whether to temporarily hide the vertical scrollbar
11353 * @hide
11354 */
11355 protected boolean isVerticalScrollBarHidden() {
11356 return false;
11357 }
11358
11359 /**
11360 * <p>Draw the horizontal scrollbar if
11361 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 * @param canvas the canvas on which to draw the scrollbar
11364 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011365 *
11366 * @see #isHorizontalScrollBarEnabled()
11367 * @see #computeHorizontalScrollRange()
11368 * @see #computeHorizontalScrollExtent()
11369 * @see #computeHorizontalScrollOffset()
11370 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011371 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011372 */
Romain Guy8fb95422010-08-17 18:38:51 -070011373 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11374 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011375 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011376 scrollBar.draw(canvas);
11377 }
Mike Reede8853fc2009-09-04 14:01:48 -040011378
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011379 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11381 * returns true.</p>
11382 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 * @param canvas the canvas on which to draw the scrollbar
11384 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 *
11386 * @see #isVerticalScrollBarEnabled()
11387 * @see #computeVerticalScrollRange()
11388 * @see #computeVerticalScrollExtent()
11389 * @see #computeVerticalScrollOffset()
11390 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011391 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011392 */
Romain Guy8fb95422010-08-17 18:38:51 -070011393 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11394 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011395 scrollBar.setBounds(l, t, r, b);
11396 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011397 }
11398
11399 /**
11400 * Implement this to do your drawing.
11401 *
11402 * @param canvas the canvas on which the background will be drawn
11403 */
11404 protected void onDraw(Canvas canvas) {
11405 }
11406
11407 /*
11408 * Caller is responsible for calling requestLayout if necessary.
11409 * (This allows addViewInLayout to not request a new layout.)
11410 */
11411 void assignParent(ViewParent parent) {
11412 if (mParent == null) {
11413 mParent = parent;
11414 } else if (parent == null) {
11415 mParent = null;
11416 } else {
11417 throw new RuntimeException("view " + this + " being added, but"
11418 + " it already has a parent");
11419 }
11420 }
11421
11422 /**
11423 * This is called when the view is attached to a window. At this point it
11424 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011425 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11426 * however it may be called any time before the first onDraw -- including
11427 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011428 *
11429 * @see #onDetachedFromWindow()
11430 */
11431 protected void onAttachedToWindow() {
11432 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
11433 mParent.requestTransparentRegion(this);
11434 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011435
Adam Powell8568c3a2010-04-19 14:26:11 -070011436 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
11437 initialAwakenScrollBars();
11438 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
11439 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011440
Chet Haasea9b61ac2010-12-20 07:40:25 -080011441 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011442
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070011443 // Order is important here: LayoutDirection MUST be resolved before Padding
11444 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011445 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011446 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011447 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011448 resolveTextAlignment();
Romain Guy2a0f2282012-05-08 14:43:12 -070011449
Svetoslav Ganov42138042012-03-20 11:51:39 -070011450 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011451 if (isFocused()) {
11452 InputMethodManager imm = InputMethodManager.peekInstance();
11453 imm.focusIn(this);
11454 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011455
11456 if (mAttachInfo != null && mDisplayList != null) {
11457 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11458 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011459 }
Cibu Johny86666632010-02-22 13:01:02 -080011460
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011461 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011462 * @see #onScreenStateChanged(int)
11463 */
11464 void dispatchScreenStateChanged(int screenState) {
11465 onScreenStateChanged(screenState);
11466 }
11467
11468 /**
11469 * This method is called whenever the state of the screen this view is
11470 * attached to changes. A state change will usually occurs when the screen
11471 * turns on or off (whether it happens automatically or the user does it
11472 * manually.)
11473 *
11474 * @param screenState The new state of the screen. Can be either
11475 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11476 */
11477 public void onScreenStateChanged(int screenState) {
11478 }
11479
11480 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011481 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11482 */
11483 private boolean hasRtlSupport() {
11484 return mContext.getApplicationInfo().hasRtlSupport();
11485 }
11486
11487 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011488 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11489 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011490 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011491 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011492 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011493 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011494 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011495
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011496 if (hasRtlSupport()) {
11497 // Set resolved depending on layout direction
11498 switch (getLayoutDirection()) {
11499 case LAYOUT_DIRECTION_INHERIT:
11500 // If this is root view, no need to look at parent's layout dir.
11501 if (canResolveLayoutDirection()) {
11502 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011503
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011504 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11505 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11506 }
11507 } else {
11508 // Nothing to do, LTR by default
11509 }
11510 break;
11511 case LAYOUT_DIRECTION_RTL:
11512 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11513 break;
11514 case LAYOUT_DIRECTION_LOCALE:
11515 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011516 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
11517 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011518 break;
11519 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011520 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011521 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011522 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011523
11524 // Set to resolved
11525 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011526 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011527 // Resolve padding
11528 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011529 }
11530
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011531 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011532 * Called when layout direction has been resolved.
11533 *
11534 * The default implementation does nothing.
11535 */
11536 public void onResolvedLayoutDirectionChanged() {
11537 }
11538
11539 /**
11540 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011541 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011542 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011543 // If the user specified the absolute padding (either with android:padding or
11544 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11545 // use the default padding or the padding from the background drawable
11546 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011547 int resolvedLayoutDirection = getResolvedLayoutDirection();
11548 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011549 case LAYOUT_DIRECTION_RTL:
11550 // Start user padding override Right user padding. Otherwise, if Right user
11551 // padding is not defined, use the default Right padding. If Right user padding
11552 // is defined, just use it.
11553 if (mUserPaddingStart >= 0) {
11554 mUserPaddingRight = mUserPaddingStart;
11555 } else if (mUserPaddingRight < 0) {
11556 mUserPaddingRight = mPaddingRight;
11557 }
11558 if (mUserPaddingEnd >= 0) {
11559 mUserPaddingLeft = mUserPaddingEnd;
11560 } else if (mUserPaddingLeft < 0) {
11561 mUserPaddingLeft = mPaddingLeft;
11562 }
11563 break;
11564 case LAYOUT_DIRECTION_LTR:
11565 default:
11566 // Start user padding override Left user padding. Otherwise, if Left user
11567 // padding is not defined, use the default left padding. If Left user padding
11568 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011569 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011570 mUserPaddingLeft = mUserPaddingStart;
11571 } else if (mUserPaddingLeft < 0) {
11572 mUserPaddingLeft = mPaddingLeft;
11573 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011574 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011575 mUserPaddingRight = mUserPaddingEnd;
11576 } else if (mUserPaddingRight < 0) {
11577 mUserPaddingRight = mPaddingRight;
11578 }
11579 }
11580
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011581 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11582
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011583 if(isPaddingRelative()) {
11584 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11585 } else {
11586 recomputePadding();
11587 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011588 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011589 }
11590
11591 /**
11592 * Resolve padding depending on the layout direction. Subclasses that care about
11593 * padding resolution should override this method. The default implementation does
11594 * nothing.
11595 *
11596 * @param layoutDirection the direction of the layout
11597 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011598 * @see {@link #LAYOUT_DIRECTION_LTR}
11599 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011600 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011601 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011602 }
11603
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011604 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011605 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011606 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011607 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011608 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011609 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011610 switch (getLayoutDirection()) {
11611 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011612 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011613 default:
11614 return true;
11615 }
11616 }
11617
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011618 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011619 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11620 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011621 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011622 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011623 // Reset the current resolved bits
11624 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011625 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011626 // Reset also the text direction
11627 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011628 }
11629
11630 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011631 * Called during reset of resolved layout direction.
11632 *
11633 * Subclasses need to override this method to clear cached information that depends on the
11634 * resolved layout direction, or to inform child views that inherit their layout direction.
11635 *
11636 * The default implementation does nothing.
11637 */
11638 public void onResolvedLayoutDirectionReset() {
11639 }
11640
11641 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011642 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011643 *
11644 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011645 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011646 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011647 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011648 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 }
11650
11651 /**
11652 * This is called when the view is detached from a window. At this point it
11653 * no longer has a surface for drawing.
11654 *
11655 * @see #onAttachedToWindow()
11656 */
11657 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011658 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011659
Romain Guya440b002010-02-24 15:57:54 -080011660 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011661 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011662 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011663 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011666
Romain Guya998dff2012-03-23 18:58:36 -070011667 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011668
11669 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011670 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011671 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011672 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011673 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011674 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011675 // Should never happen
11676 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011677 }
11678
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011679 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011680
11681 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011682 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011683 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 }
11685
11686 /**
11687 * @return The number of times this view has been attached to a window
11688 */
11689 protected int getWindowAttachCount() {
11690 return mWindowAttachCount;
11691 }
11692
11693 /**
11694 * Retrieve a unique token identifying the window this view is attached to.
11695 * @return Return the window's token for use in
11696 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11697 */
11698 public IBinder getWindowToken() {
11699 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11700 }
11701
11702 /**
11703 * Retrieve a unique token identifying the top-level "real" window of
11704 * the window that this view is attached to. That is, this is like
11705 * {@link #getWindowToken}, except if the window this view in is a panel
11706 * window (attached to another containing window), then the token of
11707 * the containing window is returned instead.
11708 *
11709 * @return Returns the associated window token, either
11710 * {@link #getWindowToken()} or the containing window's token.
11711 */
11712 public IBinder getApplicationWindowToken() {
11713 AttachInfo ai = mAttachInfo;
11714 if (ai != null) {
11715 IBinder appWindowToken = ai.mPanelParentWindowToken;
11716 if (appWindowToken == null) {
11717 appWindowToken = ai.mWindowToken;
11718 }
11719 return appWindowToken;
11720 }
11721 return null;
11722 }
11723
11724 /**
11725 * Retrieve private session object this view hierarchy is using to
11726 * communicate with the window manager.
11727 * @return the session object to communicate with the window manager
11728 */
11729 /*package*/ IWindowSession getWindowSession() {
11730 return mAttachInfo != null ? mAttachInfo.mSession : null;
11731 }
11732
11733 /**
11734 * @param info the {@link android.view.View.AttachInfo} to associated with
11735 * this view
11736 */
11737 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11738 //System.out.println("Attached! " + this);
11739 mAttachInfo = info;
11740 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011741 // We will need to evaluate the drawable state at least once.
11742 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011743 if (mFloatingTreeObserver != null) {
11744 info.mTreeObserver.merge(mFloatingTreeObserver);
11745 mFloatingTreeObserver = null;
11746 }
11747 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11748 mAttachInfo.mScrollContainers.add(this);
11749 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11750 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011751 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011753
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011754 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011755 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011756 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011757 if (listeners != null && listeners.size() > 0) {
11758 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11759 // perform the dispatching. The iterator is a safe guard against listeners that
11760 // could mutate the list by calling the various add/remove methods. This prevents
11761 // the array from being modified while we iterate it.
11762 for (OnAttachStateChangeListener listener : listeners) {
11763 listener.onViewAttachedToWindow(this);
11764 }
11765 }
11766
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 int vis = info.mWindowVisibility;
11768 if (vis != GONE) {
11769 onWindowVisibilityChanged(vis);
11770 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011771 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11772 // If nobody has evaluated the drawable state yet, then do it now.
11773 refreshDrawableState();
11774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775 }
11776
11777 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 AttachInfo info = mAttachInfo;
11779 if (info != null) {
11780 int vis = info.mWindowVisibility;
11781 if (vis != GONE) {
11782 onWindowVisibilityChanged(GONE);
11783 }
11784 }
11785
11786 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011787
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011788 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011789 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011790 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011791 if (listeners != null && listeners.size() > 0) {
11792 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11793 // perform the dispatching. The iterator is a safe guard against listeners that
11794 // could mutate the list by calling the various add/remove methods. This prevents
11795 // the array from being modified while we iterate it.
11796 for (OnAttachStateChangeListener listener : listeners) {
11797 listener.onViewDetachedFromWindow(this);
11798 }
11799 }
11800
Romain Guy01d5edc2011-01-28 11:28:53 -080011801 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011802 mAttachInfo.mScrollContainers.remove(this);
11803 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11804 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011806 mAttachInfo = null;
11807 }
11808
11809 /**
11810 * Store this view hierarchy's frozen state into the given container.
11811 *
11812 * @param container The SparseArray in which to save the view's state.
11813 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011814 * @see #restoreHierarchyState(android.util.SparseArray)
11815 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11816 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011817 */
11818 public void saveHierarchyState(SparseArray<Parcelable> container) {
11819 dispatchSaveInstanceState(container);
11820 }
11821
11822 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011823 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11824 * this view and its children. May be overridden to modify how freezing happens to a
11825 * 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 -080011826 *
11827 * @param container The SparseArray in which to save the view's state.
11828 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011829 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11830 * @see #saveHierarchyState(android.util.SparseArray)
11831 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011832 */
11833 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11834 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11835 mPrivateFlags &= ~SAVE_STATE_CALLED;
11836 Parcelable state = onSaveInstanceState();
11837 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11838 throw new IllegalStateException(
11839 "Derived class did not call super.onSaveInstanceState()");
11840 }
11841 if (state != null) {
11842 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11843 // + ": " + state);
11844 container.put(mID, state);
11845 }
11846 }
11847 }
11848
11849 /**
11850 * Hook allowing a view to generate a representation of its internal state
11851 * that can later be used to create a new instance with that same state.
11852 * This state should only contain information that is not persistent or can
11853 * not be reconstructed later. For example, you will never store your
11854 * current position on screen because that will be computed again when a
11855 * new instance of the view is placed in its view hierarchy.
11856 * <p>
11857 * Some examples of things you may store here: the current cursor position
11858 * in a text view (but usually not the text itself since that is stored in a
11859 * content provider or other persistent storage), the currently selected
11860 * item in a list view.
11861 *
11862 * @return Returns a Parcelable object containing the view's current dynamic
11863 * state, or null if there is nothing interesting to save. The
11864 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011865 * @see #onRestoreInstanceState(android.os.Parcelable)
11866 * @see #saveHierarchyState(android.util.SparseArray)
11867 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 * @see #setSaveEnabled(boolean)
11869 */
11870 protected Parcelable onSaveInstanceState() {
11871 mPrivateFlags |= SAVE_STATE_CALLED;
11872 return BaseSavedState.EMPTY_STATE;
11873 }
11874
11875 /**
11876 * Restore this view hierarchy's frozen state from the given container.
11877 *
11878 * @param container The SparseArray which holds previously frozen states.
11879 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011880 * @see #saveHierarchyState(android.util.SparseArray)
11881 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11882 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011883 */
11884 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11885 dispatchRestoreInstanceState(container);
11886 }
11887
11888 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011889 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11890 * state for this view and its children. May be overridden to modify how restoring
11891 * happens to a view's children; for example, some views may want to not store state
11892 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011893 *
11894 * @param container The SparseArray which holds previously saved state.
11895 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011896 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11897 * @see #restoreHierarchyState(android.util.SparseArray)
11898 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011899 */
11900 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11901 if (mID != NO_ID) {
11902 Parcelable state = container.get(mID);
11903 if (state != null) {
11904 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11905 // + ": " + state);
11906 mPrivateFlags &= ~SAVE_STATE_CALLED;
11907 onRestoreInstanceState(state);
11908 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11909 throw new IllegalStateException(
11910 "Derived class did not call super.onRestoreInstanceState()");
11911 }
11912 }
11913 }
11914 }
11915
11916 /**
11917 * Hook allowing a view to re-apply a representation of its internal state that had previously
11918 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11919 * null state.
11920 *
11921 * @param state The frozen state that had previously been returned by
11922 * {@link #onSaveInstanceState}.
11923 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011924 * @see #onSaveInstanceState()
11925 * @see #restoreHierarchyState(android.util.SparseArray)
11926 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011927 */
11928 protected void onRestoreInstanceState(Parcelable state) {
11929 mPrivateFlags |= SAVE_STATE_CALLED;
11930 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011931 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11932 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011933 + "when two views of different type have the same id in the same hierarchy. "
11934 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011935 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011936 }
11937 }
11938
11939 /**
11940 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11941 *
11942 * @return the drawing start time in milliseconds
11943 */
11944 public long getDrawingTime() {
11945 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11946 }
11947
11948 /**
11949 * <p>Enables or disables the duplication of the parent's state into this view. When
11950 * duplication is enabled, this view gets its drawable state from its parent rather
11951 * than from its own internal properties.</p>
11952 *
11953 * <p>Note: in the current implementation, setting this property to true after the
11954 * view was added to a ViewGroup might have no effect at all. This property should
11955 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11956 *
11957 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11958 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011959 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011960 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11961 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011962 *
11963 * @param enabled True to enable duplication of the parent's drawable state, false
11964 * to disable it.
11965 *
11966 * @see #getDrawableState()
11967 * @see #isDuplicateParentStateEnabled()
11968 */
11969 public void setDuplicateParentStateEnabled(boolean enabled) {
11970 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11971 }
11972
11973 /**
11974 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11975 *
11976 * @return True if this view's drawable state is duplicated from the parent,
11977 * false otherwise
11978 *
11979 * @see #getDrawableState()
11980 * @see #setDuplicateParentStateEnabled(boolean)
11981 */
11982 public boolean isDuplicateParentStateEnabled() {
11983 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11984 }
11985
11986 /**
Romain Guy171c5922011-01-06 10:04:23 -080011987 * <p>Specifies the type of layer backing this view. The layer can be
11988 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11989 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011990 *
Romain Guy171c5922011-01-06 10:04:23 -080011991 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11992 * instance that controls how the layer is composed on screen. The following
11993 * properties of the paint are taken into account when composing the layer:</p>
11994 * <ul>
11995 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11996 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11997 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11998 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011999 *
Romain Guy171c5922011-01-06 10:04:23 -080012000 * <p>If this view has an alpha value set to < 1.0 by calling
12001 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12002 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12003 * equivalent to setting a hardware layer on this view and providing a paint with
12004 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080012005 *
Romain Guy171c5922011-01-06 10:04:23 -080012006 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12007 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12008 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012009 *
Romain Guy171c5922011-01-06 10:04:23 -080012010 * @param layerType The ype of layer to use with this view, must be one of
12011 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12012 * {@link #LAYER_TYPE_HARDWARE}
12013 * @param paint The paint used to compose the layer. This argument is optional
12014 * and can be null. It is ignored when the layer type is
12015 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012016 *
12017 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012018 * @see #LAYER_TYPE_NONE
12019 * @see #LAYER_TYPE_SOFTWARE
12020 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012021 * @see #setAlpha(float)
12022 *
Romain Guy171c5922011-01-06 10:04:23 -080012023 * @attr ref android.R.styleable#View_layerType
12024 */
12025 public void setLayerType(int layerType, Paint paint) {
12026 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012027 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012028 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12029 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012030
Romain Guyd6cd5722011-01-17 14:42:41 -080012031 if (layerType == mLayerType) {
12032 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12033 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012034 invalidateParentCaches();
12035 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012036 }
12037 return;
12038 }
Romain Guy171c5922011-01-06 10:04:23 -080012039
12040 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012041 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012042 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012043 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012044 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012045 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012046 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012047 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012048 default:
12049 break;
Romain Guy171c5922011-01-06 10:04:23 -080012050 }
12051
12052 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012053 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12054 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12055 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012056
Romain Guy0fd89bf2011-01-26 15:41:30 -080012057 invalidateParentCaches();
12058 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012059 }
12060
12061 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012062 * Indicates whether this view has a static layer. A view with layer type
12063 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12064 * dynamic.
12065 */
12066 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012067 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012068 }
12069
12070 /**
Romain Guy171c5922011-01-06 10:04:23 -080012071 * Indicates what type of layer is currently associated with this view. By default
12072 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12073 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12074 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012075 *
Romain Guy171c5922011-01-06 10:04:23 -080012076 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12077 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012078 *
12079 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012080 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012081 * @see #LAYER_TYPE_NONE
12082 * @see #LAYER_TYPE_SOFTWARE
12083 * @see #LAYER_TYPE_HARDWARE
12084 */
12085 public int getLayerType() {
12086 return mLayerType;
12087 }
Joe Malin32736f02011-01-19 16:14:20 -080012088
Romain Guy6c319ca2011-01-11 14:29:25 -080012089 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012090 * Forces this view's layer to be created and this view to be rendered
12091 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12092 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012093 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012094 * This method can for instance be used to render a view into its layer before
12095 * starting an animation. If this view is complex, rendering into the layer
12096 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012097 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012098 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012099 *
12100 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012101 */
12102 public void buildLayer() {
12103 if (mLayerType == LAYER_TYPE_NONE) return;
12104
12105 if (mAttachInfo == null) {
12106 throw new IllegalStateException("This view must be attached to a window first");
12107 }
12108
12109 switch (mLayerType) {
12110 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012111 if (mAttachInfo.mHardwareRenderer != null &&
12112 mAttachInfo.mHardwareRenderer.isEnabled() &&
12113 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012114 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012115 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012116 break;
12117 case LAYER_TYPE_SOFTWARE:
12118 buildDrawingCache(true);
12119 break;
12120 }
12121 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012122
Romain Guy9c4b79a2011-11-10 19:23:58 -080012123 // Make sure the HardwareRenderer.validate() was invoked before calling this method
12124 void flushLayer() {
12125 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
12126 mHardwareLayer.flush();
12127 }
12128 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012129
12130 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012131 * <p>Returns a hardware layer that can be used to draw this view again
12132 * without executing its draw method.</p>
12133 *
12134 * @return A HardwareLayer ready to render, or null if an error occurred.
12135 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012136 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012137 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12138 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012139 return null;
12140 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012141
Romain Guy9c4b79a2011-11-10 19:23:58 -080012142 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012143
12144 final int width = mRight - mLeft;
12145 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012146
Romain Guy6c319ca2011-01-11 14:29:25 -080012147 if (width == 0 || height == 0) {
12148 return null;
12149 }
12150
12151 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
12152 if (mHardwareLayer == null) {
12153 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12154 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012155 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012156 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12157 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070012158 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080012159 }
12160
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012161 // The layer is not valid if the underlying GPU resources cannot be allocated
12162 if (!mHardwareLayer.isValid()) {
12163 return null;
12164 }
12165
Chet Haasea1cff502012-02-21 13:43:44 -080012166 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012167 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012168 }
12169
12170 return mHardwareLayer;
12171 }
Romain Guy171c5922011-01-06 10:04:23 -080012172
Romain Guy589b0bb2011-10-10 13:57:47 -070012173 /**
12174 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012175 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012176 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012177 *
12178 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012179 * @see #LAYER_TYPE_HARDWARE
12180 */
Romain Guya998dff2012-03-23 18:58:36 -070012181 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012182 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012183 AttachInfo info = mAttachInfo;
12184 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012185 info.mHardwareRenderer.isEnabled() &&
12186 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012187 mHardwareLayer.destroy();
12188 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012189
Romain Guy9c4b79a2011-11-10 19:23:58 -080012190 invalidate(true);
12191 invalidateParentCaches();
12192 }
Romain Guy65b345f2011-07-27 18:51:50 -070012193 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012194 }
Romain Guy65b345f2011-07-27 18:51:50 -070012195 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012196 }
12197
Romain Guy171c5922011-01-06 10:04:23 -080012198 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012199 * Destroys all hardware rendering resources. This method is invoked
12200 * when the system needs to reclaim resources. Upon execution of this
12201 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012202 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012203 * Note: you <strong>must</strong> call
12204 * <code>super.destroyHardwareResources()</code> when overriding
12205 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012206 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012207 * @hide
12208 */
12209 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012210 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012211 }
12212
12213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012214 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12215 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12216 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12217 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12218 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12219 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012220 *
Romain Guy171c5922011-01-06 10:04:23 -080012221 * <p>Enabling the drawing cache is similar to
12222 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012223 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12224 * drawing cache has no effect on rendering because the system uses a different mechanism
12225 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12226 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12227 * for information on how to enable software and hardware layers.</p>
12228 *
12229 * <p>This API can be used to manually generate
12230 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12231 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012232 *
12233 * @param enabled true to enable the drawing cache, false otherwise
12234 *
12235 * @see #isDrawingCacheEnabled()
12236 * @see #getDrawingCache()
12237 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012238 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012239 */
12240 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012241 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012242 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12243 }
12244
12245 /**
12246 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12247 *
12248 * @return true if the drawing cache is enabled
12249 *
12250 * @see #setDrawingCacheEnabled(boolean)
12251 * @see #getDrawingCache()
12252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012253 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 public boolean isDrawingCacheEnabled() {
12255 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12256 }
12257
12258 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012259 * Debugging utility which recursively outputs the dirty state of a view and its
12260 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012261 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012262 * @hide
12263 */
Romain Guy676b1732011-02-14 14:45:33 -080012264 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012265 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
12266 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
12267 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
12268 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
12269 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
12270 if (clear) {
12271 mPrivateFlags &= clearMask;
12272 }
12273 if (this instanceof ViewGroup) {
12274 ViewGroup parent = (ViewGroup) this;
12275 final int count = parent.getChildCount();
12276 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012277 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012278 child.outputDirtyFlags(indent + " ", clear, clearMask);
12279 }
12280 }
12281 }
12282
12283 /**
12284 * This method is used by ViewGroup to cause its children to restore or recreate their
12285 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12286 * to recreate its own display list, which would happen if it went through the normal
12287 * draw/dispatchDraw mechanisms.
12288 *
12289 * @hide
12290 */
12291 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012292
12293 /**
12294 * A view that is not attached or hardware accelerated cannot create a display list.
12295 * This method checks these conditions and returns the appropriate result.
12296 *
12297 * @return true if view has the ability to create a display list, false otherwise.
12298 *
12299 * @hide
12300 */
12301 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012302 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012303 }
Joe Malin32736f02011-01-19 16:14:20 -080012304
Chet Haasedaf98e92011-01-10 14:10:36 -080012305 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012306 * @return The HardwareRenderer associated with that view or null if hardware rendering
12307 * is not supported or this this has not been attached to a window.
12308 *
12309 * @hide
12310 */
12311 public HardwareRenderer getHardwareRenderer() {
12312 if (mAttachInfo != null) {
12313 return mAttachInfo.mHardwareRenderer;
12314 }
12315 return null;
12316 }
12317
12318 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012319 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12320 * Otherwise, the same display list will be returned (after having been rendered into
12321 * along the way, depending on the invalidation state of the view).
12322 *
12323 * @param displayList The previous version of this displayList, could be null.
12324 * @param isLayer Whether the requester of the display list is a layer. If so,
12325 * the view will avoid creating a layer inside the resulting display list.
12326 * @return A new or reused DisplayList object.
12327 */
12328 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12329 if (!canHaveDisplayList()) {
12330 return null;
12331 }
12332
12333 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
12334 displayList == null || !displayList.isValid() ||
12335 (!isLayer && mRecreateDisplayList))) {
12336 // Don't need to recreate the display list, just need to tell our
12337 // children to restore/recreate theirs
12338 if (displayList != null && displayList.isValid() &&
12339 !isLayer && !mRecreateDisplayList) {
12340 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12341 mPrivateFlags &= ~DIRTY_MASK;
12342 dispatchGetDisplayList();
12343
12344 return displayList;
12345 }
12346
12347 if (!isLayer) {
12348 // If we got here, we're recreating it. Mark it as such to ensure that
12349 // we copy in child display lists into ours in drawChild()
12350 mRecreateDisplayList = true;
12351 }
12352 if (displayList == null) {
12353 final String name = getClass().getSimpleName();
12354 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12355 // If we're creating a new display list, make sure our parent gets invalidated
12356 // since they will need to recreate their display list to account for this
12357 // new child display list.
12358 invalidateParentCaches();
12359 }
12360
12361 boolean caching = false;
12362 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012363 int width = mRight - mLeft;
12364 int height = mBottom - mTop;
12365
12366 try {
12367 canvas.setViewport(width, height);
12368 // The dirty rect should always be null for a display list
12369 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012370 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012371 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012372 if (layerType == LAYER_TYPE_HARDWARE) {
12373 final HardwareLayer layer = getHardwareLayer();
12374 if (layer != null && layer.isValid()) {
12375 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12376 } else {
12377 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12378 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12379 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12380 }
12381 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012382 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012383 buildDrawingCache(true);
12384 Bitmap cache = getDrawingCache(true);
12385 if (cache != null) {
12386 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12387 caching = true;
12388 }
Chet Haasea1cff502012-02-21 13:43:44 -080012389 }
Chet Haasea1cff502012-02-21 13:43:44 -080012390 } else {
12391
12392 computeScroll();
12393
Chet Haasea1cff502012-02-21 13:43:44 -080012394 canvas.translate(-mScrollX, -mScrollY);
12395 if (!isLayer) {
12396 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12397 mPrivateFlags &= ~DIRTY_MASK;
12398 }
12399
12400 // Fast path for layouts with no backgrounds
12401 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12402 dispatchDraw(canvas);
12403 } else {
12404 draw(canvas);
12405 }
12406 }
12407 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012408 canvas.onPostDraw();
12409
12410 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012411 displayList.setCaching(caching);
12412 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012413 displayList.setLeftTopRightBottom(0, 0, width, height);
12414 } else {
12415 setDisplayListProperties(displayList);
12416 }
12417 }
12418 } else if (!isLayer) {
12419 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
12420 mPrivateFlags &= ~DIRTY_MASK;
12421 }
12422
12423 return displayList;
12424 }
12425
12426 /**
12427 * Get the DisplayList for the HardwareLayer
12428 *
12429 * @param layer The HardwareLayer whose DisplayList we want
12430 * @return A DisplayList fopr the specified HardwareLayer
12431 */
12432 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12433 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12434 layer.setDisplayList(displayList);
12435 return displayList;
12436 }
12437
12438
12439 /**
Romain Guyb051e892010-09-28 19:09:36 -070012440 * <p>Returns a display list that can be used to draw this view again
12441 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012442 *
Romain Guyb051e892010-09-28 19:09:36 -070012443 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012444 *
12445 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012446 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012447 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012448 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012449 return mDisplayList;
12450 }
12451
Romain Guy38c2ece2012-05-24 14:20:56 -070012452 private void clearDisplayList() {
12453 if (mDisplayList != null) {
12454 mDisplayList.invalidate();
12455 mDisplayList.clear();
12456 }
12457 }
12458
Romain Guyb051e892010-09-28 19:09:36 -070012459 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012460 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012461 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012462 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012463 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012464 * @see #getDrawingCache(boolean)
12465 */
12466 public Bitmap getDrawingCache() {
12467 return getDrawingCache(false);
12468 }
12469
12470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12472 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12473 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12474 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12475 * request the drawing cache by calling this method and draw it on screen if the
12476 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012477 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012478 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12479 * this method will create a bitmap of the same size as this view. Because this bitmap
12480 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12481 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12482 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12483 * size than the view. This implies that your application must be able to handle this
12484 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012485 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012486 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12487 * the current density of the screen when the application is in compatibility
12488 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012490 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012491 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012492 * @see #setDrawingCacheEnabled(boolean)
12493 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012494 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 * @see #destroyDrawingCache()
12496 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012497 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12499 return null;
12500 }
12501 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012502 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012503 }
Romain Guy02890fd2010-08-06 17:58:44 -070012504 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012505 }
12506
12507 /**
12508 * <p>Frees the resources used by the drawing cache. If you call
12509 * {@link #buildDrawingCache()} manually without calling
12510 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12511 * should cleanup the cache with this method afterwards.</p>
12512 *
12513 * @see #setDrawingCacheEnabled(boolean)
12514 * @see #buildDrawingCache()
12515 * @see #getDrawingCache()
12516 */
12517 public void destroyDrawingCache() {
12518 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012519 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012520 mDrawingCache = null;
12521 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012522 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012523 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012524 mUnscaledDrawingCache = null;
12525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 }
12527
12528 /**
12529 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012530 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 * view will always be drawn on top of a solid color.
12532 *
12533 * @param color The background color to use for the drawing cache's bitmap
12534 *
12535 * @see #setDrawingCacheEnabled(boolean)
12536 * @see #buildDrawingCache()
12537 * @see #getDrawingCache()
12538 */
12539 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012540 if (color != mDrawingCacheBackgroundColor) {
12541 mDrawingCacheBackgroundColor = color;
12542 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 }
12545
12546 /**
12547 * @see #setDrawingCacheBackgroundColor(int)
12548 *
12549 * @return The background color to used for the drawing cache's bitmap
12550 */
12551 public int getDrawingCacheBackgroundColor() {
12552 return mDrawingCacheBackgroundColor;
12553 }
12554
12555 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012556 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012557 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012558 * @see #buildDrawingCache(boolean)
12559 */
12560 public void buildDrawingCache() {
12561 buildDrawingCache(false);
12562 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012563
Romain Guyfbd8f692009-06-26 14:51:58 -070012564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012565 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12566 *
12567 * <p>If you call {@link #buildDrawingCache()} manually without calling
12568 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12569 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012570 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012571 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12572 * this method will create a bitmap of the same size as this view. Because this bitmap
12573 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12574 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12575 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12576 * size than the view. This implies that your application must be able to handle this
12577 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012578 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012579 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12580 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012581 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012582 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012583 *
12584 * @see #getDrawingCache()
12585 * @see #destroyDrawingCache()
12586 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012587 public void buildDrawingCache(boolean autoScale) {
12588 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012589 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012590 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591
Romain Guy8506ab42009-06-11 17:35:47 -070012592 int width = mRight - mLeft;
12593 int height = mBottom - mTop;
12594
12595 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012596 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012597
Romain Guye1123222009-06-29 14:24:56 -070012598 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012599 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12600 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012602
12603 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012604 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012605 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012606
12607 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012608 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012609 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12611 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012612 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012613 return;
12614 }
12615
12616 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012617 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012618
12619 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012620 Bitmap.Config quality;
12621 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012622 // Never pick ARGB_4444 because it looks awful
12623 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012624 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12625 case DRAWING_CACHE_QUALITY_AUTO:
12626 quality = Bitmap.Config.ARGB_8888;
12627 break;
12628 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012629 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012630 break;
12631 case DRAWING_CACHE_QUALITY_HIGH:
12632 quality = Bitmap.Config.ARGB_8888;
12633 break;
12634 default:
12635 quality = Bitmap.Config.ARGB_8888;
12636 break;
12637 }
12638 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012639 // Optimization for translucent windows
12640 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012641 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012642 }
12643
12644 // Try to cleanup memory
12645 if (bitmap != null) bitmap.recycle();
12646
12647 try {
12648 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012649 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012650 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012651 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012652 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012653 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012654 }
Adam Powell26153a32010-11-08 15:22:27 -080012655 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012656 } catch (OutOfMemoryError e) {
12657 // If there is not enough memory to create the bitmap cache, just
12658 // ignore the issue as bitmap caches are not required to draw the
12659 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012660 if (autoScale) {
12661 mDrawingCache = null;
12662 } else {
12663 mUnscaledDrawingCache = null;
12664 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012665 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012666 return;
12667 }
12668
12669 clear = drawingCacheBackgroundColor != 0;
12670 }
12671
12672 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 if (attachInfo != null) {
12674 canvas = attachInfo.mCanvas;
12675 if (canvas == null) {
12676 canvas = new Canvas();
12677 }
12678 canvas.setBitmap(bitmap);
12679 // Temporarily clobber the cached Canvas in case one of our children
12680 // is also using a drawing cache. Without this, the children would
12681 // steal the canvas by attaching their own bitmap to it and bad, bad
12682 // thing would happen (invisible views, corrupted drawings, etc.)
12683 attachInfo.mCanvas = null;
12684 } else {
12685 // This case should hopefully never or seldom happen
12686 canvas = new Canvas(bitmap);
12687 }
12688
12689 if (clear) {
12690 bitmap.eraseColor(drawingCacheBackgroundColor);
12691 }
12692
12693 computeScroll();
12694 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012695
Romain Guye1123222009-06-29 14:24:56 -070012696 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012697 final float scale = attachInfo.mApplicationScale;
12698 canvas.scale(scale, scale);
12699 }
Joe Malin32736f02011-01-19 16:14:20 -080012700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012701 canvas.translate(-mScrollX, -mScrollY);
12702
Romain Guy5bcdff42009-05-14 21:27:18 -070012703 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012704 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12705 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012706 mPrivateFlags |= DRAWING_CACHE_VALID;
12707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012708
12709 // Fast path for layouts with no backgrounds
12710 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -070012711 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 dispatchDraw(canvas);
12713 } else {
12714 draw(canvas);
12715 }
12716
12717 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012718 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012719
12720 if (attachInfo != null) {
12721 // Restore the cached Canvas for our siblings
12722 attachInfo.mCanvas = canvas;
12723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 }
12725 }
12726
12727 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012728 * Create a snapshot of the view into a bitmap. We should probably make
12729 * some form of this public, but should think about the API.
12730 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012731 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012732 int width = mRight - mLeft;
12733 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012734
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012735 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012736 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012737 width = (int) ((width * scale) + 0.5f);
12738 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012739
Romain Guy8c11e312009-09-14 15:15:30 -070012740 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012741 if (bitmap == null) {
12742 throw new OutOfMemoryError();
12743 }
12744
Romain Guyc529d8d2011-09-06 15:01:39 -070012745 Resources resources = getResources();
12746 if (resources != null) {
12747 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12748 }
Joe Malin32736f02011-01-19 16:14:20 -080012749
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012750 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012751 if (attachInfo != null) {
12752 canvas = attachInfo.mCanvas;
12753 if (canvas == null) {
12754 canvas = new Canvas();
12755 }
12756 canvas.setBitmap(bitmap);
12757 // Temporarily clobber the cached Canvas in case one of our children
12758 // is also using a drawing cache. Without this, the children would
12759 // steal the canvas by attaching their own bitmap to it and bad, bad
12760 // things would happen (invisible views, corrupted drawings, etc.)
12761 attachInfo.mCanvas = null;
12762 } else {
12763 // This case should hopefully never or seldom happen
12764 canvas = new Canvas(bitmap);
12765 }
12766
Romain Guy5bcdff42009-05-14 21:27:18 -070012767 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012768 bitmap.eraseColor(backgroundColor);
12769 }
12770
12771 computeScroll();
12772 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012773 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012774 canvas.translate(-mScrollX, -mScrollY);
12775
Romain Guy5bcdff42009-05-14 21:27:18 -070012776 // Temporarily remove the dirty mask
12777 int flags = mPrivateFlags;
12778 mPrivateFlags &= ~DIRTY_MASK;
12779
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012780 // Fast path for layouts with no backgrounds
12781 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12782 dispatchDraw(canvas);
12783 } else {
12784 draw(canvas);
12785 }
12786
Romain Guy5bcdff42009-05-14 21:27:18 -070012787 mPrivateFlags = flags;
12788
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012789 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012790 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012791
12792 if (attachInfo != null) {
12793 // Restore the cached Canvas for our siblings
12794 attachInfo.mCanvas = canvas;
12795 }
Romain Guy8506ab42009-06-11 17:35:47 -070012796
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012797 return bitmap;
12798 }
12799
12800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012801 * Indicates whether this View is currently in edit mode. A View is usually
12802 * in edit mode when displayed within a developer tool. For instance, if
12803 * this View is being drawn by a visual user interface builder, this method
12804 * should return true.
12805 *
12806 * Subclasses should check the return value of this method to provide
12807 * different behaviors if their normal behavior might interfere with the
12808 * host environment. For instance: the class spawns a thread in its
12809 * constructor, the drawing code relies on device-specific features, etc.
12810 *
12811 * This method is usually checked in the drawing code of custom widgets.
12812 *
12813 * @return True if this View is in edit mode, false otherwise.
12814 */
12815 public boolean isInEditMode() {
12816 return false;
12817 }
12818
12819 /**
12820 * If the View draws content inside its padding and enables fading edges,
12821 * it needs to support padding offsets. Padding offsets are added to the
12822 * fading edges to extend the length of the fade so that it covers pixels
12823 * drawn inside the padding.
12824 *
12825 * Subclasses of this class should override this method if they need
12826 * to draw content inside the padding.
12827 *
12828 * @return True if padding offset must be applied, false otherwise.
12829 *
12830 * @see #getLeftPaddingOffset()
12831 * @see #getRightPaddingOffset()
12832 * @see #getTopPaddingOffset()
12833 * @see #getBottomPaddingOffset()
12834 *
12835 * @since CURRENT
12836 */
12837 protected boolean isPaddingOffsetRequired() {
12838 return false;
12839 }
12840
12841 /**
12842 * Amount by which to extend the left fading region. Called only when
12843 * {@link #isPaddingOffsetRequired()} returns true.
12844 *
12845 * @return The left padding offset in pixels.
12846 *
12847 * @see #isPaddingOffsetRequired()
12848 *
12849 * @since CURRENT
12850 */
12851 protected int getLeftPaddingOffset() {
12852 return 0;
12853 }
12854
12855 /**
12856 * Amount by which to extend the right fading region. Called only when
12857 * {@link #isPaddingOffsetRequired()} returns true.
12858 *
12859 * @return The right padding offset in pixels.
12860 *
12861 * @see #isPaddingOffsetRequired()
12862 *
12863 * @since CURRENT
12864 */
12865 protected int getRightPaddingOffset() {
12866 return 0;
12867 }
12868
12869 /**
12870 * Amount by which to extend the top fading region. Called only when
12871 * {@link #isPaddingOffsetRequired()} returns true.
12872 *
12873 * @return The top padding offset in pixels.
12874 *
12875 * @see #isPaddingOffsetRequired()
12876 *
12877 * @since CURRENT
12878 */
12879 protected int getTopPaddingOffset() {
12880 return 0;
12881 }
12882
12883 /**
12884 * Amount by which to extend the bottom fading region. Called only when
12885 * {@link #isPaddingOffsetRequired()} returns true.
12886 *
12887 * @return The bottom padding offset in pixels.
12888 *
12889 * @see #isPaddingOffsetRequired()
12890 *
12891 * @since CURRENT
12892 */
12893 protected int getBottomPaddingOffset() {
12894 return 0;
12895 }
12896
12897 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012898 * @hide
12899 * @param offsetRequired
12900 */
12901 protected int getFadeTop(boolean offsetRequired) {
12902 int top = mPaddingTop;
12903 if (offsetRequired) top += getTopPaddingOffset();
12904 return top;
12905 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012906
Romain Guyf2fc4602011-07-19 15:20:03 -070012907 /**
12908 * @hide
12909 * @param offsetRequired
12910 */
12911 protected int getFadeHeight(boolean offsetRequired) {
12912 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012913 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012914 return mBottom - mTop - mPaddingBottom - padding;
12915 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012916
Romain Guyf2fc4602011-07-19 15:20:03 -070012917 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012918 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012919 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012920 *
Romain Guy2bffd262010-09-12 17:40:02 -070012921 * <p>Even if this method returns true, it does not mean that every call
12922 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12923 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012924 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012925 * window is hardware accelerated,
12926 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12927 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012928 *
Romain Guy2bffd262010-09-12 17:40:02 -070012929 * @return True if the view is attached to a window and the window is
12930 * hardware accelerated; false in any other case.
12931 */
12932 public boolean isHardwareAccelerated() {
12933 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12934 }
Joe Malin32736f02011-01-19 16:14:20 -080012935
Romain Guy2bffd262010-09-12 17:40:02 -070012936 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012937 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12938 * case of an active Animation being run on the view.
12939 */
12940 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12941 Animation a, boolean scalingRequired) {
12942 Transformation invalidationTransform;
12943 final int flags = parent.mGroupFlags;
12944 final boolean initialized = a.isInitialized();
12945 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012946 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012947 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012948 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012949 onAnimationStart();
12950 }
12951
12952 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12953 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12954 if (parent.mInvalidationTransformation == null) {
12955 parent.mInvalidationTransformation = new Transformation();
12956 }
12957 invalidationTransform = parent.mInvalidationTransformation;
12958 a.getTransformation(drawingTime, invalidationTransform, 1f);
12959 } else {
12960 invalidationTransform = parent.mChildTransformation;
12961 }
Romain Guy393a52c2012-05-22 20:21:08 -070012962
Chet Haasebcca79a2012-02-14 08:45:14 -080012963 if (more) {
12964 if (!a.willChangeBounds()) {
12965 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12966 parent.FLAG_OPTIMIZE_INVALIDATE) {
12967 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12968 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12969 // The child need to draw an animation, potentially offscreen, so
12970 // make sure we do not cancel invalidate requests
12971 parent.mPrivateFlags |= DRAW_ANIMATION;
12972 parent.invalidate(mLeft, mTop, mRight, mBottom);
12973 }
12974 } else {
12975 if (parent.mInvalidateRegion == null) {
12976 parent.mInvalidateRegion = new RectF();
12977 }
12978 final RectF region = parent.mInvalidateRegion;
12979 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12980 invalidationTransform);
12981
12982 // The child need to draw an animation, potentially offscreen, so
12983 // make sure we do not cancel invalidate requests
12984 parent.mPrivateFlags |= DRAW_ANIMATION;
12985
12986 final int left = mLeft + (int) region.left;
12987 final int top = mTop + (int) region.top;
12988 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12989 top + (int) (region.height() + .5f));
12990 }
12991 }
12992 return more;
12993 }
12994
Chet Haasea1cff502012-02-21 13:43:44 -080012995 /**
12996 * This method is called by getDisplayList() when a display list is created or re-rendered.
12997 * It sets or resets the current value of all properties on that display list (resetting is
12998 * necessary when a display list is being re-created, because we need to make sure that
12999 * previously-set transform values
13000 */
13001 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013002 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013003 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013004 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013005 if (mParent instanceof ViewGroup) {
13006 displayList.setClipChildren(
13007 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13008 }
Chet Haase9420abd2012-03-29 16:28:32 -070013009 float alpha = 1;
13010 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13011 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13012 ViewGroup parentVG = (ViewGroup) mParent;
13013 final boolean hasTransform =
13014 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13015 if (hasTransform) {
13016 Transformation transform = parentVG.mChildTransformation;
13017 final int transformType = parentVG.mChildTransformation.getTransformationType();
13018 if (transformType != Transformation.TYPE_IDENTITY) {
13019 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13020 alpha = transform.getAlpha();
13021 }
13022 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13023 displayList.setStaticMatrix(transform.getMatrix());
13024 }
13025 }
13026 }
Chet Haasea1cff502012-02-21 13:43:44 -080013027 }
13028 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013029 alpha *= mTransformationInfo.mAlpha;
13030 if (alpha < 1) {
13031 final int multipliedAlpha = (int) (255 * alpha);
13032 if (onSetAlpha(multipliedAlpha)) {
13033 alpha = 1;
13034 }
13035 }
13036 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013037 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13038 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13039 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13040 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013041 if (mTransformationInfo.mCamera == null) {
13042 mTransformationInfo.mCamera = new Camera();
13043 mTransformationInfo.matrix3D = new Matrix();
13044 }
13045 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080013046 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
13047 displayList.setPivotX(getPivotX());
13048 displayList.setPivotY(getPivotY());
13049 }
Chet Haase9420abd2012-03-29 16:28:32 -070013050 } else if (alpha < 1) {
13051 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013052 }
13053 }
13054 }
13055
Chet Haasebcca79a2012-02-14 08:45:14 -080013056 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013057 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13058 * This draw() method is an implementation detail and is not intended to be overridden or
13059 * to be called from anywhere else other than ViewGroup.drawChild().
13060 */
13061 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013062 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013063 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013064 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013065 final int flags = parent.mGroupFlags;
13066
Chet Haasea1cff502012-02-21 13:43:44 -080013067 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013068 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013069 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013070 }
13071
13072 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013073 boolean concatMatrix = false;
13074
13075 boolean scalingRequired = false;
13076 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013077 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013078
13079 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013080 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13081 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013082 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013083 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013084 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13085 } else {
13086 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13087 }
13088
Chet Haasebcca79a2012-02-14 08:45:14 -080013089 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013090 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013091 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013092 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013093 if (concatMatrix) {
Chet Haase21433372012-06-05 07:54:09 -070013094 mPrivateFlags3 |= VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013095 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013096 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013097 } else {
Chet Haase21433372012-06-05 07:54:09 -070013098 if ((mPrivateFlags3 & VIEW_IS_ANIMATING_TRANSFORM) == VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013099 mDisplayList != null) {
13100 // No longer animating: clear out old animation matrix
13101 mDisplayList.setAnimationMatrix(null);
Chet Haase21433372012-06-05 07:54:09 -070013102 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013103 }
13104 if (!useDisplayListProperties &&
13105 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13106 final boolean hasTransform =
13107 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13108 if (hasTransform) {
13109 final int transformType = parent.mChildTransformation.getTransformationType();
13110 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13111 parent.mChildTransformation : null;
13112 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13113 }
Chet Haase64a48c12012-02-13 16:33:29 -080013114 }
13115 }
13116
13117 concatMatrix |= !childHasIdentityMatrix;
13118
13119 // Sets the flag as early as possible to allow draw() implementations
13120 // to call invalidate() successfully when doing animations
13121 mPrivateFlags |= DRAWN;
13122
Chet Haasebcca79a2012-02-14 08:45:14 -080013123 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080013124 (mPrivateFlags & DRAW_ANIMATION) == 0) {
Chet Haase1a3ab172012-05-11 08:41:20 -070013125 mPrivateFlags2 |= VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013126 return more;
13127 }
Chet Haase1a3ab172012-05-11 08:41:20 -070013128 mPrivateFlags2 &= ~VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013129
13130 if (hardwareAccelerated) {
13131 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13132 // retain the flag's value temporarily in the mRecreateDisplayList flag
13133 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
13134 mPrivateFlags &= ~INVALIDATED;
13135 }
13136
13137 computeScroll();
13138
13139 final int sx = mScrollX;
13140 final int sy = mScrollY;
13141
13142 DisplayList displayList = null;
13143 Bitmap cache = null;
13144 boolean hasDisplayList = false;
13145 if (caching) {
13146 if (!hardwareAccelerated) {
13147 if (layerType != LAYER_TYPE_NONE) {
13148 layerType = LAYER_TYPE_SOFTWARE;
13149 buildDrawingCache(true);
13150 }
13151 cache = getDrawingCache(true);
13152 } else {
13153 switch (layerType) {
13154 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013155 if (useDisplayListProperties) {
13156 hasDisplayList = canHaveDisplayList();
13157 } else {
13158 buildDrawingCache(true);
13159 cache = getDrawingCache(true);
13160 }
Chet Haase64a48c12012-02-13 16:33:29 -080013161 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013162 case LAYER_TYPE_HARDWARE:
13163 if (useDisplayListProperties) {
13164 hasDisplayList = canHaveDisplayList();
13165 }
13166 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013167 case LAYER_TYPE_NONE:
13168 // Delay getting the display list until animation-driven alpha values are
13169 // set up and possibly passed on to the view
13170 hasDisplayList = canHaveDisplayList();
13171 break;
13172 }
13173 }
13174 }
Chet Haasea1cff502012-02-21 13:43:44 -080013175 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013176 if (useDisplayListProperties) {
13177 displayList = getDisplayList();
13178 if (!displayList.isValid()) {
13179 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13180 // to getDisplayList(), the display list will be marked invalid and we should not
13181 // try to use it again.
13182 displayList = null;
13183 hasDisplayList = false;
13184 useDisplayListProperties = false;
13185 }
13186 }
Chet Haase64a48c12012-02-13 16:33:29 -080013187
13188 final boolean hasNoCache = cache == null || hasDisplayList;
13189 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13190 layerType != LAYER_TYPE_HARDWARE;
13191
Chet Haasea1cff502012-02-21 13:43:44 -080013192 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013193 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013194 restoreTo = canvas.save();
13195 }
Chet Haase64a48c12012-02-13 16:33:29 -080013196 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013197 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013198 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013199 if (!useDisplayListProperties) {
13200 canvas.translate(mLeft, mTop);
13201 }
Chet Haase64a48c12012-02-13 16:33:29 -080013202 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013203 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013204 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013205 restoreTo = canvas.save();
13206 }
Chet Haase64a48c12012-02-13 16:33:29 -080013207 // mAttachInfo cannot be null, otherwise scalingRequired == false
13208 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13209 canvas.scale(scale, scale);
13210 }
13211 }
13212
Chet Haasea1cff502012-02-21 13:43:44 -080013213 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013214 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
13215 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013216 if (transformToApply != null || !childHasIdentityMatrix) {
13217 int transX = 0;
13218 int transY = 0;
13219
13220 if (offsetForScroll) {
13221 transX = -sx;
13222 transY = -sy;
13223 }
13224
13225 if (transformToApply != null) {
13226 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013227 if (useDisplayListProperties) {
13228 displayList.setAnimationMatrix(transformToApply.getMatrix());
13229 } else {
13230 // Undo the scroll translation, apply the transformation matrix,
13231 // then redo the scroll translate to get the correct result.
13232 canvas.translate(-transX, -transY);
13233 canvas.concat(transformToApply.getMatrix());
13234 canvas.translate(transX, transY);
13235 }
Chet Haasea1cff502012-02-21 13:43:44 -080013236 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013237 }
13238
13239 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013240 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013241 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013242 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013243 }
13244 }
13245
Chet Haasea1cff502012-02-21 13:43:44 -080013246 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013247 canvas.translate(-transX, -transY);
13248 canvas.concat(getMatrix());
13249 canvas.translate(transX, transY);
13250 }
13251 }
13252
Chet Haase21433372012-06-05 07:54:09 -070013253 // Deal with alpha if it is or used to be <1
13254 if (alpha < 1 ||
13255 (mPrivateFlags3 & VIEW_IS_ANIMATING_ALPHA) == VIEW_IS_ANIMATING_ALPHA) {
13256 if (alpha < 1) {
13257 mPrivateFlags3 |= VIEW_IS_ANIMATING_ALPHA;
13258 } else {
13259 mPrivateFlags3 &= ~VIEW_IS_ANIMATING_ALPHA;
13260 }
Chet Haasea1cff502012-02-21 13:43:44 -080013261 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013262 if (hasNoCache) {
13263 final int multipliedAlpha = (int) (255 * alpha);
13264 if (!onSetAlpha(multipliedAlpha)) {
13265 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013266 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013267 layerType != LAYER_TYPE_NONE) {
13268 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13269 }
Chet Haase9420abd2012-03-29 16:28:32 -070013270 if (useDisplayListProperties) {
13271 displayList.setAlpha(alpha * getAlpha());
13272 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013273 final int scrollX = hasDisplayList ? 0 : sx;
13274 final int scrollY = hasDisplayList ? 0 : sy;
13275 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13276 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013277 }
13278 } else {
13279 // Alpha is handled by the child directly, clobber the layer's alpha
13280 mPrivateFlags |= ALPHA_SET;
13281 }
13282 }
13283 }
13284 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13285 onSetAlpha(255);
13286 mPrivateFlags &= ~ALPHA_SET;
13287 }
13288
Chet Haasea1cff502012-02-21 13:43:44 -080013289 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13290 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013291 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013292 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013293 } else {
13294 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013295 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013296 } else {
13297 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13298 }
13299 }
13300 }
13301
Chet Haase9420abd2012-03-29 16:28:32 -070013302 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013303 displayList = getDisplayList();
13304 if (!displayList.isValid()) {
13305 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13306 // to getDisplayList(), the display list will be marked invalid and we should not
13307 // try to use it again.
13308 displayList = null;
13309 hasDisplayList = false;
13310 }
13311 }
13312
13313 if (hasNoCache) {
13314 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013315 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013316 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013317 if (layer != null && layer.isValid()) {
13318 mLayerPaint.setAlpha((int) (alpha * 255));
13319 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13320 layerRendered = true;
13321 } else {
13322 final int scrollX = hasDisplayList ? 0 : sx;
13323 final int scrollY = hasDisplayList ? 0 : sy;
13324 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013325 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013326 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13327 }
13328 }
13329
13330 if (!layerRendered) {
13331 if (!hasDisplayList) {
13332 // Fast path for layouts with no backgrounds
13333 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Chet Haase64a48c12012-02-13 16:33:29 -080013334 mPrivateFlags &= ~DIRTY_MASK;
13335 dispatchDraw(canvas);
13336 } else {
13337 draw(canvas);
13338 }
13339 } else {
13340 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013341 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013342 }
13343 }
13344 } else if (cache != null) {
13345 mPrivateFlags &= ~DIRTY_MASK;
13346 Paint cachePaint;
13347
13348 if (layerType == LAYER_TYPE_NONE) {
13349 cachePaint = parent.mCachePaint;
13350 if (cachePaint == null) {
13351 cachePaint = new Paint();
13352 cachePaint.setDither(false);
13353 parent.mCachePaint = cachePaint;
13354 }
Chet Haase9420abd2012-03-29 16:28:32 -070013355 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013356 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013357 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13358 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013359 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013360 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013361 }
13362 } else {
13363 cachePaint = mLayerPaint;
13364 cachePaint.setAlpha((int) (alpha * 255));
13365 }
13366 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13367 }
13368
Chet Haasea1cff502012-02-21 13:43:44 -080013369 if (restoreTo >= 0) {
13370 canvas.restoreToCount(restoreTo);
13371 }
Chet Haase64a48c12012-02-13 16:33:29 -080013372
13373 if (a != null && !more) {
13374 if (!hardwareAccelerated && !a.getFillAfter()) {
13375 onSetAlpha(255);
13376 }
13377 parent.finishAnimatingView(this, a);
13378 }
13379
13380 if (more && hardwareAccelerated) {
13381 // invalidation is the trigger to recreate display lists, so if we're using
13382 // display lists to render, force an invalidate to allow the animation to
13383 // continue drawing another frame
13384 parent.invalidate(true);
13385 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
13386 // alpha animations should cause the child to recreate its display list
13387 invalidate(true);
13388 }
13389 }
13390
13391 mRecreateDisplayList = false;
13392
13393 return more;
13394 }
13395
13396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013397 * Manually render this view (and all of its children) to the given Canvas.
13398 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013399 * called. When implementing a view, implement
13400 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13401 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013402 *
13403 * @param canvas The Canvas to which the View is rendered.
13404 */
13405 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013406 final int privateFlags = mPrivateFlags;
13407 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
13408 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
13409 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013411 /*
13412 * Draw traversal performs several drawing steps which must be executed
13413 * in the appropriate order:
13414 *
13415 * 1. Draw the background
13416 * 2. If necessary, save the canvas' layers to prepare for fading
13417 * 3. Draw view's content
13418 * 4. Draw children
13419 * 5. If necessary, draw the fading edges and restore layers
13420 * 6. Draw decorations (scrollbars for instance)
13421 */
13422
13423 // Step 1, draw the background, if needed
13424 int saveCount;
13425
Romain Guy24443ea2009-05-11 11:56:30 -070013426 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013427 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013428 if (background != null) {
13429 final int scrollX = mScrollX;
13430 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013431
Romain Guy24443ea2009-05-11 11:56:30 -070013432 if (mBackgroundSizeChanged) {
13433 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13434 mBackgroundSizeChanged = false;
13435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013436
Romain Guy24443ea2009-05-11 11:56:30 -070013437 if ((scrollX | scrollY) == 0) {
13438 background.draw(canvas);
13439 } else {
13440 canvas.translate(scrollX, scrollY);
13441 background.draw(canvas);
13442 canvas.translate(-scrollX, -scrollY);
13443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 }
13445 }
13446
13447 // skip step 2 & 5 if possible (common case)
13448 final int viewFlags = mViewFlags;
13449 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13450 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13451 if (!verticalEdges && !horizontalEdges) {
13452 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013453 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013454
13455 // Step 4, draw the children
13456 dispatchDraw(canvas);
13457
13458 // Step 6, draw decorations (scrollbars)
13459 onDrawScrollBars(canvas);
13460
13461 // we're done...
13462 return;
13463 }
13464
13465 /*
13466 * Here we do the full fledged routine...
13467 * (this is an uncommon case where speed matters less,
13468 * this is why we repeat some of the tests that have been
13469 * done above)
13470 */
13471
13472 boolean drawTop = false;
13473 boolean drawBottom = false;
13474 boolean drawLeft = false;
13475 boolean drawRight = false;
13476
13477 float topFadeStrength = 0.0f;
13478 float bottomFadeStrength = 0.0f;
13479 float leftFadeStrength = 0.0f;
13480 float rightFadeStrength = 0.0f;
13481
13482 // Step 2, save the canvas' layers
13483 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013484
13485 final boolean offsetRequired = isPaddingOffsetRequired();
13486 if (offsetRequired) {
13487 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 }
13489
13490 int left = mScrollX + paddingLeft;
13491 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013492 int top = mScrollY + getFadeTop(offsetRequired);
13493 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494
13495 if (offsetRequired) {
13496 right += getRightPaddingOffset();
13497 bottom += getBottomPaddingOffset();
13498 }
13499
13500 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013501 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013502 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013503
13504 // clip the fade length if top and bottom fades overlap
13505 // overlapping fades produce odd-looking artifacts
13506 if (verticalEdges && (top + length > bottom - length)) {
13507 length = (bottom - top) / 2;
13508 }
13509
13510 // also clip horizontal fades if necessary
13511 if (horizontalEdges && (left + length > right - length)) {
13512 length = (right - left) / 2;
13513 }
13514
13515 if (verticalEdges) {
13516 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013517 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013518 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013519 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013520 }
13521
13522 if (horizontalEdges) {
13523 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013524 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013525 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013526 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013527 }
13528
13529 saveCount = canvas.getSaveCount();
13530
13531 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013532 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013533 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13534
13535 if (drawTop) {
13536 canvas.saveLayer(left, top, right, top + length, null, flags);
13537 }
13538
13539 if (drawBottom) {
13540 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13541 }
13542
13543 if (drawLeft) {
13544 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13545 }
13546
13547 if (drawRight) {
13548 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13549 }
13550 } else {
13551 scrollabilityCache.setFadeColor(solidColor);
13552 }
13553
13554 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013555 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013556
13557 // Step 4, draw the children
13558 dispatchDraw(canvas);
13559
13560 // Step 5, draw the fade effect and restore layers
13561 final Paint p = scrollabilityCache.paint;
13562 final Matrix matrix = scrollabilityCache.matrix;
13563 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013564
13565 if (drawTop) {
13566 matrix.setScale(1, fadeHeight * topFadeStrength);
13567 matrix.postTranslate(left, top);
13568 fade.setLocalMatrix(matrix);
13569 canvas.drawRect(left, top, right, top + length, p);
13570 }
13571
13572 if (drawBottom) {
13573 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13574 matrix.postRotate(180);
13575 matrix.postTranslate(left, bottom);
13576 fade.setLocalMatrix(matrix);
13577 canvas.drawRect(left, bottom - length, right, bottom, p);
13578 }
13579
13580 if (drawLeft) {
13581 matrix.setScale(1, fadeHeight * leftFadeStrength);
13582 matrix.postRotate(-90);
13583 matrix.postTranslate(left, top);
13584 fade.setLocalMatrix(matrix);
13585 canvas.drawRect(left, top, left + length, bottom, p);
13586 }
13587
13588 if (drawRight) {
13589 matrix.setScale(1, fadeHeight * rightFadeStrength);
13590 matrix.postRotate(90);
13591 matrix.postTranslate(right, top);
13592 fade.setLocalMatrix(matrix);
13593 canvas.drawRect(right - length, top, right, bottom, p);
13594 }
13595
13596 canvas.restoreToCount(saveCount);
13597
13598 // Step 6, draw decorations (scrollbars)
13599 onDrawScrollBars(canvas);
13600 }
13601
13602 /**
13603 * Override this if your view is known to always be drawn on top of a solid color background,
13604 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13605 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13606 * should be set to 0xFF.
13607 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013608 * @see #setVerticalFadingEdgeEnabled(boolean)
13609 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610 *
13611 * @return The known solid color background for this view, or 0 if the color may vary
13612 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013613 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013614 public int getSolidColor() {
13615 return 0;
13616 }
13617
13618 /**
13619 * Build a human readable string representation of the specified view flags.
13620 *
13621 * @param flags the view flags to convert to a string
13622 * @return a String representing the supplied flags
13623 */
13624 private static String printFlags(int flags) {
13625 String output = "";
13626 int numFlags = 0;
13627 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13628 output += "TAKES_FOCUS";
13629 numFlags++;
13630 }
13631
13632 switch (flags & VISIBILITY_MASK) {
13633 case INVISIBLE:
13634 if (numFlags > 0) {
13635 output += " ";
13636 }
13637 output += "INVISIBLE";
13638 // USELESS HERE numFlags++;
13639 break;
13640 case GONE:
13641 if (numFlags > 0) {
13642 output += " ";
13643 }
13644 output += "GONE";
13645 // USELESS HERE numFlags++;
13646 break;
13647 default:
13648 break;
13649 }
13650 return output;
13651 }
13652
13653 /**
13654 * Build a human readable string representation of the specified private
13655 * view flags.
13656 *
13657 * @param privateFlags the private view flags to convert to a string
13658 * @return a String representing the supplied flags
13659 */
13660 private static String printPrivateFlags(int privateFlags) {
13661 String output = "";
13662 int numFlags = 0;
13663
13664 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13665 output += "WANTS_FOCUS";
13666 numFlags++;
13667 }
13668
13669 if ((privateFlags & FOCUSED) == FOCUSED) {
13670 if (numFlags > 0) {
13671 output += " ";
13672 }
13673 output += "FOCUSED";
13674 numFlags++;
13675 }
13676
13677 if ((privateFlags & SELECTED) == SELECTED) {
13678 if (numFlags > 0) {
13679 output += " ";
13680 }
13681 output += "SELECTED";
13682 numFlags++;
13683 }
13684
13685 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13686 if (numFlags > 0) {
13687 output += " ";
13688 }
13689 output += "IS_ROOT_NAMESPACE";
13690 numFlags++;
13691 }
13692
13693 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13694 if (numFlags > 0) {
13695 output += " ";
13696 }
13697 output += "HAS_BOUNDS";
13698 numFlags++;
13699 }
13700
13701 if ((privateFlags & DRAWN) == DRAWN) {
13702 if (numFlags > 0) {
13703 output += " ";
13704 }
13705 output += "DRAWN";
13706 // USELESS HERE numFlags++;
13707 }
13708 return output;
13709 }
13710
13711 /**
13712 * <p>Indicates whether or not this view's layout will be requested during
13713 * the next hierarchy layout pass.</p>
13714 *
13715 * @return true if the layout will be forced during next layout pass
13716 */
13717 public boolean isLayoutRequested() {
13718 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13719 }
13720
13721 /**
13722 * Assign a size and position to a view and all of its
13723 * descendants
13724 *
13725 * <p>This is the second phase of the layout mechanism.
13726 * (The first is measuring). In this phase, each parent calls
13727 * layout on all of its children to position them.
13728 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013729 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013730 *
Chet Haase9c087442011-01-12 16:20:16 -080013731 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013732 * Derived classes with children should override
13733 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013734 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735 *
13736 * @param l Left position, relative to parent
13737 * @param t Top position, relative to parent
13738 * @param r Right position, relative to parent
13739 * @param b Bottom position, relative to parent
13740 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013741 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013742 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013743 int oldL = mLeft;
13744 int oldT = mTop;
13745 int oldB = mBottom;
13746 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 boolean changed = setFrame(l, t, r, b);
13748 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013749 onLayout(changed, l, t, r, b);
13750 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013751
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013752 ListenerInfo li = mListenerInfo;
13753 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013754 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013755 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013756 int numListeners = listenersCopy.size();
13757 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013758 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013759 }
13760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013761 }
13762 mPrivateFlags &= ~FORCE_LAYOUT;
13763 }
13764
13765 /**
13766 * Called from layout when this view should
13767 * assign a size and position to each of its children.
13768 *
13769 * Derived classes with children should override
13770 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013771 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013772 * @param changed This is a new size or position for this view
13773 * @param left Left position, relative to parent
13774 * @param top Top position, relative to parent
13775 * @param right Right position, relative to parent
13776 * @param bottom Bottom position, relative to parent
13777 */
13778 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13779 }
13780
13781 /**
13782 * Assign a size and position to this view.
13783 *
13784 * This is called from layout.
13785 *
13786 * @param left Left position, relative to parent
13787 * @param top Top position, relative to parent
13788 * @param right Right position, relative to parent
13789 * @param bottom Bottom position, relative to parent
13790 * @return true if the new size and position are different than the
13791 * previous ones
13792 * {@hide}
13793 */
13794 protected boolean setFrame(int left, int top, int right, int bottom) {
13795 boolean changed = false;
13796
13797 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013798 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799 + right + "," + bottom + ")");
13800 }
13801
13802 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13803 changed = true;
13804
13805 // Remember our drawn bit
13806 int drawn = mPrivateFlags & DRAWN;
13807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808 int oldWidth = mRight - mLeft;
13809 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013810 int newWidth = right - left;
13811 int newHeight = bottom - top;
13812 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13813
13814 // Invalidate our old position
13815 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013816
13817 mLeft = left;
13818 mTop = top;
13819 mRight = right;
13820 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013821 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013822 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824
13825 mPrivateFlags |= HAS_BOUNDS;
13826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013827
Chet Haase75755e22011-07-18 17:48:25 -070013828 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013829 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13830 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013831 if (mTransformationInfo != null) {
13832 mTransformationInfo.mMatrixDirty = true;
13833 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13836 }
13837
13838 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13839 // If we are visible, force the DRAWN bit to on so that
13840 // this invalidate will go through (at least to our parent).
13841 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013842 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013843 // the DRAWN bit.
13844 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013845 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013846 // parent display list may need to be recreated based on a change in the bounds
13847 // of any child
13848 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013849 }
13850
13851 // Reset drawn bit to original value (invalidate turns it off)
13852 mPrivateFlags |= drawn;
13853
13854 mBackgroundSizeChanged = true;
13855 }
13856 return changed;
13857 }
13858
13859 /**
13860 * Finalize inflating a view from XML. This is called as the last phase
13861 * of inflation, after all child views have been added.
13862 *
13863 * <p>Even if the subclass overrides onFinishInflate, they should always be
13864 * sure to call the super method, so that we get called.
13865 */
13866 protected void onFinishInflate() {
13867 }
13868
13869 /**
13870 * Returns the resources associated with this view.
13871 *
13872 * @return Resources object.
13873 */
13874 public Resources getResources() {
13875 return mResources;
13876 }
13877
13878 /**
13879 * Invalidates the specified Drawable.
13880 *
13881 * @param drawable the drawable to invalidate
13882 */
13883 public void invalidateDrawable(Drawable drawable) {
13884 if (verifyDrawable(drawable)) {
13885 final Rect dirty = drawable.getBounds();
13886 final int scrollX = mScrollX;
13887 final int scrollY = mScrollY;
13888
13889 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13890 dirty.right + scrollX, dirty.bottom + scrollY);
13891 }
13892 }
13893
13894 /**
13895 * Schedules an action on a drawable to occur at a specified time.
13896 *
13897 * @param who the recipient of the action
13898 * @param what the action to run on the drawable
13899 * @param when the time at which the action must occur. Uses the
13900 * {@link SystemClock#uptimeMillis} timebase.
13901 */
13902 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013903 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013904 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013905 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013906 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13907 Choreographer.CALLBACK_ANIMATION, what, who,
13908 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013909 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013910 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 }
13913 }
13914
13915 /**
13916 * Cancels a scheduled action on a drawable.
13917 *
13918 * @param who the recipient of the action
13919 * @param what the action to cancel
13920 */
13921 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013922 if (verifyDrawable(who) && what != null) {
13923 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013924 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13925 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013926 } else {
13927 ViewRootImpl.getRunQueue().removeCallbacks(what);
13928 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929 }
13930 }
13931
13932 /**
13933 * Unschedule any events associated with the given Drawable. This can be
13934 * used when selecting a new Drawable into a view, so that the previous
13935 * one is completely unscheduled.
13936 *
13937 * @param who The Drawable to unschedule.
13938 *
13939 * @see #drawableStateChanged
13940 */
13941 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013942 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013943 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13944 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 }
13946 }
13947
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013948 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013949 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13950 * that the View directionality can and will be resolved before its Drawables.
13951 *
13952 * Will call {@link View#onResolveDrawables} when resolution is done.
13953 */
13954 public void resolveDrawables() {
13955 if (mBackground != null) {
13956 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13957 }
13958 onResolveDrawables(getResolvedLayoutDirection());
13959 }
13960
13961 /**
13962 * Called when layout direction has been resolved.
13963 *
13964 * The default implementation does nothing.
13965 *
13966 * @param layoutDirection The resolved layout direction.
13967 *
13968 * @see {@link #LAYOUT_DIRECTION_LTR}
13969 * @see {@link #LAYOUT_DIRECTION_RTL}
13970 */
13971 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013972 }
13973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 /**
13975 * If your view subclass is displaying its own Drawable objects, it should
13976 * override this function and return true for any Drawable it is
13977 * displaying. This allows animations for those drawables to be
13978 * scheduled.
13979 *
13980 * <p>Be sure to call through to the super class when overriding this
13981 * function.
13982 *
13983 * @param who The Drawable to verify. Return true if it is one you are
13984 * displaying, else return the result of calling through to the
13985 * super class.
13986 *
13987 * @return boolean If true than the Drawable is being displayed in the
13988 * view; else false and it is not allowed to animate.
13989 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013990 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13991 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 */
13993 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013994 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 }
13996
13997 /**
13998 * This function is called whenever the state of the view changes in such
13999 * a way that it impacts the state of drawables being shown.
14000 *
14001 * <p>Be sure to call through to the superclass when overriding this
14002 * function.
14003 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014004 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 */
14006 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014007 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008 if (d != null && d.isStateful()) {
14009 d.setState(getDrawableState());
14010 }
14011 }
14012
14013 /**
14014 * Call this to force a view to update its drawable state. This will cause
14015 * drawableStateChanged to be called on this view. Views that are interested
14016 * in the new state should call getDrawableState.
14017 *
14018 * @see #drawableStateChanged
14019 * @see #getDrawableState
14020 */
14021 public void refreshDrawableState() {
14022 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
14023 drawableStateChanged();
14024
14025 ViewParent parent = mParent;
14026 if (parent != null) {
14027 parent.childDrawableStateChanged(this);
14028 }
14029 }
14030
14031 /**
14032 * Return an array of resource IDs of the drawable states representing the
14033 * current state of the view.
14034 *
14035 * @return The current drawable state
14036 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014037 * @see Drawable#setState(int[])
14038 * @see #drawableStateChanged()
14039 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014040 */
14041 public final int[] getDrawableState() {
14042 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
14043 return mDrawableState;
14044 } else {
14045 mDrawableState = onCreateDrawableState(0);
14046 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
14047 return mDrawableState;
14048 }
14049 }
14050
14051 /**
14052 * Generate the new {@link android.graphics.drawable.Drawable} state for
14053 * this view. This is called by the view
14054 * system when the cached Drawable state is determined to be invalid. To
14055 * retrieve the current state, you should use {@link #getDrawableState}.
14056 *
14057 * @param extraSpace if non-zero, this is the number of extra entries you
14058 * would like in the returned array in which you can place your own
14059 * states.
14060 *
14061 * @return Returns an array holding the current {@link Drawable} state of
14062 * the view.
14063 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014064 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065 */
14066 protected int[] onCreateDrawableState(int extraSpace) {
14067 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14068 mParent instanceof View) {
14069 return ((View) mParent).onCreateDrawableState(extraSpace);
14070 }
14071
14072 int[] drawableState;
14073
14074 int privateFlags = mPrivateFlags;
14075
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014076 int viewStateIndex = 0;
14077 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
14078 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14079 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070014080 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014081 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
14082 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014083 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14084 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014085 // This is set if HW acceleration is requested, even if the current
14086 // process doesn't allow it. This is just to allow app preview
14087 // windows to better match their app.
14088 viewStateIndex |= VIEW_STATE_ACCELERATED;
14089 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070014090 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091
Christopher Tate3d4bf172011-03-28 16:16:46 -070014092 final int privateFlags2 = mPrivateFlags2;
14093 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14094 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
14095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 drawableState = VIEW_STATE_SETS[viewStateIndex];
14097
14098 //noinspection ConstantIfStatement
14099 if (false) {
14100 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14101 Log.i("View", toString()
14102 + " pressed=" + ((privateFlags & PRESSED) != 0)
14103 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14104 + " fo=" + hasFocus()
14105 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014106 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014107 + ": " + Arrays.toString(drawableState));
14108 }
14109
14110 if (extraSpace == 0) {
14111 return drawableState;
14112 }
14113
14114 final int[] fullState;
14115 if (drawableState != null) {
14116 fullState = new int[drawableState.length + extraSpace];
14117 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14118 } else {
14119 fullState = new int[extraSpace];
14120 }
14121
14122 return fullState;
14123 }
14124
14125 /**
14126 * Merge your own state values in <var>additionalState</var> into the base
14127 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014128 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014129 *
14130 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014131 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014132 * own additional state values.
14133 *
14134 * @param additionalState The additional state values you would like
14135 * added to <var>baseState</var>; this array is not modified.
14136 *
14137 * @return As a convenience, the <var>baseState</var> array you originally
14138 * passed into the function is returned.
14139 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014140 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014141 */
14142 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14143 final int N = baseState.length;
14144 int i = N - 1;
14145 while (i >= 0 && baseState[i] == 0) {
14146 i--;
14147 }
14148 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14149 return baseState;
14150 }
14151
14152 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014153 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14154 * on all Drawable objects associated with this view.
14155 */
14156 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014157 if (mBackground != null) {
14158 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014159 }
14160 }
14161
14162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014163 * Sets the background color for this view.
14164 * @param color the color of the background
14165 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014166 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014168 if (mBackground instanceof ColorDrawable) {
14169 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070014170 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014171 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014173 }
14174
14175 /**
14176 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014177 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014178 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014179 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014180 * @attr ref android.R.styleable#View_background
14181 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014182 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 public void setBackgroundResource(int resid) {
14184 if (resid != 0 && resid == mBackgroundResource) {
14185 return;
14186 }
14187
14188 Drawable d= null;
14189 if (resid != 0) {
14190 d = mResources.getDrawable(resid);
14191 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014192 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014193
14194 mBackgroundResource = resid;
14195 }
14196
14197 /**
14198 * Set the background to a given Drawable, or remove the background. If the
14199 * background has padding, this View's padding is set to the background's
14200 * padding. However, when a background is removed, this View's padding isn't
14201 * touched. If setting the padding is desired, please use
14202 * {@link #setPadding(int, int, int, int)}.
14203 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014204 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205 * background
14206 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014207 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014208 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014209 setBackgroundDrawable(background);
14210 }
14211
14212 /**
14213 * @deprecated use {@link #setBackground(Drawable)} instead
14214 */
14215 @Deprecated
14216 public void setBackgroundDrawable(Drawable background) {
14217 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014218 return;
14219 }
14220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014221 boolean requestLayout = false;
14222
14223 mBackgroundResource = 0;
14224
14225 /*
14226 * Regardless of whether we're setting a new background or not, we want
14227 * to clear the previous drawable.
14228 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014229 if (mBackground != null) {
14230 mBackground.setCallback(null);
14231 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014232 }
14233
Philip Milne6c8ea062012-04-03 17:38:43 -070014234 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 Rect padding = sThreadLocal.get();
14236 if (padding == null) {
14237 padding = new Rect();
14238 sThreadLocal.set(padding);
14239 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014240 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014241 if (background.getPadding(padding)) {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014242 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014243 case LAYOUT_DIRECTION_RTL:
14244 setPadding(padding.right, padding.top, padding.left, padding.bottom);
14245 break;
14246 case LAYOUT_DIRECTION_LTR:
14247 default:
14248 setPadding(padding.left, padding.top, padding.right, padding.bottom);
14249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250 }
14251
14252 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14253 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014254 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14255 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 requestLayout = true;
14257 }
14258
Philip Milne6c8ea062012-04-03 17:38:43 -070014259 background.setCallback(this);
14260 if (background.isStateful()) {
14261 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014263 background.setVisible(getVisibility() == VISIBLE, false);
14264 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014265
14266 if ((mPrivateFlags & SKIP_DRAW) != 0) {
14267 mPrivateFlags &= ~SKIP_DRAW;
14268 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
14269 requestLayout = true;
14270 }
14271 } else {
14272 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014273 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274
14275 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
14276 /*
14277 * This view ONLY drew the background before and we're removing
14278 * the background, so now it won't draw anything
14279 * (hence we SKIP_DRAW)
14280 */
14281 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
14282 mPrivateFlags |= SKIP_DRAW;
14283 }
14284
14285 /*
14286 * When the background is set, we try to apply its padding to this
14287 * View. When the background is removed, we don't touch this View's
14288 * padding. This is noted in the Javadocs. Hence, we don't need to
14289 * requestLayout(), the invalidate() below is sufficient.
14290 */
14291
14292 // The old background's minimum size could have affected this
14293 // View's layout, so let's requestLayout
14294 requestLayout = true;
14295 }
14296
Romain Guy8f1344f52009-05-15 16:03:59 -070014297 computeOpaqueFlags();
14298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014299 if (requestLayout) {
14300 requestLayout();
14301 }
14302
14303 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014304 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014305 }
14306
14307 /**
14308 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014310 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014311 *
14312 * @see #setBackground(Drawable)
14313 *
14314 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014315 */
14316 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014317 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014318 }
14319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 /**
14321 * Sets the padding. The view may add on the space required to display
14322 * the scrollbars, depending on the style and visibility of the scrollbars.
14323 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14324 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14325 * from the values set in this call.
14326 *
14327 * @attr ref android.R.styleable#View_padding
14328 * @attr ref android.R.styleable#View_paddingBottom
14329 * @attr ref android.R.styleable#View_paddingLeft
14330 * @attr ref android.R.styleable#View_paddingRight
14331 * @attr ref android.R.styleable#View_paddingTop
14332 * @param left the left padding in pixels
14333 * @param top the top padding in pixels
14334 * @param right the right padding in pixels
14335 * @param bottom the bottom padding in pixels
14336 */
14337 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014338 mUserPaddingStart = -1;
14339 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014340 mUserPaddingRelative = false;
14341
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014342 internalSetPadding(left, top, right, bottom);
14343 }
14344
14345 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014346 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014347 mUserPaddingRight = right;
14348 mUserPaddingBottom = bottom;
14349
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014350 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014351 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014352
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014353 // Common case is there are no scroll bars.
14354 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014355 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014356 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014357 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014358 switch (mVerticalScrollbarPosition) {
14359 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014360 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
14361 left += offset;
14362 } else {
14363 right += offset;
14364 }
14365 break;
Adam Powell20232d02010-12-08 21:08:53 -080014366 case SCROLLBAR_POSITION_RIGHT:
14367 right += offset;
14368 break;
14369 case SCROLLBAR_POSITION_LEFT:
14370 left += offset;
14371 break;
14372 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014373 }
Adam Powell20232d02010-12-08 21:08:53 -080014374 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014375 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14376 ? 0 : getHorizontalScrollbarHeight();
14377 }
14378 }
Romain Guy8506ab42009-06-11 17:35:47 -070014379
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014380 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 changed = true;
14382 mPaddingLeft = left;
14383 }
14384 if (mPaddingTop != top) {
14385 changed = true;
14386 mPaddingTop = top;
14387 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014388 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014389 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014390 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014391 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014392 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014393 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014394 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014395 }
14396
14397 if (changed) {
14398 requestLayout();
14399 }
14400 }
14401
14402 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014403 * Sets the relative padding. The view may add on the space required to display
14404 * the scrollbars, depending on the style and visibility of the scrollbars.
14405 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14406 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14407 * from the values set in this call.
14408 *
14409 * @attr ref android.R.styleable#View_padding
14410 * @attr ref android.R.styleable#View_paddingBottom
14411 * @attr ref android.R.styleable#View_paddingStart
14412 * @attr ref android.R.styleable#View_paddingEnd
14413 * @attr ref android.R.styleable#View_paddingTop
14414 * @param start the start padding in pixels
14415 * @param top the top padding in pixels
14416 * @param end the end padding in pixels
14417 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014418 */
14419 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014420 mUserPaddingStart = start;
14421 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014422 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014423
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014424 switch(getResolvedLayoutDirection()) {
14425 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014426 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014427 break;
14428 case LAYOUT_DIRECTION_LTR:
14429 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014430 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014431 }
14432 }
14433
14434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014435 * Returns the top padding of this view.
14436 *
14437 * @return the top padding in pixels
14438 */
14439 public int getPaddingTop() {
14440 return mPaddingTop;
14441 }
14442
14443 /**
14444 * Returns the bottom padding of this view. If there are inset and enabled
14445 * scrollbars, this value may include the space required to display the
14446 * scrollbars as well.
14447 *
14448 * @return the bottom padding in pixels
14449 */
14450 public int getPaddingBottom() {
14451 return mPaddingBottom;
14452 }
14453
14454 /**
14455 * Returns the left padding of this view. If there are inset and enabled
14456 * scrollbars, this value may include the space required to display the
14457 * scrollbars as well.
14458 *
14459 * @return the left padding in pixels
14460 */
14461 public int getPaddingLeft() {
14462 return mPaddingLeft;
14463 }
14464
14465 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014466 * Returns the start padding of this view depending on its resolved layout direction.
14467 * If there are inset and enabled scrollbars, this value may include the space
14468 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014469 *
14470 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014471 */
14472 public int getPaddingStart() {
14473 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14474 mPaddingRight : mPaddingLeft;
14475 }
14476
14477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014478 * Returns the right padding of this view. If there are inset and enabled
14479 * scrollbars, this value may include the space required to display the
14480 * scrollbars as well.
14481 *
14482 * @return the right padding in pixels
14483 */
14484 public int getPaddingRight() {
14485 return mPaddingRight;
14486 }
14487
14488 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014489 * Returns the end padding of this view depending on its resolved layout direction.
14490 * If there are inset and enabled scrollbars, this value may include the space
14491 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014492 *
14493 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014494 */
14495 public int getPaddingEnd() {
14496 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14497 mPaddingLeft : mPaddingRight;
14498 }
14499
14500 /**
14501 * Return if the padding as been set thru relative values
14502 * {@link #setPaddingRelative(int, int, int, int)} or thru
14503 * @attr ref android.R.styleable#View_paddingStart or
14504 * @attr ref android.R.styleable#View_paddingEnd
14505 *
14506 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014507 */
14508 public boolean isPaddingRelative() {
14509 return mUserPaddingRelative;
14510 }
14511
14512 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014513 * @hide
14514 */
Philip Milne7a23b492012-04-24 22:12:36 -070014515 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014516 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014517 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014518 }
14519 return mLayoutInsets;
14520 }
14521
14522 /**
14523 * @hide
14524 */
14525 public void setLayoutInsets(Insets layoutInsets) {
14526 mLayoutInsets = layoutInsets;
14527 }
14528
14529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014530 * Changes the selection state of this view. A view can be selected or not.
14531 * Note that selection is not the same as focus. Views are typically
14532 * selected in the context of an AdapterView like ListView or GridView;
14533 * the selected view is the view that is highlighted.
14534 *
14535 * @param selected true if the view must be selected, false otherwise
14536 */
14537 public void setSelected(boolean selected) {
14538 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14539 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014540 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014541 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014542 refreshDrawableState();
14543 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014544 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14545 notifyAccessibilityStateChanged();
14546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 }
14548 }
14549
14550 /**
14551 * Dispatch setSelected to all of this View's children.
14552 *
14553 * @see #setSelected(boolean)
14554 *
14555 * @param selected The new selected state
14556 */
14557 protected void dispatchSetSelected(boolean selected) {
14558 }
14559
14560 /**
14561 * Indicates the selection state of this view.
14562 *
14563 * @return true if the view is selected, false otherwise
14564 */
14565 @ViewDebug.ExportedProperty
14566 public boolean isSelected() {
14567 return (mPrivateFlags & SELECTED) != 0;
14568 }
14569
14570 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014571 * Changes the activated state of this view. A view can be activated or not.
14572 * Note that activation is not the same as selection. Selection is
14573 * a transient property, representing the view (hierarchy) the user is
14574 * currently interacting with. Activation is a longer-term state that the
14575 * user can move views in and out of. For example, in a list view with
14576 * single or multiple selection enabled, the views in the current selection
14577 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14578 * here.) The activated state is propagated down to children of the view it
14579 * is set on.
14580 *
14581 * @param activated true if the view must be activated, false otherwise
14582 */
14583 public void setActivated(boolean activated) {
14584 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14585 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014586 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014587 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014588 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014589 }
14590 }
14591
14592 /**
14593 * Dispatch setActivated to all of this View's children.
14594 *
14595 * @see #setActivated(boolean)
14596 *
14597 * @param activated The new activated state
14598 */
14599 protected void dispatchSetActivated(boolean activated) {
14600 }
14601
14602 /**
14603 * Indicates the activation state of this view.
14604 *
14605 * @return true if the view is activated, false otherwise
14606 */
14607 @ViewDebug.ExportedProperty
14608 public boolean isActivated() {
14609 return (mPrivateFlags & ACTIVATED) != 0;
14610 }
14611
14612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014613 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14614 * observer can be used to get notifications when global events, like
14615 * layout, happen.
14616 *
14617 * The returned ViewTreeObserver observer is not guaranteed to remain
14618 * valid for the lifetime of this View. If the caller of this method keeps
14619 * a long-lived reference to ViewTreeObserver, it should always check for
14620 * the return value of {@link ViewTreeObserver#isAlive()}.
14621 *
14622 * @return The ViewTreeObserver for this view's hierarchy.
14623 */
14624 public ViewTreeObserver getViewTreeObserver() {
14625 if (mAttachInfo != null) {
14626 return mAttachInfo.mTreeObserver;
14627 }
14628 if (mFloatingTreeObserver == null) {
14629 mFloatingTreeObserver = new ViewTreeObserver();
14630 }
14631 return mFloatingTreeObserver;
14632 }
14633
14634 /**
14635 * <p>Finds the topmost view in the current view hierarchy.</p>
14636 *
14637 * @return the topmost view containing this view
14638 */
14639 public View getRootView() {
14640 if (mAttachInfo != null) {
14641 final View v = mAttachInfo.mRootView;
14642 if (v != null) {
14643 return v;
14644 }
14645 }
Romain Guy8506ab42009-06-11 17:35:47 -070014646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 View parent = this;
14648
14649 while (parent.mParent != null && parent.mParent instanceof View) {
14650 parent = (View) parent.mParent;
14651 }
14652
14653 return parent;
14654 }
14655
14656 /**
14657 * <p>Computes the coordinates of this view on the screen. The argument
14658 * must be an array of two integers. After the method returns, the array
14659 * contains the x and y location in that order.</p>
14660 *
14661 * @param location an array of two integers in which to hold the coordinates
14662 */
14663 public void getLocationOnScreen(int[] location) {
14664 getLocationInWindow(location);
14665
14666 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014667 if (info != null) {
14668 location[0] += info.mWindowLeft;
14669 location[1] += info.mWindowTop;
14670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014671 }
14672
14673 /**
14674 * <p>Computes the coordinates of this view in its window. The argument
14675 * must be an array of two integers. After the method returns, the array
14676 * contains the x and y location in that order.</p>
14677 *
14678 * @param location an array of two integers in which to hold the coordinates
14679 */
14680 public void getLocationInWindow(int[] location) {
14681 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014682 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014683 }
14684
Gilles Debunne6583ce52011-12-06 18:09:02 -080014685 if (mAttachInfo == null) {
14686 // When the view is not attached to a window, this method does not make sense
14687 location[0] = location[1] = 0;
14688 return;
14689 }
14690
Gilles Debunnecea45132011-11-24 02:19:27 +010014691 float[] position = mAttachInfo.mTmpTransformLocation;
14692 position[0] = position[1] = 0.0f;
14693
14694 if (!hasIdentityMatrix()) {
14695 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014697
Gilles Debunnecea45132011-11-24 02:19:27 +010014698 position[0] += mLeft;
14699 position[1] += mTop;
14700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014701 ViewParent viewParent = mParent;
14702 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014703 final View view = (View) viewParent;
14704
14705 position[0] -= view.mScrollX;
14706 position[1] -= view.mScrollY;
14707
14708 if (!view.hasIdentityMatrix()) {
14709 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014710 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014711
14712 position[0] += view.mLeft;
14713 position[1] += view.mTop;
14714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014715 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014716 }
Romain Guy8506ab42009-06-11 17:35:47 -070014717
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014718 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014719 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014720 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14721 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014722 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014723
14724 location[0] = (int) (position[0] + 0.5f);
14725 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014726 }
14727
14728 /**
14729 * {@hide}
14730 * @param id the id of the view to be found
14731 * @return the view of the specified id, null if cannot be found
14732 */
14733 protected View findViewTraversal(int id) {
14734 if (id == mID) {
14735 return this;
14736 }
14737 return null;
14738 }
14739
14740 /**
14741 * {@hide}
14742 * @param tag the tag of the view to be found
14743 * @return the view of specified tag, null if cannot be found
14744 */
14745 protected View findViewWithTagTraversal(Object tag) {
14746 if (tag != null && tag.equals(mTag)) {
14747 return this;
14748 }
14749 return null;
14750 }
14751
14752 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014753 * {@hide}
14754 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014755 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014756 * @return The first view that matches the predicate or null.
14757 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014758 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014759 if (predicate.apply(this)) {
14760 return this;
14761 }
14762 return null;
14763 }
14764
14765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014766 * Look for a child view with the given id. If this view has the given
14767 * id, return this view.
14768 *
14769 * @param id The id to search for.
14770 * @return The view that has the given id in the hierarchy or null
14771 */
14772 public final View findViewById(int id) {
14773 if (id < 0) {
14774 return null;
14775 }
14776 return findViewTraversal(id);
14777 }
14778
14779 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014780 * Finds a view by its unuque and stable accessibility id.
14781 *
14782 * @param accessibilityId The searched accessibility id.
14783 * @return The found view.
14784 */
14785 final View findViewByAccessibilityId(int accessibilityId) {
14786 if (accessibilityId < 0) {
14787 return null;
14788 }
14789 return findViewByAccessibilityIdTraversal(accessibilityId);
14790 }
14791
14792 /**
14793 * Performs the traversal to find a view by its unuque and stable accessibility id.
14794 *
14795 * <strong>Note:</strong>This method does not stop at the root namespace
14796 * boundary since the user can touch the screen at an arbitrary location
14797 * potentially crossing the root namespace bounday which will send an
14798 * accessibility event to accessibility services and they should be able
14799 * to obtain the event source. Also accessibility ids are guaranteed to be
14800 * unique in the window.
14801 *
14802 * @param accessibilityId The accessibility id.
14803 * @return The found view.
14804 */
14805 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14806 if (getAccessibilityViewId() == accessibilityId) {
14807 return this;
14808 }
14809 return null;
14810 }
14811
14812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014813 * Look for a child view with the given tag. If this view has the given
14814 * tag, return this view.
14815 *
14816 * @param tag The tag to search for, using "tag.equals(getTag())".
14817 * @return The View that has the given tag in the hierarchy or null
14818 */
14819 public final View findViewWithTag(Object tag) {
14820 if (tag == null) {
14821 return null;
14822 }
14823 return findViewWithTagTraversal(tag);
14824 }
14825
14826 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014827 * {@hide}
14828 * Look for a child view that matches the specified predicate.
14829 * If this view matches the predicate, return this view.
14830 *
14831 * @param predicate The predicate to evaluate.
14832 * @return The first view that matches the predicate or null.
14833 */
14834 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014835 return findViewByPredicateTraversal(predicate, null);
14836 }
14837
14838 /**
14839 * {@hide}
14840 * Look for a child view that matches the specified predicate,
14841 * starting with the specified view and its descendents and then
14842 * recusively searching the ancestors and siblings of that view
14843 * until this view is reached.
14844 *
14845 * This method is useful in cases where the predicate does not match
14846 * a single unique view (perhaps multiple views use the same id)
14847 * and we are trying to find the view that is "closest" in scope to the
14848 * starting view.
14849 *
14850 * @param start The view to start from.
14851 * @param predicate The predicate to evaluate.
14852 * @return The first view that matches the predicate or null.
14853 */
14854 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14855 View childToSkip = null;
14856 for (;;) {
14857 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14858 if (view != null || start == this) {
14859 return view;
14860 }
14861
14862 ViewParent parent = start.getParent();
14863 if (parent == null || !(parent instanceof View)) {
14864 return null;
14865 }
14866
14867 childToSkip = start;
14868 start = (View) parent;
14869 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014870 }
14871
14872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014873 * Sets the identifier for this view. The identifier does not have to be
14874 * unique in this view's hierarchy. The identifier should be a positive
14875 * number.
14876 *
14877 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014878 * @see #getId()
14879 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 *
14881 * @param id a number used to identify the view
14882 *
14883 * @attr ref android.R.styleable#View_id
14884 */
14885 public void setId(int id) {
14886 mID = id;
14887 }
14888
14889 /**
14890 * {@hide}
14891 *
14892 * @param isRoot true if the view belongs to the root namespace, false
14893 * otherwise
14894 */
14895 public void setIsRootNamespace(boolean isRoot) {
14896 if (isRoot) {
14897 mPrivateFlags |= IS_ROOT_NAMESPACE;
14898 } else {
14899 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14900 }
14901 }
14902
14903 /**
14904 * {@hide}
14905 *
14906 * @return true if the view belongs to the root namespace, false otherwise
14907 */
14908 public boolean isRootNamespace() {
14909 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14910 }
14911
14912 /**
14913 * Returns this view's identifier.
14914 *
14915 * @return a positive integer used to identify the view or {@link #NO_ID}
14916 * if the view has no ID
14917 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014918 * @see #setId(int)
14919 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014920 * @attr ref android.R.styleable#View_id
14921 */
14922 @ViewDebug.CapturedViewProperty
14923 public int getId() {
14924 return mID;
14925 }
14926
14927 /**
14928 * Returns this view's tag.
14929 *
14930 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014931 *
14932 * @see #setTag(Object)
14933 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014934 */
14935 @ViewDebug.ExportedProperty
14936 public Object getTag() {
14937 return mTag;
14938 }
14939
14940 /**
14941 * Sets the tag associated with this view. A tag can be used to mark
14942 * a view in its hierarchy and does not have to be unique within the
14943 * hierarchy. Tags can also be used to store data within a view without
14944 * resorting to another data structure.
14945 *
14946 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014947 *
14948 * @see #getTag()
14949 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014950 */
14951 public void setTag(final Object tag) {
14952 mTag = tag;
14953 }
14954
14955 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014956 * Returns the tag associated with this view and the specified key.
14957 *
14958 * @param key The key identifying the tag
14959 *
14960 * @return the Object stored in this view as a tag
14961 *
14962 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014963 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014964 */
14965 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014966 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014967 return null;
14968 }
14969
14970 /**
14971 * Sets a tag associated with this view and a key. A tag can be used
14972 * to mark a view in its hierarchy and does not have to be unique within
14973 * the hierarchy. Tags can also be used to store data within a view
14974 * without resorting to another data structure.
14975 *
14976 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014977 * application to ensure it is unique (see the <a
14978 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14979 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014980 * the Android framework or not associated with any package will cause
14981 * an {@link IllegalArgumentException} to be thrown.
14982 *
14983 * @param key The key identifying the tag
14984 * @param tag An Object to tag the view with
14985 *
14986 * @throws IllegalArgumentException If they specified key is not valid
14987 *
14988 * @see #setTag(Object)
14989 * @see #getTag(int)
14990 */
14991 public void setTag(int key, final Object tag) {
14992 // If the package id is 0x00 or 0x01, it's either an undefined package
14993 // or a framework id
14994 if ((key >>> 24) < 2) {
14995 throw new IllegalArgumentException("The key must be an application-specific "
14996 + "resource id.");
14997 }
14998
Adam Powell2b2f6d62011-09-23 11:15:39 -070014999 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015000 }
15001
15002 /**
15003 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15004 * framework id.
15005 *
15006 * @hide
15007 */
15008 public void setTagInternal(int key, Object tag) {
15009 if ((key >>> 24) != 0x1) {
15010 throw new IllegalArgumentException("The key must be a framework-specific "
15011 + "resource id.");
15012 }
15013
Adam Powell2b2f6d62011-09-23 11:15:39 -070015014 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015015 }
15016
Adam Powell2b2f6d62011-09-23 11:15:39 -070015017 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015018 if (mKeyedTags == null) {
15019 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015020 }
15021
Adam Powell7db82ac2011-09-22 19:44:04 -070015022 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015023 }
15024
15025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015026 * Prints information about this view in the log output, with the tag
15027 * {@link #VIEW_LOG_TAG}.
15028 *
15029 * @hide
15030 */
15031 public void debug() {
15032 debug(0);
15033 }
15034
15035 /**
15036 * Prints information about this view in the log output, with the tag
15037 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15038 * indentation defined by the <code>depth</code>.
15039 *
15040 * @param depth the indentation level
15041 *
15042 * @hide
15043 */
15044 protected void debug(int depth) {
15045 String output = debugIndent(depth - 1);
15046
15047 output += "+ " + this;
15048 int id = getId();
15049 if (id != -1) {
15050 output += " (id=" + id + ")";
15051 }
15052 Object tag = getTag();
15053 if (tag != null) {
15054 output += " (tag=" + tag + ")";
15055 }
15056 Log.d(VIEW_LOG_TAG, output);
15057
15058 if ((mPrivateFlags & FOCUSED) != 0) {
15059 output = debugIndent(depth) + " FOCUSED";
15060 Log.d(VIEW_LOG_TAG, output);
15061 }
15062
15063 output = debugIndent(depth);
15064 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15065 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15066 + "} ";
15067 Log.d(VIEW_LOG_TAG, output);
15068
15069 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15070 || mPaddingBottom != 0) {
15071 output = debugIndent(depth);
15072 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15073 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15074 Log.d(VIEW_LOG_TAG, output);
15075 }
15076
15077 output = debugIndent(depth);
15078 output += "mMeasureWidth=" + mMeasuredWidth +
15079 " mMeasureHeight=" + mMeasuredHeight;
15080 Log.d(VIEW_LOG_TAG, output);
15081
15082 output = debugIndent(depth);
15083 if (mLayoutParams == null) {
15084 output += "BAD! no layout params";
15085 } else {
15086 output = mLayoutParams.debug(output);
15087 }
15088 Log.d(VIEW_LOG_TAG, output);
15089
15090 output = debugIndent(depth);
15091 output += "flags={";
15092 output += View.printFlags(mViewFlags);
15093 output += "}";
15094 Log.d(VIEW_LOG_TAG, output);
15095
15096 output = debugIndent(depth);
15097 output += "privateFlags={";
15098 output += View.printPrivateFlags(mPrivateFlags);
15099 output += "}";
15100 Log.d(VIEW_LOG_TAG, output);
15101 }
15102
15103 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015104 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015105 *
15106 * @param depth the indentation level
15107 * @return a String containing (depth * 2 + 3) * 2 white spaces
15108 *
15109 * @hide
15110 */
15111 protected static String debugIndent(int depth) {
15112 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15113 for (int i = 0; i < (depth * 2) + 3; i++) {
15114 spaces.append(' ').append(' ');
15115 }
15116 return spaces.toString();
15117 }
15118
15119 /**
15120 * <p>Return the offset of the widget's text baseline from the widget's top
15121 * boundary. If this widget does not support baseline alignment, this
15122 * method returns -1. </p>
15123 *
15124 * @return the offset of the baseline within the widget's bounds or -1
15125 * if baseline alignment is not supported
15126 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015127 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 public int getBaseline() {
15129 return -1;
15130 }
15131
15132 /**
15133 * Call this when something has changed which has invalidated the
15134 * layout of this view. This will schedule a layout pass of the view
15135 * tree.
15136 */
15137 public void requestLayout() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015138 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015139 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015140
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015141 if (mLayoutParams != null) {
15142 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
15143 }
15144
15145 if (mParent != null && !mParent.isLayoutRequested()) {
15146 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015147 }
15148 }
15149
15150 /**
15151 * Forces this view to be laid out during the next layout pass.
15152 * This method does not call requestLayout() or forceLayout()
15153 * on the parent.
15154 */
15155 public void forceLayout() {
15156 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080015157 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015158 }
15159
15160 /**
15161 * <p>
15162 * This is called to find out how big a view should be. The parent
15163 * supplies constraint information in the width and height parameters.
15164 * </p>
15165 *
15166 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015167 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015168 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015169 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015170 * </p>
15171 *
15172 *
15173 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15174 * parent
15175 * @param heightMeasureSpec Vertical space requirements as imposed by the
15176 * parent
15177 *
15178 * @see #onMeasure(int, int)
15179 */
15180 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
15181 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
15182 widthMeasureSpec != mOldWidthMeasureSpec ||
15183 heightMeasureSpec != mOldHeightMeasureSpec) {
15184
15185 // first clears the measured dimension flag
15186 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
15187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015188 // measure ourselves, this should set the measured dimension flag back
15189 onMeasure(widthMeasureSpec, heightMeasureSpec);
15190
15191 // flag not set, setMeasuredDimension() was not invoked, we raise
15192 // an exception to warn the developer
15193 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
15194 throw new IllegalStateException("onMeasure() did not set the"
15195 + " measured dimension by calling"
15196 + " setMeasuredDimension()");
15197 }
15198
15199 mPrivateFlags |= LAYOUT_REQUIRED;
15200 }
15201
15202 mOldWidthMeasureSpec = widthMeasureSpec;
15203 mOldHeightMeasureSpec = heightMeasureSpec;
15204 }
15205
15206 /**
15207 * <p>
15208 * Measure the view and its content to determine the measured width and the
15209 * measured height. This method is invoked by {@link #measure(int, int)} and
15210 * should be overriden by subclasses to provide accurate and efficient
15211 * measurement of their contents.
15212 * </p>
15213 *
15214 * <p>
15215 * <strong>CONTRACT:</strong> When overriding this method, you
15216 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15217 * measured width and height of this view. Failure to do so will trigger an
15218 * <code>IllegalStateException</code>, thrown by
15219 * {@link #measure(int, int)}. Calling the superclass'
15220 * {@link #onMeasure(int, int)} is a valid use.
15221 * </p>
15222 *
15223 * <p>
15224 * The base class implementation of measure defaults to the background size,
15225 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15226 * override {@link #onMeasure(int, int)} to provide better measurements of
15227 * their content.
15228 * </p>
15229 *
15230 * <p>
15231 * If this method is overridden, it is the subclass's responsibility to make
15232 * sure the measured height and width are at least the view's minimum height
15233 * and width ({@link #getSuggestedMinimumHeight()} and
15234 * {@link #getSuggestedMinimumWidth()}).
15235 * </p>
15236 *
15237 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15238 * The requirements are encoded with
15239 * {@link android.view.View.MeasureSpec}.
15240 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15241 * The requirements are encoded with
15242 * {@link android.view.View.MeasureSpec}.
15243 *
15244 * @see #getMeasuredWidth()
15245 * @see #getMeasuredHeight()
15246 * @see #setMeasuredDimension(int, int)
15247 * @see #getSuggestedMinimumHeight()
15248 * @see #getSuggestedMinimumWidth()
15249 * @see android.view.View.MeasureSpec#getMode(int)
15250 * @see android.view.View.MeasureSpec#getSize(int)
15251 */
15252 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15253 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15254 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15255 }
15256
15257 /**
15258 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15259 * measured width and measured height. Failing to do so will trigger an
15260 * exception at measurement time.</p>
15261 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015262 * @param measuredWidth The measured width of this view. May be a complex
15263 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15264 * {@link #MEASURED_STATE_TOO_SMALL}.
15265 * @param measuredHeight The measured height of this view. May be a complex
15266 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15267 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015268 */
15269 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15270 mMeasuredWidth = measuredWidth;
15271 mMeasuredHeight = measuredHeight;
15272
15273 mPrivateFlags |= MEASURED_DIMENSION_SET;
15274 }
15275
15276 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015277 * Merge two states as returned by {@link #getMeasuredState()}.
15278 * @param curState The current state as returned from a view or the result
15279 * of combining multiple views.
15280 * @param newState The new view state to combine.
15281 * @return Returns a new integer reflecting the combination of the two
15282 * states.
15283 */
15284 public static int combineMeasuredStates(int curState, int newState) {
15285 return curState | newState;
15286 }
15287
15288 /**
15289 * Version of {@link #resolveSizeAndState(int, int, int)}
15290 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15291 */
15292 public static int resolveSize(int size, int measureSpec) {
15293 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15294 }
15295
15296 /**
15297 * Utility to reconcile a desired size and state, with constraints imposed
15298 * by a MeasureSpec. Will take the desired size, unless a different size
15299 * is imposed by the constraints. The returned value is a compound integer,
15300 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15301 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15302 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 *
15304 * @param size How big the view wants to be
15305 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015306 * @return Size information bit mask as defined by
15307 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015308 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015309 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015310 int result = size;
15311 int specMode = MeasureSpec.getMode(measureSpec);
15312 int specSize = MeasureSpec.getSize(measureSpec);
15313 switch (specMode) {
15314 case MeasureSpec.UNSPECIFIED:
15315 result = size;
15316 break;
15317 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015318 if (specSize < size) {
15319 result = specSize | MEASURED_STATE_TOO_SMALL;
15320 } else {
15321 result = size;
15322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015323 break;
15324 case MeasureSpec.EXACTLY:
15325 result = specSize;
15326 break;
15327 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015328 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015329 }
15330
15331 /**
15332 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015333 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015334 * by the MeasureSpec.
15335 *
15336 * @param size Default size for this view
15337 * @param measureSpec Constraints imposed by the parent
15338 * @return The size this view should be.
15339 */
15340 public static int getDefaultSize(int size, int measureSpec) {
15341 int result = size;
15342 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015343 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015344
15345 switch (specMode) {
15346 case MeasureSpec.UNSPECIFIED:
15347 result = size;
15348 break;
15349 case MeasureSpec.AT_MOST:
15350 case MeasureSpec.EXACTLY:
15351 result = specSize;
15352 break;
15353 }
15354 return result;
15355 }
15356
15357 /**
15358 * Returns the suggested minimum height that the view should use. This
15359 * returns the maximum of the view's minimum height
15360 * and the background's minimum height
15361 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15362 * <p>
15363 * When being used in {@link #onMeasure(int, int)}, the caller should still
15364 * ensure the returned height is within the requirements of the parent.
15365 *
15366 * @return The suggested minimum height of the view.
15367 */
15368 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015369 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015371 }
15372
15373 /**
15374 * Returns the suggested minimum width that the view should use. This
15375 * returns the maximum of the view's minimum width)
15376 * and the background's minimum width
15377 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15378 * <p>
15379 * When being used in {@link #onMeasure(int, int)}, the caller should still
15380 * ensure the returned width is within the requirements of the parent.
15381 *
15382 * @return The suggested minimum width of the view.
15383 */
15384 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015385 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015387
Philip Milne6c8ea062012-04-03 17:38:43 -070015388 /**
15389 * Returns the minimum height of the view.
15390 *
15391 * @return the minimum height the view will try to be.
15392 *
15393 * @see #setMinimumHeight(int)
15394 *
15395 * @attr ref android.R.styleable#View_minHeight
15396 */
15397 public int getMinimumHeight() {
15398 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015399 }
15400
15401 /**
15402 * Sets the minimum height of the view. It is not guaranteed the view will
15403 * be able to achieve this minimum height (for example, if its parent layout
15404 * constrains it with less available height).
15405 *
15406 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015407 *
15408 * @see #getMinimumHeight()
15409 *
15410 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015411 */
15412 public void setMinimumHeight(int minHeight) {
15413 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015414 requestLayout();
15415 }
15416
15417 /**
15418 * Returns the minimum width of the view.
15419 *
15420 * @return the minimum width the view will try to be.
15421 *
15422 * @see #setMinimumWidth(int)
15423 *
15424 * @attr ref android.R.styleable#View_minWidth
15425 */
15426 public int getMinimumWidth() {
15427 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015428 }
15429
15430 /**
15431 * Sets the minimum width of the view. It is not guaranteed the view will
15432 * be able to achieve this minimum width (for example, if its parent layout
15433 * constrains it with less available width).
15434 *
15435 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015436 *
15437 * @see #getMinimumWidth()
15438 *
15439 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015440 */
15441 public void setMinimumWidth(int minWidth) {
15442 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015443 requestLayout();
15444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015445 }
15446
15447 /**
15448 * Get the animation currently associated with this view.
15449 *
15450 * @return The animation that is currently playing or
15451 * scheduled to play for this view.
15452 */
15453 public Animation getAnimation() {
15454 return mCurrentAnimation;
15455 }
15456
15457 /**
15458 * Start the specified animation now.
15459 *
15460 * @param animation the animation to start now
15461 */
15462 public void startAnimation(Animation animation) {
15463 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15464 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015465 invalidateParentCaches();
15466 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015467 }
15468
15469 /**
15470 * Cancels any animations for this view.
15471 */
15472 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015473 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015474 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015476 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015477 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015478 }
15479
15480 /**
15481 * Sets the next animation to play for this view.
15482 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015483 * {@link #startAnimation(android.view.animation.Animation)} instead.
15484 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015485 * control over the start time and invalidation, but you
15486 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015487 * 2) the view's parent (which controls animations on its children)
15488 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015489 * start.
15490 *
15491 * @param animation The next animation, or null.
15492 */
15493 public void setAnimation(Animation animation) {
15494 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015496 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015497 // If the screen is off assume the animation start time is now instead of
15498 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15499 // would cause the animation to start when the screen turns back on
15500 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15501 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15502 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015504 animation.reset();
15505 }
15506 }
15507
15508 /**
15509 * Invoked by a parent ViewGroup to notify the start of the animation
15510 * currently associated with this view. If you override this method,
15511 * always call super.onAnimationStart();
15512 *
15513 * @see #setAnimation(android.view.animation.Animation)
15514 * @see #getAnimation()
15515 */
15516 protected void onAnimationStart() {
15517 mPrivateFlags |= ANIMATION_STARTED;
15518 }
15519
15520 /**
15521 * Invoked by a parent ViewGroup to notify the end of the animation
15522 * currently associated with this view. If you override this method,
15523 * always call super.onAnimationEnd();
15524 *
15525 * @see #setAnimation(android.view.animation.Animation)
15526 * @see #getAnimation()
15527 */
15528 protected void onAnimationEnd() {
15529 mPrivateFlags &= ~ANIMATION_STARTED;
15530 }
15531
15532 /**
15533 * Invoked if there is a Transform that involves alpha. Subclass that can
15534 * draw themselves with the specified alpha should return true, and then
15535 * respect that alpha when their onDraw() is called. If this returns false
15536 * then the view may be redirected to draw into an offscreen buffer to
15537 * fulfill the request, which will look fine, but may be slower than if the
15538 * subclass handles it internally. The default implementation returns false.
15539 *
15540 * @param alpha The alpha (0..255) to apply to the view's drawing
15541 * @return true if the view can draw with the specified alpha.
15542 */
15543 protected boolean onSetAlpha(int alpha) {
15544 return false;
15545 }
15546
15547 /**
15548 * This is used by the RootView to perform an optimization when
15549 * the view hierarchy contains one or several SurfaceView.
15550 * SurfaceView is always considered transparent, but its children are not,
15551 * therefore all View objects remove themselves from the global transparent
15552 * region (passed as a parameter to this function).
15553 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015554 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015555 *
15556 * @return Returns true if the effective visibility of the view at this
15557 * point is opaque, regardless of the transparent region; returns false
15558 * if it is possible for underlying windows to be seen behind the view.
15559 *
15560 * {@hide}
15561 */
15562 public boolean gatherTransparentRegion(Region region) {
15563 final AttachInfo attachInfo = mAttachInfo;
15564 if (region != null && attachInfo != null) {
15565 final int pflags = mPrivateFlags;
15566 if ((pflags & SKIP_DRAW) == 0) {
15567 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15568 // remove it from the transparent region.
15569 final int[] location = attachInfo.mTransparentLocation;
15570 getLocationInWindow(location);
15571 region.op(location[0], location[1], location[0] + mRight - mLeft,
15572 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015573 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015574 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15575 // exists, so we remove the background drawable's non-transparent
15576 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015577 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015578 }
15579 }
15580 return true;
15581 }
15582
15583 /**
15584 * Play a sound effect for this view.
15585 *
15586 * <p>The framework will play sound effects for some built in actions, such as
15587 * clicking, but you may wish to play these effects in your widget,
15588 * for instance, for internal navigation.
15589 *
15590 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15591 * {@link #isSoundEffectsEnabled()} is true.
15592 *
15593 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15594 */
15595 public void playSoundEffect(int soundConstant) {
15596 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15597 return;
15598 }
15599 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15600 }
15601
15602 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015603 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015604 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015605 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015606 *
15607 * <p>The framework will provide haptic feedback for some built in actions,
15608 * such as long presses, but you may wish to provide feedback for your
15609 * own widget.
15610 *
15611 * <p>The feedback will only be performed if
15612 * {@link #isHapticFeedbackEnabled()} is true.
15613 *
15614 * @param feedbackConstant One of the constants defined in
15615 * {@link HapticFeedbackConstants}
15616 */
15617 public boolean performHapticFeedback(int feedbackConstant) {
15618 return performHapticFeedback(feedbackConstant, 0);
15619 }
15620
15621 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015622 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015623 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015624 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015625 *
15626 * @param feedbackConstant One of the constants defined in
15627 * {@link HapticFeedbackConstants}
15628 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15629 */
15630 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15631 if (mAttachInfo == null) {
15632 return false;
15633 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015634 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015635 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015636 && !isHapticFeedbackEnabled()) {
15637 return false;
15638 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015639 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15640 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015641 }
15642
15643 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015644 * Request that the visibility of the status bar or other screen/window
15645 * decorations be changed.
15646 *
15647 * <p>This method is used to put the over device UI into temporary modes
15648 * where the user's attention is focused more on the application content,
15649 * by dimming or hiding surrounding system affordances. This is typically
15650 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15651 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15652 * to be placed behind the action bar (and with these flags other system
15653 * affordances) so that smooth transitions between hiding and showing them
15654 * can be done.
15655 *
15656 * <p>Two representative examples of the use of system UI visibility is
15657 * implementing a content browsing application (like a magazine reader)
15658 * and a video playing application.
15659 *
15660 * <p>The first code shows a typical implementation of a View in a content
15661 * browsing application. In this implementation, the application goes
15662 * into a content-oriented mode by hiding the status bar and action bar,
15663 * and putting the navigation elements into lights out mode. The user can
15664 * then interact with content while in this mode. Such an application should
15665 * provide an easy way for the user to toggle out of the mode (such as to
15666 * check information in the status bar or access notifications). In the
15667 * implementation here, this is done simply by tapping on the content.
15668 *
15669 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15670 * content}
15671 *
15672 * <p>This second code sample shows a typical implementation of a View
15673 * in a video playing application. In this situation, while the video is
15674 * playing the application would like to go into a complete full-screen mode,
15675 * to use as much of the display as possible for the video. When in this state
15676 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015677 * touching on the screen to pop the UI out of full screen mode. See
15678 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015679 *
15680 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15681 * content}
15682 *
15683 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15684 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15685 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15686 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015687 */
15688 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015689 if (visibility != mSystemUiVisibility) {
15690 mSystemUiVisibility = visibility;
15691 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15692 mParent.recomputeViewAttributes(this);
15693 }
Joe Onorato664644d2011-01-23 17:53:23 -080015694 }
15695 }
15696
15697 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015698 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15699 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15700 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15701 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15702 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015703 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015704 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015705 return mSystemUiVisibility;
15706 }
15707
Scott Mainec6331b2011-05-24 16:55:56 -070015708 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015709 * Returns the current system UI visibility that is currently set for
15710 * the entire window. This is the combination of the
15711 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15712 * views in the window.
15713 */
15714 public int getWindowSystemUiVisibility() {
15715 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15716 }
15717
15718 /**
15719 * Override to find out when the window's requested system UI visibility
15720 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15721 * This is different from the callbacks recieved through
15722 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15723 * in that this is only telling you about the local request of the window,
15724 * not the actual values applied by the system.
15725 */
15726 public void onWindowSystemUiVisibilityChanged(int visible) {
15727 }
15728
15729 /**
15730 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15731 * the view hierarchy.
15732 */
15733 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15734 onWindowSystemUiVisibilityChanged(visible);
15735 }
15736
15737 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015738 * Set a listener to receive callbacks when the visibility of the system bar changes.
15739 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15740 */
Joe Onorato664644d2011-01-23 17:53:23 -080015741 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015742 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015743 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15744 mParent.recomputeViewAttributes(this);
15745 }
15746 }
15747
15748 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015749 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15750 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015751 */
15752 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015753 ListenerInfo li = mListenerInfo;
15754 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15755 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015756 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015757 }
15758 }
15759
Dianne Hackborncf675782012-05-10 15:07:24 -070015760 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015761 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15762 if (val != mSystemUiVisibility) {
15763 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015764 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015765 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015766 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015767 }
15768
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015769 /** @hide */
15770 public void setDisabledSystemUiVisibility(int flags) {
15771 if (mAttachInfo != null) {
15772 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15773 mAttachInfo.mDisabledSystemUiVisibility = flags;
15774 if (mParent != null) {
15775 mParent.recomputeViewAttributes(this);
15776 }
15777 }
15778 }
15779 }
15780
Joe Onorato664644d2011-01-23 17:53:23 -080015781 /**
Joe Malin32736f02011-01-19 16:14:20 -080015782 * Creates an image that the system displays during the drag and drop
15783 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15784 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15785 * appearance as the given View. The default also positions the center of the drag shadow
15786 * directly under the touch point. If no View is provided (the constructor with no parameters
15787 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15788 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15789 * default is an invisible drag shadow.
15790 * <p>
15791 * You are not required to use the View you provide to the constructor as the basis of the
15792 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15793 * anything you want as the drag shadow.
15794 * </p>
15795 * <p>
15796 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15797 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15798 * size and position of the drag shadow. It uses this data to construct a
15799 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15800 * so that your application can draw the shadow image in the Canvas.
15801 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015802 *
15803 * <div class="special reference">
15804 * <h3>Developer Guides</h3>
15805 * <p>For a guide to implementing drag and drop features, read the
15806 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15807 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015808 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015809 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015810 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015811
15812 /**
Joe Malin32736f02011-01-19 16:14:20 -080015813 * Constructs a shadow image builder based on a View. By default, the resulting drag
15814 * shadow will have the same appearance and dimensions as the View, with the touch point
15815 * over the center of the View.
15816 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015817 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015818 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015819 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015820 }
15821
Christopher Tate17ed60c2011-01-18 12:50:26 -080015822 /**
15823 * Construct a shadow builder object with no associated View. This
15824 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15825 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15826 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015827 * reference to any View object. If they are not overridden, then the result is an
15828 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015829 */
15830 public DragShadowBuilder() {
15831 mView = new WeakReference<View>(null);
15832 }
15833
15834 /**
15835 * Returns the View object that had been passed to the
15836 * {@link #View.DragShadowBuilder(View)}
15837 * constructor. If that View parameter was {@code null} or if the
15838 * {@link #View.DragShadowBuilder()}
15839 * constructor was used to instantiate the builder object, this method will return
15840 * null.
15841 *
15842 * @return The View object associate with this builder object.
15843 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015844 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015845 final public View getView() {
15846 return mView.get();
15847 }
15848
Christopher Tate2c095f32010-10-04 14:13:40 -070015849 /**
Joe Malin32736f02011-01-19 16:14:20 -080015850 * Provides the metrics for the shadow image. These include the dimensions of
15851 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015852 * be centered under the touch location while dragging.
15853 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015854 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015855 * same as the dimensions of the View itself and centers the shadow under
15856 * the touch point.
15857 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015858 *
Joe Malin32736f02011-01-19 16:14:20 -080015859 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15860 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15861 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15862 * image.
15863 *
15864 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15865 * shadow image that should be underneath the touch point during the drag and drop
15866 * operation. Your application must set {@link android.graphics.Point#x} to the
15867 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015868 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015869 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015870 final View view = mView.get();
15871 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015872 shadowSize.set(view.getWidth(), view.getHeight());
15873 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015874 } else {
15875 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15876 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015877 }
15878
15879 /**
Joe Malin32736f02011-01-19 16:14:20 -080015880 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15881 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015882 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015883 *
Joe Malin32736f02011-01-19 16:14:20 -080015884 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015885 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015886 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015887 final View view = mView.get();
15888 if (view != null) {
15889 view.draw(canvas);
15890 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015891 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015892 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015893 }
15894 }
15895
15896 /**
Joe Malin32736f02011-01-19 16:14:20 -080015897 * Starts a drag and drop operation. When your application calls this method, it passes a
15898 * {@link android.view.View.DragShadowBuilder} object to the system. The
15899 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15900 * to get metrics for the drag shadow, and then calls the object's
15901 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15902 * <p>
15903 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15904 * drag events to all the View objects in your application that are currently visible. It does
15905 * this either by calling the View object's drag listener (an implementation of
15906 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15907 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15908 * Both are passed a {@link android.view.DragEvent} object that has a
15909 * {@link android.view.DragEvent#getAction()} value of
15910 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15911 * </p>
15912 * <p>
15913 * Your application can invoke startDrag() on any attached View object. The View object does not
15914 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15915 * be related to the View the user selected for dragging.
15916 * </p>
15917 * @param data A {@link android.content.ClipData} object pointing to the data to be
15918 * transferred by the drag and drop operation.
15919 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15920 * drag shadow.
15921 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15922 * drop operation. This Object is put into every DragEvent object sent by the system during the
15923 * current drag.
15924 * <p>
15925 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15926 * to the target Views. For example, it can contain flags that differentiate between a
15927 * a copy operation and a move operation.
15928 * </p>
15929 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15930 * so the parameter should be set to 0.
15931 * @return {@code true} if the method completes successfully, or
15932 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15933 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015934 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015935 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015936 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015937 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015938 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015939 }
15940 boolean okay = false;
15941
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015942 Point shadowSize = new Point();
15943 Point shadowTouchPoint = new Point();
15944 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015945
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015946 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15947 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15948 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015949 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015950
Chris Tatea32dcf72010-10-14 12:13:50 -070015951 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015952 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15953 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015954 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015955 Surface surface = new Surface();
15956 try {
15957 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015958 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015959 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015960 + " surface=" + surface);
15961 if (token != null) {
15962 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015963 try {
Chris Tate6b391282010-10-14 15:48:59 -070015964 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015965 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015966 } finally {
15967 surface.unlockCanvasAndPost(canvas);
15968 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015969
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015970 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015971
15972 // Cache the local state object for delivery with DragEvents
15973 root.setLocalDragState(myLocalState);
15974
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015975 // repurpose 'shadowSize' for the last touch point
15976 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015977
Christopher Tatea53146c2010-09-07 11:57:52 -070015978 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015979 shadowSize.x, shadowSize.y,
15980 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015981 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015982
15983 // Off and running! Release our local surface instance; the drag
15984 // shadow surface is now managed by the system process.
15985 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015986 }
15987 } catch (Exception e) {
15988 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15989 surface.destroy();
15990 }
15991
15992 return okay;
15993 }
15994
Christopher Tatea53146c2010-09-07 11:57:52 -070015995 /**
Joe Malin32736f02011-01-19 16:14:20 -080015996 * Handles drag events sent by the system following a call to
15997 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15998 *<p>
15999 * When the system calls this method, it passes a
16000 * {@link android.view.DragEvent} object. A call to
16001 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16002 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16003 * operation.
16004 * @param event The {@link android.view.DragEvent} sent by the system.
16005 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16006 * in DragEvent, indicating the type of drag event represented by this object.
16007 * @return {@code true} if the method was successful, otherwise {@code false}.
16008 * <p>
16009 * The method should return {@code true} in response to an action type of
16010 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16011 * operation.
16012 * </p>
16013 * <p>
16014 * The method should also return {@code true} in response to an action type of
16015 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16016 * {@code false} if it didn't.
16017 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016018 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016019 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016020 return false;
16021 }
16022
16023 /**
Joe Malin32736f02011-01-19 16:14:20 -080016024 * Detects if this View is enabled and has a drag event listener.
16025 * If both are true, then it calls the drag event listener with the
16026 * {@link android.view.DragEvent} it received. If the drag event listener returns
16027 * {@code true}, then dispatchDragEvent() returns {@code true}.
16028 * <p>
16029 * For all other cases, the method calls the
16030 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16031 * method and returns its result.
16032 * </p>
16033 * <p>
16034 * This ensures that a drag event is always consumed, even if the View does not have a drag
16035 * event listener. However, if the View has a listener and the listener returns true, then
16036 * onDragEvent() is not called.
16037 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016038 */
16039 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016040 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016041 ListenerInfo li = mListenerInfo;
16042 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16043 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016044 return true;
16045 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016046 return onDragEvent(event);
16047 }
16048
Christopher Tate3d4bf172011-03-28 16:16:46 -070016049 boolean canAcceptDrag() {
16050 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
16051 }
16052
Christopher Tatea53146c2010-09-07 11:57:52 -070016053 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016054 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16055 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016056 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016057 */
16058 public void onCloseSystemDialogs(String reason) {
16059 }
Joe Malin32736f02011-01-19 16:14:20 -080016060
Dianne Hackbornffa42482009-09-23 22:20:11 -070016061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016062 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016063 * update a Region being computed for
16064 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016065 * that any non-transparent parts of the Drawable are removed from the
16066 * given transparent region.
16067 *
16068 * @param dr The Drawable whose transparency is to be applied to the region.
16069 * @param region A Region holding the current transparency information,
16070 * where any parts of the region that are set are considered to be
16071 * transparent. On return, this region will be modified to have the
16072 * transparency information reduced by the corresponding parts of the
16073 * Drawable that are not transparent.
16074 * {@hide}
16075 */
16076 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16077 if (DBG) {
16078 Log.i("View", "Getting transparent region for: " + this);
16079 }
16080 final Region r = dr.getTransparentRegion();
16081 final Rect db = dr.getBounds();
16082 final AttachInfo attachInfo = mAttachInfo;
16083 if (r != null && attachInfo != null) {
16084 final int w = getRight()-getLeft();
16085 final int h = getBottom()-getTop();
16086 if (db.left > 0) {
16087 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16088 r.op(0, 0, db.left, h, Region.Op.UNION);
16089 }
16090 if (db.right < w) {
16091 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16092 r.op(db.right, 0, w, h, Region.Op.UNION);
16093 }
16094 if (db.top > 0) {
16095 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16096 r.op(0, 0, w, db.top, Region.Op.UNION);
16097 }
16098 if (db.bottom < h) {
16099 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16100 r.op(0, db.bottom, w, h, Region.Op.UNION);
16101 }
16102 final int[] location = attachInfo.mTransparentLocation;
16103 getLocationInWindow(location);
16104 r.translate(location[0], location[1]);
16105 region.op(r, Region.Op.INTERSECT);
16106 } else {
16107 region.op(db, Region.Op.DIFFERENCE);
16108 }
16109 }
16110
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016111 private void checkForLongClick(int delayOffset) {
16112 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16113 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016114
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016115 if (mPendingCheckForLongPress == null) {
16116 mPendingCheckForLongPress = new CheckForLongPress();
16117 }
16118 mPendingCheckForLongPress.rememberWindowAttachCount();
16119 postDelayed(mPendingCheckForLongPress,
16120 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016122 }
16123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016124 /**
16125 * Inflate a view from an XML resource. This convenience method wraps the {@link
16126 * LayoutInflater} class, which provides a full range of options for view inflation.
16127 *
16128 * @param context The Context object for your activity or application.
16129 * @param resource The resource ID to inflate
16130 * @param root A view group that will be the parent. Used to properly inflate the
16131 * layout_* parameters.
16132 * @see LayoutInflater
16133 */
16134 public static View inflate(Context context, int resource, ViewGroup root) {
16135 LayoutInflater factory = LayoutInflater.from(context);
16136 return factory.inflate(resource, root);
16137 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016139 /**
Adam Powell637d3372010-08-25 14:37:03 -070016140 * Scroll the view with standard behavior for scrolling beyond the normal
16141 * content boundaries. Views that call this method should override
16142 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16143 * results of an over-scroll operation.
16144 *
16145 * Views can use this method to handle any touch or fling-based scrolling.
16146 *
16147 * @param deltaX Change in X in pixels
16148 * @param deltaY Change in Y in pixels
16149 * @param scrollX Current X scroll value in pixels before applying deltaX
16150 * @param scrollY Current Y scroll value in pixels before applying deltaY
16151 * @param scrollRangeX Maximum content scroll range along the X axis
16152 * @param scrollRangeY Maximum content scroll range along the Y axis
16153 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16154 * along the X axis.
16155 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16156 * along the Y axis.
16157 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16158 * @return true if scrolling was clamped to an over-scroll boundary along either
16159 * axis, false otherwise.
16160 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016161 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016162 protected boolean overScrollBy(int deltaX, int deltaY,
16163 int scrollX, int scrollY,
16164 int scrollRangeX, int scrollRangeY,
16165 int maxOverScrollX, int maxOverScrollY,
16166 boolean isTouchEvent) {
16167 final int overScrollMode = mOverScrollMode;
16168 final boolean canScrollHorizontal =
16169 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16170 final boolean canScrollVertical =
16171 computeVerticalScrollRange() > computeVerticalScrollExtent();
16172 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16173 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16174 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16175 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16176
16177 int newScrollX = scrollX + deltaX;
16178 if (!overScrollHorizontal) {
16179 maxOverScrollX = 0;
16180 }
16181
16182 int newScrollY = scrollY + deltaY;
16183 if (!overScrollVertical) {
16184 maxOverScrollY = 0;
16185 }
16186
16187 // Clamp values if at the limits and record
16188 final int left = -maxOverScrollX;
16189 final int right = maxOverScrollX + scrollRangeX;
16190 final int top = -maxOverScrollY;
16191 final int bottom = maxOverScrollY + scrollRangeY;
16192
16193 boolean clampedX = false;
16194 if (newScrollX > right) {
16195 newScrollX = right;
16196 clampedX = true;
16197 } else if (newScrollX < left) {
16198 newScrollX = left;
16199 clampedX = true;
16200 }
16201
16202 boolean clampedY = false;
16203 if (newScrollY > bottom) {
16204 newScrollY = bottom;
16205 clampedY = true;
16206 } else if (newScrollY < top) {
16207 newScrollY = top;
16208 clampedY = true;
16209 }
16210
16211 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16212
16213 return clampedX || clampedY;
16214 }
16215
16216 /**
16217 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16218 * respond to the results of an over-scroll operation.
16219 *
16220 * @param scrollX New X scroll value in pixels
16221 * @param scrollY New Y scroll value in pixels
16222 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16223 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16224 */
16225 protected void onOverScrolled(int scrollX, int scrollY,
16226 boolean clampedX, boolean clampedY) {
16227 // Intentionally empty.
16228 }
16229
16230 /**
16231 * Returns the over-scroll mode for this view. The result will be
16232 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16233 * (allow over-scrolling only if the view content is larger than the container),
16234 * or {@link #OVER_SCROLL_NEVER}.
16235 *
16236 * @return This view's over-scroll mode.
16237 */
16238 public int getOverScrollMode() {
16239 return mOverScrollMode;
16240 }
16241
16242 /**
16243 * Set the over-scroll mode for this view. Valid over-scroll modes are
16244 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16245 * (allow over-scrolling only if the view content is larger than the container),
16246 * or {@link #OVER_SCROLL_NEVER}.
16247 *
16248 * Setting the over-scroll mode of a view will have an effect only if the
16249 * view is capable of scrolling.
16250 *
16251 * @param overScrollMode The new over-scroll mode for this view.
16252 */
16253 public void setOverScrollMode(int overScrollMode) {
16254 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16255 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16256 overScrollMode != OVER_SCROLL_NEVER) {
16257 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16258 }
16259 mOverScrollMode = overScrollMode;
16260 }
16261
16262 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016263 * Gets a scale factor that determines the distance the view should scroll
16264 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16265 * @return The vertical scroll scale factor.
16266 * @hide
16267 */
16268 protected float getVerticalScrollFactor() {
16269 if (mVerticalScrollFactor == 0) {
16270 TypedValue outValue = new TypedValue();
16271 if (!mContext.getTheme().resolveAttribute(
16272 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16273 throw new IllegalStateException(
16274 "Expected theme to define listPreferredItemHeight.");
16275 }
16276 mVerticalScrollFactor = outValue.getDimension(
16277 mContext.getResources().getDisplayMetrics());
16278 }
16279 return mVerticalScrollFactor;
16280 }
16281
16282 /**
16283 * Gets a scale factor that determines the distance the view should scroll
16284 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16285 * @return The horizontal scroll scale factor.
16286 * @hide
16287 */
16288 protected float getHorizontalScrollFactor() {
16289 // TODO: Should use something else.
16290 return getVerticalScrollFactor();
16291 }
16292
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016293 /**
16294 * Return the value specifying the text direction or policy that was set with
16295 * {@link #setTextDirection(int)}.
16296 *
16297 * @return the defined text direction. It can be one of:
16298 *
16299 * {@link #TEXT_DIRECTION_INHERIT},
16300 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16301 * {@link #TEXT_DIRECTION_ANY_RTL},
16302 * {@link #TEXT_DIRECTION_LTR},
16303 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016304 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016305 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016306 @ViewDebug.ExportedProperty(category = "text", mapping = {
16307 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16308 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16309 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16310 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16311 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16312 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16313 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016314 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016315 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016316 }
16317
16318 /**
16319 * Set the text direction.
16320 *
16321 * @param textDirection the direction to set. Should be one of:
16322 *
16323 * {@link #TEXT_DIRECTION_INHERIT},
16324 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16325 * {@link #TEXT_DIRECTION_ANY_RTL},
16326 * {@link #TEXT_DIRECTION_LTR},
16327 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016328 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016329 */
16330 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016331 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016332 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016333 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016334 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016335 // Set the new text direction
16336 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016337 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016338 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016339 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016340 }
16341 }
16342
16343 /**
16344 * Return the resolved text direction.
16345 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016346 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16347 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16348 * up the parent chain of the view. if there is no parent, then it will return the default
16349 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16350 *
16351 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016352 *
Doug Feltcb3791202011-07-07 11:57:48 -070016353 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16354 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016355 * {@link #TEXT_DIRECTION_LTR},
16356 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016357 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016358 */
16359 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016360 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016361 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016362 resolveTextDirection();
16363 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016364 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016365 }
16366
16367 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016368 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16369 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016370 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016371 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016372 // Reset any previous text direction resolution
16373 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
16374
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016375 if (hasRtlSupport()) {
16376 // Set resolved text direction flag depending on text direction flag
16377 final int textDirection = getTextDirection();
16378 switch(textDirection) {
16379 case TEXT_DIRECTION_INHERIT:
16380 if (canResolveTextDirection()) {
16381 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016382
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016383 // Set current resolved direction to the same value as the parent's one
16384 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16385 switch (parentResolvedDirection) {
16386 case TEXT_DIRECTION_FIRST_STRONG:
16387 case TEXT_DIRECTION_ANY_RTL:
16388 case TEXT_DIRECTION_LTR:
16389 case TEXT_DIRECTION_RTL:
16390 case TEXT_DIRECTION_LOCALE:
16391 mPrivateFlags2 |=
16392 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16393 break;
16394 default:
16395 // Default resolved direction is "first strong" heuristic
16396 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
16397 }
16398 } else {
16399 // We cannot do the resolution if there is no parent, so use the default one
16400 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016401 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016402 break;
16403 case TEXT_DIRECTION_FIRST_STRONG:
16404 case TEXT_DIRECTION_ANY_RTL:
16405 case TEXT_DIRECTION_LTR:
16406 case TEXT_DIRECTION_RTL:
16407 case TEXT_DIRECTION_LOCALE:
16408 // Resolved direction is the same as text direction
16409 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16410 break;
16411 default:
16412 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016413 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016414 }
16415 } else {
16416 // Default resolved direction is "first strong" heuristic
16417 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016418 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016419
16420 // Set to resolved
16421 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016422 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016423 }
16424
16425 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016426 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016427 * resolution should override this method.
16428 *
16429 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016430 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016431 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016432 }
16433
16434 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016435 * Check if text direction resolution can be done.
16436 *
16437 * @return true if text direction resolution can be done otherwise return false.
16438 */
16439 public boolean canResolveTextDirection() {
16440 switch (getTextDirection()) {
16441 case TEXT_DIRECTION_INHERIT:
16442 return (mParent != null) && (mParent instanceof ViewGroup);
16443 default:
16444 return true;
16445 }
16446 }
16447
16448 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016449 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016450 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016451 * reset is done.
16452 */
16453 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016454 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016455 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016456 }
16457
16458 /**
16459 * Called when text direction is reset. Subclasses that care about text direction reset should
16460 * override this method and do a reset of the text direction of their children. The default
16461 * implementation does nothing.
16462 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016463 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016464 }
16465
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016466 /**
16467 * Return the value specifying the text alignment or policy that was set with
16468 * {@link #setTextAlignment(int)}.
16469 *
16470 * @return the defined text alignment. It can be one of:
16471 *
16472 * {@link #TEXT_ALIGNMENT_INHERIT},
16473 * {@link #TEXT_ALIGNMENT_GRAVITY},
16474 * {@link #TEXT_ALIGNMENT_CENTER},
16475 * {@link #TEXT_ALIGNMENT_TEXT_START},
16476 * {@link #TEXT_ALIGNMENT_TEXT_END},
16477 * {@link #TEXT_ALIGNMENT_VIEW_START},
16478 * {@link #TEXT_ALIGNMENT_VIEW_END}
16479 */
16480 @ViewDebug.ExportedProperty(category = "text", mapping = {
16481 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16482 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16483 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16484 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16485 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16486 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16487 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16488 })
16489 public int getTextAlignment() {
16490 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16491 }
16492
16493 /**
16494 * Set the text alignment.
16495 *
16496 * @param textAlignment The text alignment to set. Should be one of
16497 *
16498 * {@link #TEXT_ALIGNMENT_INHERIT},
16499 * {@link #TEXT_ALIGNMENT_GRAVITY},
16500 * {@link #TEXT_ALIGNMENT_CENTER},
16501 * {@link #TEXT_ALIGNMENT_TEXT_START},
16502 * {@link #TEXT_ALIGNMENT_TEXT_END},
16503 * {@link #TEXT_ALIGNMENT_VIEW_START},
16504 * {@link #TEXT_ALIGNMENT_VIEW_END}
16505 *
16506 * @attr ref android.R.styleable#View_textAlignment
16507 */
16508 public void setTextAlignment(int textAlignment) {
16509 if (textAlignment != getTextAlignment()) {
16510 // Reset the current and resolved text alignment
16511 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16512 resetResolvedTextAlignment();
16513 // Set the new text alignment
16514 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16515 // Refresh
16516 requestLayout();
16517 invalidate(true);
16518 }
16519 }
16520
16521 /**
16522 * Return the resolved text alignment.
16523 *
16524 * The resolved text alignment. This needs resolution if the value is
16525 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16526 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16527 *
16528 * @return the resolved text alignment. Returns one of:
16529 *
16530 * {@link #TEXT_ALIGNMENT_GRAVITY},
16531 * {@link #TEXT_ALIGNMENT_CENTER},
16532 * {@link #TEXT_ALIGNMENT_TEXT_START},
16533 * {@link #TEXT_ALIGNMENT_TEXT_END},
16534 * {@link #TEXT_ALIGNMENT_VIEW_START},
16535 * {@link #TEXT_ALIGNMENT_VIEW_END}
16536 */
16537 @ViewDebug.ExportedProperty(category = "text", mapping = {
16538 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16539 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16540 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16541 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16542 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16543 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16544 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16545 })
16546 public int getResolvedTextAlignment() {
16547 // If text alignment is not resolved, then resolve it
16548 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16549 resolveTextAlignment();
16550 }
16551 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16552 }
16553
16554 /**
16555 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16556 * resolution is done.
16557 */
16558 public void resolveTextAlignment() {
16559 // Reset any previous text alignment resolution
16560 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16561
16562 if (hasRtlSupport()) {
16563 // Set resolved text alignment flag depending on text alignment flag
16564 final int textAlignment = getTextAlignment();
16565 switch (textAlignment) {
16566 case TEXT_ALIGNMENT_INHERIT:
16567 // Check if we can resolve the text alignment
16568 if (canResolveLayoutDirection() && mParent instanceof View) {
16569 View view = (View) mParent;
16570
16571 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16572 switch (parentResolvedTextAlignment) {
16573 case TEXT_ALIGNMENT_GRAVITY:
16574 case TEXT_ALIGNMENT_TEXT_START:
16575 case TEXT_ALIGNMENT_TEXT_END:
16576 case TEXT_ALIGNMENT_CENTER:
16577 case TEXT_ALIGNMENT_VIEW_START:
16578 case TEXT_ALIGNMENT_VIEW_END:
16579 // Resolved text alignment is the same as the parent resolved
16580 // text alignment
16581 mPrivateFlags2 |=
16582 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16583 break;
16584 default:
16585 // Use default resolved text alignment
16586 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16587 }
16588 }
16589 else {
16590 // We cannot do the resolution if there is no parent so use the default
16591 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16592 }
16593 break;
16594 case TEXT_ALIGNMENT_GRAVITY:
16595 case TEXT_ALIGNMENT_TEXT_START:
16596 case TEXT_ALIGNMENT_TEXT_END:
16597 case TEXT_ALIGNMENT_CENTER:
16598 case TEXT_ALIGNMENT_VIEW_START:
16599 case TEXT_ALIGNMENT_VIEW_END:
16600 // Resolved text alignment is the same as text alignment
16601 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16602 break;
16603 default:
16604 // Use default resolved text alignment
16605 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16606 }
16607 } else {
16608 // Use default resolved text alignment
16609 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16610 }
16611
16612 // Set the resolved
16613 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16614 onResolvedTextAlignmentChanged();
16615 }
16616
16617 /**
16618 * Check if text alignment resolution can be done.
16619 *
16620 * @return true if text alignment resolution can be done otherwise return false.
16621 */
16622 public boolean canResolveTextAlignment() {
16623 switch (getTextAlignment()) {
16624 case TEXT_DIRECTION_INHERIT:
16625 return (mParent != null);
16626 default:
16627 return true;
16628 }
16629 }
16630
16631 /**
16632 * Called when text alignment has been resolved. Subclasses that care about text alignment
16633 * resolution should override this method.
16634 *
16635 * The default implementation does nothing.
16636 */
16637 public void onResolvedTextAlignmentChanged() {
16638 }
16639
16640 /**
16641 * Reset resolved text alignment. Text alignment can be resolved with a call to
16642 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16643 * reset is done.
16644 */
16645 public void resetResolvedTextAlignment() {
16646 // Reset any previous text alignment resolution
16647 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16648 onResolvedTextAlignmentReset();
16649 }
16650
16651 /**
16652 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16653 * override this method and do a reset of the text alignment of their children. The default
16654 * implementation does nothing.
16655 */
16656 public void onResolvedTextAlignmentReset() {
16657 }
16658
Chet Haaseb39f0512011-05-24 14:36:40 -070016659 //
16660 // Properties
16661 //
16662 /**
16663 * A Property wrapper around the <code>alpha</code> functionality handled by the
16664 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16665 */
Chet Haased47f1532011-12-16 11:18:52 -080016666 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016667 @Override
16668 public void setValue(View object, float value) {
16669 object.setAlpha(value);
16670 }
16671
16672 @Override
16673 public Float get(View object) {
16674 return object.getAlpha();
16675 }
16676 };
16677
16678 /**
16679 * A Property wrapper around the <code>translationX</code> functionality handled by the
16680 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16681 */
Chet Haased47f1532011-12-16 11:18:52 -080016682 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016683 @Override
16684 public void setValue(View object, float value) {
16685 object.setTranslationX(value);
16686 }
16687
16688 @Override
16689 public Float get(View object) {
16690 return object.getTranslationX();
16691 }
16692 };
16693
16694 /**
16695 * A Property wrapper around the <code>translationY</code> functionality handled by the
16696 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16697 */
Chet Haased47f1532011-12-16 11:18:52 -080016698 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016699 @Override
16700 public void setValue(View object, float value) {
16701 object.setTranslationY(value);
16702 }
16703
16704 @Override
16705 public Float get(View object) {
16706 return object.getTranslationY();
16707 }
16708 };
16709
16710 /**
16711 * A Property wrapper around the <code>x</code> functionality handled by the
16712 * {@link View#setX(float)} and {@link View#getX()} methods.
16713 */
Chet Haased47f1532011-12-16 11:18:52 -080016714 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016715 @Override
16716 public void setValue(View object, float value) {
16717 object.setX(value);
16718 }
16719
16720 @Override
16721 public Float get(View object) {
16722 return object.getX();
16723 }
16724 };
16725
16726 /**
16727 * A Property wrapper around the <code>y</code> functionality handled by the
16728 * {@link View#setY(float)} and {@link View#getY()} methods.
16729 */
Chet Haased47f1532011-12-16 11:18:52 -080016730 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016731 @Override
16732 public void setValue(View object, float value) {
16733 object.setY(value);
16734 }
16735
16736 @Override
16737 public Float get(View object) {
16738 return object.getY();
16739 }
16740 };
16741
16742 /**
16743 * A Property wrapper around the <code>rotation</code> functionality handled by the
16744 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16745 */
Chet Haased47f1532011-12-16 11:18:52 -080016746 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016747 @Override
16748 public void setValue(View object, float value) {
16749 object.setRotation(value);
16750 }
16751
16752 @Override
16753 public Float get(View object) {
16754 return object.getRotation();
16755 }
16756 };
16757
16758 /**
16759 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16760 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16761 */
Chet Haased47f1532011-12-16 11:18:52 -080016762 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016763 @Override
16764 public void setValue(View object, float value) {
16765 object.setRotationX(value);
16766 }
16767
16768 @Override
16769 public Float get(View object) {
16770 return object.getRotationX();
16771 }
16772 };
16773
16774 /**
16775 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16776 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16777 */
Chet Haased47f1532011-12-16 11:18:52 -080016778 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016779 @Override
16780 public void setValue(View object, float value) {
16781 object.setRotationY(value);
16782 }
16783
16784 @Override
16785 public Float get(View object) {
16786 return object.getRotationY();
16787 }
16788 };
16789
16790 /**
16791 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16792 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16793 */
Chet Haased47f1532011-12-16 11:18:52 -080016794 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016795 @Override
16796 public void setValue(View object, float value) {
16797 object.setScaleX(value);
16798 }
16799
16800 @Override
16801 public Float get(View object) {
16802 return object.getScaleX();
16803 }
16804 };
16805
16806 /**
16807 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16808 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16809 */
Chet Haased47f1532011-12-16 11:18:52 -080016810 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016811 @Override
16812 public void setValue(View object, float value) {
16813 object.setScaleY(value);
16814 }
16815
16816 @Override
16817 public Float get(View object) {
16818 return object.getScaleY();
16819 }
16820 };
16821
Jeff Brown33bbfd22011-02-24 20:55:35 -080016822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016823 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16824 * Each MeasureSpec represents a requirement for either the width or the height.
16825 * A MeasureSpec is comprised of a size and a mode. There are three possible
16826 * modes:
16827 * <dl>
16828 * <dt>UNSPECIFIED</dt>
16829 * <dd>
16830 * The parent has not imposed any constraint on the child. It can be whatever size
16831 * it wants.
16832 * </dd>
16833 *
16834 * <dt>EXACTLY</dt>
16835 * <dd>
16836 * The parent has determined an exact size for the child. The child is going to be
16837 * given those bounds regardless of how big it wants to be.
16838 * </dd>
16839 *
16840 * <dt>AT_MOST</dt>
16841 * <dd>
16842 * The child can be as large as it wants up to the specified size.
16843 * </dd>
16844 * </dl>
16845 *
16846 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16847 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16848 */
16849 public static class MeasureSpec {
16850 private static final int MODE_SHIFT = 30;
16851 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16852
16853 /**
16854 * Measure specification mode: The parent has not imposed any constraint
16855 * on the child. It can be whatever size it wants.
16856 */
16857 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16858
16859 /**
16860 * Measure specification mode: The parent has determined an exact size
16861 * for the child. The child is going to be given those bounds regardless
16862 * of how big it wants to be.
16863 */
16864 public static final int EXACTLY = 1 << MODE_SHIFT;
16865
16866 /**
16867 * Measure specification mode: The child can be as large as it wants up
16868 * to the specified size.
16869 */
16870 public static final int AT_MOST = 2 << MODE_SHIFT;
16871
16872 /**
16873 * Creates a measure specification based on the supplied size and mode.
16874 *
16875 * The mode must always be one of the following:
16876 * <ul>
16877 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16878 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16879 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16880 * </ul>
16881 *
16882 * @param size the size of the measure specification
16883 * @param mode the mode of the measure specification
16884 * @return the measure specification based on size and mode
16885 */
16886 public static int makeMeasureSpec(int size, int mode) {
16887 return size + mode;
16888 }
16889
16890 /**
16891 * Extracts the mode from the supplied measure specification.
16892 *
16893 * @param measureSpec the measure specification to extract the mode from
16894 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16895 * {@link android.view.View.MeasureSpec#AT_MOST} or
16896 * {@link android.view.View.MeasureSpec#EXACTLY}
16897 */
16898 public static int getMode(int measureSpec) {
16899 return (measureSpec & MODE_MASK);
16900 }
16901
16902 /**
16903 * Extracts the size from the supplied measure specification.
16904 *
16905 * @param measureSpec the measure specification to extract the size from
16906 * @return the size in pixels defined in the supplied measure specification
16907 */
16908 public static int getSize(int measureSpec) {
16909 return (measureSpec & ~MODE_MASK);
16910 }
16911
16912 /**
16913 * Returns a String representation of the specified measure
16914 * specification.
16915 *
16916 * @param measureSpec the measure specification to convert to a String
16917 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16918 */
16919 public static String toString(int measureSpec) {
16920 int mode = getMode(measureSpec);
16921 int size = getSize(measureSpec);
16922
16923 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16924
16925 if (mode == UNSPECIFIED)
16926 sb.append("UNSPECIFIED ");
16927 else if (mode == EXACTLY)
16928 sb.append("EXACTLY ");
16929 else if (mode == AT_MOST)
16930 sb.append("AT_MOST ");
16931 else
16932 sb.append(mode).append(" ");
16933
16934 sb.append(size);
16935 return sb.toString();
16936 }
16937 }
16938
16939 class CheckForLongPress implements Runnable {
16940
16941 private int mOriginalWindowAttachCount;
16942
16943 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016944 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016945 && mOriginalWindowAttachCount == mWindowAttachCount) {
16946 if (performLongClick()) {
16947 mHasPerformedLongPress = true;
16948 }
16949 }
16950 }
16951
16952 public void rememberWindowAttachCount() {
16953 mOriginalWindowAttachCount = mWindowAttachCount;
16954 }
16955 }
Joe Malin32736f02011-01-19 16:14:20 -080016956
Adam Powelle14579b2009-12-16 18:39:52 -080016957 private final class CheckForTap implements Runnable {
16958 public void run() {
16959 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016960 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016961 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016962 }
16963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016964
Adam Powella35d7682010-03-12 14:48:13 -080016965 private final class PerformClick implements Runnable {
16966 public void run() {
16967 performClick();
16968 }
16969 }
16970
Dianne Hackborn63042d62011-01-26 18:56:29 -080016971 /** @hide */
16972 public void hackTurnOffWindowResizeAnim(boolean off) {
16973 mAttachInfo.mTurnOffWindowResizeAnim = off;
16974 }
Joe Malin32736f02011-01-19 16:14:20 -080016975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016976 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016977 * This method returns a ViewPropertyAnimator object, which can be used to animate
16978 * specific properties on this View.
16979 *
16980 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16981 */
16982 public ViewPropertyAnimator animate() {
16983 if (mAnimator == null) {
16984 mAnimator = new ViewPropertyAnimator(this);
16985 }
16986 return mAnimator;
16987 }
16988
16989 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016990 * Interface definition for a callback to be invoked when a hardware key event is
16991 * dispatched to this view. The callback will be invoked before the key event is
16992 * given to the view. This is only useful for hardware keyboards; a software input
16993 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016994 */
16995 public interface OnKeyListener {
16996 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016997 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016998 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016999 * <p>Key presses in software keyboards will generally NOT trigger this method,
17000 * although some may elect to do so in some situations. Do not assume a
17001 * software input method has to be key-based; even if it is, it may use key presses
17002 * in a different way than you expect, so there is no way to reliably catch soft
17003 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017004 *
17005 * @param v The view the key has been dispatched to.
17006 * @param keyCode The code for the physical key that was pressed
17007 * @param event The KeyEvent object containing full information about
17008 * the event.
17009 * @return True if the listener has consumed the event, false otherwise.
17010 */
17011 boolean onKey(View v, int keyCode, KeyEvent event);
17012 }
17013
17014 /**
17015 * Interface definition for a callback to be invoked when a touch event is
17016 * dispatched to this view. The callback will be invoked before the touch
17017 * event is given to the view.
17018 */
17019 public interface OnTouchListener {
17020 /**
17021 * Called when a touch event is dispatched to a view. This allows listeners to
17022 * get a chance to respond before the target view.
17023 *
17024 * @param v The view the touch event has been dispatched to.
17025 * @param event The MotionEvent object containing full information about
17026 * the event.
17027 * @return True if the listener has consumed the event, false otherwise.
17028 */
17029 boolean onTouch(View v, MotionEvent event);
17030 }
17031
17032 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017033 * Interface definition for a callback to be invoked when a hover event is
17034 * dispatched to this view. The callback will be invoked before the hover
17035 * event is given to the view.
17036 */
17037 public interface OnHoverListener {
17038 /**
17039 * Called when a hover event is dispatched to a view. This allows listeners to
17040 * get a chance to respond before the target view.
17041 *
17042 * @param v The view the hover event has been dispatched to.
17043 * @param event The MotionEvent object containing full information about
17044 * the event.
17045 * @return True if the listener has consumed the event, false otherwise.
17046 */
17047 boolean onHover(View v, MotionEvent event);
17048 }
17049
17050 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017051 * Interface definition for a callback to be invoked when a generic motion event is
17052 * dispatched to this view. The callback will be invoked before the generic motion
17053 * event is given to the view.
17054 */
17055 public interface OnGenericMotionListener {
17056 /**
17057 * Called when a generic motion event is dispatched to a view. This allows listeners to
17058 * get a chance to respond before the target view.
17059 *
17060 * @param v The view the generic motion event has been dispatched to.
17061 * @param event The MotionEvent object containing full information about
17062 * the event.
17063 * @return True if the listener has consumed the event, false otherwise.
17064 */
17065 boolean onGenericMotion(View v, MotionEvent event);
17066 }
17067
17068 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017069 * Interface definition for a callback to be invoked when a view has been clicked and held.
17070 */
17071 public interface OnLongClickListener {
17072 /**
17073 * Called when a view has been clicked and held.
17074 *
17075 * @param v The view that was clicked and held.
17076 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017077 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017078 */
17079 boolean onLongClick(View v);
17080 }
17081
17082 /**
Chris Tate32affef2010-10-18 15:29:21 -070017083 * Interface definition for a callback to be invoked when a drag is being dispatched
17084 * to this view. The callback will be invoked before the hosting view's own
17085 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17086 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017087 *
17088 * <div class="special reference">
17089 * <h3>Developer Guides</h3>
17090 * <p>For a guide to implementing drag and drop features, read the
17091 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17092 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017093 */
17094 public interface OnDragListener {
17095 /**
17096 * Called when a drag event is dispatched to a view. This allows listeners
17097 * to get a chance to override base View behavior.
17098 *
Joe Malin32736f02011-01-19 16:14:20 -080017099 * @param v The View that received the drag event.
17100 * @param event The {@link android.view.DragEvent} object for the drag event.
17101 * @return {@code true} if the drag event was handled successfully, or {@code false}
17102 * if the drag event was not handled. Note that {@code false} will trigger the View
17103 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017104 */
17105 boolean onDrag(View v, DragEvent event);
17106 }
17107
17108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017109 * Interface definition for a callback to be invoked when the focus state of
17110 * a view changed.
17111 */
17112 public interface OnFocusChangeListener {
17113 /**
17114 * Called when the focus state of a view has changed.
17115 *
17116 * @param v The view whose state has changed.
17117 * @param hasFocus The new focus state of v.
17118 */
17119 void onFocusChange(View v, boolean hasFocus);
17120 }
17121
17122 /**
17123 * Interface definition for a callback to be invoked when a view is clicked.
17124 */
17125 public interface OnClickListener {
17126 /**
17127 * Called when a view has been clicked.
17128 *
17129 * @param v The view that was clicked.
17130 */
17131 void onClick(View v);
17132 }
17133
17134 /**
17135 * Interface definition for a callback to be invoked when the context menu
17136 * for this view is being built.
17137 */
17138 public interface OnCreateContextMenuListener {
17139 /**
17140 * Called when the context menu for this view is being built. It is not
17141 * safe to hold onto the menu after this method returns.
17142 *
17143 * @param menu The context menu that is being built
17144 * @param v The view for which the context menu is being built
17145 * @param menuInfo Extra information about the item for which the
17146 * context menu should be shown. This information will vary
17147 * depending on the class of v.
17148 */
17149 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17150 }
17151
Joe Onorato664644d2011-01-23 17:53:23 -080017152 /**
17153 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017154 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017155 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017156 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017157 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017158 */
17159 public interface OnSystemUiVisibilityChangeListener {
17160 /**
17161 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017162 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017163 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017164 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17165 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17166 * This tells you the <strong>global</strong> state of these UI visibility
17167 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017168 */
17169 public void onSystemUiVisibilityChange(int visibility);
17170 }
17171
Adam Powell4afd62b2011-02-18 15:02:18 -080017172 /**
17173 * Interface definition for a callback to be invoked when this view is attached
17174 * or detached from its window.
17175 */
17176 public interface OnAttachStateChangeListener {
17177 /**
17178 * Called when the view is attached to a window.
17179 * @param v The view that was attached
17180 */
17181 public void onViewAttachedToWindow(View v);
17182 /**
17183 * Called when the view is detached from a window.
17184 * @param v The view that was detached
17185 */
17186 public void onViewDetachedFromWindow(View v);
17187 }
17188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017189 private final class UnsetPressedState implements Runnable {
17190 public void run() {
17191 setPressed(false);
17192 }
17193 }
17194
17195 /**
17196 * Base class for derived classes that want to save and restore their own
17197 * state in {@link android.view.View#onSaveInstanceState()}.
17198 */
17199 public static class BaseSavedState extends AbsSavedState {
17200 /**
17201 * Constructor used when reading from a parcel. Reads the state of the superclass.
17202 *
17203 * @param source
17204 */
17205 public BaseSavedState(Parcel source) {
17206 super(source);
17207 }
17208
17209 /**
17210 * Constructor called by derived classes when creating their SavedState objects
17211 *
17212 * @param superState The state of the superclass of this view
17213 */
17214 public BaseSavedState(Parcelable superState) {
17215 super(superState);
17216 }
17217
17218 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17219 new Parcelable.Creator<BaseSavedState>() {
17220 public BaseSavedState createFromParcel(Parcel in) {
17221 return new BaseSavedState(in);
17222 }
17223
17224 public BaseSavedState[] newArray(int size) {
17225 return new BaseSavedState[size];
17226 }
17227 };
17228 }
17229
17230 /**
17231 * A set of information given to a view when it is attached to its parent
17232 * window.
17233 */
17234 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017235 interface Callbacks {
17236 void playSoundEffect(int effectId);
17237 boolean performHapticFeedback(int effectId, boolean always);
17238 }
17239
17240 /**
17241 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17242 * to a Handler. This class contains the target (View) to invalidate and
17243 * the coordinates of the dirty rectangle.
17244 *
17245 * For performance purposes, this class also implements a pool of up to
17246 * POOL_LIMIT objects that get reused. This reduces memory allocations
17247 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017248 */
Romain Guyd928d682009-03-31 17:52:16 -070017249 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017250 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017251 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17252 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017253 public InvalidateInfo newInstance() {
17254 return new InvalidateInfo();
17255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017256
Romain Guyd928d682009-03-31 17:52:16 -070017257 public void onAcquired(InvalidateInfo element) {
17258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017259
Romain Guyd928d682009-03-31 17:52:16 -070017260 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017261 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017262 }
17263 }, POOL_LIMIT)
17264 );
17265
17266 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017267 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268
17269 View target;
17270
17271 int left;
17272 int top;
17273 int right;
17274 int bottom;
17275
Romain Guyd928d682009-03-31 17:52:16 -070017276 public void setNextPoolable(InvalidateInfo element) {
17277 mNext = element;
17278 }
17279
17280 public InvalidateInfo getNextPoolable() {
17281 return mNext;
17282 }
17283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017285 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017286 }
17287
17288 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017289 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017290 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017291
17292 public boolean isPooled() {
17293 return mIsPooled;
17294 }
17295
17296 public void setPooled(boolean isPooled) {
17297 mIsPooled = isPooled;
17298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017299 }
17300
17301 final IWindowSession mSession;
17302
17303 final IWindow mWindow;
17304
17305 final IBinder mWindowToken;
17306
17307 final Callbacks mRootCallbacks;
17308
Romain Guy59a12ca2011-06-09 17:48:21 -070017309 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017311 /**
17312 * The top view of the hierarchy.
17313 */
17314 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017316 IBinder mPanelParentWindowToken;
17317 Surface mSurface;
17318
Romain Guyb051e892010-09-28 19:09:36 -070017319 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017320 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017321 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017322
Romain Guy7e4e5612012-03-05 14:37:29 -080017323 boolean mScreenOn;
17324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017326 * Scale factor used by the compatibility mode
17327 */
17328 float mApplicationScale;
17329
17330 /**
17331 * Indicates whether the application is in compatibility mode
17332 */
17333 boolean mScalingRequired;
17334
17335 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017336 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017337 */
17338 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017339
Dianne Hackborn63042d62011-01-26 18:56:29 -080017340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017341 * Left position of this view's window
17342 */
17343 int mWindowLeft;
17344
17345 /**
17346 * Top position of this view's window
17347 */
17348 int mWindowTop;
17349
17350 /**
Svetoslav Ganov86783472012-06-06 21:12:20 -070017351 * Left actual position of this view's window.
17352 *
17353 * TODO: This is a workaround for 6623031. Remove when fixed.
17354 */
17355 int mActualWindowLeft;
17356
17357 /**
17358 * Actual top position of this view's window.
17359 *
17360 * TODO: This is a workaround for 6623031. Remove when fixed.
17361 */
17362 int mActualWindowTop;
17363
17364 /**
Adam Powell26153a32010-11-08 15:22:27 -080017365 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017366 */
Adam Powell26153a32010-11-08 15:22:27 -080017367 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017368
17369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017370 * For windows that are full-screen but using insets to layout inside
17371 * of the screen decorations, these are the current insets for the
17372 * content of the window.
17373 */
17374 final Rect mContentInsets = new Rect();
17375
17376 /**
17377 * For windows that are full-screen but using insets to layout inside
17378 * of the screen decorations, these are the current insets for the
17379 * actual visible parts of the window.
17380 */
17381 final Rect mVisibleInsets = new Rect();
17382
17383 /**
17384 * The internal insets given by this window. This value is
17385 * supplied by the client (through
17386 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17387 * be given to the window manager when changed to be used in laying
17388 * out windows behind it.
17389 */
17390 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17391 = new ViewTreeObserver.InternalInsetsInfo();
17392
17393 /**
17394 * All views in the window's hierarchy that serve as scroll containers,
17395 * used to determine if the window can be resized or must be panned
17396 * to adjust for a soft input area.
17397 */
17398 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17399
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017400 final KeyEvent.DispatcherState mKeyDispatchState
17401 = new KeyEvent.DispatcherState();
17402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017403 /**
17404 * Indicates whether the view's window currently has the focus.
17405 */
17406 boolean mHasWindowFocus;
17407
17408 /**
17409 * The current visibility of the window.
17410 */
17411 int mWindowVisibility;
17412
17413 /**
17414 * Indicates the time at which drawing started to occur.
17415 */
17416 long mDrawingTime;
17417
17418 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017419 * Indicates whether or not ignoring the DIRTY_MASK flags.
17420 */
17421 boolean mIgnoreDirtyState;
17422
17423 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017424 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17425 * to avoid clearing that flag prematurely.
17426 */
17427 boolean mSetIgnoreDirtyState = false;
17428
17429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017430 * Indicates whether the view's window is currently in touch mode.
17431 */
17432 boolean mInTouchMode;
17433
17434 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017435 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017436 * the next time it performs a traversal
17437 */
17438 boolean mRecomputeGlobalAttributes;
17439
17440 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017441 * Always report new attributes at next traversal.
17442 */
17443 boolean mForceReportNewAttributes;
17444
17445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017446 * Set during a traveral if any views want to keep the screen on.
17447 */
17448 boolean mKeepScreenOn;
17449
17450 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017451 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17452 */
17453 int mSystemUiVisibility;
17454
17455 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017456 * Hack to force certain system UI visibility flags to be cleared.
17457 */
17458 int mDisabledSystemUiVisibility;
17459
17460 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017461 * Last global system UI visibility reported by the window manager.
17462 */
17463 int mGlobalSystemUiVisibility;
17464
17465 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017466 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17467 * attached.
17468 */
17469 boolean mHasSystemUiListeners;
17470
17471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017472 * Set if the visibility of any views has changed.
17473 */
17474 boolean mViewVisibilityChanged;
17475
17476 /**
17477 * Set to true if a view has been scrolled.
17478 */
17479 boolean mViewScrollChanged;
17480
17481 /**
17482 * Global to the view hierarchy used as a temporary for dealing with
17483 * x/y points in the transparent region computations.
17484 */
17485 final int[] mTransparentLocation = new int[2];
17486
17487 /**
17488 * Global to the view hierarchy used as a temporary for dealing with
17489 * x/y points in the ViewGroup.invalidateChild implementation.
17490 */
17491 final int[] mInvalidateChildLocation = new int[2];
17492
Chet Haasec3aa3612010-06-17 08:50:37 -070017493
17494 /**
17495 * Global to the view hierarchy used as a temporary for dealing with
17496 * x/y location when view is transformed.
17497 */
17498 final float[] mTmpTransformLocation = new float[2];
17499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017500 /**
17501 * The view tree observer used to dispatch global events like
17502 * layout, pre-draw, touch mode change, etc.
17503 */
17504 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17505
17506 /**
17507 * A Canvas used by the view hierarchy to perform bitmap caching.
17508 */
17509 Canvas mCanvas;
17510
17511 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017512 * The view root impl.
17513 */
17514 final ViewRootImpl mViewRootImpl;
17515
17516 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017517 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017518 * handler can be used to pump events in the UI events queue.
17519 */
17520 final Handler mHandler;
17521
17522 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017523 * Temporary for use in computing invalidate rectangles while
17524 * calling up the hierarchy.
17525 */
17526 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017527
17528 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017529 * Temporary for use in computing hit areas with transformed views
17530 */
17531 final RectF mTmpTransformRect = new RectF();
17532
17533 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017534 * Temporary list for use in collecting focusable descendents of a view.
17535 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017536 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017538 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017539 * The id of the window for accessibility purposes.
17540 */
17541 int mAccessibilityWindowId = View.NO_ID;
17542
17543 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017544 * Whether to ingore not exposed for accessibility Views when
17545 * reporting the view tree to accessibility services.
17546 */
17547 boolean mIncludeNotImportantViews;
17548
17549 /**
17550 * The drawable for highlighting accessibility focus.
17551 */
17552 Drawable mAccessibilityFocusDrawable;
17553
17554 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017555 * Show where the margins, bounds and layout bounds are for each view.
17556 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017557 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017558
17559 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017560 * Point used to compute visible regions.
17561 */
17562 final Point mPoint = new Point();
17563
17564 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017565 * Creates a new set of attachment information with the specified
17566 * events handler and thread.
17567 *
17568 * @param handler the events handler the view must use
17569 */
17570 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017571 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017572 mSession = session;
17573 mWindow = window;
17574 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017575 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017576 mHandler = handler;
17577 mRootCallbacks = effectPlayer;
17578 }
17579 }
17580
17581 /**
17582 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17583 * is supported. This avoids keeping too many unused fields in most
17584 * instances of View.</p>
17585 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017586 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017587
Mike Cleronf116bf82009-09-27 19:14:12 -070017588 /**
17589 * Scrollbars are not visible
17590 */
17591 public static final int OFF = 0;
17592
17593 /**
17594 * Scrollbars are visible
17595 */
17596 public static final int ON = 1;
17597
17598 /**
17599 * Scrollbars are fading away
17600 */
17601 public static final int FADING = 2;
17602
17603 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017605 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017606 public int scrollBarDefaultDelayBeforeFade;
17607 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017608
17609 public int scrollBarSize;
17610 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017611 public float[] interpolatorValues;
17612 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017613
17614 public final Paint paint;
17615 public final Matrix matrix;
17616 public Shader shader;
17617
Mike Cleronf116bf82009-09-27 19:14:12 -070017618 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17619
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017620 private static final float[] OPAQUE = { 255 };
17621 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017622
Mike Cleronf116bf82009-09-27 19:14:12 -070017623 /**
17624 * When fading should start. This time moves into the future every time
17625 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17626 */
17627 public long fadeStartTime;
17628
17629
17630 /**
17631 * The current state of the scrollbars: ON, OFF, or FADING
17632 */
17633 public int state = OFF;
17634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017635 private int mLastColor;
17636
Mike Cleronf116bf82009-09-27 19:14:12 -070017637 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017638 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17639 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017640 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17641 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017642
17643 paint = new Paint();
17644 matrix = new Matrix();
17645 // use use a height of 1, and then wack the matrix each time we
17646 // actually use it.
17647 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017649 paint.setShader(shader);
17650 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017651 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017652 }
Romain Guy8506ab42009-06-11 17:35:47 -070017653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017654 public void setFadeColor(int color) {
17655 if (color != 0 && color != mLastColor) {
17656 mLastColor = color;
17657 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017658
Romain Guye55e1a72009-08-27 10:42:26 -070017659 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17660 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017662 paint.setShader(shader);
17663 // Restore the default transfer mode (src_over)
17664 paint.setXfermode(null);
17665 }
17666 }
Joe Malin32736f02011-01-19 16:14:20 -080017667
Mike Cleronf116bf82009-09-27 19:14:12 -070017668 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017669 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017670 if (now >= fadeStartTime) {
17671
17672 // the animation fades the scrollbars out by changing
17673 // the opacity (alpha) from fully opaque to fully
17674 // transparent
17675 int nextFrame = (int) now;
17676 int framesCount = 0;
17677
17678 Interpolator interpolator = scrollBarInterpolator;
17679
17680 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017681 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017682
17683 // End transparent
17684 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017685 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017686
17687 state = FADING;
17688
17689 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017690 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017691 }
17692 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017693 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017694
Svetoslav Ganova0156172011-06-26 17:55:44 -070017695 /**
17696 * Resuable callback for sending
17697 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17698 */
17699 private class SendViewScrolledAccessibilityEvent implements Runnable {
17700 public volatile boolean mIsPending;
17701
17702 public void run() {
17703 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17704 mIsPending = false;
17705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017706 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017707
17708 /**
17709 * <p>
17710 * This class represents a delegate that can be registered in a {@link View}
17711 * to enhance accessibility support via composition rather via inheritance.
17712 * It is specifically targeted to widget developers that extend basic View
17713 * classes i.e. classes in package android.view, that would like their
17714 * applications to be backwards compatible.
17715 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017716 * <div class="special reference">
17717 * <h3>Developer Guides</h3>
17718 * <p>For more information about making applications accessible, read the
17719 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17720 * developer guide.</p>
17721 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017722 * <p>
17723 * A scenario in which a developer would like to use an accessibility delegate
17724 * is overriding a method introduced in a later API version then the minimal API
17725 * version supported by the application. For example, the method
17726 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17727 * in API version 4 when the accessibility APIs were first introduced. If a
17728 * developer would like his application to run on API version 4 devices (assuming
17729 * all other APIs used by the application are version 4 or lower) and take advantage
17730 * of this method, instead of overriding the method which would break the application's
17731 * backwards compatibility, he can override the corresponding method in this
17732 * delegate and register the delegate in the target View if the API version of
17733 * the system is high enough i.e. the API version is same or higher to the API
17734 * version that introduced
17735 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17736 * </p>
17737 * <p>
17738 * Here is an example implementation:
17739 * </p>
17740 * <code><pre><p>
17741 * if (Build.VERSION.SDK_INT >= 14) {
17742 * // If the API version is equal of higher than the version in
17743 * // which onInitializeAccessibilityNodeInfo was introduced we
17744 * // register a delegate with a customized implementation.
17745 * View view = findViewById(R.id.view_id);
17746 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17747 * public void onInitializeAccessibilityNodeInfo(View host,
17748 * AccessibilityNodeInfo info) {
17749 * // Let the default implementation populate the info.
17750 * super.onInitializeAccessibilityNodeInfo(host, info);
17751 * // Set some other information.
17752 * info.setEnabled(host.isEnabled());
17753 * }
17754 * });
17755 * }
17756 * </code></pre></p>
17757 * <p>
17758 * This delegate contains methods that correspond to the accessibility methods
17759 * in View. If a delegate has been specified the implementation in View hands
17760 * off handling to the corresponding method in this delegate. The default
17761 * implementation the delegate methods behaves exactly as the corresponding
17762 * method in View for the case of no accessibility delegate been set. Hence,
17763 * to customize the behavior of a View method, clients can override only the
17764 * corresponding delegate method without altering the behavior of the rest
17765 * accessibility related methods of the host view.
17766 * </p>
17767 */
17768 public static class AccessibilityDelegate {
17769
17770 /**
17771 * Sends an accessibility event of the given type. If accessibility is not
17772 * enabled this method has no effect.
17773 * <p>
17774 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17775 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17776 * been set.
17777 * </p>
17778 *
17779 * @param host The View hosting the delegate.
17780 * @param eventType The type of the event to send.
17781 *
17782 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17783 */
17784 public void sendAccessibilityEvent(View host, int eventType) {
17785 host.sendAccessibilityEventInternal(eventType);
17786 }
17787
17788 /**
alanv8eeefef2012-05-07 16:57:53 -070017789 * Performs the specified accessibility action on the view. For
17790 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17791 * <p>
17792 * The default implementation behaves as
17793 * {@link View#performAccessibilityAction(int, Bundle)
17794 * View#performAccessibilityAction(int, Bundle)} for the case of
17795 * no accessibility delegate been set.
17796 * </p>
17797 *
17798 * @param action The action to perform.
17799 * @return Whether the action was performed.
17800 *
17801 * @see View#performAccessibilityAction(int, Bundle)
17802 * View#performAccessibilityAction(int, Bundle)
17803 */
17804 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17805 return host.performAccessibilityActionInternal(action, args);
17806 }
17807
17808 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017809 * Sends an accessibility event. This method behaves exactly as
17810 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17811 * empty {@link AccessibilityEvent} and does not perform a check whether
17812 * accessibility is enabled.
17813 * <p>
17814 * The default implementation behaves as
17815 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17816 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17817 * the case of no accessibility delegate been set.
17818 * </p>
17819 *
17820 * @param host The View hosting the delegate.
17821 * @param event The event to send.
17822 *
17823 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17824 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17825 */
17826 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17827 host.sendAccessibilityEventUncheckedInternal(event);
17828 }
17829
17830 /**
17831 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17832 * to its children for adding their text content to the event.
17833 * <p>
17834 * The default implementation behaves as
17835 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17836 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17837 * the case of no accessibility delegate been set.
17838 * </p>
17839 *
17840 * @param host The View hosting the delegate.
17841 * @param event The event.
17842 * @return True if the event population was completed.
17843 *
17844 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17845 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17846 */
17847 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17848 return host.dispatchPopulateAccessibilityEventInternal(event);
17849 }
17850
17851 /**
17852 * Gives a chance to the host View to populate the accessibility event with its
17853 * text content.
17854 * <p>
17855 * The default implementation behaves as
17856 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17857 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17858 * the case of no accessibility delegate been set.
17859 * </p>
17860 *
17861 * @param host The View hosting the delegate.
17862 * @param event The accessibility event which to populate.
17863 *
17864 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17865 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17866 */
17867 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17868 host.onPopulateAccessibilityEventInternal(event);
17869 }
17870
17871 /**
17872 * Initializes an {@link AccessibilityEvent} with information about the
17873 * the host View which is the event source.
17874 * <p>
17875 * The default implementation behaves as
17876 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17877 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17878 * the case of no accessibility delegate been set.
17879 * </p>
17880 *
17881 * @param host The View hosting the delegate.
17882 * @param event The event to initialize.
17883 *
17884 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17885 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17886 */
17887 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17888 host.onInitializeAccessibilityEventInternal(event);
17889 }
17890
17891 /**
17892 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17893 * <p>
17894 * The default implementation behaves as
17895 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17896 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17897 * the case of no accessibility delegate been set.
17898 * </p>
17899 *
17900 * @param host The View hosting the delegate.
17901 * @param info The instance to initialize.
17902 *
17903 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17904 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17905 */
17906 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17907 host.onInitializeAccessibilityNodeInfoInternal(info);
17908 }
17909
17910 /**
17911 * Called when a child of the host View has requested sending an
17912 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17913 * to augment the event.
17914 * <p>
17915 * The default implementation behaves as
17916 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17917 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17918 * the case of no accessibility delegate been set.
17919 * </p>
17920 *
17921 * @param host The View hosting the delegate.
17922 * @param child The child which requests sending the event.
17923 * @param event The event to be sent.
17924 * @return True if the event should be sent
17925 *
17926 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17927 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17928 */
17929 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17930 AccessibilityEvent event) {
17931 return host.onRequestSendAccessibilityEventInternal(child, event);
17932 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017933
17934 /**
17935 * Gets the provider for managing a virtual view hierarchy rooted at this View
17936 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17937 * that explore the window content.
17938 * <p>
17939 * The default implementation behaves as
17940 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17941 * the case of no accessibility delegate been set.
17942 * </p>
17943 *
17944 * @return The provider.
17945 *
17946 * @see AccessibilityNodeProvider
17947 */
17948 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17949 return null;
17950 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017952}