blob: 4828d1285cbb1794cae02e82df9233e04092f253 [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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070050import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070053import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070059import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070063import android.view.AccessibilityIterators.TextSegmentIterator;
64import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
65import android.view.AccessibilityIterators.WordTextSegmentIterator;
66import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.accessibility.AccessibilityEvent;
68import android.view.accessibility.AccessibilityEventSource;
69import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070070import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070071import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070073import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080074import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.inputmethod.InputConnection;
77import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.ScrollBarDrawable;
79
Romain Guy1ef3fdb2011-09-09 15:30:30 -070080import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070081import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070082
Doug Feltcb3791202011-07-07 11:57:48 -070083import com.android.internal.R;
84import com.android.internal.util.Predicate;
85import com.android.internal.view.menu.MenuBuilder;
86
Christopher Tatea0374192010-10-05 13:06:41 -070087import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070088import java.lang.reflect.InvocationTargetException;
89import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.util.ArrayList;
91import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070092import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080093import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -070094import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96/**
97 * <p>
98 * This class represents the basic building block for user interface components. A View
99 * occupies a rectangular area on the screen and is responsible for drawing and
100 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700101 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
103 * are invisible containers that hold other Views (or other ViewGroups) and define
104 * their layout properties.
105 * </p>
106 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700107 * <div class="special reference">
108 * <h3>Developer Guides</h3>
109 * <p>For information about using this class to develop your application's user interface,
110 * 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 -0800111 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700112 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * <a name="Using"></a>
114 * <h3>Using Views</h3>
115 * <p>
116 * All of the views in a window are arranged in a single tree. You can add views
117 * either from code or by specifying a tree of views in one or more XML layout
118 * files. There are many specialized subclasses of views that act as controls or
119 * are capable of displaying text, images, or other content.
120 * </p>
121 * <p>
122 * Once you have created a tree of views, there are typically a few types of
123 * common operations you may wish to perform:
124 * <ul>
125 * <li><strong>Set properties:</strong> for example setting the text of a
126 * {@link android.widget.TextView}. The available properties and the methods
127 * that set them will vary among the different subclasses of views. Note that
128 * properties that are known at build time can be set in the XML layout
129 * files.</li>
130 * <li><strong>Set focus:</strong> The framework will handled moving focus in
131 * response to user input. To force focus to a specific view, call
132 * {@link #requestFocus}.</li>
133 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
134 * that will be notified when something interesting happens to the view. For
135 * example, all views will let you set a listener to be notified when the view
136 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700137 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700138 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700141 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * </ul>
143 * </p>
144 * <p><em>
145 * Note: The Android framework is responsible for measuring, laying out and
146 * drawing views. You should not call methods that perform these actions on
147 * views yourself unless you are actually implementing a
148 * {@link android.view.ViewGroup}.
149 * </em></p>
150 *
151 * <a name="Lifecycle"></a>
152 * <h3>Implementing a Custom View</h3>
153 *
154 * <p>
155 * To implement a custom view, you will usually begin by providing overrides for
156 * some of the standard methods that the framework calls on all views. You do
157 * not need to override all of these methods. In fact, you can start by just
158 * overriding {@link #onDraw(android.graphics.Canvas)}.
159 * <table border="2" width="85%" align="center" cellpadding="5">
160 * <thead>
161 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
162 * </thead>
163 *
164 * <tbody>
165 * <tr>
166 * <td rowspan="2">Creation</td>
167 * <td>Constructors</td>
168 * <td>There is a form of the constructor that are called when the view
169 * is created from code and a form that is called when the view is
170 * inflated from a layout file. The second form should parse and apply
171 * any attributes defined in the layout file.
172 * </td>
173 * </tr>
174 * <tr>
175 * <td><code>{@link #onFinishInflate()}</code></td>
176 * <td>Called after a view and all of its children has been inflated
177 * from XML.</td>
178 * </tr>
179 *
180 * <tr>
181 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700182 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 * <td>Called to determine the size requirements for this view and all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called when this view should assign a size and position to all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when the size of this view has changed.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700201 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * <td>Called when the view should render its content.
203 * </td>
204 * </tr>
205 *
206 * <tr>
207 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900209 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900214 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a trackball motion event occurs.
220 * </td>
221 * </tr>
222 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700223 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 * <td>Called when a touch screen motion event occurs.
225 * </td>
226 * </tr>
227 *
228 * <tr>
229 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700230 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 * <td>Called when the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the window containing the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td rowspan="3">Attaching</td>
243 * <td><code>{@link #onAttachedToWindow()}</code></td>
244 * <td>Called when the view is attached to a window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onDetachedFromWindow}</code></td>
250 * <td>Called when the view is detached from its window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700255 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 * <td>Called when the visibility of the window containing the view
257 * has changed.
258 * </td>
259 * </tr>
260 * </tbody>
261 *
262 * </table>
263 * </p>
264 *
265 * <a name="IDs"></a>
266 * <h3>IDs</h3>
267 * Views may have an integer id associated with them. These ids are typically
268 * assigned in the layout XML files, and are used to find specific views within
269 * the view tree. A common pattern is to:
270 * <ul>
271 * <li>Define a Button in the layout file and assign it a unique ID.
272 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700273 * &lt;Button
274 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * android:layout_width="wrap_content"
276 * android:layout_height="wrap_content"
277 * android:text="@string/my_button_text"/&gt;
278 * </pre></li>
279 * <li>From the onCreate method of an Activity, find the Button
280 * <pre class="prettyprint">
281 * Button myButton = (Button) findViewById(R.id.my_button);
282 * </pre></li>
283 * </ul>
284 * <p>
285 * View IDs need not be unique throughout the tree, but it is good practice to
286 * ensure that they are at least unique within the part of the tree you are
287 * searching.
288 * </p>
289 *
290 * <a name="Position"></a>
291 * <h3>Position</h3>
292 * <p>
293 * The geometry of a view is that of a rectangle. A view has a location,
294 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
295 * two dimensions, expressed as a width and a height. The unit for location
296 * and dimensions is the pixel.
297 * </p>
298 *
299 * <p>
300 * It is possible to retrieve the location of a view by invoking the methods
301 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
302 * coordinate of the rectangle representing the view. The latter returns the
303 * top, or Y, coordinate of the rectangle representing the view. These methods
304 * both return the location of the view relative to its parent. For instance,
305 * when getLeft() returns 20, that means the view is located 20 pixels to the
306 * right of the left edge of its direct parent.
307 * </p>
308 *
309 * <p>
310 * In addition, several convenience methods are offered to avoid unnecessary
311 * computations, namely {@link #getRight()} and {@link #getBottom()}.
312 * These methods return the coordinates of the right and bottom edges of the
313 * rectangle representing the view. For instance, calling {@link #getRight()}
314 * is similar to the following computation: <code>getLeft() + getWidth()</code>
315 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
316 * </p>
317 *
318 * <a name="SizePaddingMargins"></a>
319 * <h3>Size, padding and margins</h3>
320 * <p>
321 * The size of a view is expressed with a width and a height. A view actually
322 * possess two pairs of width and height values.
323 * </p>
324 *
325 * <p>
326 * The first pair is known as <em>measured width</em> and
327 * <em>measured height</em>. These dimensions define how big a view wants to be
328 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
329 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
330 * and {@link #getMeasuredHeight()}.
331 * </p>
332 *
333 * <p>
334 * The second pair is simply known as <em>width</em> and <em>height</em>, or
335 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
336 * dimensions define the actual size of the view on screen, at drawing time and
337 * after layout. These values may, but do not have to, be different from the
338 * measured width and height. The width and height can be obtained by calling
339 * {@link #getWidth()} and {@link #getHeight()}.
340 * </p>
341 *
342 * <p>
343 * To measure its dimensions, a view takes into account its padding. The padding
344 * is expressed in pixels for the left, top, right and bottom parts of the view.
345 * Padding can be used to offset the content of the view by a specific amount of
346 * pixels. For instance, a left padding of 2 will push the view's content by
347 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700348 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
349 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
350 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
351 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 * </p>
353 *
354 * <p>
355 * Even though a view can define a padding, it does not provide any support for
356 * margins. However, view groups provide such a support. Refer to
357 * {@link android.view.ViewGroup} and
358 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
359 * </p>
360 *
361 * <a name="Layout"></a>
362 * <h3>Layout</h3>
363 * <p>
364 * Layout is a two pass process: a measure pass and a layout pass. The measuring
365 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
366 * of the view tree. Each view pushes dimension specifications down the tree
367 * during the recursion. At the end of the measure pass, every view has stored
368 * its measurements. The second pass happens in
369 * {@link #layout(int,int,int,int)} and is also top-down. During
370 * this pass each parent is responsible for positioning all of its children
371 * using the sizes computed in the measure pass.
372 * </p>
373 *
374 * <p>
375 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
376 * {@link #getMeasuredHeight()} values must be set, along with those for all of
377 * that view's descendants. A view's measured width and measured height values
378 * must respect the constraints imposed by the view's parents. This guarantees
379 * that at the end of the measure pass, all parents accept all of their
380 * children's measurements. A parent view may call measure() more than once on
381 * its children. For example, the parent may measure each child once with
382 * unspecified dimensions to find out how big they want to be, then call
383 * measure() on them again with actual numbers if the sum of all the children's
384 * unconstrained sizes is too big or too small.
385 * </p>
386 *
387 * <p>
388 * The measure pass uses two classes to communicate dimensions. The
389 * {@link MeasureSpec} class is used by views to tell their parents how they
390 * want to be measured and positioned. The base LayoutParams class just
391 * describes how big the view wants to be for both width and height. For each
392 * dimension, it can specify one of:
393 * <ul>
394 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800395 * <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 -0800396 * (minus padding)
397 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
398 * enclose its content (plus padding).
399 * </ul>
400 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
401 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
402 * an X and Y value.
403 * </p>
404 *
405 * <p>
406 * MeasureSpecs are used to push requirements down the tree from parent to
407 * child. A MeasureSpec can be in one of three modes:
408 * <ul>
409 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
410 * of a child view. For example, a LinearLayout may call measure() on its child
411 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
412 * tall the child view wants to be given a width of 240 pixels.
413 * <li>EXACTLY: This is used by the parent to impose an exact size on the
414 * child. The child must use this size, and guarantee that all of its
415 * descendants will fit within this size.
416 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
417 * child. The child must gurantee that it and all of its descendants will fit
418 * within this size.
419 * </ul>
420 * </p>
421 *
422 * <p>
423 * To intiate a layout, call {@link #requestLayout}. This method is typically
424 * called by a view on itself when it believes that is can no longer fit within
425 * its current bounds.
426 * </p>
427 *
428 * <a name="Drawing"></a>
429 * <h3>Drawing</h3>
430 * <p>
431 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700432 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 * this means that parents will draw before (i.e., behind) their children, with
434 * siblings drawn in the order they appear in the tree.
435 * If you set a background drawable for a View, then the View will draw it for you
436 * before calling back to its <code>onDraw()</code> method.
437 * </p>
438 *
439 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700440 * 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 -0800441 * </p>
442 *
443 * <p>
444 * To force a view to draw, call {@link #invalidate()}.
445 * </p>
446 *
447 * <a name="EventHandlingThreading"></a>
448 * <h3>Event Handling and Threading</h3>
449 * <p>
450 * The basic cycle of a view is as follows:
451 * <ol>
452 * <li>An event comes in and is dispatched to the appropriate view. The view
453 * handles the event and notifies any listeners.</li>
454 * <li>If in the course of processing the event, the view's bounds may need
455 * to be changed, the view will call {@link #requestLayout()}.</li>
456 * <li>Similarly, if in the course of processing the event the view's appearance
457 * may need to be changed, the view will call {@link #invalidate()}.</li>
458 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
459 * the framework will take care of measuring, laying out, and drawing the tree
460 * as appropriate.</li>
461 * </ol>
462 * </p>
463 *
464 * <p><em>Note: The entire view tree is single threaded. You must always be on
465 * the UI thread when calling any method on any view.</em>
466 * If you are doing work on other threads and want to update the state of a view
467 * from that thread, you should use a {@link Handler}.
468 * </p>
469 *
470 * <a name="FocusHandling"></a>
471 * <h3>Focus Handling</h3>
472 * <p>
473 * The framework will handle routine focus movement in response to user input.
474 * This includes changing the focus as views are removed or hidden, or as new
475 * views become available. Views indicate their willingness to take focus
476 * through the {@link #isFocusable} method. To change whether a view can take
477 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
478 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
479 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
480 * </p>
481 * <p>
482 * Focus movement is based on an algorithm which finds the nearest neighbor in a
483 * given direction. In rare cases, the default algorithm may not match the
484 * intended behavior of the developer. In these situations, you can provide
485 * explicit overrides by using these XML attributes in the layout file:
486 * <pre>
487 * nextFocusDown
488 * nextFocusLeft
489 * nextFocusRight
490 * nextFocusUp
491 * </pre>
492 * </p>
493 *
494 *
495 * <p>
496 * To get a particular view to take focus, call {@link #requestFocus()}.
497 * </p>
498 *
499 * <a name="TouchMode"></a>
500 * <h3>Touch Mode</h3>
501 * <p>
502 * When a user is navigating a user interface via directional keys such as a D-pad, it is
503 * necessary to give focus to actionable items such as buttons so the user can see
504 * what will take input. If the device has touch capabilities, however, and the user
505 * begins interacting with the interface by touching it, it is no longer necessary to
506 * always highlight, or give focus to, a particular view. This motivates a mode
507 * for interaction named 'touch mode'.
508 * </p>
509 * <p>
510 * For a touch capable device, once the user touches the screen, the device
511 * will enter touch mode. From this point onward, only views for which
512 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
513 * Other views that are touchable, like buttons, will not take focus when touched; they will
514 * only fire the on click listeners.
515 * </p>
516 * <p>
517 * Any time a user hits a directional key, such as a D-pad direction, the view device will
518 * exit touch mode, and find a view to take focus, so that the user may resume interacting
519 * with the user interface without touching the screen again.
520 * </p>
521 * <p>
522 * The touch mode state is maintained across {@link android.app.Activity}s. Call
523 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
524 * </p>
525 *
526 * <a name="Scrolling"></a>
527 * <h3>Scrolling</h3>
528 * <p>
529 * The framework provides basic support for views that wish to internally
530 * scroll their content. This includes keeping track of the X and Y scroll
531 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800532 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700533 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 * </p>
535 *
536 * <a name="Tags"></a>
537 * <h3>Tags</h3>
538 * <p>
539 * Unlike IDs, tags are not used to identify views. Tags are essentially an
540 * extra piece of information that can be associated with a view. They are most
541 * often used as a convenience to store data related to views in the views
542 * themselves rather than by putting them in a separate structure.
543 * </p>
544 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700545 * <a name="Properties"></a>
546 * <h3>Properties</h3>
547 * <p>
548 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
549 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
550 * available both in the {@link Property} form as well as in similarly-named setter/getter
551 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
552 * be used to set persistent state associated with these rendering-related properties on the view.
553 * The properties and methods can also be used in conjunction with
554 * {@link android.animation.Animator Animator}-based animations, described more in the
555 * <a href="#Animation">Animation</a> section.
556 * </p>
557 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 * <a name="Animation"></a>
559 * <h3>Animation</h3>
560 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700561 * Starting with Android 3.0, the preferred way of animating views is to use the
562 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
563 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
564 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
565 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
566 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
567 * makes animating these View properties particularly easy and efficient.
568 * </p>
569 * <p>
570 * 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 -0800571 * You can attach an {@link Animation} object to a view using
572 * {@link #setAnimation(Animation)} or
573 * {@link #startAnimation(Animation)}. The animation can alter the scale,
574 * rotation, translation and alpha of a view over time. If the animation is
575 * attached to a view that has children, the animation will affect the entire
576 * subtree rooted by that node. When an animation is started, the framework will
577 * take care of redrawing the appropriate views until the animation completes.
578 * </p>
579 *
Jeff Brown85a31762010-09-01 17:01:00 -0700580 * <a name="Security"></a>
581 * <h3>Security</h3>
582 * <p>
583 * Sometimes it is essential that an application be able to verify that an action
584 * is being performed with the full knowledge and consent of the user, such as
585 * granting a permission request, making a purchase or clicking on an advertisement.
586 * Unfortunately, a malicious application could try to spoof the user into
587 * performing these actions, unaware, by concealing the intended purpose of the view.
588 * As a remedy, the framework offers a touch filtering mechanism that can be used to
589 * improve the security of views that provide access to sensitive functionality.
590 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700591 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800592 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700593 * will discard touches that are received whenever the view's window is obscured by
594 * another visible window. As a result, the view will not receive touches whenever a
595 * toast, dialog or other window appears above the view's window.
596 * </p><p>
597 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700598 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
599 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700600 * </p>
601 *
Romain Guy171c5922011-01-06 10:04:23 -0800602 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700603 * @attr ref android.R.styleable#View_background
604 * @attr ref android.R.styleable#View_clickable
605 * @attr ref android.R.styleable#View_contentDescription
606 * @attr ref android.R.styleable#View_drawingCacheQuality
607 * @attr ref android.R.styleable#View_duplicateParentState
608 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700609 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700610 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700611 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700612 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_isScrollContainer
615 * @attr ref android.R.styleable#View_focusable
616 * @attr ref android.R.styleable#View_focusableInTouchMode
617 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
618 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800619 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_longClickable
621 * @attr ref android.R.styleable#View_minHeight
622 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_nextFocusDown
624 * @attr ref android.R.styleable#View_nextFocusLeft
625 * @attr ref android.R.styleable#View_nextFocusRight
626 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700627 * @attr ref android.R.styleable#View_onClick
628 * @attr ref android.R.styleable#View_padding
629 * @attr ref android.R.styleable#View_paddingBottom
630 * @attr ref android.R.styleable#View_paddingLeft
631 * @attr ref android.R.styleable#View_paddingRight
632 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800633 * @attr ref android.R.styleable#View_paddingStart
634 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800636 * @attr ref android.R.styleable#View_rotation
637 * @attr ref android.R.styleable#View_rotationX
638 * @attr ref android.R.styleable#View_rotationY
639 * @attr ref android.R.styleable#View_scaleX
640 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * @attr ref android.R.styleable#View_scrollX
642 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700643 * @attr ref android.R.styleable#View_scrollbarSize
644 * @attr ref android.R.styleable#View_scrollbarStyle
645 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700646 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
647 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
649 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 * @attr ref android.R.styleable#View_scrollbarThumbVertical
651 * @attr ref android.R.styleable#View_scrollbarTrackVertical
652 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
653 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700654 * @attr ref android.R.styleable#View_soundEffectsEnabled
655 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700656 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800657 * @attr ref android.R.styleable#View_transformPivotX
658 * @attr ref android.R.styleable#View_transformPivotY
659 * @attr ref android.R.styleable#View_translationX
660 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700661 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 *
663 * @see android.view.ViewGroup
664 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700665public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700666 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 private static final boolean DBG = false;
668
669 /**
670 * The logging tag used by this class with android.util.Log.
671 */
672 protected static final String VIEW_LOG_TAG = "View";
673
674 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700675 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700676 *
677 * @hide
678 */
679 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
680
681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 * Used to mark a View that has no ID.
683 */
684 public static final int NO_ID = -1;
685
686 /**
687 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
688 * calling setFlags.
689 */
690 private static final int NOT_FOCUSABLE = 0x00000000;
691
692 /**
693 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
694 * setFlags.
695 */
696 private static final int FOCUSABLE = 0x00000001;
697
698 /**
699 * Mask for use with setFlags indicating bits used for focus.
700 */
701 private static final int FOCUSABLE_MASK = 0x00000001;
702
703 /**
704 * This view will adjust its padding to fit sytem windows (e.g. status bar)
705 */
706 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
707
708 /**
Scott Main812634c22011-07-27 13:22:35 -0700709 * This view is visible.
710 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
711 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 */
713 public static final int VISIBLE = 0x00000000;
714
715 /**
716 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700717 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
718 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 */
720 public static final int INVISIBLE = 0x00000004;
721
722 /**
723 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700724 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
725 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 */
727 public static final int GONE = 0x00000008;
728
729 /**
730 * Mask for use with setFlags indicating bits used for visibility.
731 * {@hide}
732 */
733 static final int VISIBILITY_MASK = 0x0000000C;
734
735 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
736
737 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700738 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 * Use with ENABLED_MASK when calling setFlags.
740 * {@hide}
741 */
742 static final int ENABLED = 0x00000000;
743
744 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700745 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 * Use with ENABLED_MASK when calling setFlags.
747 * {@hide}
748 */
749 static final int DISABLED = 0x00000020;
750
751 /**
752 * Mask for use with setFlags indicating bits used for indicating whether
753 * this view is enabled
754 * {@hide}
755 */
756 static final int ENABLED_MASK = 0x00000020;
757
758 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700759 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
760 * called and further optimizations will be performed. It is okay to have
761 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 * {@hide}
763 */
764 static final int WILL_NOT_DRAW = 0x00000080;
765
766 /**
767 * Mask for use with setFlags indicating bits used for indicating whether
768 * this view is will draw
769 * {@hide}
770 */
771 static final int DRAW_MASK = 0x00000080;
772
773 /**
774 * <p>This view doesn't show scrollbars.</p>
775 * {@hide}
776 */
777 static final int SCROLLBARS_NONE = 0x00000000;
778
779 /**
780 * <p>This view shows horizontal scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
784
785 /**
786 * <p>This view shows vertical scrollbars.</p>
787 * {@hide}
788 */
789 static final int SCROLLBARS_VERTICAL = 0x00000200;
790
791 /**
792 * <p>Mask for use with setFlags indicating bits used for indicating which
793 * scrollbars are enabled.</p>
794 * {@hide}
795 */
796 static final int SCROLLBARS_MASK = 0x00000300;
797
Jeff Brown85a31762010-09-01 17:01:00 -0700798 /**
799 * Indicates that the view should filter touches when its window is obscured.
800 * Refer to the class comments for more information about this security feature.
801 * {@hide}
802 */
803 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
804
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700805 /**
806 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
807 * that they are optional and should be skipped if the window has
808 * requested system UI flags that ignore those insets for layout.
809 */
810 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811
812 /**
813 * <p>This view doesn't show fading edges.</p>
814 * {@hide}
815 */
816 static final int FADING_EDGE_NONE = 0x00000000;
817
818 /**
819 * <p>This view shows horizontal fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
823
824 /**
825 * <p>This view shows vertical fading edges.</p>
826 * {@hide}
827 */
828 static final int FADING_EDGE_VERTICAL = 0x00002000;
829
830 /**
831 * <p>Mask for use with setFlags indicating bits used for indicating which
832 * fading edges are enabled.</p>
833 * {@hide}
834 */
835 static final int FADING_EDGE_MASK = 0x00003000;
836
837 /**
838 * <p>Indicates this view can be clicked. When clickable, a View reacts
839 * to clicks by notifying the OnClickListener.<p>
840 * {@hide}
841 */
842 static final int CLICKABLE = 0x00004000;
843
844 /**
845 * <p>Indicates this view is caching its drawing into a bitmap.</p>
846 * {@hide}
847 */
848 static final int DRAWING_CACHE_ENABLED = 0x00008000;
849
850 /**
851 * <p>Indicates that no icicle should be saved for this view.<p>
852 * {@hide}
853 */
854 static final int SAVE_DISABLED = 0x000010000;
855
856 /**
857 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
858 * property.</p>
859 * {@hide}
860 */
861 static final int SAVE_DISABLED_MASK = 0x000010000;
862
863 /**
864 * <p>Indicates that no drawing cache should ever be created for this view.<p>
865 * {@hide}
866 */
867 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
868
869 /**
870 * <p>Indicates this view can take / keep focus when int touch mode.</p>
871 * {@hide}
872 */
873 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
874
875 /**
876 * <p>Enables low quality mode for the drawing cache.</p>
877 */
878 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
879
880 /**
881 * <p>Enables high quality mode for the drawing cache.</p>
882 */
883 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
884
885 /**
886 * <p>Enables automatic quality mode for the drawing cache.</p>
887 */
888 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
889
890 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
891 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
892 };
893
894 /**
895 * <p>Mask for use with setFlags indicating bits used for the cache
896 * quality property.</p>
897 * {@hide}
898 */
899 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
900
901 /**
902 * <p>
903 * Indicates this view can be long clicked. When long clickable, a View
904 * reacts to long clicks by notifying the OnLongClickListener or showing a
905 * context menu.
906 * </p>
907 * {@hide}
908 */
909 static final int LONG_CLICKABLE = 0x00200000;
910
911 /**
912 * <p>Indicates that this view gets its drawable states from its direct parent
913 * and ignores its original internal states.</p>
914 *
915 * @hide
916 */
917 static final int DUPLICATE_PARENT_STATE = 0x00400000;
918
919 /**
920 * The scrollbar style to display the scrollbars inside the content area,
921 * without increasing the padding. The scrollbars will be overlaid with
922 * translucency on the view's content.
923 */
924 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
925
926 /**
927 * The scrollbar style to display the scrollbars inside the padded area,
928 * increasing the padding of the view. The scrollbars will not overlap the
929 * content area of the view.
930 */
931 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
932
933 /**
934 * The scrollbar style to display the scrollbars at the edge of the view,
935 * without increasing the padding. The scrollbars will be overlaid with
936 * translucency.
937 */
938 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
939
940 /**
941 * The scrollbar style to display the scrollbars at the edge of the view,
942 * increasing the padding of the view. The scrollbars will only overlap the
943 * background, if any.
944 */
945 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
946
947 /**
948 * Mask to check if the scrollbar style is overlay or inset.
949 * {@hide}
950 */
951 static final int SCROLLBARS_INSET_MASK = 0x01000000;
952
953 /**
954 * Mask to check if the scrollbar style is inside or outside.
955 * {@hide}
956 */
957 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
958
959 /**
960 * Mask for scrollbar style.
961 * {@hide}
962 */
963 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
964
965 /**
966 * View flag indicating that the screen should remain on while the
967 * window containing this view is visible to the user. This effectively
968 * takes care of automatically setting the WindowManager's
969 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
970 */
971 public static final int KEEP_SCREEN_ON = 0x04000000;
972
973 /**
974 * View flag indicating whether this view should have sound effects enabled
975 * for events such as clicking and touching.
976 */
977 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
978
979 /**
980 * View flag indicating whether this view should have haptic feedback
981 * enabled for events such as long presses.
982 */
983 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
984
985 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700986 * <p>Indicates that the view hierarchy should stop saving state when
987 * it reaches this view. If state saving is initiated immediately at
988 * the view, it will be allowed.
989 * {@hide}
990 */
991 static final int PARENT_SAVE_DISABLED = 0x20000000;
992
993 /**
994 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
995 * {@hide}
996 */
997 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
998
999 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001000 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1001 * should add all focusable Views regardless if they are focusable in touch mode.
1002 */
1003 public static final int FOCUSABLES_ALL = 0x00000000;
1004
1005 /**
1006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add only Views focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1010
1011 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001012 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 * item.
1014 */
1015 public static final int FOCUS_BACKWARD = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_FORWARD = 0x00000002;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 */
1026 public static final int FOCUS_LEFT = 0x00000011;
1027
1028 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001029 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 */
1031 public static final int FOCUS_UP = 0x00000021;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_RIGHT = 0x00000042;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_DOWN = 0x00000082;
1042
Svetoslav Ganov42138042012-03-20 11:51:39 -07001043 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001044 * Bits of {@link #getMeasuredWidthAndState()} and
1045 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1046 */
1047 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1048
1049 /**
1050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1052 */
1053 public static final int MEASURED_STATE_MASK = 0xff000000;
1054
1055 /**
1056 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1057 * for functions that combine both width and height into a single int,
1058 * such as {@link #getMeasuredState()} and the childState argument of
1059 * {@link #resolveSizeAndState(int, int, int)}.
1060 */
1061 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1062
1063 /**
1064 * Bit of {@link #getMeasuredWidthAndState()} and
1065 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1066 * is smaller that the space the view would like to have.
1067 */
1068 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1069
1070 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 * Base View state sets
1072 */
1073 // Singles
1074 /**
1075 * Indicates the view has no states set. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Indicates the view is enabled. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is focused. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is selected. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is pressed. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 * @hide
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 /**
1121 * Indicates the view's window has focus. States are used with
1122 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1123 * view depending on its state.
1124 *
1125 * @see android.graphics.drawable.Drawable
1126 * @see #getDrawableState()
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 // Doubles
1130 /**
1131 * Indicates the view is enabled and has the focus.
1132 *
1133 * @see #ENABLED_STATE_SET
1134 * @see #FOCUSED_STATE_SET
1135 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001136 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 /**
1138 * Indicates the view is enabled and selected.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #SELECTED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and that its window has focus.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused and selected.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view has the focus and that its window has the focus.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #WINDOW_FOCUSED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view is selected and that its window has the focus.
1167 *
1168 * @see #SELECTED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 // Triples
1173 /**
1174 * Indicates the view is enabled, focused and selected.
1175 *
1176 * @see #ENABLED_STATE_SET
1177 * @see #FOCUSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is enabled, focused and its window has the focus.
1183 *
1184 * @see #ENABLED_STATE_SET
1185 * @see #FOCUSED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is enabled, selected and its window has the focus.
1191 *
1192 * @see #ENABLED_STATE_SET
1193 * @see #SELECTED_STATE_SET
1194 * @see #WINDOW_FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is focused, selected and its window has the focus.
1199 *
1200 * @see #FOCUSED_STATE_SET
1201 * @see #SELECTED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is enabled, focused, selected and its window
1207 * has the focus.
1208 *
1209 * @see #ENABLED_STATE_SET
1210 * @see #FOCUSED_STATE_SET
1211 * @see #SELECTED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is pressed and its window has the focus.
1217 *
1218 * @see #PRESSED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and selected.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #SELECTED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed, selected and its window has the focus.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed and focused.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed, focused and its window has the focus.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 * @see #WINDOW_FOCUSED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed, focused and selected.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed, focused, selected and its window has the focus.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #WINDOW_FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed and enabled.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #ENABLED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed, enabled and its window has the focus.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 * @see #WINDOW_FOCUSED_STATE_SET
1282 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001283 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 /**
1285 * Indicates the view is pressed, enabled and selected.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 /**
1293 * Indicates the view is pressed, enabled, selected and its window has the
1294 * focus.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 * @see #WINDOW_FOCUSED_STATE_SET
1300 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001301 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 /**
1303 * Indicates the view is pressed, enabled and focused.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #FOCUSED_STATE_SET
1308 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001309 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 /**
1311 * Indicates the view is pressed, enabled, focused and its window has the
1312 * focus.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 * @see #WINDOW_FOCUSED_STATE_SET
1318 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 /**
1321 * Indicates the view is pressed, enabled, focused and selected.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #SELECTED_STATE_SET
1326 * @see #FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused, selected and its window
1331 * has the focus.
1332 *
1333 * @see #PRESSED_STATE_SET
1334 * @see #ENABLED_STATE_SET
1335 * @see #SELECTED_STATE_SET
1336 * @see #FOCUSED_STATE_SET
1337 * @see #WINDOW_FOCUSED_STATE_SET
1338 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001339 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340
1341 /**
1342 * The order here is very important to {@link #getDrawableState()}
1343 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001344 private static final int[][] VIEW_STATE_SETS;
1345
Romain Guyb051e892010-09-28 19:09:36 -07001346 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1347 static final int VIEW_STATE_SELECTED = 1 << 1;
1348 static final int VIEW_STATE_FOCUSED = 1 << 2;
1349 static final int VIEW_STATE_ENABLED = 1 << 3;
1350 static final int VIEW_STATE_PRESSED = 1 << 4;
1351 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001352 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001353 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001354 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1355 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356
1357 static final int[] VIEW_STATE_IDS = new int[] {
1358 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1359 R.attr.state_selected, VIEW_STATE_SELECTED,
1360 R.attr.state_focused, VIEW_STATE_FOCUSED,
1361 R.attr.state_enabled, VIEW_STATE_ENABLED,
1362 R.attr.state_pressed, VIEW_STATE_PRESSED,
1363 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001364 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001365 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001366 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001367 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 };
1369
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001370 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1372 throw new IllegalStateException(
1373 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1374 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001375 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001376 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001378 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001380 orderedIds[i * 2] = viewState;
1381 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 }
1383 }
1384 }
Romain Guyb051e892010-09-28 19:09:36 -07001385 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1386 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1387 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 int numBits = Integer.bitCount(i);
1389 int[] set = new int[numBits];
1390 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001391 for (int j = 0; j < orderedIds.length; j += 2) {
1392 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393 set[pos++] = orderedIds[j];
1394 }
1395 }
1396 VIEW_STATE_SETS[i] = set;
1397 }
1398
1399 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1400 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1401 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1402 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1404 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1405 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1407 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1409 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1411 | VIEW_STATE_FOCUSED];
1412 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1413 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1415 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1417 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_ENABLED];
1420 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED];
1425 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1427 | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1430 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1431
1432 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1433 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1435 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1437 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_PRESSED];
1440 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1444 | VIEW_STATE_PRESSED];
1445 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1447 | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1450 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1455 | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1458 | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1461 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1473 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1474 | VIEW_STATE_PRESSED];
1475 }
1476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001478 * Accessibility event types that are dispatched for text population.
1479 */
1480 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1481 AccessibilityEvent.TYPE_VIEW_CLICKED
1482 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1483 | AccessibilityEvent.TYPE_VIEW_SELECTED
1484 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1485 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1486 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001487 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001488 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001489 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001490 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1491 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001492
1493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 * Temporary Rect currently for use in setBackground(). This will probably
1495 * be extended in the future to hold our own class with more than just
1496 * a Rect. :)
1497 */
1498 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001499
1500 /**
1501 * Map used to store views' tags.
1502 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001503 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001506 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001507 */
1508 private static int sNextAccessibilityViewId;
1509
1510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 * The animation currently associated with this view.
1512 * @hide
1513 */
1514 protected Animation mCurrentAnimation = null;
1515
1516 /**
1517 * Width as measured during measure pass.
1518 * {@hide}
1519 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001520 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001521 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
1523 /**
1524 * Height as measured during measure pass.
1525 * {@hide}
1526 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001527 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001528 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
1530 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001531 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1532 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1533 * its display list. This flag, used only when hw accelerated, allows us to clear the
1534 * flag while retaining this information until it's needed (at getDisplayList() time and
1535 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1536 *
1537 * {@hide}
1538 */
1539 boolean mRecreateDisplayList = false;
1540
1541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 * The view's identifier.
1543 * {@hide}
1544 *
1545 * @see #setId(int)
1546 * @see #getId()
1547 */
1548 @ViewDebug.ExportedProperty(resolveId = true)
1549 int mID = NO_ID;
1550
1551 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001552 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001553 */
1554 int mAccessibilityViewId = NO_ID;
1555
1556 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001557 * @hide
1558 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001559 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001560
1561 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 * The view's tag.
1563 * {@hide}
1564 *
1565 * @see #setTag(Object)
1566 * @see #getTag()
1567 */
1568 protected Object mTag;
1569
1570 // for mPrivateFlags:
1571 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001572 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001574 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001576 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001578 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /**
1584 * When this flag is set, this view is running an animation on behalf of its
1585 * children and should therefore not cancel invalidate requests, even if they
1586 * lie outside of this view's bounds.
1587 *
1588 * {@hide}
1589 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001590 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001592 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001594 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
1608 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001609 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 /**
1611 * Flag used to indicate that this view should be drawn once more (and only once
1612 * more) after its animation has completed.
1613 * {@hide}
1614 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616
Dianne Hackborn4702a852012-08-17 15:18:29 -07001617 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
1619 /**
1620 * Indicates that the View returned true when onSetAlpha() was called and that
1621 * the alpha must be restored.
1622 * {@hide}
1623 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001624 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625
1626 /**
1627 * Set by {@link #setScrollContainer(boolean)}.
1628 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001629 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630
1631 /**
1632 * Set by {@link #setScrollContainer(boolean)}.
1633 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001634 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635
1636 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001637 * View flag indicating whether this view was invalidated (fully or partially.)
1638 *
1639 * @hide
1640 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001641 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001642
1643 /**
1644 * View flag indicating whether this view was invalidated by an opaque
1645 * invalidate request.
1646 *
1647 * @hide
1648 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001649 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001650
1651 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001652 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001653 *
1654 * @hide
1655 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001656 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001657
1658 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001659 * Indicates whether the background is opaque.
1660 *
1661 * @hide
1662 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001663 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001664
1665 /**
1666 * Indicates whether the scrollbars are opaque.
1667 *
1668 * @hide
1669 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001670 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001671
1672 /**
1673 * Indicates whether the view is opaque.
1674 *
1675 * @hide
1676 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001677 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001678
Adam Powelle14579b2009-12-16 18:39:52 -08001679 /**
1680 * Indicates a prepressed state;
1681 * the short time between ACTION_DOWN and recognizing
1682 * a 'real' press. Prepressed is used to recognize quick taps
1683 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001684 *
Adam Powelle14579b2009-12-16 18:39:52 -08001685 * @hide
1686 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001687 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001688
Adam Powellc9fbaab2010-02-16 17:16:19 -08001689 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001690 * Indicates whether the view is temporarily detached.
1691 *
1692 * @hide
1693 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001694 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001695
Adam Powell8568c3a2010-04-19 14:26:11 -07001696 /**
1697 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001698 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001699 * @hide
1700 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001701 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001702
1703 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001704 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1705 * @hide
1706 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001707 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001708
1709 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001710 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1711 * for transform operations
1712 *
1713 * @hide
1714 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001715 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001716
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001717 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001718 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001719
Chet Haasefd2b0022010-08-06 13:08:56 -07001720 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001721 * Indicates that this view was specifically invalidated, not just dirtied because some
1722 * child view was invalidated. The flag is used to determine when we need to recreate
1723 * a view's display list (as opposed to just returning a reference to its existing
1724 * display list).
1725 *
1726 * @hide
1727 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001728 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001729
Christopher Tate3d4bf172011-03-28 16:16:46 -07001730 /* Masks for mPrivateFlags2 */
1731
1732 /**
1733 * Indicates that this view has reported that it can accept the current drag's content.
1734 * Cleared when the drag operation concludes.
1735 * @hide
1736 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001737 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001738
1739 /**
1740 * Indicates that this view is currently directly under the drag location in a
1741 * drag-and-drop operation involving content that it can accept. Cleared when
1742 * the drag exits the view, or when the drag operation concludes.
1743 * @hide
1744 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001745 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001746
Cibu Johny86666632010-02-22 13:01:02 -08001747 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001748 * Horizontal layout direction of this view is from Left to Right.
1749 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001750 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001751 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001752
1753 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001754 * Horizontal layout direction of this view is from Right to Left.
1755 * Use with {@link #setLayoutDirection}.
1756 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001757 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001758
1759 /**
1760 * Horizontal layout direction of this view is inherited from its parent.
1761 * Use with {@link #setLayoutDirection}.
1762 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001763 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001764
1765 /**
1766 * Horizontal layout direction of this view is from deduced from the default language
1767 * script for the locale. Use with {@link #setLayoutDirection}.
1768 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001769 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001770
1771 /**
1772 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001773 * @hide
1774 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001775 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001776
1777 /**
1778 * Mask for use with private flags indicating bits used for horizontal layout direction.
1779 * @hide
1780 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001781 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001782
1783 /**
1784 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1785 * right-to-left direction.
1786 * @hide
1787 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001788 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001789
1790 /**
1791 * Indicates whether the view horizontal layout direction has been resolved.
1792 * @hide
1793 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001794 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001795
1796 /**
1797 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1798 * @hide
1799 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001800 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1801 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001802
1803 /*
1804 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1805 * flag value.
1806 * @hide
1807 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001808 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1809 LAYOUT_DIRECTION_LTR,
1810 LAYOUT_DIRECTION_RTL,
1811 LAYOUT_DIRECTION_INHERIT,
1812 LAYOUT_DIRECTION_LOCALE
1813 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Default horizontal layout direction.
1817 * @hide
1818 */
1819 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001820
Adam Powell539ee872012-02-03 19:00:49 -08001821 /**
1822 * Indicates that the view is tracking some sort of transient state
1823 * that the app should not need to be aware of, but that the framework
1824 * should take special care to preserve.
1825 *
1826 * @hide
1827 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001828 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001829
1830
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001831 /**
1832 * Text direction is inherited thru {@link ViewGroup}
1833 */
1834 public static final int TEXT_DIRECTION_INHERIT = 0;
1835
1836 /**
1837 * Text direction is using "first strong algorithm". The first strong directional character
1838 * determines the paragraph direction. If there is no strong directional character, the
1839 * paragraph direction is the view's resolved layout direction.
1840 */
1841 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1842
1843 /**
1844 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1845 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1846 * If there are neither, the paragraph direction is the view's resolved layout direction.
1847 */
1848 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1849
1850 /**
1851 * Text direction is forced to LTR.
1852 */
1853 public static final int TEXT_DIRECTION_LTR = 3;
1854
1855 /**
1856 * Text direction is forced to RTL.
1857 */
1858 public static final int TEXT_DIRECTION_RTL = 4;
1859
1860 /**
1861 * Text direction is coming from the system Locale.
1862 */
1863 public static final int TEXT_DIRECTION_LOCALE = 5;
1864
1865 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001866 * Default text direction is inherited
1867 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001868 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001869
1870 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001871 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1872 * @hide
1873 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001874 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001875
1876 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001877 * Mask for use with private flags indicating bits used for text direction.
1878 * @hide
1879 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001880 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1881 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001882
1883 /**
1884 * Array of text direction flags for mapping attribute "textDirection" to correct
1885 * flag value.
1886 * @hide
1887 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001888 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1889 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1890 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1891 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1892 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1893 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1894 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001895 };
1896
1897 /**
1898 * Indicates whether the view text direction has been resolved.
1899 * @hide
1900 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001901 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1902 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001903
1904 /**
1905 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1906 * @hide
1907 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001908 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001909
1910 /**
1911 * Mask for use with private flags indicating bits used for resolved text direction.
1912 * @hide
1913 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001914 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1915 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001916
1917 /**
1918 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1919 * @hide
1920 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001921 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1922 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001923
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001924 /*
1925 * Default text alignment. The text alignment of this View is inherited from its parent.
1926 * Use with {@link #setTextAlignment(int)}
1927 */
1928 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1929
1930 /**
1931 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1932 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1933 *
1934 * Use with {@link #setTextAlignment(int)}
1935 */
1936 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1937
1938 /**
1939 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1940 *
1941 * Use with {@link #setTextAlignment(int)}
1942 */
1943 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1944
1945 /**
1946 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1947 *
1948 * Use with {@link #setTextAlignment(int)}
1949 */
1950 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1951
1952 /**
1953 * Center the paragraph, e.g. ALIGN_CENTER.
1954 *
1955 * Use with {@link #setTextAlignment(int)}
1956 */
1957 public static final int TEXT_ALIGNMENT_CENTER = 4;
1958
1959 /**
1960 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
1961 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
1962 *
1963 * Use with {@link #setTextAlignment(int)}
1964 */
1965 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
1966
1967 /**
1968 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
1969 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
1970 *
1971 * Use with {@link #setTextAlignment(int)}
1972 */
1973 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
1974
1975 /**
1976 * Default text alignment is inherited
1977 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001978 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001979
1980 /**
1981 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1982 * @hide
1983 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001984 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001985
1986 /**
1987 * Mask for use with private flags indicating bits used for text alignment.
1988 * @hide
1989 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001990 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001991
1992 /**
1993 * Array of text direction flags for mapping attribute "textAlignment" to correct
1994 * flag value.
1995 * @hide
1996 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001997 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
1998 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
1999 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2000 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2001 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2002 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2003 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2004 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002005 };
2006
2007 /**
2008 * Indicates whether the view text alignment has been resolved.
2009 * @hide
2010 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002011 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002012
2013 /**
2014 * Bit shift to get the resolved text alignment.
2015 * @hide
2016 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002017 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002018
2019 /**
2020 * Mask for use with private flags indicating bits used for text alignment.
2021 * @hide
2022 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002023 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2024 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002025
2026 /**
2027 * Indicates whether if the view text alignment has been resolved to gravity
2028 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002029 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2030 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002031
Svetoslav Ganov42138042012-03-20 11:51:39 -07002032 // Accessiblity constants for mPrivateFlags2
2033
2034 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002035 * Shift for the bits in {@link #mPrivateFlags2} related to the
2036 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002037 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002038 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002039
2040 /**
2041 * Automatically determine whether a view is important for accessibility.
2042 */
2043 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2044
2045 /**
2046 * The view is important for accessibility.
2047 */
2048 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2049
2050 /**
2051 * The view is not important for accessibility.
2052 */
2053 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2054
2055 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002056 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002057 */
2058 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2059
2060 /**
2061 * Mask for obtainig the bits which specify how to determine
2062 * whether a view is important for accessibility.
2063 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002064 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002065 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002066 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002067
2068 /**
2069 * Flag indicating whether a view has accessibility focus.
2070 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002071 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002072
2073 /**
2074 * Flag indicating whether a view state for accessibility has changed.
2075 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002076 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2077 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002078
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002079 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002080 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2081 * is used to check whether later changes to the view's transform should invalidate the
2082 * view to force the quickReject test to run again.
2083 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002084 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002085
Adam Powell0090f202012-08-07 17:15:30 -07002086 /**
2087 * Flag indicating that start/end padding has been resolved into left/right padding
2088 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2089 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2090 * during measurement. In some special cases this is required such as when an adapter-based
2091 * view measures prospective children without attaching them to a window.
2092 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002093 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002094
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002095 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002096
Christopher Tate3d4bf172011-03-28 16:16:46 -07002097 /* End of masks for mPrivateFlags2 */
2098
Chet Haase21433372012-06-05 07:54:09 -07002099 /* Masks for mPrivateFlags3 */
2100
2101 /**
2102 * Flag indicating that view has a transform animation set on it. This is used to track whether
2103 * an animation is cleared between successive frames, in order to tell the associated
2104 * DisplayList to clear its animation matrix.
2105 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002106 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002107
2108 /**
2109 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2110 * animation is cleared between successive frames, in order to tell the associated
2111 * DisplayList to restore its alpha value.
2112 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002113 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002114
2115
2116 /* End of masks for mPrivateFlags3 */
2117
Dianne Hackborn4702a852012-08-17 15:18:29 -07002118 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002119
Chet Haasedaf98e92011-01-10 14:10:36 -08002120 /**
Adam Powell637d3372010-08-25 14:37:03 -07002121 * Always allow a user to over-scroll this view, provided it is a
2122 * view that can scroll.
2123 *
2124 * @see #getOverScrollMode()
2125 * @see #setOverScrollMode(int)
2126 */
2127 public static final int OVER_SCROLL_ALWAYS = 0;
2128
2129 /**
2130 * Allow a user to over-scroll this view only if the content is large
2131 * enough to meaningfully scroll, provided it is a view that can scroll.
2132 *
2133 * @see #getOverScrollMode()
2134 * @see #setOverScrollMode(int)
2135 */
2136 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2137
2138 /**
2139 * Never allow a user to over-scroll this view.
2140 *
2141 * @see #getOverScrollMode()
2142 * @see #setOverScrollMode(int)
2143 */
2144 public static final int OVER_SCROLL_NEVER = 2;
2145
2146 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002147 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2148 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002149 *
Joe Malin32736f02011-01-19 16:14:20 -08002150 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002151 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002152 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002153
2154 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002155 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2156 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002157 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002158 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002159 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002160 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002162 *
Joe Malin32736f02011-01-19 16:14:20 -08002163 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002164 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002165 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2166
2167 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002168 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2169 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002170 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002171 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002172 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2173 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2174 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002175 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002176 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2177 * window flags) for displaying content using every last pixel on the display.
2178 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002179 * <p>There is a limitation: because navigation controls are so important, the least user
2180 * interaction will cause them to reappear immediately. When this happens, both
2181 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2182 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002183 *
2184 * @see #setSystemUiVisibility(int)
2185 */
2186 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2187
2188 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002189 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2190 * into the normal fullscreen mode so that its content can take over the screen
2191 * while still allowing the user to interact with the application.
2192 *
2193 * <p>This has the same visual effect as
2194 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2195 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2196 * meaning that non-critical screen decorations (such as the status bar) will be
2197 * hidden while the user is in the View's window, focusing the experience on
2198 * that content. Unlike the window flag, if you are using ActionBar in
2199 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2200 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2201 * hide the action bar.
2202 *
2203 * <p>This approach to going fullscreen is best used over the window flag when
2204 * it is a transient state -- that is, the application does this at certain
2205 * points in its user interaction where it wants to allow the user to focus
2206 * on content, but not as a continuous state. For situations where the application
2207 * would like to simply stay full screen the entire time (such as a game that
2208 * wants to take over the screen), the
2209 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2210 * is usually a better approach. The state set here will be removed by the system
2211 * in various situations (such as the user moving to another application) like
2212 * the other system UI states.
2213 *
2214 * <p>When using this flag, the application should provide some easy facility
2215 * for the user to go out of it. A common example would be in an e-book
2216 * reader, where tapping on the screen brings back whatever screen and UI
2217 * decorations that had been hidden while the user was immersed in reading
2218 * the book.
2219 *
2220 * @see #setSystemUiVisibility(int)
2221 */
2222 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2223
2224 /**
2225 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2226 * flags, we would like a stable view of the content insets given to
2227 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2228 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002229 * as a continuous state. In the stock Android UI this is the space for
2230 * the system bar, nav bar, and status bar, but not more transient elements
2231 * such as an input method.
2232 *
2233 * The stable layout your UI sees is based on the system UI modes you can
2234 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2235 * then you will get a stable layout for changes of the
2236 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2237 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2238 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2239 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2240 * with a stable layout. (Note that you should avoid using
2241 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2242 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002243 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002244 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2245 * then a hidden status bar will be considered a "stable" state for purposes
2246 * here. This allows your UI to continually hide the status bar, while still
2247 * using the system UI flags to hide the action bar while still retaining
2248 * a stable layout. Note that changing the window fullscreen flag will never
2249 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002250 *
2251 * <p>If you are using ActionBar in
2252 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2253 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2254 * insets it adds to those given to the application.
2255 */
2256 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2257
2258 /**
2259 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2260 * to be layed out as if it has requested
2261 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2262 * allows it to avoid artifacts when switching in and out of that mode, at
2263 * the expense that some of its user interface may be covered by screen
2264 * decorations when they are shown. You can perform layout of your inner
2265 * UI elements to account for the navagation system UI through the
2266 * {@link #fitSystemWindows(Rect)} method.
2267 */
2268 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2269
2270 /**
2271 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2272 * to be layed out as if it has requested
2273 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2274 * allows it to avoid artifacts when switching in and out of that mode, at
2275 * the expense that some of its user interface may be covered by screen
2276 * decorations when they are shown. You can perform layout of your inner
2277 * UI elements to account for non-fullscreen system UI through the
2278 * {@link #fitSystemWindows(Rect)} method.
2279 */
2280 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2281
2282 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002283 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2284 */
2285 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2286
2287 /**
2288 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2289 */
2290 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002291
2292 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002293 * @hide
2294 *
2295 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2296 * out of the public fields to keep the undefined bits out of the developer's way.
2297 *
2298 * Flag to make the status bar not expandable. Unless you also
2299 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2300 */
2301 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2302
2303 /**
2304 * @hide
2305 *
2306 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2307 * out of the public fields to keep the undefined bits out of the developer's way.
2308 *
2309 * Flag to hide notification icons and scrolling ticker text.
2310 */
2311 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2312
2313 /**
2314 * @hide
2315 *
2316 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2317 * out of the public fields to keep the undefined bits out of the developer's way.
2318 *
2319 * Flag to disable incoming notification alerts. This will not block
2320 * icons, but it will block sound, vibrating and other visual or aural notifications.
2321 */
2322 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2323
2324 /**
2325 * @hide
2326 *
2327 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2328 * out of the public fields to keep the undefined bits out of the developer's way.
2329 *
2330 * Flag to hide only the scrolling ticker. Note that
2331 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2332 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2333 */
2334 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2335
2336 /**
2337 * @hide
2338 *
2339 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2340 * out of the public fields to keep the undefined bits out of the developer's way.
2341 *
2342 * Flag to hide the center system info area.
2343 */
2344 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2345
2346 /**
2347 * @hide
2348 *
2349 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2350 * out of the public fields to keep the undefined bits out of the developer's way.
2351 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002352 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002353 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2354 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002355 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356
2357 /**
2358 * @hide
2359 *
2360 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2361 * out of the public fields to keep the undefined bits out of the developer's way.
2362 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002363 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002364 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2365 */
2366 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2367
2368 /**
2369 * @hide
2370 *
2371 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2372 * out of the public fields to keep the undefined bits out of the developer's way.
2373 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002374 * Flag to hide only the clock. You might use this if your activity has
2375 * its own clock making the status bar's clock redundant.
2376 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002377 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2378
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002379 /**
2380 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002381 *
2382 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2383 * out of the public fields to keep the undefined bits out of the developer's way.
2384 *
2385 * Flag to hide only the recent apps button. Don't use this
2386 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2387 */
2388 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2389
2390 /**
2391 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002392 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002393 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002394
2395 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002396 * These are the system UI flags that can be cleared by events outside
2397 * of an application. Currently this is just the ability to tap on the
2398 * screen while hiding the navigation bar to have it return.
2399 * @hide
2400 */
2401 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002402 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2403 | SYSTEM_UI_FLAG_FULLSCREEN;
2404
2405 /**
2406 * Flags that can impact the layout in relation to system UI.
2407 */
2408 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2409 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2410 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002411
2412 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002413 * Find views that render the specified text.
2414 *
2415 * @see #findViewsWithText(ArrayList, CharSequence, int)
2416 */
2417 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2418
2419 /**
2420 * Find find views that contain the specified content description.
2421 *
2422 * @see #findViewsWithText(ArrayList, CharSequence, int)
2423 */
2424 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2425
2426 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002427 * Find views that contain {@link AccessibilityNodeProvider}. Such
2428 * a View is a root of virtual view hierarchy and may contain the searched
2429 * text. If this flag is set Views with providers are automatically
2430 * added and it is a responsibility of the client to call the APIs of
2431 * the provider to determine whether the virtual tree rooted at this View
2432 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2433 * represeting the virtual views with this text.
2434 *
2435 * @see #findViewsWithText(ArrayList, CharSequence, int)
2436 *
2437 * @hide
2438 */
2439 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2440
2441 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002442 * The undefined cursor position.
2443 */
2444 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2445
2446 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002447 * Indicates that the screen has changed state and is now off.
2448 *
2449 * @see #onScreenStateChanged(int)
2450 */
2451 public static final int SCREEN_STATE_OFF = 0x0;
2452
2453 /**
2454 * Indicates that the screen has changed state and is now on.
2455 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002456 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002457 */
2458 public static final int SCREEN_STATE_ON = 0x1;
2459
2460 /**
Adam Powell637d3372010-08-25 14:37:03 -07002461 * Controls the over-scroll mode for this view.
2462 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2463 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2464 * and {@link #OVER_SCROLL_NEVER}.
2465 */
2466 private int mOverScrollMode;
2467
2468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 * The parent this view is attached to.
2470 * {@hide}
2471 *
2472 * @see #getParent()
2473 */
2474 protected ViewParent mParent;
2475
2476 /**
2477 * {@hide}
2478 */
2479 AttachInfo mAttachInfo;
2480
2481 /**
2482 * {@hide}
2483 */
Romain Guy809a7f62009-05-14 15:44:42 -07002484 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002485 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002486 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002487 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002488 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002489 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002490 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002491 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2492 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2493 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2494 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002495 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002497 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002498 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499
2500 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002501 * This view's request for the visibility of the status bar.
2502 * @hide
2503 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002504 @ViewDebug.ExportedProperty(flagMapping = {
2505 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2506 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2507 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2508 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2509 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2510 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2511 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2512 equals = SYSTEM_UI_FLAG_VISIBLE,
2513 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2514 })
Joe Onorato664644d2011-01-23 17:53:23 -08002515 int mSystemUiVisibility;
2516
2517 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002518 * Reference count for transient state.
2519 * @see #setHasTransientState(boolean)
2520 */
2521 int mTransientStateCount = 0;
2522
2523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 * Count of how many windows this view has been attached to.
2525 */
2526 int mWindowAttachCount;
2527
2528 /**
2529 * The layout parameters associated with this view and used by the parent
2530 * {@link android.view.ViewGroup} to determine how this view should be
2531 * laid out.
2532 * {@hide}
2533 */
2534 protected ViewGroup.LayoutParams mLayoutParams;
2535
2536 /**
2537 * The view flags hold various views states.
2538 * {@hide}
2539 */
2540 @ViewDebug.ExportedProperty
2541 int mViewFlags;
2542
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002543 static class TransformationInfo {
2544 /**
2545 * The transform matrix for the View. This transform is calculated internally
2546 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2547 * is used by default. Do *not* use this variable directly; instead call
2548 * getMatrix(), which will automatically recalculate the matrix if necessary
2549 * to get the correct matrix based on the latest rotation and scale properties.
2550 */
2551 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002553 /**
2554 * The transform matrix for the View. This transform is calculated internally
2555 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2556 * is used by default. Do *not* use this variable directly; instead call
2557 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2558 * to get the correct matrix based on the latest rotation and scale properties.
2559 */
2560 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002561
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002562 /**
2563 * An internal variable that tracks whether we need to recalculate the
2564 * transform matrix, based on whether the rotation or scaleX/Y properties
2565 * have changed since the matrix was last calculated.
2566 */
2567 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002568
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002569 /**
2570 * An internal variable that tracks whether we need to recalculate the
2571 * transform matrix, based on whether the rotation or scaleX/Y properties
2572 * have changed since the matrix was last calculated.
2573 */
2574 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002575
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002576 /**
2577 * A variable that tracks whether we need to recalculate the
2578 * transform matrix, based on whether the rotation or scaleX/Y properties
2579 * have changed since the matrix was last calculated. This variable
2580 * is only valid after a call to updateMatrix() or to a function that
2581 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2582 */
2583 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002585 /**
2586 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2587 */
2588 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002589
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002590 /**
2591 * This matrix is used when computing the matrix for 3D rotations.
2592 */
2593 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002594
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002595 /**
2596 * These prev values are used to recalculate a centered pivot point when necessary. The
2597 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2598 * set), so thes values are only used then as well.
2599 */
2600 private int mPrevWidth = -1;
2601 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002602
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002603 /**
2604 * The degrees rotation around the vertical axis through the pivot point.
2605 */
2606 @ViewDebug.ExportedProperty
2607 float mRotationY = 0f;
2608
2609 /**
2610 * The degrees rotation around the horizontal axis through the pivot point.
2611 */
2612 @ViewDebug.ExportedProperty
2613 float mRotationX = 0f;
2614
2615 /**
2616 * The degrees rotation around the pivot point.
2617 */
2618 @ViewDebug.ExportedProperty
2619 float mRotation = 0f;
2620
2621 /**
2622 * The amount of translation of the object away from its left property (post-layout).
2623 */
2624 @ViewDebug.ExportedProperty
2625 float mTranslationX = 0f;
2626
2627 /**
2628 * The amount of translation of the object away from its top property (post-layout).
2629 */
2630 @ViewDebug.ExportedProperty
2631 float mTranslationY = 0f;
2632
2633 /**
2634 * The amount of scale in the x direction around the pivot point. A
2635 * value of 1 means no scaling is applied.
2636 */
2637 @ViewDebug.ExportedProperty
2638 float mScaleX = 1f;
2639
2640 /**
2641 * The amount of scale in the y direction around the pivot point. A
2642 * value of 1 means no scaling is applied.
2643 */
2644 @ViewDebug.ExportedProperty
2645 float mScaleY = 1f;
2646
2647 /**
Chet Haasea33de552012-02-03 16:28:24 -08002648 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002649 */
2650 @ViewDebug.ExportedProperty
2651 float mPivotX = 0f;
2652
2653 /**
Chet Haasea33de552012-02-03 16:28:24 -08002654 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002655 */
2656 @ViewDebug.ExportedProperty
2657 float mPivotY = 0f;
2658
2659 /**
2660 * The opacity of the View. This is a value from 0 to 1, where 0 means
2661 * completely transparent and 1 means completely opaque.
2662 */
2663 @ViewDebug.ExportedProperty
2664 float mAlpha = 1f;
2665 }
2666
2667 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002668
Joe Malin32736f02011-01-19 16:14:20 -08002669 private boolean mLastIsOpaque;
2670
Chet Haasefd2b0022010-08-06 13:08:56 -07002671 /**
2672 * Convenience value to check for float values that are close enough to zero to be considered
2673 * zero.
2674 */
Romain Guy2542d192010-08-18 11:47:12 -07002675 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002676
2677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 * The distance in pixels from the left edge of this view's parent
2679 * to the left edge of this view.
2680 * {@hide}
2681 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002682 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 protected int mLeft;
2684 /**
2685 * The distance in pixels from the left edge of this view's parent
2686 * to the right edge of this view.
2687 * {@hide}
2688 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002689 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 protected int mRight;
2691 /**
2692 * The distance in pixels from the top edge of this view's parent
2693 * to the top edge of this view.
2694 * {@hide}
2695 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002696 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 protected int mTop;
2698 /**
2699 * The distance in pixels from the top edge of this view's parent
2700 * to the bottom edge of this view.
2701 * {@hide}
2702 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002703 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 protected int mBottom;
2705
2706 /**
2707 * The offset, in pixels, by which the content of this view is scrolled
2708 * horizontally.
2709 * {@hide}
2710 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002711 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 protected int mScrollX;
2713 /**
2714 * The offset, in pixels, by which the content of this view is scrolled
2715 * vertically.
2716 * {@hide}
2717 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002718 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 protected int mScrollY;
2720
2721 /**
2722 * The left padding in pixels, that is the distance in pixels between the
2723 * left edge of this view and the left edge of its content.
2724 * {@hide}
2725 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002726 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 protected int mPaddingLeft;
2728 /**
2729 * The right padding in pixels, that is the distance in pixels between the
2730 * right edge of this view and the right edge of its content.
2731 * {@hide}
2732 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002733 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 protected int mPaddingRight;
2735 /**
2736 * The top padding in pixels, that is the distance in pixels between the
2737 * top edge of this view and the top edge of its content.
2738 * {@hide}
2739 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002740 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 protected int mPaddingTop;
2742 /**
2743 * The bottom padding in pixels, that is the distance in pixels between the
2744 * bottom edge of this view and the bottom edge of its content.
2745 * {@hide}
2746 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002747 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 protected int mPaddingBottom;
2749
2750 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002751 * The layout insets in pixels, that is the distance in pixels between the
2752 * visible edges of this view its bounds.
2753 */
2754 private Insets mLayoutInsets;
2755
2756 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002757 * Briefly describes the view and is primarily used for accessibility support.
2758 */
2759 private CharSequence mContentDescription;
2760
2761 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002762 * Specifies the id of a view for which this view serves as a label for
2763 * accessibility purposes.
2764 */
2765 private int mLabelForId = View.NO_ID;
2766
2767 /**
2768 * Predicate for matching labeled view id with its label for
2769 * accessibility purposes.
2770 */
2771 private MatchLabelForPredicate mMatchLabelForPredicate;
2772
2773 /**
2774 * Predicate for matching a view by its id.
2775 */
2776 private MatchIdPredicate mMatchIdPredicate;
2777
2778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002780 *
2781 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002783 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002784 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785
2786 /**
2787 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002788 *
2789 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002791 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002792 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002794 /**
Adam Powell20232d02010-12-08 21:08:53 -08002795 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002796 *
2797 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002798 */
2799 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002800 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002801
2802 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002803 * Cache the paddingStart set by the user to append to the scrollbar's size.
2804 *
2805 */
2806 @ViewDebug.ExportedProperty(category = "padding")
2807 int mUserPaddingStart;
2808
2809 /**
2810 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2811 *
2812 */
2813 @ViewDebug.ExportedProperty(category = "padding")
2814 int mUserPaddingEnd;
2815
2816 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002817 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002818 *
2819 * @hide
2820 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002821 int mUserPaddingLeftInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002822
2823 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002824 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002825 *
2826 * @hide
2827 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002828 int mUserPaddingRightInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002829
2830 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002831 * Default undefined padding
2832 */
2833 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2834
2835 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002836 * @hide
2837 */
2838 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2839 /**
2840 * @hide
2841 */
2842 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843
Philip Milne6c8ea062012-04-03 17:38:43 -07002844 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845
2846 private int mBackgroundResource;
2847 private boolean mBackgroundSizeChanged;
2848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 static class ListenerInfo {
2850 /**
2851 * Listener used to dispatch focus change events.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 /**
2858 * Listeners for layout change events.
2859 */
2860 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002861
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002862 /**
2863 * Listeners for attach events.
2864 */
2865 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002866
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002867 /**
2868 * Listener used to dispatch click events.
2869 * This field should be made private, so it is hidden from the SDK.
2870 * {@hide}
2871 */
2872 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002874 /**
2875 * Listener used to dispatch long click events.
2876 * This field should be made private, so it is hidden from the SDK.
2877 * {@hide}
2878 */
2879 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002881 /**
2882 * Listener used to build the context menu.
2883 * This field should be made private, so it is hidden from the SDK.
2884 * {@hide}
2885 */
2886 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002888 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002890 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002892 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002893
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002894 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002895
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002896 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002897
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002898 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2899 }
2900
2901 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 /**
2904 * The application environment this view lives in.
2905 * This field should be made private, so it is hidden from the SDK.
2906 * {@hide}
2907 */
2908 protected Context mContext;
2909
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002910 private final Resources mResources;
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 private ScrollabilityCache mScrollCache;
2913
2914 private int[] mDrawableState = null;
2915
Romain Guy0211a0a2011-02-14 16:34:59 -08002916 /**
2917 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002918 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002919 * @hide
2920 */
2921 public boolean mCachingFailed;
2922
Romain Guy02890fd2010-08-06 17:58:44 -07002923 private Bitmap mDrawingCache;
2924 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002925 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002926 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927
2928 /**
2929 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2930 * the user may specify which view to go to next.
2931 */
2932 private int mNextFocusLeftId = View.NO_ID;
2933
2934 /**
2935 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2936 * the user may specify which view to go to next.
2937 */
2938 private int mNextFocusRightId = View.NO_ID;
2939
2940 /**
2941 * When this view has focus and the next focus is {@link #FOCUS_UP},
2942 * the user may specify which view to go to next.
2943 */
2944 private int mNextFocusUpId = View.NO_ID;
2945
2946 /**
2947 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2948 * the user may specify which view to go to next.
2949 */
2950 private int mNextFocusDownId = View.NO_ID;
2951
Jeff Brown4e6319b2010-12-13 10:36:51 -08002952 /**
2953 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2954 * the user may specify which view to go to next.
2955 */
2956 int mNextFocusForwardId = View.NO_ID;
2957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002959 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002960 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002961 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 private UnsetPressedState mUnsetPressedState;
2964
2965 /**
2966 * Whether the long press's action has been invoked. The tap's action is invoked on the
2967 * up event while a long press is invoked as soon as the long press duration is reached, so
2968 * a long press could be performed before the tap is checked, in which case the tap's action
2969 * should not be invoked.
2970 */
2971 private boolean mHasPerformedLongPress;
2972
2973 /**
2974 * The minimum height of the view. We'll try our best to have the height
2975 * of this view to at least this amount.
2976 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002977 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 private int mMinHeight;
2979
2980 /**
2981 * The minimum width of the view. We'll try our best to have the width
2982 * of this view to at least this amount.
2983 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002984 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 private int mMinWidth;
2986
2987 /**
2988 * The delegate to handle touch events that are physically in this view
2989 * but should be handled by another view.
2990 */
2991 private TouchDelegate mTouchDelegate = null;
2992
2993 /**
2994 * Solid color to use as a background when creating the drawing cache. Enables
2995 * the cache to use 16 bit bitmaps instead of 32 bit.
2996 */
2997 private int mDrawingCacheBackgroundColor = 0;
2998
2999 /**
3000 * Special tree observer used when mAttachInfo is null.
3001 */
3002 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003003
Adam Powelle14579b2009-12-16 18:39:52 -08003004 /**
3005 * Cache the touch slop from the context that created the view.
3006 */
3007 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003010 * Object that handles automatic animation of view properties.
3011 */
3012 private ViewPropertyAnimator mAnimator = null;
3013
3014 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003015 * Flag indicating that a drag can cross window boundaries. When
3016 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3017 * with this flag set, all visible applications will be able to participate
3018 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003019 *
3020 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003021 */
3022 public static final int DRAG_FLAG_GLOBAL = 1;
3023
3024 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003025 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3026 */
3027 private float mVerticalScrollFactor;
3028
3029 /**
Adam Powell20232d02010-12-08 21:08:53 -08003030 * Position of the vertical scroll bar.
3031 */
3032 private int mVerticalScrollbarPosition;
3033
3034 /**
3035 * Position the scroll bar at the default position as determined by the system.
3036 */
3037 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3038
3039 /**
3040 * Position the scroll bar along the left edge.
3041 */
3042 public static final int SCROLLBAR_POSITION_LEFT = 1;
3043
3044 /**
3045 * Position the scroll bar along the right edge.
3046 */
3047 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3048
3049 /**
Romain Guy171c5922011-01-06 10:04:23 -08003050 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003051 *
3052 * @see #getLayerType()
3053 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003054 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003055 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003056 */
3057 public static final int LAYER_TYPE_NONE = 0;
3058
3059 /**
3060 * <p>Indicates that the view has a software layer. A software layer is backed
3061 * by a bitmap and causes the view to be rendered using Android's software
3062 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003063 *
Romain Guy171c5922011-01-06 10:04:23 -08003064 * <p>Software layers have various usages:</p>
3065 * <p>When the application is not using hardware acceleration, a software layer
3066 * is useful to apply a specific color filter and/or blending mode and/or
3067 * translucency to a view and all its children.</p>
3068 * <p>When the application is using hardware acceleration, a software layer
3069 * is useful to render drawing primitives not supported by the hardware
3070 * accelerated pipeline. It can also be used to cache a complex view tree
3071 * into a texture and reduce the complexity of drawing operations. For instance,
3072 * when animating a complex view tree with a translation, a software layer can
3073 * be used to render the view tree only once.</p>
3074 * <p>Software layers should be avoided when the affected view tree updates
3075 * often. Every update will require to re-render the software layer, which can
3076 * potentially be slow (particularly when hardware acceleration is turned on
3077 * since the layer will have to be uploaded into a hardware texture after every
3078 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003079 *
3080 * @see #getLayerType()
3081 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003082 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003083 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003084 */
3085 public static final int LAYER_TYPE_SOFTWARE = 1;
3086
3087 /**
3088 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3089 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3090 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3091 * rendering pipeline, but only if hardware acceleration is turned on for the
3092 * view hierarchy. When hardware acceleration is turned off, hardware layers
3093 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003094 *
Romain Guy171c5922011-01-06 10:04:23 -08003095 * <p>A hardware layer is useful to apply a specific color filter and/or
3096 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003097 * <p>A hardware layer can be used to cache a complex view tree into a
3098 * texture and reduce the complexity of drawing operations. For instance,
3099 * when animating a complex view tree with a translation, a hardware layer can
3100 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003101 * <p>A hardware layer can also be used to increase the rendering quality when
3102 * rotation transformations are applied on a view. It can also be used to
3103 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003104 *
3105 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003106 * @see #setLayerType(int, android.graphics.Paint)
3107 * @see #LAYER_TYPE_NONE
3108 * @see #LAYER_TYPE_SOFTWARE
3109 */
3110 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003111
Romain Guy3aaff3a2011-01-12 14:18:47 -08003112 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3113 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3114 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3115 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3116 })
Romain Guy171c5922011-01-06 10:04:23 -08003117 int mLayerType = LAYER_TYPE_NONE;
3118 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003119 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003120
3121 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003122 * Set to true when the view is sending hover accessibility events because it
3123 * is the innermost hovered view.
3124 */
3125 private boolean mSendingHoverAccessibilityEvents;
3126
Dianne Hackborn4702a852012-08-17 15:18:29 -07003127 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003128 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003129 */
3130 AccessibilityDelegate mAccessibilityDelegate;
3131
3132 /**
3133 * Consistency verifier for debugging purposes.
3134 * @hide
3135 */
3136 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3137 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3138 new InputEventConsistencyVerifier(this, 0) : null;
3139
Adam Powella9108a22012-07-18 11:18:09 -07003140 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3141
Jeff Brown87b7f802011-06-21 18:35:45 -07003142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 * Simple constructor to use when creating a view from code.
3144 *
3145 * @param context The Context the view is running in, through which it can
3146 * access the current theme, resources, etc.
3147 */
3148 public View(Context context) {
3149 mContext = context;
3150 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003151 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003152 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003153 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3154 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3155 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3156 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003157 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003158 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003159 mUserPaddingStart = UNDEFINED_PADDING;
3160 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 }
3162
3163 /**
3164 * Constructor that is called when inflating a view from XML. This is called
3165 * when a view is being constructed from an XML file, supplying attributes
3166 * that were specified in the XML file. This version uses a default style of
3167 * 0, so the only attribute values applied are those in the Context's Theme
3168 * and the given AttributeSet.
3169 *
3170 * <p>
3171 * The method onFinishInflate() will be called after all children have been
3172 * added.
3173 *
3174 * @param context The Context the view is running in, through which it can
3175 * access the current theme, resources, etc.
3176 * @param attrs The attributes of the XML tag that is inflating the view.
3177 * @see #View(Context, AttributeSet, int)
3178 */
3179 public View(Context context, AttributeSet attrs) {
3180 this(context, attrs, 0);
3181 }
3182
3183 /**
3184 * Perform inflation from XML and apply a class-specific base style. This
3185 * constructor of View allows subclasses to use their own base style when
3186 * they are inflating. For example, a Button class's constructor would call
3187 * this version of the super class constructor and supply
3188 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3189 * the theme's button style to modify all of the base view attributes (in
3190 * particular its background) as well as the Button class's attributes.
3191 *
3192 * @param context The Context the view is running in, through which it can
3193 * access the current theme, resources, etc.
3194 * @param attrs The attributes of the XML tag that is inflating the view.
3195 * @param defStyle The default style to apply to this view. If 0, no style
3196 * will be applied (beyond what is included in the theme). This may
3197 * either be an attribute resource, whose value will be retrieved
3198 * from the current theme, or an explicit style resource.
3199 * @see #View(Context, AttributeSet)
3200 */
3201 public View(Context context, AttributeSet attrs, int defStyle) {
3202 this(context);
3203
3204 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3205 defStyle, 0);
3206
3207 Drawable background = null;
3208
3209 int leftPadding = -1;
3210 int topPadding = -1;
3211 int rightPadding = -1;
3212 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003213 int startPadding = UNDEFINED_PADDING;
3214 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215
3216 int padding = -1;
3217
3218 int viewFlagValues = 0;
3219 int viewFlagMasks = 0;
3220
3221 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 int x = 0;
3224 int y = 0;
3225
Chet Haase73066682010-11-29 15:55:32 -08003226 float tx = 0;
3227 float ty = 0;
3228 float rotation = 0;
3229 float rotationX = 0;
3230 float rotationY = 0;
3231 float sx = 1f;
3232 float sy = 1f;
3233 boolean transformSet = false;
3234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003236 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003237 boolean initializeScrollbars = false;
3238
3239 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 final int N = a.getIndexCount();
3242 for (int i = 0; i < N; i++) {
3243 int attr = a.getIndex(i);
3244 switch (attr) {
3245 case com.android.internal.R.styleable.View_background:
3246 background = a.getDrawable(attr);
3247 break;
3248 case com.android.internal.R.styleable.View_padding:
3249 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003250 mUserPaddingLeftInitial = padding;
3251 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 break;
3253 case com.android.internal.R.styleable.View_paddingLeft:
3254 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003255 mUserPaddingLeftInitial = leftPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 break;
3257 case com.android.internal.R.styleable.View_paddingTop:
3258 topPadding = a.getDimensionPixelSize(attr, -1);
3259 break;
3260 case com.android.internal.R.styleable.View_paddingRight:
3261 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003262 mUserPaddingRightInitial = rightPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 break;
3264 case com.android.internal.R.styleable.View_paddingBottom:
3265 bottomPadding = a.getDimensionPixelSize(attr, -1);
3266 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003267 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003268 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003269 break;
3270 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003271 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003272 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 case com.android.internal.R.styleable.View_scrollX:
3274 x = a.getDimensionPixelOffset(attr, 0);
3275 break;
3276 case com.android.internal.R.styleable.View_scrollY:
3277 y = a.getDimensionPixelOffset(attr, 0);
3278 break;
Chet Haase73066682010-11-29 15:55:32 -08003279 case com.android.internal.R.styleable.View_alpha:
3280 setAlpha(a.getFloat(attr, 1f));
3281 break;
3282 case com.android.internal.R.styleable.View_transformPivotX:
3283 setPivotX(a.getDimensionPixelOffset(attr, 0));
3284 break;
3285 case com.android.internal.R.styleable.View_transformPivotY:
3286 setPivotY(a.getDimensionPixelOffset(attr, 0));
3287 break;
3288 case com.android.internal.R.styleable.View_translationX:
3289 tx = a.getDimensionPixelOffset(attr, 0);
3290 transformSet = true;
3291 break;
3292 case com.android.internal.R.styleable.View_translationY:
3293 ty = a.getDimensionPixelOffset(attr, 0);
3294 transformSet = true;
3295 break;
3296 case com.android.internal.R.styleable.View_rotation:
3297 rotation = a.getFloat(attr, 0);
3298 transformSet = true;
3299 break;
3300 case com.android.internal.R.styleable.View_rotationX:
3301 rotationX = a.getFloat(attr, 0);
3302 transformSet = true;
3303 break;
3304 case com.android.internal.R.styleable.View_rotationY:
3305 rotationY = a.getFloat(attr, 0);
3306 transformSet = true;
3307 break;
3308 case com.android.internal.R.styleable.View_scaleX:
3309 sx = a.getFloat(attr, 1f);
3310 transformSet = true;
3311 break;
3312 case com.android.internal.R.styleable.View_scaleY:
3313 sy = a.getFloat(attr, 1f);
3314 transformSet = true;
3315 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 case com.android.internal.R.styleable.View_id:
3317 mID = a.getResourceId(attr, NO_ID);
3318 break;
3319 case com.android.internal.R.styleable.View_tag:
3320 mTag = a.getText(attr);
3321 break;
3322 case com.android.internal.R.styleable.View_fitsSystemWindows:
3323 if (a.getBoolean(attr, false)) {
3324 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3325 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3326 }
3327 break;
3328 case com.android.internal.R.styleable.View_focusable:
3329 if (a.getBoolean(attr, false)) {
3330 viewFlagValues |= FOCUSABLE;
3331 viewFlagMasks |= FOCUSABLE_MASK;
3332 }
3333 break;
3334 case com.android.internal.R.styleable.View_focusableInTouchMode:
3335 if (a.getBoolean(attr, false)) {
3336 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3337 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3338 }
3339 break;
3340 case com.android.internal.R.styleable.View_clickable:
3341 if (a.getBoolean(attr, false)) {
3342 viewFlagValues |= CLICKABLE;
3343 viewFlagMasks |= CLICKABLE;
3344 }
3345 break;
3346 case com.android.internal.R.styleable.View_longClickable:
3347 if (a.getBoolean(attr, false)) {
3348 viewFlagValues |= LONG_CLICKABLE;
3349 viewFlagMasks |= LONG_CLICKABLE;
3350 }
3351 break;
3352 case com.android.internal.R.styleable.View_saveEnabled:
3353 if (!a.getBoolean(attr, true)) {
3354 viewFlagValues |= SAVE_DISABLED;
3355 viewFlagMasks |= SAVE_DISABLED_MASK;
3356 }
3357 break;
3358 case com.android.internal.R.styleable.View_duplicateParentState:
3359 if (a.getBoolean(attr, false)) {
3360 viewFlagValues |= DUPLICATE_PARENT_STATE;
3361 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3362 }
3363 break;
3364 case com.android.internal.R.styleable.View_visibility:
3365 final int visibility = a.getInt(attr, 0);
3366 if (visibility != 0) {
3367 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3368 viewFlagMasks |= VISIBILITY_MASK;
3369 }
3370 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003371 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003372 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003373 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003374 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003375 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003376 final int value = (layoutDirection != -1) ?
3377 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003378 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003379 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 case com.android.internal.R.styleable.View_drawingCacheQuality:
3381 final int cacheQuality = a.getInt(attr, 0);
3382 if (cacheQuality != 0) {
3383 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3384 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3385 }
3386 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003387 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003388 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003389 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003390 case com.android.internal.R.styleable.View_labelFor:
3391 setLabelFor(a.getResourceId(attr, NO_ID));
3392 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3394 if (!a.getBoolean(attr, true)) {
3395 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3396 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3397 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003398 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3400 if (!a.getBoolean(attr, true)) {
3401 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3402 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3403 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003404 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 case R.styleable.View_scrollbars:
3406 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3407 if (scrollbars != SCROLLBARS_NONE) {
3408 viewFlagValues |= scrollbars;
3409 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003410 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
3412 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003413 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003415 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003416 // Ignore the attribute starting with ICS
3417 break;
3418 }
3419 // With builds < ICS, fall through and apply fading edges
3420 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003421 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3422 if (fadingEdge != FADING_EDGE_NONE) {
3423 viewFlagValues |= fadingEdge;
3424 viewFlagMasks |= FADING_EDGE_MASK;
3425 initializeFadingEdge(a);
3426 }
3427 break;
3428 case R.styleable.View_scrollbarStyle:
3429 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3430 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3431 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3432 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3433 }
3434 break;
3435 case R.styleable.View_isScrollContainer:
3436 setScrollContainer = true;
3437 if (a.getBoolean(attr, false)) {
3438 setScrollContainer(true);
3439 }
3440 break;
3441 case com.android.internal.R.styleable.View_keepScreenOn:
3442 if (a.getBoolean(attr, false)) {
3443 viewFlagValues |= KEEP_SCREEN_ON;
3444 viewFlagMasks |= KEEP_SCREEN_ON;
3445 }
3446 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003447 case R.styleable.View_filterTouchesWhenObscured:
3448 if (a.getBoolean(attr, false)) {
3449 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3450 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3451 }
3452 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 case R.styleable.View_nextFocusLeft:
3454 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3455 break;
3456 case R.styleable.View_nextFocusRight:
3457 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3458 break;
3459 case R.styleable.View_nextFocusUp:
3460 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3461 break;
3462 case R.styleable.View_nextFocusDown:
3463 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3464 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003465 case R.styleable.View_nextFocusForward:
3466 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3467 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 case R.styleable.View_minWidth:
3469 mMinWidth = a.getDimensionPixelSize(attr, 0);
3470 break;
3471 case R.styleable.View_minHeight:
3472 mMinHeight = a.getDimensionPixelSize(attr, 0);
3473 break;
Romain Guy9a817362009-05-01 10:57:14 -07003474 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003475 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003476 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003477 + "be used within a restricted context");
3478 }
3479
Romain Guy9a817362009-05-01 10:57:14 -07003480 final String handlerName = a.getString(attr);
3481 if (handlerName != null) {
3482 setOnClickListener(new OnClickListener() {
3483 private Method mHandler;
3484
3485 public void onClick(View v) {
3486 if (mHandler == null) {
3487 try {
3488 mHandler = getContext().getClass().getMethod(handlerName,
3489 View.class);
3490 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003491 int id = getId();
3492 String idText = id == NO_ID ? "" : " with id '"
3493 + getContext().getResources().getResourceEntryName(
3494 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003495 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003496 handlerName + "(View) in the activity "
3497 + getContext().getClass() + " for onClick handler"
3498 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003499 }
3500 }
3501
3502 try {
3503 mHandler.invoke(getContext(), View.this);
3504 } catch (IllegalAccessException e) {
3505 throw new IllegalStateException("Could not execute non "
3506 + "public method of the activity", e);
3507 } catch (InvocationTargetException e) {
3508 throw new IllegalStateException("Could not execute "
3509 + "method of the activity", e);
3510 }
3511 }
3512 });
3513 }
3514 break;
Adam Powell637d3372010-08-25 14:37:03 -07003515 case R.styleable.View_overScrollMode:
3516 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3517 break;
Adam Powell20232d02010-12-08 21:08:53 -08003518 case R.styleable.View_verticalScrollbarPosition:
3519 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3520 break;
Romain Guy171c5922011-01-06 10:04:23 -08003521 case R.styleable.View_layerType:
3522 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3523 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003524 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003525 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003526 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003527 // Set the text direction flags depending on the value of the attribute
3528 final int textDirection = a.getInt(attr, -1);
3529 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003530 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003531 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003532 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003533 case R.styleable.View_textAlignment:
3534 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003535 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003536 // Set the text alignment flag depending on the value of the attribute
3537 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003538 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003539 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003540 case R.styleable.View_importantForAccessibility:
3541 setImportantForAccessibility(a.getInt(attr,
3542 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003543 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 }
3545 }
3546
Adam Powell637d3372010-08-25 14:37:03 -07003547 setOverScrollMode(overScrollMode);
3548
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003549 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3550 // the resolved layout direction). Those cached values will be used later during padding
3551 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003552 mUserPaddingStart = startPadding;
3553 mUserPaddingEnd = endPadding;
3554
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003555 if (background != null) {
3556 setBackground(background);
3557 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 if (padding >= 0) {
3560 leftPadding = padding;
3561 topPadding = padding;
3562 rightPadding = padding;
3563 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003564 mUserPaddingLeftInitial = padding;
3565 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 }
3567
3568 // If the user specified the padding (either with android:padding or
3569 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3570 // use the default padding or the padding from the background drawable
3571 // (stored at this point in mPadding*)
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003572 mUserPaddingLeftInitial = leftPadding >= 0 ? leftPadding : mPaddingLeft;
3573 mUserPaddingRightInitial = rightPadding >= 0 ? rightPadding : mPaddingRight;
3574 internalSetPadding(mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003576 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3578
3579 if (viewFlagMasks != 0) {
3580 setFlags(viewFlagValues, viewFlagMasks);
3581 }
3582
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003583 if (initializeScrollbars) {
3584 initializeScrollbars(a);
3585 }
3586
3587 a.recycle();
3588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 // Needs to be called after mViewFlags is set
3590 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3591 recomputePadding();
3592 }
3593
3594 if (x != 0 || y != 0) {
3595 scrollTo(x, y);
3596 }
3597
Chet Haase73066682010-11-29 15:55:32 -08003598 if (transformSet) {
3599 setTranslationX(tx);
3600 setTranslationY(ty);
3601 setRotation(rotation);
3602 setRotationX(rotationX);
3603 setRotationY(rotationY);
3604 setScaleX(sx);
3605 setScaleY(sy);
3606 }
3607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3609 setScrollContainer(true);
3610 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003611
3612 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614
3615 /**
3616 * Non-public constructor for use in testing
3617 */
3618 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003619 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 }
3621
Dianne Hackborn4702a852012-08-17 15:18:29 -07003622 public String toString() {
3623 StringBuilder out = new StringBuilder(128);
3624 out.append(getClass().getName());
3625 out.append('{');
3626 out.append(Integer.toHexString(System.identityHashCode(this)));
3627 out.append(' ');
3628 switch (mViewFlags&VISIBILITY_MASK) {
3629 case VISIBLE: out.append('V'); break;
3630 case INVISIBLE: out.append('I'); break;
3631 case GONE: out.append('G'); break;
3632 default: out.append('.'); break;
3633 }
3634 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3635 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3636 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3637 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3638 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3639 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3640 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3641 out.append(' ');
3642 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3643 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3644 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3645 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3646 out.append('p');
3647 } else {
3648 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3649 }
3650 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3651 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3652 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3653 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3654 out.append(' ');
3655 out.append(mLeft);
3656 out.append(',');
3657 out.append(mTop);
3658 out.append('-');
3659 out.append(mRight);
3660 out.append(',');
3661 out.append(mBottom);
3662 final int id = getId();
3663 if (id != NO_ID) {
3664 out.append(" #");
3665 out.append(Integer.toHexString(id));
3666 final Resources r = mResources;
3667 if (id != 0 && r != null) {
3668 try {
3669 String pkgname;
3670 switch (id&0xff000000) {
3671 case 0x7f000000:
3672 pkgname="app";
3673 break;
3674 case 0x01000000:
3675 pkgname="android";
3676 break;
3677 default:
3678 pkgname = r.getResourcePackageName(id);
3679 break;
3680 }
3681 String typename = r.getResourceTypeName(id);
3682 String entryname = r.getResourceEntryName(id);
3683 out.append(" ");
3684 out.append(pkgname);
3685 out.append(":");
3686 out.append(typename);
3687 out.append("/");
3688 out.append(entryname);
3689 } catch (Resources.NotFoundException e) {
3690 }
3691 }
3692 }
3693 out.append("}");
3694 return out.toString();
3695 }
3696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 /**
3698 * <p>
3699 * Initializes the fading edges from a given set of styled attributes. This
3700 * method should be called by subclasses that need fading edges and when an
3701 * instance of these subclasses is created programmatically rather than
3702 * being inflated from XML. This method is automatically called when the XML
3703 * is inflated.
3704 * </p>
3705 *
3706 * @param a the styled attributes set to initialize the fading edges from
3707 */
3708 protected void initializeFadingEdge(TypedArray a) {
3709 initScrollCache();
3710
3711 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3712 R.styleable.View_fadingEdgeLength,
3713 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3714 }
3715
3716 /**
3717 * Returns the size of the vertical faded edges used to indicate that more
3718 * content in this view is visible.
3719 *
3720 * @return The size in pixels of the vertical faded edge or 0 if vertical
3721 * faded edges are not enabled for this view.
3722 * @attr ref android.R.styleable#View_fadingEdgeLength
3723 */
3724 public int getVerticalFadingEdgeLength() {
3725 if (isVerticalFadingEdgeEnabled()) {
3726 ScrollabilityCache cache = mScrollCache;
3727 if (cache != null) {
3728 return cache.fadingEdgeLength;
3729 }
3730 }
3731 return 0;
3732 }
3733
3734 /**
3735 * Set the size of the faded edge used to indicate that more content in this
3736 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003737 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3738 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3739 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 *
3741 * @param length The size in pixels of the faded edge used to indicate that more
3742 * content in this view is visible.
3743 */
3744 public void setFadingEdgeLength(int length) {
3745 initScrollCache();
3746 mScrollCache.fadingEdgeLength = length;
3747 }
3748
3749 /**
3750 * Returns the size of the horizontal faded edges used to indicate that more
3751 * content in this view is visible.
3752 *
3753 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3754 * faded edges are not enabled for this view.
3755 * @attr ref android.R.styleable#View_fadingEdgeLength
3756 */
3757 public int getHorizontalFadingEdgeLength() {
3758 if (isHorizontalFadingEdgeEnabled()) {
3759 ScrollabilityCache cache = mScrollCache;
3760 if (cache != null) {
3761 return cache.fadingEdgeLength;
3762 }
3763 }
3764 return 0;
3765 }
3766
3767 /**
3768 * Returns the width of the vertical scrollbar.
3769 *
3770 * @return The width in pixels of the vertical scrollbar or 0 if there
3771 * is no vertical scrollbar.
3772 */
3773 public int getVerticalScrollbarWidth() {
3774 ScrollabilityCache cache = mScrollCache;
3775 if (cache != null) {
3776 ScrollBarDrawable scrollBar = cache.scrollBar;
3777 if (scrollBar != null) {
3778 int size = scrollBar.getSize(true);
3779 if (size <= 0) {
3780 size = cache.scrollBarSize;
3781 }
3782 return size;
3783 }
3784 return 0;
3785 }
3786 return 0;
3787 }
3788
3789 /**
3790 * Returns the height of the horizontal scrollbar.
3791 *
3792 * @return The height in pixels of the horizontal scrollbar or 0 if
3793 * there is no horizontal scrollbar.
3794 */
3795 protected int getHorizontalScrollbarHeight() {
3796 ScrollabilityCache cache = mScrollCache;
3797 if (cache != null) {
3798 ScrollBarDrawable scrollBar = cache.scrollBar;
3799 if (scrollBar != null) {
3800 int size = scrollBar.getSize(false);
3801 if (size <= 0) {
3802 size = cache.scrollBarSize;
3803 }
3804 return size;
3805 }
3806 return 0;
3807 }
3808 return 0;
3809 }
3810
3811 /**
3812 * <p>
3813 * Initializes the scrollbars from a given set of styled attributes. This
3814 * method should be called by subclasses that need scrollbars and when an
3815 * instance of these subclasses is created programmatically rather than
3816 * being inflated from XML. This method is automatically called when the XML
3817 * is inflated.
3818 * </p>
3819 *
3820 * @param a the styled attributes set to initialize the scrollbars from
3821 */
3822 protected void initializeScrollbars(TypedArray a) {
3823 initScrollCache();
3824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003826
Mike Cleronf116bf82009-09-27 19:14:12 -07003827 if (scrollabilityCache.scrollBar == null) {
3828 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3829 }
Joe Malin32736f02011-01-19 16:14:20 -08003830
Romain Guy8bda2482010-03-02 11:42:11 -08003831 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832
Mike Cleronf116bf82009-09-27 19:14:12 -07003833 if (!fadeScrollbars) {
3834 scrollabilityCache.state = ScrollabilityCache.ON;
3835 }
3836 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003837
3838
Mike Cleronf116bf82009-09-27 19:14:12 -07003839 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3840 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3841 .getScrollBarFadeDuration());
3842 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3843 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3844 ViewConfiguration.getScrollDefaultDelay());
3845
Joe Malin32736f02011-01-19 16:14:20 -08003846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3848 com.android.internal.R.styleable.View_scrollbarSize,
3849 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3850
3851 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3852 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3853
3854 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3855 if (thumb != null) {
3856 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3857 }
3858
3859 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3860 false);
3861 if (alwaysDraw) {
3862 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3863 }
3864
3865 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3866 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3867
3868 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3869 if (thumb != null) {
3870 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3871 }
3872
3873 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3874 false);
3875 if (alwaysDraw) {
3876 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3877 }
3878
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003879 // Apply layout direction to the new Drawables if needed
3880 final int layoutDirection = getResolvedLayoutDirection();
3881 if (track != null) {
3882 track.setLayoutDirection(layoutDirection);
3883 }
3884 if (thumb != null) {
3885 thumb.setLayoutDirection(layoutDirection);
3886 }
3887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003889 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 }
3891
3892 /**
3893 * <p>
3894 * Initalizes the scrollability cache if necessary.
3895 * </p>
3896 */
3897 private void initScrollCache() {
3898 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003899 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900 }
3901 }
3902
Philip Milne6c8ea062012-04-03 17:38:43 -07003903 private ScrollabilityCache getScrollCache() {
3904 initScrollCache();
3905 return mScrollCache;
3906 }
3907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 /**
Adam Powell20232d02010-12-08 21:08:53 -08003909 * Set the position of the vertical scroll bar. Should be one of
3910 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3911 * {@link #SCROLLBAR_POSITION_RIGHT}.
3912 *
3913 * @param position Where the vertical scroll bar should be positioned.
3914 */
3915 public void setVerticalScrollbarPosition(int position) {
3916 if (mVerticalScrollbarPosition != position) {
3917 mVerticalScrollbarPosition = position;
3918 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003919 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003920 }
3921 }
3922
3923 /**
3924 * @return The position where the vertical scroll bar will show, if applicable.
3925 * @see #setVerticalScrollbarPosition(int)
3926 */
3927 public int getVerticalScrollbarPosition() {
3928 return mVerticalScrollbarPosition;
3929 }
3930
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003931 ListenerInfo getListenerInfo() {
3932 if (mListenerInfo != null) {
3933 return mListenerInfo;
3934 }
3935 mListenerInfo = new ListenerInfo();
3936 return mListenerInfo;
3937 }
3938
Adam Powell20232d02010-12-08 21:08:53 -08003939 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 * Register a callback to be invoked when focus of this view changed.
3941 *
3942 * @param l The callback that will run.
3943 */
3944 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003945 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 }
3947
3948 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003949 * Add a listener that will be called when the bounds of the view change due to
3950 * layout processing.
3951 *
3952 * @param listener The listener that will be called when layout bounds change.
3953 */
3954 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003955 ListenerInfo li = getListenerInfo();
3956 if (li.mOnLayoutChangeListeners == null) {
3957 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003958 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003959 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3960 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003961 }
Chet Haase21cd1382010-09-01 17:42:29 -07003962 }
3963
3964 /**
3965 * Remove a listener for layout changes.
3966 *
3967 * @param listener The listener for layout bounds change.
3968 */
3969 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003970 ListenerInfo li = mListenerInfo;
3971 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003972 return;
3973 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003974 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003975 }
3976
3977 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003978 * Add a listener for attach state changes.
3979 *
3980 * This listener will be called whenever this view is attached or detached
3981 * from a window. Remove the listener using
3982 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3983 *
3984 * @param listener Listener to attach
3985 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3986 */
3987 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003988 ListenerInfo li = getListenerInfo();
3989 if (li.mOnAttachStateChangeListeners == null) {
3990 li.mOnAttachStateChangeListeners
3991 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003992 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003993 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003994 }
3995
3996 /**
3997 * Remove a listener for attach state changes. The listener will receive no further
3998 * notification of window attach/detach events.
3999 *
4000 * @param listener Listener to remove
4001 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4002 */
4003 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004004 ListenerInfo li = mListenerInfo;
4005 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004006 return;
4007 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004008 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004009 }
4010
4011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 * Returns the focus-change callback registered for this view.
4013 *
4014 * @return The callback, or null if one is not registered.
4015 */
4016 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004017 ListenerInfo li = mListenerInfo;
4018 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 }
4020
4021 /**
4022 * Register a callback to be invoked when this view is clicked. If this view is not
4023 * clickable, it becomes clickable.
4024 *
4025 * @param l The callback that will run
4026 *
4027 * @see #setClickable(boolean)
4028 */
4029 public void setOnClickListener(OnClickListener l) {
4030 if (!isClickable()) {
4031 setClickable(true);
4032 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004033 getListenerInfo().mOnClickListener = l;
4034 }
4035
4036 /**
4037 * Return whether this view has an attached OnClickListener. Returns
4038 * true if there is a listener, false if there is none.
4039 */
4040 public boolean hasOnClickListeners() {
4041 ListenerInfo li = mListenerInfo;
4042 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 }
4044
4045 /**
4046 * Register a callback to be invoked when this view is clicked and held. If this view is not
4047 * long clickable, it becomes long clickable.
4048 *
4049 * @param l The callback that will run
4050 *
4051 * @see #setLongClickable(boolean)
4052 */
4053 public void setOnLongClickListener(OnLongClickListener l) {
4054 if (!isLongClickable()) {
4055 setLongClickable(true);
4056 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004057 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 }
4059
4060 /**
4061 * Register a callback to be invoked when the context menu for this view is
4062 * being built. If this view is not long clickable, it becomes long clickable.
4063 *
4064 * @param l The callback that will run
4065 *
4066 */
4067 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4068 if (!isLongClickable()) {
4069 setLongClickable(true);
4070 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004071 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 }
4073
4074 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004075 * Call this view's OnClickListener, if it is defined. Performs all normal
4076 * actions associated with clicking: reporting accessibility event, playing
4077 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 *
4079 * @return True there was an assigned OnClickListener that was called, false
4080 * otherwise is returned.
4081 */
4082 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004083 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4084
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004085 ListenerInfo li = mListenerInfo;
4086 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004088 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 return true;
4090 }
4091
4092 return false;
4093 }
4094
4095 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004096 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4097 * this only calls the listener, and does not do any associated clicking
4098 * actions like reporting an accessibility event.
4099 *
4100 * @return True there was an assigned OnClickListener that was called, false
4101 * otherwise is returned.
4102 */
4103 public boolean callOnClick() {
4104 ListenerInfo li = mListenerInfo;
4105 if (li != null && li.mOnClickListener != null) {
4106 li.mOnClickListener.onClick(this);
4107 return true;
4108 }
4109 return false;
4110 }
4111
4112 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004113 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4114 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004116 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 */
4118 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004119 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004122 ListenerInfo li = mListenerInfo;
4123 if (li != null && li.mOnLongClickListener != null) {
4124 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 }
4126 if (!handled) {
4127 handled = showContextMenu();
4128 }
4129 if (handled) {
4130 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4131 }
4132 return handled;
4133 }
4134
4135 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004136 * Performs button-related actions during a touch down event.
4137 *
4138 * @param event The event.
4139 * @return True if the down was consumed.
4140 *
4141 * @hide
4142 */
4143 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4144 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4145 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4146 return true;
4147 }
4148 }
4149 return false;
4150 }
4151
4152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 * Bring up the context menu for this view.
4154 *
4155 * @return Whether a context menu was displayed.
4156 */
4157 public boolean showContextMenu() {
4158 return getParent().showContextMenuForChild(this);
4159 }
4160
4161 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004162 * Bring up the context menu for this view, referring to the item under the specified point.
4163 *
4164 * @param x The referenced x coordinate.
4165 * @param y The referenced y coordinate.
4166 * @param metaState The keyboard modifiers that were pressed.
4167 * @return Whether a context menu was displayed.
4168 *
4169 * @hide
4170 */
4171 public boolean showContextMenu(float x, float y, int metaState) {
4172 return showContextMenu();
4173 }
4174
4175 /**
Adam Powell6e346362010-07-23 10:18:23 -07004176 * Start an action mode.
4177 *
4178 * @param callback Callback that will control the lifecycle of the action mode
4179 * @return The new action mode if it is started, null otherwise
4180 *
4181 * @see ActionMode
4182 */
4183 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004184 ViewParent parent = getParent();
4185 if (parent == null) return null;
4186 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004187 }
4188
4189 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004190 * Register a callback to be invoked when a hardware key is pressed in this view.
4191 * Key presses in software input methods will generally not trigger the methods of
4192 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 * @param l the key listener to attach to this view
4194 */
4195 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004196 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198
4199 /**
4200 * Register a callback to be invoked when a touch event is sent to this view.
4201 * @param l the touch listener to attach to this view
4202 */
4203 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004204 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 }
4206
4207 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004208 * Register a callback to be invoked when a generic motion event is sent to this view.
4209 * @param l the generic motion listener to attach to this view
4210 */
4211 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004212 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004213 }
4214
4215 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004216 * Register a callback to be invoked when a hover event is sent to this view.
4217 * @param l the hover listener to attach to this view
4218 */
4219 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004220 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004221 }
4222
4223 /**
Joe Malin32736f02011-01-19 16:14:20 -08004224 * Register a drag event listener callback object for this View. The parameter is
4225 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4226 * View, the system calls the
4227 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4228 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004229 */
4230 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004231 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004232 }
4233
4234 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004235 * Give this view focus. This will cause
4236 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 *
4238 * Note: this does not check whether this {@link View} should get focus, it just
4239 * gives it focus no matter what. It should only be called internally by framework
4240 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4241 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004242 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4243 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 * focus moved when requestFocus() is called. It may not always
4245 * apply, in which case use the default View.FOCUS_DOWN.
4246 * @param previouslyFocusedRect The rectangle of the view that had focus
4247 * prior in this View's coordinate system.
4248 */
4249 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4250 if (DBG) {
4251 System.out.println(this + " requestFocus()");
4252 }
4253
Dianne Hackborn4702a852012-08-17 15:18:29 -07004254 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4255 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256
4257 if (mParent != null) {
4258 mParent.requestChildFocus(this, this);
4259 }
4260
4261 onFocusChanged(true, direction, previouslyFocusedRect);
4262 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004263
4264 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4265 notifyAccessibilityStateChanged();
4266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 }
4268 }
4269
4270 /**
4271 * Request that a rectangle of this view be visible on the screen,
4272 * scrolling if necessary just enough.
4273 *
4274 * <p>A View should call this if it maintains some notion of which part
4275 * of its content is interesting. For example, a text editing view
4276 * should call this when its cursor moves.
4277 *
4278 * @param rectangle The rectangle.
4279 * @return Whether any parent scrolled.
4280 */
4281 public boolean requestRectangleOnScreen(Rect rectangle) {
4282 return requestRectangleOnScreen(rectangle, false);
4283 }
4284
4285 /**
4286 * Request that a rectangle of this view be visible on the screen,
4287 * scrolling if necessary just enough.
4288 *
4289 * <p>A View should call this if it maintains some notion of which part
4290 * of its content is interesting. For example, a text editing view
4291 * should call this when its cursor moves.
4292 *
4293 * <p>When <code>immediate</code> is set to true, scrolling will not be
4294 * animated.
4295 *
4296 * @param rectangle The rectangle.
4297 * @param immediate True to forbid animated scrolling, false otherwise
4298 * @return Whether any parent scrolled.
4299 */
4300 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004301 if (mAttachInfo == null) {
4302 return false;
4303 }
4304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004306
4307 RectF position = mAttachInfo.mTmpTransformRect;
4308 position.set(rectangle);
4309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 ViewParent parent = mParent;
4311 boolean scrolled = false;
4312 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004313 rectangle.set((int) position.left, (int) position.top,
4314 (int) position.right, (int) position.bottom);
4315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 scrolled |= parent.requestChildRectangleOnScreen(child,
4317 rectangle, immediate);
4318
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004319 if (!child.hasIdentityMatrix()) {
4320 child.getMatrix().mapRect(position);
4321 }
4322
4323 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324
4325 if (!(parent instanceof View)) {
4326 break;
4327 }
Romain Guy8506ab42009-06-11 17:35:47 -07004328
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004329 View parentView = (View) parent;
4330
4331 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4332
4333 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 parent = child.getParent();
4335 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 return scrolled;
4338 }
4339
4340 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004341 * Called when this view wants to give up focus. If focus is cleared
4342 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4343 * <p>
4344 * <strong>Note:</strong> When a View clears focus the framework is trying
4345 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004346 * View is the first from the top that can take focus, then all callbacks
4347 * related to clearing focus will be invoked after wich the framework will
4348 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004349 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 */
4351 public void clearFocus() {
4352 if (DBG) {
4353 System.out.println(this + " clearFocus()");
4354 }
4355
Dianne Hackborn4702a852012-08-17 15:18:29 -07004356 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4357 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358
4359 if (mParent != null) {
4360 mParent.clearChildFocus(this);
4361 }
4362
4363 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004366
4367 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004368
4369 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4370 notifyAccessibilityStateChanged();
4371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 }
4373 }
4374
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004375 void ensureInputFocusOnFirstFocusable() {
4376 View root = getRootView();
4377 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004378 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 }
4380 }
4381
4382 /**
4383 * Called internally by the view system when a new view is getting focus.
4384 * This is what clears the old focus.
4385 */
4386 void unFocus() {
4387 if (DBG) {
4388 System.out.println(this + " unFocus()");
4389 }
4390
Dianne Hackborn4702a852012-08-17 15:18:29 -07004391 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4392 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393
4394 onFocusChanged(false, 0, null);
4395 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004396
4397 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4398 notifyAccessibilityStateChanged();
4399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 }
4401 }
4402
4403 /**
4404 * Returns true if this view has focus iteself, or is the ancestor of the
4405 * view that has focus.
4406 *
4407 * @return True if this view has or contains focus, false otherwise.
4408 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004409 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004411 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 }
4413
4414 /**
4415 * Returns true if this view is focusable or if it contains a reachable View
4416 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4417 * is a View whose parents do not block descendants focus.
4418 *
4419 * Only {@link #VISIBLE} views are considered focusable.
4420 *
4421 * @return True if the view is focusable or if the view contains a focusable
4422 * View, false otherwise.
4423 *
4424 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4425 */
4426 public boolean hasFocusable() {
4427 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4428 }
4429
4430 /**
4431 * Called by the view system when the focus state of this view changes.
4432 * When the focus change event is caused by directional navigation, direction
4433 * and previouslyFocusedRect provide insight into where the focus is coming from.
4434 * When overriding, be sure to call up through to the super class so that
4435 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004436 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 * @param gainFocus True if the View has focus; false otherwise.
4438 * @param direction The direction focus has moved when requestFocus()
4439 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004440 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4441 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4442 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4444 * system, of the previously focused view. If applicable, this will be
4445 * passed in as finer grained information about where the focus is coming
4446 * from (in addition to direction). Will be <code>null</code> otherwise.
4447 */
4448 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004449 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004450 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4451 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004452 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004453 }
4454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 InputMethodManager imm = InputMethodManager.peekInstance();
4456 if (!gainFocus) {
4457 if (isPressed()) {
4458 setPressed(false);
4459 }
4460 if (imm != null && mAttachInfo != null
4461 && mAttachInfo.mHasWindowFocus) {
4462 imm.focusOut(this);
4463 }
Romain Guya2431d02009-04-30 16:30:00 -07004464 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 } else if (imm != null && mAttachInfo != null
4466 && mAttachInfo.mHasWindowFocus) {
4467 imm.focusIn(this);
4468 }
Romain Guy8506ab42009-06-11 17:35:47 -07004469
Romain Guy0fd89bf2011-01-26 15:41:30 -08004470 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004471 ListenerInfo li = mListenerInfo;
4472 if (li != null && li.mOnFocusChangeListener != null) {
4473 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 }
Joe Malin32736f02011-01-19 16:14:20 -08004475
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004476 if (mAttachInfo != null) {
4477 mAttachInfo.mKeyDispatchState.reset(this);
4478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 }
4480
4481 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004482 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004483 * not enabled this method has no effect. The default implementation calls
4484 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4485 * to populate information about the event source (this View), then calls
4486 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4487 * populate the text content of the event source including its descendants,
4488 * and last calls
4489 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4490 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004491 * <p>
4492 * If an {@link AccessibilityDelegate} has been specified via calling
4493 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4494 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4495 * responsible for handling this call.
4496 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004497 *
Scott Mainb303d832011-10-12 16:45:18 -07004498 * @param eventType The type of the event to send, as defined by several types from
4499 * {@link android.view.accessibility.AccessibilityEvent}, such as
4500 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4501 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004502 *
4503 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4504 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4505 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004506 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004507 */
4508 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004509 if (mAccessibilityDelegate != null) {
4510 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4511 } else {
4512 sendAccessibilityEventInternal(eventType);
4513 }
4514 }
4515
4516 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004517 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4518 * {@link AccessibilityEvent} to make an announcement which is related to some
4519 * sort of a context change for which none of the events representing UI transitions
4520 * is a good fit. For example, announcing a new page in a book. If accessibility
4521 * is not enabled this method does nothing.
4522 *
4523 * @param text The announcement text.
4524 */
4525 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004526 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004527 AccessibilityEvent event = AccessibilityEvent.obtain(
4528 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004529 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004530 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004531 event.setContentDescription(null);
4532 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004533 }
4534 }
4535
4536 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004537 * @see #sendAccessibilityEvent(int)
4538 *
4539 * Note: Called from the default {@link AccessibilityDelegate}.
4540 */
4541 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004542 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4543 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4544 }
4545 }
4546
4547 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004548 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4549 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004550 * perform a check whether accessibility is enabled.
4551 * <p>
4552 * If an {@link AccessibilityDelegate} has been specified via calling
4553 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4554 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4555 * is responsible for handling this call.
4556 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004557 *
4558 * @param event The event to send.
4559 *
4560 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004561 */
4562 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004563 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004564 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004565 } else {
4566 sendAccessibilityEventUncheckedInternal(event);
4567 }
4568 }
4569
4570 /**
4571 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4572 *
4573 * Note: Called from the default {@link AccessibilityDelegate}.
4574 */
4575 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004576 if (!isShown()) {
4577 return;
4578 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004579 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004580 // Only a subset of accessibility events populates text content.
4581 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4582 dispatchPopulateAccessibilityEvent(event);
4583 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004584 // In the beginning we called #isShown(), so we know that getParent() is not null.
4585 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004586 }
4587
4588 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004589 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4590 * to its children for adding their text content to the event. Note that the
4591 * event text is populated in a separate dispatch path since we add to the
4592 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004593 * A typical implementation will call
4594 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4595 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4596 * on each child. Override this method if custom population of the event text
4597 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004598 * <p>
4599 * If an {@link AccessibilityDelegate} has been specified via calling
4600 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4601 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4602 * is responsible for handling this call.
4603 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004604 * <p>
4605 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4606 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4607 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004608 *
4609 * @param event The event.
4610 *
4611 * @return True if the event population was completed.
4612 */
4613 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004614 if (mAccessibilityDelegate != null) {
4615 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4616 } else {
4617 return dispatchPopulateAccessibilityEventInternal(event);
4618 }
4619 }
4620
4621 /**
4622 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4623 *
4624 * Note: Called from the default {@link AccessibilityDelegate}.
4625 */
4626 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004627 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004628 return false;
4629 }
4630
4631 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004632 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004633 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004634 * text content. While this method is free to modify event
4635 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004636 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4637 * <p>
4638 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004639 * to the text added by the super implementation:
4640 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004641 * super.onPopulateAccessibilityEvent(event);
4642 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4643 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4644 * mCurrentDate.getTimeInMillis(), flags);
4645 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004646 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004647 * <p>
4648 * If an {@link AccessibilityDelegate} has been specified via calling
4649 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4650 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4651 * is responsible for handling this call.
4652 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004653 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4654 * information to the event, in case the default implementation has basic information to add.
4655 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004656 *
4657 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004658 *
4659 * @see #sendAccessibilityEvent(int)
4660 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004661 */
4662 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004663 if (mAccessibilityDelegate != null) {
4664 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4665 } else {
4666 onPopulateAccessibilityEventInternal(event);
4667 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004668 }
4669
4670 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004671 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4672 *
4673 * Note: Called from the default {@link AccessibilityDelegate}.
4674 */
4675 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4676
4677 }
4678
4679 /**
4680 * Initializes an {@link AccessibilityEvent} with information about
4681 * this View which is the event source. In other words, the source of
4682 * an accessibility event is the view whose state change triggered firing
4683 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004684 * <p>
4685 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004686 * to properties set by the super implementation:
4687 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4688 * super.onInitializeAccessibilityEvent(event);
4689 * event.setPassword(true);
4690 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004691 * <p>
4692 * If an {@link AccessibilityDelegate} has been specified via calling
4693 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4694 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4695 * is responsible for handling this call.
4696 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004697 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4698 * information to the event, in case the default implementation has basic information to add.
4699 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004700 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004701 *
4702 * @see #sendAccessibilityEvent(int)
4703 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4704 */
4705 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004706 if (mAccessibilityDelegate != null) {
4707 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4708 } else {
4709 onInitializeAccessibilityEventInternal(event);
4710 }
4711 }
4712
4713 /**
4714 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4715 *
4716 * Note: Called from the default {@link AccessibilityDelegate}.
4717 */
4718 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004719 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004720 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004721 event.setPackageName(getContext().getPackageName());
4722 event.setEnabled(isEnabled());
4723 event.setContentDescription(mContentDescription);
4724
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004725 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004726 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004727 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4728 FOCUSABLES_ALL);
4729 event.setItemCount(focusablesTempList.size());
4730 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4731 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004732 }
4733 }
4734
4735 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004736 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4737 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4738 * This method is responsible for obtaining an accessibility node info from a
4739 * pool of reusable instances and calling
4740 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4741 * initialize the former.
4742 * <p>
4743 * Note: The client is responsible for recycling the obtained instance by calling
4744 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4745 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004746 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004747 * @return A populated {@link AccessibilityNodeInfo}.
4748 *
4749 * @see AccessibilityNodeInfo
4750 */
4751 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004752 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4753 if (provider != null) {
4754 return provider.createAccessibilityNodeInfo(View.NO_ID);
4755 } else {
4756 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4757 onInitializeAccessibilityNodeInfo(info);
4758 return info;
4759 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004760 }
4761
4762 /**
4763 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4764 * The base implementation sets:
4765 * <ul>
4766 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004767 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4768 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004769 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4770 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4771 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4772 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4773 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4774 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4775 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4776 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4777 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4778 * </ul>
4779 * <p>
4780 * Subclasses should override this method, call the super implementation,
4781 * and set additional attributes.
4782 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004783 * <p>
4784 * If an {@link AccessibilityDelegate} has been specified via calling
4785 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4786 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4787 * is responsible for handling this call.
4788 * </p>
4789 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004790 * @param info The instance to initialize.
4791 */
4792 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004793 if (mAccessibilityDelegate != null) {
4794 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4795 } else {
4796 onInitializeAccessibilityNodeInfoInternal(info);
4797 }
4798 }
4799
4800 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004801 * Gets the location of this view in screen coordintates.
4802 *
4803 * @param outRect The output location
4804 */
4805 private void getBoundsOnScreen(Rect outRect) {
4806 if (mAttachInfo == null) {
4807 return;
4808 }
4809
4810 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004811 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004812
4813 if (!hasIdentityMatrix()) {
4814 getMatrix().mapRect(position);
4815 }
4816
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004817 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004818
4819 ViewParent parent = mParent;
4820 while (parent instanceof View) {
4821 View parentView = (View) parent;
4822
4823 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4824
4825 if (!parentView.hasIdentityMatrix()) {
4826 parentView.getMatrix().mapRect(position);
4827 }
4828
4829 position.offset(parentView.mLeft, parentView.mTop);
4830
4831 parent = parentView.mParent;
4832 }
4833
4834 if (parent instanceof ViewRootImpl) {
4835 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4836 position.offset(0, -viewRootImpl.mCurScrollY);
4837 }
4838
4839 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4840
4841 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4842 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4843 }
4844
4845 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004846 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4847 *
4848 * Note: Called from the default {@link AccessibilityDelegate}.
4849 */
4850 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004851 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004852
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004853 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004854 info.setBoundsInParent(bounds);
4855
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004856 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004857 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004858
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004859 ViewParent parent = getParentForAccessibility();
4860 if (parent instanceof View) {
4861 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004862 }
4863
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004864 if (mID != View.NO_ID) {
4865 View rootView = getRootView();
4866 if (rootView == null) {
4867 rootView = this;
4868 }
4869 View label = rootView.findLabelForView(this, mID);
4870 if (label != null) {
4871 info.setLabeledBy(label);
4872 }
4873 }
4874
4875 if (mLabelForId != View.NO_ID) {
4876 View rootView = getRootView();
4877 if (rootView == null) {
4878 rootView = this;
4879 }
4880 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4881 if (labeled != null) {
4882 info.setLabelFor(labeled);
4883 }
4884 }
4885
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004886 info.setVisibleToUser(isVisibleToUser());
4887
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004888 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004889 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004890 info.setContentDescription(getContentDescription());
4891
4892 info.setEnabled(isEnabled());
4893 info.setClickable(isClickable());
4894 info.setFocusable(isFocusable());
4895 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004896 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004897 info.setSelected(isSelected());
4898 info.setLongClickable(isLongClickable());
4899
4900 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004901 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004902 // we should report as selectable view in an AdapterView.
4903 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4904 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4905
4906 if (isFocusable()) {
4907 if (isFocused()) {
4908 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4909 } else {
4910 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4911 }
4912 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004913
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004914 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004915 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004916 } else {
4917 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4918 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004919
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004920 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004921 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4922 }
4923
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004924 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004925 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4926 }
4927
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004928 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004929 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4930 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4931 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004932 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4933 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004934 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004935 }
4936
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004937 private View findLabelForView(View view, int labeledId) {
4938 if (mMatchLabelForPredicate == null) {
4939 mMatchLabelForPredicate = new MatchLabelForPredicate();
4940 }
4941 mMatchLabelForPredicate.mLabeledId = labeledId;
4942 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
4943 }
4944
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004945 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004946 * Computes whether this view is visible to the user. Such a view is
4947 * attached, visible, all its predecessors are visible, it is not clipped
4948 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004949 *
4950 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004951 *
4952 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004953 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004954 protected boolean isVisibleToUser() {
4955 return isVisibleToUser(null);
4956 }
4957
4958 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004959 * Computes whether the given portion of this view is visible to the user.
4960 * Such a view is attached, visible, all its predecessors are visible,
4961 * has an alpha greater than zero, and the specified portion is not
4962 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004963 *
4964 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4965 * <code>null</code>, and the entire view will be tested in this case.
4966 * When <code>true</code> is returned by the function, the actual visible
4967 * region will be stored in this parameter; that is, if boundInView is fully
4968 * contained within the view, no modification will be made, otherwise regions
4969 * outside of the visible area of the view will be clipped.
4970 *
4971 * @return Whether the specified portion of the view is visible on the screen.
4972 *
4973 * @hide
4974 */
4975 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004976 if (mAttachInfo != null) {
4977 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4978 Point offset = mAttachInfo.mPoint;
4979 // The first two checks are made also made by isShown() which
4980 // however traverses the tree up to the parent to catch that.
4981 // Therefore, we do some fail fast check to minimize the up
4982 // tree traversal.
4983 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4984 && getAlpha() > 0
4985 && isShown()
4986 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004987 if (isVisible && boundInView != null) {
4988 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004989 // isVisible is always true here, use a simple assignment
4990 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004991 }
4992 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004993 }
4994
4995 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004996 }
4997
4998 /**
alanvb72fe7a2012-08-27 16:44:25 -07004999 * Returns the delegate for implementing accessibility support via
5000 * composition. For more details see {@link AccessibilityDelegate}.
5001 *
5002 * @return The delegate, or null if none set.
5003 *
5004 * @hide
5005 */
5006 public AccessibilityDelegate getAccessibilityDelegate() {
5007 return mAccessibilityDelegate;
5008 }
5009
5010 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005011 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005012 * opposed to inheritance. The delegate's primary use is for implementing
5013 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5014 *
5015 * @param delegate The delegate instance.
5016 *
5017 * @see AccessibilityDelegate
5018 */
5019 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5020 mAccessibilityDelegate = delegate;
5021 }
5022
5023 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005024 * Gets the provider for managing a virtual view hierarchy rooted at this View
5025 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5026 * that explore the window content.
5027 * <p>
5028 * If this method returns an instance, this instance is responsible for managing
5029 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5030 * View including the one representing the View itself. Similarly the returned
5031 * instance is responsible for performing accessibility actions on any virtual
5032 * view or the root view itself.
5033 * </p>
5034 * <p>
5035 * If an {@link AccessibilityDelegate} has been specified via calling
5036 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5037 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5038 * is responsible for handling this call.
5039 * </p>
5040 *
5041 * @return The provider.
5042 *
5043 * @see AccessibilityNodeProvider
5044 */
5045 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5046 if (mAccessibilityDelegate != null) {
5047 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5048 } else {
5049 return null;
5050 }
5051 }
5052
5053 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005054 * Gets the unique identifier of this view on the screen for accessibility purposes.
5055 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5056 *
5057 * @return The view accessibility id.
5058 *
5059 * @hide
5060 */
5061 public int getAccessibilityViewId() {
5062 if (mAccessibilityViewId == NO_ID) {
5063 mAccessibilityViewId = sNextAccessibilityViewId++;
5064 }
5065 return mAccessibilityViewId;
5066 }
5067
5068 /**
5069 * Gets the unique identifier of the window in which this View reseides.
5070 *
5071 * @return The window accessibility id.
5072 *
5073 * @hide
5074 */
5075 public int getAccessibilityWindowId() {
5076 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5077 }
5078
5079 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005080 * Gets the {@link View} description. It briefly describes the view and is
5081 * primarily used for accessibility support. Set this property to enable
5082 * better accessibility support for your application. This is especially
5083 * true for views that do not have textual representation (For example,
5084 * ImageButton).
5085 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005086 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005087 *
5088 * @attr ref android.R.styleable#View_contentDescription
5089 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005090 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005091 public CharSequence getContentDescription() {
5092 return mContentDescription;
5093 }
5094
5095 /**
5096 * Sets the {@link View} description. It briefly describes the view and is
5097 * primarily used for accessibility support. Set this property to enable
5098 * better accessibility support for your application. This is especially
5099 * true for views that do not have textual representation (For example,
5100 * ImageButton).
5101 *
5102 * @param contentDescription The content description.
5103 *
5104 * @attr ref android.R.styleable#View_contentDescription
5105 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005106 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005107 public void setContentDescription(CharSequence contentDescription) {
5108 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005109 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5110 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5111 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5112 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005113 }
5114
5115 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005116 * Gets the id of a view for which this view serves as a label for
5117 * accessibility purposes.
5118 *
5119 * @return The labeled view id.
5120 */
5121 @ViewDebug.ExportedProperty(category = "accessibility")
5122 public int getLabelFor() {
5123 return mLabelForId;
5124 }
5125
5126 /**
5127 * Sets the id of a view for which this view serves as a label for
5128 * accessibility purposes.
5129 *
5130 * @param id The labeled view id.
5131 */
5132 @RemotableViewMethod
5133 public void setLabelFor(int id) {
5134 mLabelForId = id;
5135 if (mLabelForId != View.NO_ID
5136 && mID == View.NO_ID) {
5137 mID = generateViewId();
5138 }
5139 }
5140
5141 /**
Romain Guya2431d02009-04-30 16:30:00 -07005142 * Invoked whenever this view loses focus, either by losing window focus or by losing
5143 * focus within its window. This method can be used to clear any state tied to the
5144 * focus. For instance, if a button is held pressed with the trackball and the window
5145 * loses focus, this method can be used to cancel the press.
5146 *
5147 * Subclasses of View overriding this method should always call super.onFocusLost().
5148 *
5149 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005150 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005151 *
5152 * @hide pending API council approval
5153 */
5154 protected void onFocusLost() {
5155 resetPressedState();
5156 }
5157
5158 private void resetPressedState() {
5159 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5160 return;
5161 }
5162
5163 if (isPressed()) {
5164 setPressed(false);
5165
5166 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005167 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005168 }
5169 }
5170 }
5171
5172 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 * Returns true if this view has focus
5174 *
5175 * @return True if this view has focus, false otherwise.
5176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005177 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005179 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 }
5181
5182 /**
5183 * Find the view in the hierarchy rooted at this view that currently has
5184 * focus.
5185 *
5186 * @return The view that currently has focus, or null if no focused view can
5187 * be found.
5188 */
5189 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005190 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005191 }
5192
5193 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005194 * Indicates whether this view is one of the set of scrollable containers in
5195 * its window.
5196 *
5197 * @return whether this view is one of the set of scrollable containers in
5198 * its window
5199 *
5200 * @attr ref android.R.styleable#View_isScrollContainer
5201 */
5202 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005203 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005204 }
5205
5206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 * Change whether this view is one of the set of scrollable containers in
5208 * its window. This will be used to determine whether the window can
5209 * resize or must pan when a soft input area is open -- scrollable
5210 * containers allow the window to use resize mode since the container
5211 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005212 *
5213 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 */
5215 public void setScrollContainer(boolean isScrollContainer) {
5216 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005217 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005219 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005221 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005223 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 mAttachInfo.mScrollContainers.remove(this);
5225 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005226 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 }
5228 }
5229
5230 /**
5231 * Returns the quality of the drawing cache.
5232 *
5233 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5234 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5235 *
5236 * @see #setDrawingCacheQuality(int)
5237 * @see #setDrawingCacheEnabled(boolean)
5238 * @see #isDrawingCacheEnabled()
5239 *
5240 * @attr ref android.R.styleable#View_drawingCacheQuality
5241 */
5242 public int getDrawingCacheQuality() {
5243 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5244 }
5245
5246 /**
5247 * Set the drawing cache quality of this view. This value is used only when the
5248 * drawing cache is enabled
5249 *
5250 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5251 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5252 *
5253 * @see #getDrawingCacheQuality()
5254 * @see #setDrawingCacheEnabled(boolean)
5255 * @see #isDrawingCacheEnabled()
5256 *
5257 * @attr ref android.R.styleable#View_drawingCacheQuality
5258 */
5259 public void setDrawingCacheQuality(int quality) {
5260 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5261 }
5262
5263 /**
5264 * Returns whether the screen should remain on, corresponding to the current
5265 * value of {@link #KEEP_SCREEN_ON}.
5266 *
5267 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5268 *
5269 * @see #setKeepScreenOn(boolean)
5270 *
5271 * @attr ref android.R.styleable#View_keepScreenOn
5272 */
5273 public boolean getKeepScreenOn() {
5274 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5275 }
5276
5277 /**
5278 * Controls whether the screen should remain on, modifying the
5279 * value of {@link #KEEP_SCREEN_ON}.
5280 *
5281 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5282 *
5283 * @see #getKeepScreenOn()
5284 *
5285 * @attr ref android.R.styleable#View_keepScreenOn
5286 */
5287 public void setKeepScreenOn(boolean keepScreenOn) {
5288 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5289 }
5290
5291 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005292 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5293 * @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 -08005294 *
5295 * @attr ref android.R.styleable#View_nextFocusLeft
5296 */
5297 public int getNextFocusLeftId() {
5298 return mNextFocusLeftId;
5299 }
5300
5301 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005302 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5303 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5304 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005305 *
5306 * @attr ref android.R.styleable#View_nextFocusLeft
5307 */
5308 public void setNextFocusLeftId(int nextFocusLeftId) {
5309 mNextFocusLeftId = nextFocusLeftId;
5310 }
5311
5312 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005313 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5314 * @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 -08005315 *
5316 * @attr ref android.R.styleable#View_nextFocusRight
5317 */
5318 public int getNextFocusRightId() {
5319 return mNextFocusRightId;
5320 }
5321
5322 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005323 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5324 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5325 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 *
5327 * @attr ref android.R.styleable#View_nextFocusRight
5328 */
5329 public void setNextFocusRightId(int nextFocusRightId) {
5330 mNextFocusRightId = nextFocusRightId;
5331 }
5332
5333 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005334 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5335 * @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 -08005336 *
5337 * @attr ref android.R.styleable#View_nextFocusUp
5338 */
5339 public int getNextFocusUpId() {
5340 return mNextFocusUpId;
5341 }
5342
5343 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005344 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5345 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5346 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 *
5348 * @attr ref android.R.styleable#View_nextFocusUp
5349 */
5350 public void setNextFocusUpId(int nextFocusUpId) {
5351 mNextFocusUpId = nextFocusUpId;
5352 }
5353
5354 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005355 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5356 * @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 -08005357 *
5358 * @attr ref android.R.styleable#View_nextFocusDown
5359 */
5360 public int getNextFocusDownId() {
5361 return mNextFocusDownId;
5362 }
5363
5364 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005365 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5366 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5367 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005368 *
5369 * @attr ref android.R.styleable#View_nextFocusDown
5370 */
5371 public void setNextFocusDownId(int nextFocusDownId) {
5372 mNextFocusDownId = nextFocusDownId;
5373 }
5374
5375 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005376 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5377 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5378 *
5379 * @attr ref android.R.styleable#View_nextFocusForward
5380 */
5381 public int getNextFocusForwardId() {
5382 return mNextFocusForwardId;
5383 }
5384
5385 /**
5386 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5387 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5388 * decide automatically.
5389 *
5390 * @attr ref android.R.styleable#View_nextFocusForward
5391 */
5392 public void setNextFocusForwardId(int nextFocusForwardId) {
5393 mNextFocusForwardId = nextFocusForwardId;
5394 }
5395
5396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005397 * Returns the visibility of this view and all of its ancestors
5398 *
5399 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5400 */
5401 public boolean isShown() {
5402 View current = this;
5403 //noinspection ConstantConditions
5404 do {
5405 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5406 return false;
5407 }
5408 ViewParent parent = current.mParent;
5409 if (parent == null) {
5410 return false; // We are not attached to the view root
5411 }
5412 if (!(parent instanceof View)) {
5413 return true;
5414 }
5415 current = (View) parent;
5416 } while (current != null);
5417
5418 return false;
5419 }
5420
5421 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005422 * Called by the view hierarchy when the content insets for a window have
5423 * changed, to allow it to adjust its content to fit within those windows.
5424 * The content insets tell you the space that the status bar, input method,
5425 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005427 * <p>You do not normally need to deal with this function, since the default
5428 * window decoration given to applications takes care of applying it to the
5429 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5430 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5431 * and your content can be placed under those system elements. You can then
5432 * use this method within your view hierarchy if you have parts of your UI
5433 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005435 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005436 * inset's to the view's padding, consuming that content (modifying the
5437 * insets to be 0), and returning true. This behavior is off by default, but can
5438 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5439 *
5440 * <p>This function's traversal down the hierarchy is depth-first. The same content
5441 * insets object is propagated down the hierarchy, so any changes made to it will
5442 * be seen by all following views (including potentially ones above in
5443 * the hierarchy since this is a depth-first traversal). The first view
5444 * that returns true will abort the entire traversal.
5445 *
5446 * <p>The default implementation works well for a situation where it is
5447 * used with a container that covers the entire window, allowing it to
5448 * apply the appropriate insets to its content on all edges. If you need
5449 * a more complicated layout (such as two different views fitting system
5450 * windows, one on the top of the window, and one on the bottom),
5451 * you can override the method and handle the insets however you would like.
5452 * Note that the insets provided by the framework are always relative to the
5453 * far edges of the window, not accounting for the location of the called view
5454 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005455 * where the layout will place the view, as it is done before layout happens.)
5456 *
5457 * <p>Note: unlike many View methods, there is no dispatch phase to this
5458 * call. If you are overriding it in a ViewGroup and want to allow the
5459 * call to continue to your children, you must be sure to call the super
5460 * implementation.
5461 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005462 * <p>Here is a sample layout that makes use of fitting system windows
5463 * to have controls for a video view placed inside of the window decorations
5464 * that it hides and shows. This can be used with code like the second
5465 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5466 *
5467 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5468 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005469 * @param insets Current content insets of the window. Prior to
5470 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5471 * the insets or else you and Android will be unhappy.
5472 *
5473 * @return Return true if this view applied the insets and it should not
5474 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005475 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005476 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005477 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 */
5479 protected boolean fitSystemWindows(Rect insets) {
5480 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005481 mUserPaddingStart = UNDEFINED_PADDING;
5482 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005483 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5484 || mAttachInfo == null
5485 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5486 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5487 return true;
5488 } else {
5489 internalSetPadding(0, 0, 0, 0);
5490 return false;
5491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 }
5493 return false;
5494 }
5495
5496 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005497 * Sets whether or not this view should account for system screen decorations
5498 * such as the status bar and inset its content; that is, controlling whether
5499 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5500 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005501 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005502 * <p>Note that if you are providing your own implementation of
5503 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5504 * flag to true -- your implementation will be overriding the default
5505 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005506 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005507 * @param fitSystemWindows If true, then the default implementation of
5508 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005509 *
5510 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005511 * @see #getFitsSystemWindows()
5512 * @see #fitSystemWindows(Rect)
5513 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005514 */
5515 public void setFitsSystemWindows(boolean fitSystemWindows) {
5516 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5517 }
5518
5519 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005520 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005521 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5522 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005523 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005524 * @return Returns true if the default implementation of
5525 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005526 *
5527 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005528 * @see #setFitsSystemWindows()
5529 * @see #fitSystemWindows(Rect)
5530 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005531 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005532 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005533 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5534 }
5535
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005536 /** @hide */
5537 public boolean fitsSystemWindows() {
5538 return getFitsSystemWindows();
5539 }
5540
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005541 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005542 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5543 */
5544 public void requestFitSystemWindows() {
5545 if (mParent != null) {
5546 mParent.requestFitSystemWindows();
5547 }
5548 }
5549
5550 /**
5551 * For use by PhoneWindow to make its own system window fitting optional.
5552 * @hide
5553 */
5554 public void makeOptionalFitsSystemWindows() {
5555 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5556 }
5557
5558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 * Returns the visibility status for this view.
5560 *
5561 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5562 * @attr ref android.R.styleable#View_visibility
5563 */
5564 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005565 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5566 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5567 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 })
5569 public int getVisibility() {
5570 return mViewFlags & VISIBILITY_MASK;
5571 }
5572
5573 /**
5574 * Set the enabled state of this view.
5575 *
5576 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5577 * @attr ref android.R.styleable#View_visibility
5578 */
5579 @RemotableViewMethod
5580 public void setVisibility(int visibility) {
5581 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005582 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 }
5584
5585 /**
5586 * Returns the enabled status for this view. The interpretation of the
5587 * enabled state varies by subclass.
5588 *
5589 * @return True if this view is enabled, false otherwise.
5590 */
5591 @ViewDebug.ExportedProperty
5592 public boolean isEnabled() {
5593 return (mViewFlags & ENABLED_MASK) == ENABLED;
5594 }
5595
5596 /**
5597 * Set the enabled state of this view. The interpretation of the enabled
5598 * state varies by subclass.
5599 *
5600 * @param enabled True if this view is enabled, false otherwise.
5601 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005602 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005604 if (enabled == isEnabled()) return;
5605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5607
5608 /*
5609 * The View most likely has to change its appearance, so refresh
5610 * the drawable state.
5611 */
5612 refreshDrawableState();
5613
5614 // Invalidate too, since the default behavior for views is to be
5615 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005616 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 }
5618
5619 /**
5620 * Set whether this view can receive the focus.
5621 *
5622 * Setting this to false will also ensure that this view is not focusable
5623 * in touch mode.
5624 *
5625 * @param focusable If true, this view can receive the focus.
5626 *
5627 * @see #setFocusableInTouchMode(boolean)
5628 * @attr ref android.R.styleable#View_focusable
5629 */
5630 public void setFocusable(boolean focusable) {
5631 if (!focusable) {
5632 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5633 }
5634 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5635 }
5636
5637 /**
5638 * Set whether this view can receive focus while in touch mode.
5639 *
5640 * Setting this to true will also ensure that this view is focusable.
5641 *
5642 * @param focusableInTouchMode If true, this view can receive the focus while
5643 * in touch mode.
5644 *
5645 * @see #setFocusable(boolean)
5646 * @attr ref android.R.styleable#View_focusableInTouchMode
5647 */
5648 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5649 // Focusable in touch mode should always be set before the focusable flag
5650 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5651 // which, in touch mode, will not successfully request focus on this view
5652 // because the focusable in touch mode flag is not set
5653 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5654 if (focusableInTouchMode) {
5655 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5656 }
5657 }
5658
5659 /**
5660 * Set whether this view should have sound effects enabled for events such as
5661 * clicking and touching.
5662 *
5663 * <p>You may wish to disable sound effects for a view if you already play sounds,
5664 * for instance, a dial key that plays dtmf tones.
5665 *
5666 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5667 * @see #isSoundEffectsEnabled()
5668 * @see #playSoundEffect(int)
5669 * @attr ref android.R.styleable#View_soundEffectsEnabled
5670 */
5671 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5672 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5673 }
5674
5675 /**
5676 * @return whether this view should have sound effects enabled for events such as
5677 * clicking and touching.
5678 *
5679 * @see #setSoundEffectsEnabled(boolean)
5680 * @see #playSoundEffect(int)
5681 * @attr ref android.R.styleable#View_soundEffectsEnabled
5682 */
5683 @ViewDebug.ExportedProperty
5684 public boolean isSoundEffectsEnabled() {
5685 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5686 }
5687
5688 /**
5689 * Set whether this view should have haptic feedback for events such as
5690 * long presses.
5691 *
5692 * <p>You may wish to disable haptic feedback if your view already controls
5693 * its own haptic feedback.
5694 *
5695 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5696 * @see #isHapticFeedbackEnabled()
5697 * @see #performHapticFeedback(int)
5698 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5699 */
5700 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5701 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5702 }
5703
5704 /**
5705 * @return whether this view should have haptic feedback enabled for events
5706 * long presses.
5707 *
5708 * @see #setHapticFeedbackEnabled(boolean)
5709 * @see #performHapticFeedback(int)
5710 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5711 */
5712 @ViewDebug.ExportedProperty
5713 public boolean isHapticFeedbackEnabled() {
5714 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5715 }
5716
5717 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005718 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005719 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005720 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5721 * {@link #LAYOUT_DIRECTION_RTL},
5722 * {@link #LAYOUT_DIRECTION_INHERIT} or
5723 * {@link #LAYOUT_DIRECTION_LOCALE}.
5724 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005725 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005726 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005727 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5728 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5729 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5730 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005731 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005732 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005733 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005734 }
5735
5736 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005737 * Set the layout direction for this view. This will propagate a reset of layout direction
5738 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005739 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005740 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5741 * {@link #LAYOUT_DIRECTION_RTL},
5742 * {@link #LAYOUT_DIRECTION_INHERIT} or
5743 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005744 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005745 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005746 */
5747 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005748 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005749 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005750 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005751 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005752 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005753 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005754 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005755 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005756 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005757 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005758 resolveRtlProperties();
5759 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005760 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005761 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005762 }
5763
5764 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005765 * Returns the resolved layout direction for this view.
5766 *
5767 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005768 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005769 */
5770 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005771 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5772 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005773 })
5774 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005775 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5776 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005777 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005778 return LAYOUT_DIRECTION_LTR;
5779 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005780 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005781 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005782 resolveLayoutDirection();
5783 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005784 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005785 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5786 }
5787
5788 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005789 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5790 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005791 *
5792 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005793 */
5794 @ViewDebug.ExportedProperty(category = "layout")
5795 public boolean isLayoutRtl() {
5796 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5797 }
5798
5799 /**
Adam Powell539ee872012-02-03 19:00:49 -08005800 * Indicates whether the view is currently tracking transient state that the
5801 * app should not need to concern itself with saving and restoring, but that
5802 * the framework should take special note to preserve when possible.
5803 *
Adam Powell785c4472012-05-02 21:25:39 -07005804 * <p>A view with transient state cannot be trivially rebound from an external
5805 * data source, such as an adapter binding item views in a list. This may be
5806 * because the view is performing an animation, tracking user selection
5807 * of content, or similar.</p>
5808 *
Adam Powell539ee872012-02-03 19:00:49 -08005809 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005810 */
5811 @ViewDebug.ExportedProperty(category = "layout")
5812 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005813 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005814 }
5815
5816 /**
5817 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005818 * framework should attempt to preserve when possible. This flag is reference counted,
5819 * so every call to setHasTransientState(true) should be paired with a later call
5820 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005821 *
Adam Powell785c4472012-05-02 21:25:39 -07005822 * <p>A view with transient state cannot be trivially rebound from an external
5823 * data source, such as an adapter binding item views in a list. This may be
5824 * because the view is performing an animation, tracking user selection
5825 * of content, or similar.</p>
5826 *
Adam Powell539ee872012-02-03 19:00:49 -08005827 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005828 */
5829 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005830 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5831 mTransientStateCount - 1;
5832 if (mTransientStateCount < 0) {
5833 mTransientStateCount = 0;
5834 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5835 "unmatched pair of setHasTransientState calls");
5836 }
5837 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005838 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005839 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005840 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5841 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005842 if (mParent != null) {
5843 try {
5844 mParent.childHasTransientStateChanged(this, hasTransientState);
5845 } catch (AbstractMethodError e) {
5846 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5847 " does not fully implement ViewParent", e);
5848 }
Adam Powell539ee872012-02-03 19:00:49 -08005849 }
5850 }
5851 }
5852
5853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 * If this view doesn't do any drawing on its own, set this flag to
5855 * allow further optimizations. By default, this flag is not set on
5856 * View, but could be set on some View subclasses such as ViewGroup.
5857 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005858 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5859 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 *
5861 * @param willNotDraw whether or not this View draw on its own
5862 */
5863 public void setWillNotDraw(boolean willNotDraw) {
5864 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5865 }
5866
5867 /**
5868 * Returns whether or not this View draws on its own.
5869 *
5870 * @return true if this view has nothing to draw, false otherwise
5871 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005872 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 public boolean willNotDraw() {
5874 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5875 }
5876
5877 /**
5878 * When a View's drawing cache is enabled, drawing is redirected to an
5879 * offscreen bitmap. Some views, like an ImageView, must be able to
5880 * bypass this mechanism if they already draw a single bitmap, to avoid
5881 * unnecessary usage of the memory.
5882 *
5883 * @param willNotCacheDrawing true if this view does not cache its
5884 * drawing, false otherwise
5885 */
5886 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5887 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5888 }
5889
5890 /**
5891 * Returns whether or not this View can cache its drawing or not.
5892 *
5893 * @return true if this view does not cache its drawing, false otherwise
5894 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005895 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896 public boolean willNotCacheDrawing() {
5897 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5898 }
5899
5900 /**
5901 * Indicates whether this view reacts to click events or not.
5902 *
5903 * @return true if the view is clickable, false otherwise
5904 *
5905 * @see #setClickable(boolean)
5906 * @attr ref android.R.styleable#View_clickable
5907 */
5908 @ViewDebug.ExportedProperty
5909 public boolean isClickable() {
5910 return (mViewFlags & CLICKABLE) == CLICKABLE;
5911 }
5912
5913 /**
5914 * Enables or disables click events for this view. When a view
5915 * is clickable it will change its state to "pressed" on every click.
5916 * Subclasses should set the view clickable to visually react to
5917 * user's clicks.
5918 *
5919 * @param clickable true to make the view clickable, false otherwise
5920 *
5921 * @see #isClickable()
5922 * @attr ref android.R.styleable#View_clickable
5923 */
5924 public void setClickable(boolean clickable) {
5925 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5926 }
5927
5928 /**
5929 * Indicates whether this view reacts to long click events or not.
5930 *
5931 * @return true if the view is long clickable, false otherwise
5932 *
5933 * @see #setLongClickable(boolean)
5934 * @attr ref android.R.styleable#View_longClickable
5935 */
5936 public boolean isLongClickable() {
5937 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5938 }
5939
5940 /**
5941 * Enables or disables long click events for this view. When a view is long
5942 * clickable it reacts to the user holding down the button for a longer
5943 * duration than a tap. This event can either launch the listener or a
5944 * context menu.
5945 *
5946 * @param longClickable true to make the view long clickable, false otherwise
5947 * @see #isLongClickable()
5948 * @attr ref android.R.styleable#View_longClickable
5949 */
5950 public void setLongClickable(boolean longClickable) {
5951 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5952 }
5953
5954 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005955 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 *
5957 * @see #isClickable()
5958 * @see #setClickable(boolean)
5959 *
5960 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5961 * the View's internal state from a previously set "pressed" state.
5962 */
5963 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005964 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005967 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005969 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005971
5972 if (needsRefresh) {
5973 refreshDrawableState();
5974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 dispatchSetPressed(pressed);
5976 }
5977
5978 /**
5979 * Dispatch setPressed to all of this View's children.
5980 *
5981 * @see #setPressed(boolean)
5982 *
5983 * @param pressed The new pressed state
5984 */
5985 protected void dispatchSetPressed(boolean pressed) {
5986 }
5987
5988 /**
5989 * Indicates whether the view is currently in pressed state. Unless
5990 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5991 * the pressed state.
5992 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005993 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 * @see #isClickable()
5995 * @see #setClickable(boolean)
5996 *
5997 * @return true if the view is currently pressed, false otherwise
5998 */
5999 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006000 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 }
6002
6003 /**
6004 * Indicates whether this view will save its state (that is,
6005 * whether its {@link #onSaveInstanceState} method will be called).
6006 *
6007 * @return Returns true if the view state saving is enabled, else false.
6008 *
6009 * @see #setSaveEnabled(boolean)
6010 * @attr ref android.R.styleable#View_saveEnabled
6011 */
6012 public boolean isSaveEnabled() {
6013 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6014 }
6015
6016 /**
6017 * Controls whether the saving of this view's state is
6018 * enabled (that is, whether its {@link #onSaveInstanceState} method
6019 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006020 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 * for its state to be saved. This flag can only disable the
6022 * saving of this view; any child views may still have their state saved.
6023 *
6024 * @param enabled Set to false to <em>disable</em> state saving, or true
6025 * (the default) to allow it.
6026 *
6027 * @see #isSaveEnabled()
6028 * @see #setId(int)
6029 * @see #onSaveInstanceState()
6030 * @attr ref android.R.styleable#View_saveEnabled
6031 */
6032 public void setSaveEnabled(boolean enabled) {
6033 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6034 }
6035
Jeff Brown85a31762010-09-01 17:01:00 -07006036 /**
6037 * Gets whether the framework should discard touches when the view's
6038 * window is obscured by another visible window.
6039 * Refer to the {@link View} security documentation for more details.
6040 *
6041 * @return True if touch filtering is enabled.
6042 *
6043 * @see #setFilterTouchesWhenObscured(boolean)
6044 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6045 */
6046 @ViewDebug.ExportedProperty
6047 public boolean getFilterTouchesWhenObscured() {
6048 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6049 }
6050
6051 /**
6052 * Sets whether the framework should discard touches when the view's
6053 * window is obscured by another visible window.
6054 * Refer to the {@link View} security documentation for more details.
6055 *
6056 * @param enabled True if touch filtering should be enabled.
6057 *
6058 * @see #getFilterTouchesWhenObscured
6059 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6060 */
6061 public void setFilterTouchesWhenObscured(boolean enabled) {
6062 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6063 FILTER_TOUCHES_WHEN_OBSCURED);
6064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065
6066 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006067 * Indicates whether the entire hierarchy under this view will save its
6068 * state when a state saving traversal occurs from its parent. The default
6069 * is true; if false, these views will not be saved unless
6070 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6071 *
6072 * @return Returns true if the view state saving from parent is enabled, else false.
6073 *
6074 * @see #setSaveFromParentEnabled(boolean)
6075 */
6076 public boolean isSaveFromParentEnabled() {
6077 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6078 }
6079
6080 /**
6081 * Controls whether the entire hierarchy under this view will save its
6082 * state when a state saving traversal occurs from its parent. The default
6083 * is true; if false, these views will not be saved unless
6084 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6085 *
6086 * @param enabled Set to false to <em>disable</em> state saving, or true
6087 * (the default) to allow it.
6088 *
6089 * @see #isSaveFromParentEnabled()
6090 * @see #setId(int)
6091 * @see #onSaveInstanceState()
6092 */
6093 public void setSaveFromParentEnabled(boolean enabled) {
6094 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6095 }
6096
6097
6098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006099 * Returns whether this View is able to take focus.
6100 *
6101 * @return True if this view can take focus, or false otherwise.
6102 * @attr ref android.R.styleable#View_focusable
6103 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006104 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 public final boolean isFocusable() {
6106 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6107 }
6108
6109 /**
6110 * When a view is focusable, it may not want to take focus when in touch mode.
6111 * For example, a button would like focus when the user is navigating via a D-pad
6112 * so that the user can click on it, but once the user starts touching the screen,
6113 * the button shouldn't take focus
6114 * @return Whether the view is focusable in touch mode.
6115 * @attr ref android.R.styleable#View_focusableInTouchMode
6116 */
6117 @ViewDebug.ExportedProperty
6118 public final boolean isFocusableInTouchMode() {
6119 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6120 }
6121
6122 /**
6123 * Find the nearest view in the specified direction that can take focus.
6124 * This does not actually give focus to that view.
6125 *
6126 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6127 *
6128 * @return The nearest focusable in the specified direction, or null if none
6129 * can be found.
6130 */
6131 public View focusSearch(int direction) {
6132 if (mParent != null) {
6133 return mParent.focusSearch(this, direction);
6134 } else {
6135 return null;
6136 }
6137 }
6138
6139 /**
6140 * This method is the last chance for the focused view and its ancestors to
6141 * respond to an arrow key. This is called when the focused view did not
6142 * consume the key internally, nor could the view system find a new view in
6143 * the requested direction to give focus to.
6144 *
6145 * @param focused The currently focused view.
6146 * @param direction The direction focus wants to move. One of FOCUS_UP,
6147 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6148 * @return True if the this view consumed this unhandled move.
6149 */
6150 public boolean dispatchUnhandledMove(View focused, int direction) {
6151 return false;
6152 }
6153
6154 /**
6155 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006156 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006158 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6159 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 * @return The user specified next view, or null if there is none.
6161 */
6162 View findUserSetNextFocus(View root, int direction) {
6163 switch (direction) {
6164 case FOCUS_LEFT:
6165 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006166 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006167 case FOCUS_RIGHT:
6168 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006169 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 case FOCUS_UP:
6171 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006172 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006173 case FOCUS_DOWN:
6174 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006175 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006176 case FOCUS_FORWARD:
6177 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006178 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006179 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006180 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006181 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006182 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006183 @Override
6184 public boolean apply(View t) {
6185 return t.mNextFocusForwardId == id;
6186 }
6187 });
6188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 }
6190 return null;
6191 }
6192
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006193 private View findViewInsideOutShouldExist(View root, int id) {
6194 if (mMatchIdPredicate == null) {
6195 mMatchIdPredicate = new MatchIdPredicate();
6196 }
6197 mMatchIdPredicate.mId = id;
6198 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006200 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 }
6202 return result;
6203 }
6204
6205 /**
6206 * Find and return all focusable views that are descendants of this view,
6207 * possibly including this view if it is focusable itself.
6208 *
6209 * @param direction The direction of the focus
6210 * @return A list of focusable views
6211 */
6212 public ArrayList<View> getFocusables(int direction) {
6213 ArrayList<View> result = new ArrayList<View>(24);
6214 addFocusables(result, direction);
6215 return result;
6216 }
6217
6218 /**
6219 * Add any focusable views that are descendants of this view (possibly
6220 * including this view if it is focusable itself) to views. If we are in touch mode,
6221 * only add views that are also focusable in touch mode.
6222 *
6223 * @param views Focusable views found so far
6224 * @param direction The direction of the focus
6225 */
6226 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006227 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229
svetoslavganov75986cf2009-05-14 22:28:01 -07006230 /**
6231 * Adds any focusable views that are descendants of this view (possibly
6232 * including this view if it is focusable itself) to views. This method
6233 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006234 * only views focusable in touch mode if we are in touch mode or
6235 * only views that can take accessibility focus if accessibility is enabeld
6236 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006237 *
6238 * @param views Focusable views found so far or null if all we are interested is
6239 * the number of focusables.
6240 * @param direction The direction of the focus.
6241 * @param focusableMode The type of focusables to be added.
6242 *
6243 * @see #FOCUSABLES_ALL
6244 * @see #FOCUSABLES_TOUCH_MODE
6245 */
6246 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006247 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006248 return;
6249 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006250 if (!isFocusable()) {
6251 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006252 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006253 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6254 && isInTouchMode() && !isFocusableInTouchMode()) {
6255 return;
6256 }
6257 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 }
6259
6260 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006261 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006262 * The search is performed by either the text that the View renders or the content
6263 * description that describes the view for accessibility purposes and the view does
6264 * not render or both. Clients can specify how the search is to be performed via
6265 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6266 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006267 *
6268 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006269 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006270 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006271 * @see #FIND_VIEWS_WITH_TEXT
6272 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6273 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006274 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006275 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006276 if (getAccessibilityNodeProvider() != null) {
6277 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6278 outViews.add(this);
6279 }
6280 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006281 && (searched != null && searched.length() > 0)
6282 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006283 String searchedLowerCase = searched.toString().toLowerCase();
6284 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6285 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6286 outViews.add(this);
6287 }
6288 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006289 }
6290
6291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 * Find and return all touchable views that are descendants of this view,
6293 * possibly including this view if it is touchable itself.
6294 *
6295 * @return A list of touchable views
6296 */
6297 public ArrayList<View> getTouchables() {
6298 ArrayList<View> result = new ArrayList<View>();
6299 addTouchables(result);
6300 return result;
6301 }
6302
6303 /**
6304 * Add any touchable views that are descendants of this view (possibly
6305 * including this view if it is touchable itself) to views.
6306 *
6307 * @param views Touchable views found so far
6308 */
6309 public void addTouchables(ArrayList<View> views) {
6310 final int viewFlags = mViewFlags;
6311
6312 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6313 && (viewFlags & ENABLED_MASK) == ENABLED) {
6314 views.add(this);
6315 }
6316 }
6317
6318 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006319 * Returns whether this View is accessibility focused.
6320 *
6321 * @return True if this View is accessibility focused.
6322 */
6323 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006324 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006325 }
6326
6327 /**
6328 * Call this to try to give accessibility focus to this view.
6329 *
6330 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6331 * returns false or the view is no visible or the view already has accessibility
6332 * focus.
6333 *
6334 * See also {@link #focusSearch(int)}, which is what you call to say that you
6335 * have focus, and you want your parent to look for the next one.
6336 *
6337 * @return Whether this view actually took accessibility focus.
6338 *
6339 * @hide
6340 */
6341 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006342 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6343 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006344 return false;
6345 }
6346 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6347 return false;
6348 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006349 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6350 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006351 ViewRootImpl viewRootImpl = getViewRootImpl();
6352 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006353 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006354 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006355 if (mAttachInfo != null) {
6356 Rect rectangle = mAttachInfo.mTmpInvalRect;
6357 getDrawingRect(rectangle);
6358 requestRectangleOnScreen(rectangle);
6359 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006360 invalidate();
6361 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6362 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006363 return true;
6364 }
6365 return false;
6366 }
6367
6368 /**
6369 * Call this to try to clear accessibility focus of this view.
6370 *
6371 * See also {@link #focusSearch(int)}, which is what you call to say that you
6372 * have focus, and you want your parent to look for the next one.
6373 *
6374 * @hide
6375 */
6376 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006377 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6378 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006379 invalidate();
6380 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6381 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006382 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006383 // Clear the global reference of accessibility focus if this
6384 // view or any of its descendants had accessibility focus.
6385 ViewRootImpl viewRootImpl = getViewRootImpl();
6386 if (viewRootImpl != null) {
6387 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6388 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006389 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006390 }
6391 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006392 }
6393
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006394 private void sendAccessibilityHoverEvent(int eventType) {
6395 // Since we are not delivering to a client accessibility events from not
6396 // important views (unless the clinet request that) we need to fire the
6397 // event from the deepest view exposed to the client. As a consequence if
6398 // the user crosses a not exposed view the client will see enter and exit
6399 // of the exposed predecessor followed by and enter and exit of that same
6400 // predecessor when entering and exiting the not exposed descendant. This
6401 // is fine since the client has a clear idea which view is hovered at the
6402 // price of a couple more events being sent. This is a simple and
6403 // working solution.
6404 View source = this;
6405 while (true) {
6406 if (source.includeForAccessibility()) {
6407 source.sendAccessibilityEvent(eventType);
6408 return;
6409 }
6410 ViewParent parent = source.getParent();
6411 if (parent instanceof View) {
6412 source = (View) parent;
6413 } else {
6414 return;
6415 }
6416 }
6417 }
6418
Svetoslav Ganov42138042012-03-20 11:51:39 -07006419 /**
6420 * Clears accessibility focus without calling any callback methods
6421 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6422 * is used for clearing accessibility focus when giving this focus to
6423 * another view.
6424 */
6425 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006426 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6427 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006428 invalidate();
6429 }
6430 }
6431
6432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 * Call this to try to give focus to a specific view or to one of its
6434 * descendants.
6435 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006436 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6437 * false), or if it is focusable and it is not focusable in touch mode
6438 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006440 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006441 * have focus, and you want your parent to look for the next one.
6442 *
6443 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6444 * {@link #FOCUS_DOWN} and <code>null</code>.
6445 *
6446 * @return Whether this view or one of its descendants actually took focus.
6447 */
6448 public final boolean requestFocus() {
6449 return requestFocus(View.FOCUS_DOWN);
6450 }
6451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 /**
6453 * Call this to try to give focus to a specific view or to one of its
6454 * descendants and give it a hint about what direction focus is heading.
6455 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006456 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6457 * false), or if it is focusable and it is not focusable in touch mode
6458 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006460 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006461 * have focus, and you want your parent to look for the next one.
6462 *
6463 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6464 * <code>null</code> set for the previously focused rectangle.
6465 *
6466 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6467 * @return Whether this view or one of its descendants actually took focus.
6468 */
6469 public final boolean requestFocus(int direction) {
6470 return requestFocus(direction, null);
6471 }
6472
6473 /**
6474 * Call this to try to give focus to a specific view or to one of its descendants
6475 * and give it hints about the direction and a specific rectangle that the focus
6476 * is coming from. The rectangle can help give larger views a finer grained hint
6477 * about where focus is coming from, and therefore, where to show selection, or
6478 * forward focus change internally.
6479 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006480 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6481 * false), or if it is focusable and it is not focusable in touch mode
6482 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 *
6484 * A View will not take focus if it is not visible.
6485 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006486 * A View will not take focus if one of its parents has
6487 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6488 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006490 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 * have focus, and you want your parent to look for the next one.
6492 *
6493 * You may wish to override this method if your custom {@link View} has an internal
6494 * {@link View} that it wishes to forward the request to.
6495 *
6496 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6497 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6498 * to give a finer grained hint about where focus is coming from. May be null
6499 * if there is no hint.
6500 * @return Whether this view or one of its descendants actually took focus.
6501 */
6502 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006503 return requestFocusNoSearch(direction, previouslyFocusedRect);
6504 }
6505
6506 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 // need to be focusable
6508 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6509 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6510 return false;
6511 }
6512
6513 // need to be focusable in touch mode if in touch mode
6514 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006515 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6516 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 }
6518
6519 // need to not have any parents blocking us
6520 if (hasAncestorThatBlocksDescendantFocus()) {
6521 return false;
6522 }
6523
6524 handleFocusGainInternal(direction, previouslyFocusedRect);
6525 return true;
6526 }
6527
6528 /**
6529 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6530 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6531 * touch mode to request focus when they are touched.
6532 *
6533 * @return Whether this view or one of its descendants actually took focus.
6534 *
6535 * @see #isInTouchMode()
6536 *
6537 */
6538 public final boolean requestFocusFromTouch() {
6539 // Leave touch mode if we need to
6540 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006541 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006542 if (viewRoot != null) {
6543 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 }
6545 }
6546 return requestFocus(View.FOCUS_DOWN);
6547 }
6548
6549 /**
6550 * @return Whether any ancestor of this view blocks descendant focus.
6551 */
6552 private boolean hasAncestorThatBlocksDescendantFocus() {
6553 ViewParent ancestor = mParent;
6554 while (ancestor instanceof ViewGroup) {
6555 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6556 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6557 return true;
6558 } else {
6559 ancestor = vgAncestor.getParent();
6560 }
6561 }
6562 return false;
6563 }
6564
6565 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006566 * Gets the mode for determining whether this View is important for accessibility
6567 * which is if it fires accessibility events and if it is reported to
6568 * accessibility services that query the screen.
6569 *
6570 * @return The mode for determining whether a View is important for accessibility.
6571 *
6572 * @attr ref android.R.styleable#View_importantForAccessibility
6573 *
6574 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6575 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6576 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6577 */
6578 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006579 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6580 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6581 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006582 })
6583 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006584 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6585 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006586 }
6587
6588 /**
6589 * Sets how to determine whether this view is important for accessibility
6590 * which is if it fires accessibility events and if it is reported to
6591 * accessibility services that query the screen.
6592 *
6593 * @param mode How to determine whether this view is important for accessibility.
6594 *
6595 * @attr ref android.R.styleable#View_importantForAccessibility
6596 *
6597 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6598 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6599 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6600 */
6601 public void setImportantForAccessibility(int mode) {
6602 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006603 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6604 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6605 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006606 notifyAccessibilityStateChanged();
6607 }
6608 }
6609
6610 /**
6611 * Gets whether this view should be exposed for accessibility.
6612 *
6613 * @return Whether the view is exposed for accessibility.
6614 *
6615 * @hide
6616 */
6617 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006618 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6619 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006620 switch (mode) {
6621 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6622 return true;
6623 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6624 return false;
6625 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006626 return isActionableForAccessibility() || hasListenersForAccessibility()
6627 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006628 default:
6629 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6630 + mode);
6631 }
6632 }
6633
6634 /**
6635 * Gets the parent for accessibility purposes. Note that the parent for
6636 * accessibility is not necessary the immediate parent. It is the first
6637 * predecessor that is important for accessibility.
6638 *
6639 * @return The parent for accessibility purposes.
6640 */
6641 public ViewParent getParentForAccessibility() {
6642 if (mParent instanceof View) {
6643 View parentView = (View) mParent;
6644 if (parentView.includeForAccessibility()) {
6645 return mParent;
6646 } else {
6647 return mParent.getParentForAccessibility();
6648 }
6649 }
6650 return null;
6651 }
6652
6653 /**
6654 * Adds the children of a given View for accessibility. Since some Views are
6655 * not important for accessibility the children for accessibility are not
6656 * necessarily direct children of the riew, rather they are the first level of
6657 * descendants important for accessibility.
6658 *
6659 * @param children The list of children for accessibility.
6660 */
6661 public void addChildrenForAccessibility(ArrayList<View> children) {
6662 if (includeForAccessibility()) {
6663 children.add(this);
6664 }
6665 }
6666
6667 /**
6668 * Whether to regard this view for accessibility. A view is regarded for
6669 * accessibility if it is important for accessibility or the querying
6670 * accessibility service has explicitly requested that view not
6671 * important for accessibility are regarded.
6672 *
6673 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006674 *
6675 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006676 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006677 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006678 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006679 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006680 }
6681 return false;
6682 }
6683
6684 /**
6685 * Returns whether the View is considered actionable from
6686 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006687 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006688 *
6689 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006690 *
6691 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006692 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006693 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006694 return (isClickable() || isLongClickable() || isFocusable());
6695 }
6696
6697 /**
6698 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006699 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006700 *
6701 * @return True if the view is actionable for accessibility.
6702 */
6703 private boolean hasListenersForAccessibility() {
6704 ListenerInfo info = getListenerInfo();
6705 return mTouchDelegate != null || info.mOnKeyListener != null
6706 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6707 || info.mOnHoverListener != null || info.mOnDragListener != null;
6708 }
6709
6710 /**
6711 * Notifies accessibility services that some view's important for
6712 * accessibility state has changed. Note that such notifications
6713 * are made at most once every
6714 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6715 * to avoid unnecessary load to the system. Also once a view has
6716 * made a notifucation this method is a NOP until the notification has
6717 * been sent to clients.
6718 *
6719 * @hide
6720 *
6721 * TODO: Makse sure this method is called for any view state change
6722 * that is interesting for accessilility purposes.
6723 */
6724 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006725 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6726 return;
6727 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006728 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6729 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006730 if (mParent != null) {
6731 mParent.childAccessibilityStateChanged(this);
6732 }
6733 }
6734 }
6735
6736 /**
6737 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006738 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006739 *
6740 * @hide
6741 */
6742 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006743 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006744 }
6745
6746 /**
6747 * Performs the specified accessibility action on the view. For
6748 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006749 * <p>
6750 * If an {@link AccessibilityDelegate} has been specified via calling
6751 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6752 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6753 * is responsible for handling this call.
6754 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006755 *
6756 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006757 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006758 * @return Whether the action was performed.
6759 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006760 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006761 if (mAccessibilityDelegate != null) {
6762 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6763 } else {
6764 return performAccessibilityActionInternal(action, arguments);
6765 }
6766 }
6767
6768 /**
6769 * @see #performAccessibilityAction(int, Bundle)
6770 *
6771 * Note: Called from the default {@link AccessibilityDelegate}.
6772 */
6773 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006774 switch (action) {
6775 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006776 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006777 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006778 }
6779 } break;
6780 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6781 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006782 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006783 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006784 } break;
6785 case AccessibilityNodeInfo.ACTION_FOCUS: {
6786 if (!hasFocus()) {
6787 // Get out of touch mode since accessibility
6788 // wants to move focus around.
6789 getViewRootImpl().ensureTouchMode(false);
6790 return requestFocus();
6791 }
6792 } break;
6793 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6794 if (hasFocus()) {
6795 clearFocus();
6796 return !isFocused();
6797 }
6798 } break;
6799 case AccessibilityNodeInfo.ACTION_SELECT: {
6800 if (!isSelected()) {
6801 setSelected(true);
6802 return isSelected();
6803 }
6804 } break;
6805 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6806 if (isSelected()) {
6807 setSelected(false);
6808 return !isSelected();
6809 }
6810 } break;
6811 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006812 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006813 return requestAccessibilityFocus();
6814 }
6815 } break;
6816 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6817 if (isAccessibilityFocused()) {
6818 clearAccessibilityFocus();
6819 return true;
6820 }
6821 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006822 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6823 if (arguments != null) {
6824 final int granularity = arguments.getInt(
6825 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6826 return nextAtGranularity(granularity);
6827 }
6828 } break;
6829 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6830 if (arguments != null) {
6831 final int granularity = arguments.getInt(
6832 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6833 return previousAtGranularity(granularity);
6834 }
6835 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006836 }
6837 return false;
6838 }
6839
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006840 private boolean nextAtGranularity(int granularity) {
6841 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006842 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006843 return false;
6844 }
6845 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6846 if (iterator == null) {
6847 return false;
6848 }
6849 final int current = getAccessibilityCursorPosition();
6850 final int[] range = iterator.following(current);
6851 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006852 return false;
6853 }
6854 final int start = range[0];
6855 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006856 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006857 sendViewTextTraversedAtGranularityEvent(
6858 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6859 granularity, start, end);
6860 return true;
6861 }
6862
6863 private boolean previousAtGranularity(int granularity) {
6864 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006865 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006866 return false;
6867 }
6868 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6869 if (iterator == null) {
6870 return false;
6871 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006872 int current = getAccessibilityCursorPosition();
6873 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6874 current = text.length();
6875 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6876 // When traversing by character we always put the cursor after the character
6877 // to ease edit and have to compensate before asking the for previous segment.
6878 current--;
6879 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006880 final int[] range = iterator.preceding(current);
6881 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006882 return false;
6883 }
6884 final int start = range[0];
6885 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006886 // Always put the cursor after the character to ease edit.
6887 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6888 setAccessibilityCursorPosition(end);
6889 } else {
6890 setAccessibilityCursorPosition(start);
6891 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006892 sendViewTextTraversedAtGranularityEvent(
6893 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6894 granularity, start, end);
6895 return true;
6896 }
6897
6898 /**
6899 * Gets the text reported for accessibility purposes.
6900 *
6901 * @return The accessibility text.
6902 *
6903 * @hide
6904 */
6905 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07006906 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006907 }
6908
6909 /**
6910 * @hide
6911 */
6912 public int getAccessibilityCursorPosition() {
6913 return mAccessibilityCursorPosition;
6914 }
6915
6916 /**
6917 * @hide
6918 */
6919 public void setAccessibilityCursorPosition(int position) {
6920 mAccessibilityCursorPosition = position;
6921 }
6922
6923 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6924 int fromIndex, int toIndex) {
6925 if (mParent == null) {
6926 return;
6927 }
6928 AccessibilityEvent event = AccessibilityEvent.obtain(
6929 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6930 onInitializeAccessibilityEvent(event);
6931 onPopulateAccessibilityEvent(event);
6932 event.setFromIndex(fromIndex);
6933 event.setToIndex(toIndex);
6934 event.setAction(action);
6935 event.setMovementGranularity(granularity);
6936 mParent.requestSendAccessibilityEvent(this, event);
6937 }
6938
6939 /**
6940 * @hide
6941 */
6942 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6943 switch (granularity) {
6944 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6945 CharSequence text = getIterableTextForAccessibility();
6946 if (text != null && text.length() > 0) {
6947 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006948 CharacterTextSegmentIterator.getInstance(
6949 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006950 iterator.initialize(text.toString());
6951 return iterator;
6952 }
6953 } break;
6954 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6955 CharSequence text = getIterableTextForAccessibility();
6956 if (text != null && text.length() > 0) {
6957 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006958 WordTextSegmentIterator.getInstance(
6959 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006960 iterator.initialize(text.toString());
6961 return iterator;
6962 }
6963 } break;
6964 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6965 CharSequence text = getIterableTextForAccessibility();
6966 if (text != null && text.length() > 0) {
6967 ParagraphTextSegmentIterator iterator =
6968 ParagraphTextSegmentIterator.getInstance();
6969 iterator.initialize(text.toString());
6970 return iterator;
6971 }
6972 } break;
6973 }
6974 return null;
6975 }
6976
Svetoslav Ganov42138042012-03-20 11:51:39 -07006977 /**
Romain Guya440b002010-02-24 15:57:54 -08006978 * @hide
6979 */
6980 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006981 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006982 clearDisplayList();
6983
Romain Guya440b002010-02-24 15:57:54 -08006984 onStartTemporaryDetach();
6985 }
6986
6987 /**
6988 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6990 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006991 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006992 */
6993 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006994 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006995 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006996 }
6997
6998 /**
6999 * @hide
7000 */
7001 public void dispatchFinishTemporaryDetach() {
7002 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 }
Romain Guy8506ab42009-06-11 17:35:47 -07007004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 /**
7006 * Called after {@link #onStartTemporaryDetach} when the container is done
7007 * changing the view.
7008 */
7009 public void onFinishTemporaryDetach() {
7010 }
Romain Guy8506ab42009-06-11 17:35:47 -07007011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007013 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7014 * for this view's window. Returns null if the view is not currently attached
7015 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007016 * just use the standard high-level event callbacks like
7017 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007018 */
7019 public KeyEvent.DispatcherState getKeyDispatcherState() {
7020 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7021 }
Joe Malin32736f02011-01-19 16:14:20 -08007022
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007024 * Dispatch a key event before it is processed by any input method
7025 * associated with the view hierarchy. This can be used to intercept
7026 * key events in special situations before the IME consumes them; a
7027 * typical example would be handling the BACK key to update the application's
7028 * UI instead of allowing the IME to see it and close itself.
7029 *
7030 * @param event The key event to be dispatched.
7031 * @return True if the event was handled, false otherwise.
7032 */
7033 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7034 return onKeyPreIme(event.getKeyCode(), event);
7035 }
7036
7037 /**
7038 * Dispatch a key event to the next view on the focus path. This path runs
7039 * from the top of the view tree down to the currently focused view. If this
7040 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7041 * the next node down the focus path. This method also fires any key
7042 * listeners.
7043 *
7044 * @param event The key event to be dispatched.
7045 * @return True if the event was handled, false otherwise.
7046 */
7047 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007048 if (mInputEventConsistencyVerifier != null) {
7049 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051
Jeff Brown21bc5c92011-02-28 18:27:14 -08007052 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007053 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007054 ListenerInfo li = mListenerInfo;
7055 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7056 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057 return true;
7058 }
7059
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007060 if (event.dispatch(this, mAttachInfo != null
7061 ? mAttachInfo.mKeyDispatchState : null, this)) {
7062 return true;
7063 }
7064
7065 if (mInputEventConsistencyVerifier != null) {
7066 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7067 }
7068 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 }
7070
7071 /**
7072 * Dispatches a key shortcut event.
7073 *
7074 * @param event The key event to be dispatched.
7075 * @return True if the event was handled by the view, false otherwise.
7076 */
7077 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7078 return onKeyShortcut(event.getKeyCode(), event);
7079 }
7080
7081 /**
7082 * Pass the touch screen motion event down to the target view, or this
7083 * view if it is the target.
7084 *
7085 * @param event The motion event to be dispatched.
7086 * @return True if the event was handled by the view, false otherwise.
7087 */
7088 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007089 if (mInputEventConsistencyVerifier != null) {
7090 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7091 }
7092
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007093 if (onFilterTouchEventForSecurity(event)) {
7094 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007095 ListenerInfo li = mListenerInfo;
7096 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7097 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007098 return true;
7099 }
7100
7101 if (onTouchEvent(event)) {
7102 return true;
7103 }
Jeff Brown85a31762010-09-01 17:01:00 -07007104 }
7105
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007106 if (mInputEventConsistencyVerifier != null) {
7107 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007109 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 }
7111
7112 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007113 * Filter the touch event to apply security policies.
7114 *
7115 * @param event The motion event to be filtered.
7116 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007117 *
Jeff Brown85a31762010-09-01 17:01:00 -07007118 * @see #getFilterTouchesWhenObscured
7119 */
7120 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007121 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007122 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7123 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7124 // Window is obscured, drop this touch.
7125 return false;
7126 }
7127 return true;
7128 }
7129
7130 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007131 * Pass a trackball motion event down to the focused view.
7132 *
7133 * @param event The motion event to be dispatched.
7134 * @return True if the event was handled by the view, false otherwise.
7135 */
7136 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007137 if (mInputEventConsistencyVerifier != null) {
7138 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7139 }
7140
Romain Guy02ccac62011-06-24 13:20:23 -07007141 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 }
7143
7144 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007145 * Dispatch a generic motion event.
7146 * <p>
7147 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7148 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007149 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007150 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007151 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007152 *
7153 * @param event The motion event to be dispatched.
7154 * @return True if the event was handled by the view, false otherwise.
7155 */
7156 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007157 if (mInputEventConsistencyVerifier != null) {
7158 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7159 }
7160
Jeff Browna032cc02011-03-07 16:56:21 -08007161 final int source = event.getSource();
7162 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7163 final int action = event.getAction();
7164 if (action == MotionEvent.ACTION_HOVER_ENTER
7165 || action == MotionEvent.ACTION_HOVER_MOVE
7166 || action == MotionEvent.ACTION_HOVER_EXIT) {
7167 if (dispatchHoverEvent(event)) {
7168 return true;
7169 }
7170 } else if (dispatchGenericPointerEvent(event)) {
7171 return true;
7172 }
7173 } else if (dispatchGenericFocusedEvent(event)) {
7174 return true;
7175 }
7176
Jeff Brown10b62902011-06-20 16:40:37 -07007177 if (dispatchGenericMotionEventInternal(event)) {
7178 return true;
7179 }
7180
7181 if (mInputEventConsistencyVerifier != null) {
7182 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7183 }
7184 return false;
7185 }
7186
7187 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007188 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007189 ListenerInfo li = mListenerInfo;
7190 if (li != null && li.mOnGenericMotionListener != null
7191 && (mViewFlags & ENABLED_MASK) == ENABLED
7192 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007193 return true;
7194 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007195
7196 if (onGenericMotionEvent(event)) {
7197 return true;
7198 }
7199
7200 if (mInputEventConsistencyVerifier != null) {
7201 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7202 }
7203 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007204 }
7205
7206 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007207 * Dispatch a hover event.
7208 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007209 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007210 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007211 * </p>
7212 *
7213 * @param event The motion event to be dispatched.
7214 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007215 */
7216 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007217 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007218 ListenerInfo li = mListenerInfo;
7219 if (li != null && li.mOnHoverListener != null
7220 && (mViewFlags & ENABLED_MASK) == ENABLED
7221 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007222 return true;
7223 }
7224
Jeff Browna032cc02011-03-07 16:56:21 -08007225 return onHoverEvent(event);
7226 }
7227
7228 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007229 * Returns true if the view has a child to which it has recently sent
7230 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7231 * it does not have a hovered child, then it must be the innermost hovered view.
7232 * @hide
7233 */
7234 protected boolean hasHoveredChild() {
7235 return false;
7236 }
7237
7238 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007239 * Dispatch a generic motion event to the view under the first pointer.
7240 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007241 * Do not call this method directly.
7242 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007243 * </p>
7244 *
7245 * @param event The motion event to be dispatched.
7246 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007247 */
7248 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7249 return false;
7250 }
7251
7252 /**
7253 * Dispatch a generic motion event to the currently focused view.
7254 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007255 * Do not call this method directly.
7256 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007257 * </p>
7258 *
7259 * @param event The motion event to be dispatched.
7260 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007261 */
7262 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7263 return false;
7264 }
7265
7266 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007267 * Dispatch a pointer event.
7268 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007269 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7270 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7271 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007272 * and should not be expected to handle other pointing device features.
7273 * </p>
7274 *
7275 * @param event The motion event to be dispatched.
7276 * @return True if the event was handled by the view, false otherwise.
7277 * @hide
7278 */
7279 public final boolean dispatchPointerEvent(MotionEvent event) {
7280 if (event.isTouchEvent()) {
7281 return dispatchTouchEvent(event);
7282 } else {
7283 return dispatchGenericMotionEvent(event);
7284 }
7285 }
7286
7287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 * Called when the window containing this view gains or loses window focus.
7289 * ViewGroups should override to route to their children.
7290 *
7291 * @param hasFocus True if the window containing this view now has focus,
7292 * false otherwise.
7293 */
7294 public void dispatchWindowFocusChanged(boolean hasFocus) {
7295 onWindowFocusChanged(hasFocus);
7296 }
7297
7298 /**
7299 * Called when the window containing this view gains or loses focus. Note
7300 * that this is separate from view focus: to receive key events, both
7301 * your view and its window must have focus. If a window is displayed
7302 * on top of yours that takes input focus, then your own window will lose
7303 * focus but the view focus will remain unchanged.
7304 *
7305 * @param hasWindowFocus True if the window containing this view now has
7306 * focus, false otherwise.
7307 */
7308 public void onWindowFocusChanged(boolean hasWindowFocus) {
7309 InputMethodManager imm = InputMethodManager.peekInstance();
7310 if (!hasWindowFocus) {
7311 if (isPressed()) {
7312 setPressed(false);
7313 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007314 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 imm.focusOut(this);
7316 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007317 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007318 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007319 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007320 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 imm.focusIn(this);
7322 }
7323 refreshDrawableState();
7324 }
7325
7326 /**
7327 * Returns true if this view is in a window that currently has window focus.
7328 * Note that this is not the same as the view itself having focus.
7329 *
7330 * @return True if this view is in a window that currently has window focus.
7331 */
7332 public boolean hasWindowFocus() {
7333 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7334 }
7335
7336 /**
Adam Powell326d8082009-12-09 15:10:07 -08007337 * Dispatch a view visibility change down the view hierarchy.
7338 * ViewGroups should override to route to their children.
7339 * @param changedView The view whose visibility changed. Could be 'this' or
7340 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007341 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7342 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007343 */
7344 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7345 onVisibilityChanged(changedView, visibility);
7346 }
7347
7348 /**
7349 * Called when the visibility of the view or an ancestor of the view is changed.
7350 * @param changedView The view whose visibility changed. Could be 'this' or
7351 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007352 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7353 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007354 */
7355 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007356 if (visibility == VISIBLE) {
7357 if (mAttachInfo != null) {
7358 initialAwakenScrollBars();
7359 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007360 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007361 }
7362 }
Adam Powell326d8082009-12-09 15:10:07 -08007363 }
7364
7365 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007366 * Dispatch a hint about whether this view is displayed. For instance, when
7367 * a View moves out of the screen, it might receives a display hint indicating
7368 * the view is not displayed. Applications should not <em>rely</em> on this hint
7369 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007370 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007371 * @param hint A hint about whether or not this view is displayed:
7372 * {@link #VISIBLE} or {@link #INVISIBLE}.
7373 */
7374 public void dispatchDisplayHint(int hint) {
7375 onDisplayHint(hint);
7376 }
7377
7378 /**
7379 * Gives this view a hint about whether is displayed or not. For instance, when
7380 * a View moves out of the screen, it might receives a display hint indicating
7381 * the view is not displayed. Applications should not <em>rely</em> on this hint
7382 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007383 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007384 * @param hint A hint about whether or not this view is displayed:
7385 * {@link #VISIBLE} or {@link #INVISIBLE}.
7386 */
7387 protected void onDisplayHint(int hint) {
7388 }
7389
7390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 * Dispatch a window visibility change down the view hierarchy.
7392 * ViewGroups should override to route to their children.
7393 *
7394 * @param visibility The new visibility of the window.
7395 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007396 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 */
7398 public void dispatchWindowVisibilityChanged(int visibility) {
7399 onWindowVisibilityChanged(visibility);
7400 }
7401
7402 /**
7403 * Called when the window containing has change its visibility
7404 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7405 * that this tells you whether or not your window is being made visible
7406 * to the window manager; this does <em>not</em> tell you whether or not
7407 * your window is obscured by other windows on the screen, even if it
7408 * is itself visible.
7409 *
7410 * @param visibility The new visibility of the window.
7411 */
7412 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007413 if (visibility == VISIBLE) {
7414 initialAwakenScrollBars();
7415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 }
7417
7418 /**
7419 * Returns the current visibility of the window this view is attached to
7420 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7421 *
7422 * @return Returns the current visibility of the view's window.
7423 */
7424 public int getWindowVisibility() {
7425 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7426 }
7427
7428 /**
7429 * Retrieve the overall visible display size in which the window this view is
7430 * attached to has been positioned in. This takes into account screen
7431 * decorations above the window, for both cases where the window itself
7432 * is being position inside of them or the window is being placed under
7433 * then and covered insets are used for the window to position its content
7434 * inside. In effect, this tells you the available area where content can
7435 * be placed and remain visible to users.
7436 *
7437 * <p>This function requires an IPC back to the window manager to retrieve
7438 * the requested information, so should not be used in performance critical
7439 * code like drawing.
7440 *
7441 * @param outRect Filled in with the visible display frame. If the view
7442 * is not attached to a window, this is simply the raw display size.
7443 */
7444 public void getWindowVisibleDisplayFrame(Rect outRect) {
7445 if (mAttachInfo != null) {
7446 try {
7447 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7448 } catch (RemoteException e) {
7449 return;
7450 }
7451 // XXX This is really broken, and probably all needs to be done
7452 // in the window manager, and we need to know more about whether
7453 // we want the area behind or in front of the IME.
7454 final Rect insets = mAttachInfo.mVisibleInsets;
7455 outRect.left += insets.left;
7456 outRect.top += insets.top;
7457 outRect.right -= insets.right;
7458 outRect.bottom -= insets.bottom;
7459 return;
7460 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007461 // The view is not attached to a display so we don't have a context.
7462 // Make a best guess about the display size.
7463 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007464 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 }
7466
7467 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007468 * Dispatch a notification about a resource configuration change down
7469 * the view hierarchy.
7470 * ViewGroups should override to route to their children.
7471 *
7472 * @param newConfig The new resource configuration.
7473 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007474 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007475 */
7476 public void dispatchConfigurationChanged(Configuration newConfig) {
7477 onConfigurationChanged(newConfig);
7478 }
7479
7480 /**
7481 * Called when the current configuration of the resources being used
7482 * by the application have changed. You can use this to decide when
7483 * to reload resources that can changed based on orientation and other
7484 * configuration characterstics. You only need to use this if you are
7485 * not relying on the normal {@link android.app.Activity} mechanism of
7486 * recreating the activity instance upon a configuration change.
7487 *
7488 * @param newConfig The new resource configuration.
7489 */
7490 protected void onConfigurationChanged(Configuration newConfig) {
7491 }
7492
7493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 * Private function to aggregate all per-view attributes in to the view
7495 * root.
7496 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007497 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7498 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 }
7500
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007501 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7502 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007503 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007504 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007505 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007506 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007507 ListenerInfo li = mListenerInfo;
7508 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007509 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 }
7512 }
7513
7514 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007515 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007516 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007517 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7518 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 ai.mRecomputeGlobalAttributes = true;
7520 }
7521 }
7522 }
7523
7524 /**
7525 * Returns whether the device is currently in touch mode. Touch mode is entered
7526 * once the user begins interacting with the device by touch, and affects various
7527 * things like whether focus is always visible to the user.
7528 *
7529 * @return Whether the device is in touch mode.
7530 */
7531 @ViewDebug.ExportedProperty
7532 public boolean isInTouchMode() {
7533 if (mAttachInfo != null) {
7534 return mAttachInfo.mInTouchMode;
7535 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007536 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537 }
7538 }
7539
7540 /**
7541 * Returns the context the view is running in, through which it can
7542 * access the current theme, resources, etc.
7543 *
7544 * @return The view's Context.
7545 */
7546 @ViewDebug.CapturedViewProperty
7547 public final Context getContext() {
7548 return mContext;
7549 }
7550
7551 /**
7552 * Handle a key event before it is processed by any input method
7553 * associated with the view hierarchy. This can be used to intercept
7554 * key events in special situations before the IME consumes them; a
7555 * typical example would be handling the BACK key to update the application's
7556 * UI instead of allowing the IME to see it and close itself.
7557 *
7558 * @param keyCode The value in event.getKeyCode().
7559 * @param event Description of the key event.
7560 * @return If you handled the event, return true. If you want to allow the
7561 * event to be handled by the next receiver, return false.
7562 */
7563 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7564 return false;
7565 }
7566
7567 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007568 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7569 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7571 * is released, if the view is enabled and clickable.
7572 *
Jean Chalard405bc512012-05-29 19:12:34 +09007573 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7574 * although some may elect to do so in some situations. Do not rely on this to
7575 * catch software key presses.
7576 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 * @param keyCode A key code that represents the button pressed, from
7578 * {@link android.view.KeyEvent}.
7579 * @param event The KeyEvent object that defines the button action.
7580 */
7581 public boolean onKeyDown(int keyCode, KeyEvent event) {
7582 boolean result = false;
7583
7584 switch (keyCode) {
7585 case KeyEvent.KEYCODE_DPAD_CENTER:
7586 case KeyEvent.KEYCODE_ENTER: {
7587 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7588 return true;
7589 }
7590 // Long clickable items don't necessarily have to be clickable
7591 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7592 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7593 (event.getRepeatCount() == 0)) {
7594 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007595 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 return true;
7597 }
7598 break;
7599 }
7600 }
7601 return result;
7602 }
7603
7604 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007605 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7606 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7607 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007608 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7609 * although some may elect to do so in some situations. Do not rely on this to
7610 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007611 */
7612 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7613 return false;
7614 }
7615
7616 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007617 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7618 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7620 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007621 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7622 * although some may elect to do so in some situations. Do not rely on this to
7623 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 *
7625 * @param keyCode A key code that represents the button pressed, from
7626 * {@link android.view.KeyEvent}.
7627 * @param event The KeyEvent object that defines the button action.
7628 */
7629 public boolean onKeyUp(int keyCode, KeyEvent event) {
7630 boolean result = false;
7631
7632 switch (keyCode) {
7633 case KeyEvent.KEYCODE_DPAD_CENTER:
7634 case KeyEvent.KEYCODE_ENTER: {
7635 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7636 return true;
7637 }
7638 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7639 setPressed(false);
7640
7641 if (!mHasPerformedLongPress) {
7642 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007643 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007644
7645 result = performClick();
7646 }
7647 }
7648 break;
7649 }
7650 }
7651 return result;
7652 }
7653
7654 /**
7655 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7656 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7657 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007658 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7659 * although some may elect to do so in some situations. Do not rely on this to
7660 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 *
7662 * @param keyCode A key code that represents the button pressed, from
7663 * {@link android.view.KeyEvent}.
7664 * @param repeatCount The number of times the action was made.
7665 * @param event The KeyEvent object that defines the button action.
7666 */
7667 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7668 return false;
7669 }
7670
7671 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007672 * Called on the focused view when a key shortcut event is not handled.
7673 * Override this method to implement local key shortcuts for the View.
7674 * Key shortcuts can also be implemented by setting the
7675 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 *
7677 * @param keyCode The value in event.getKeyCode().
7678 * @param event Description of the key event.
7679 * @return If you handled the event, return true. If you want to allow the
7680 * event to be handled by the next receiver, return false.
7681 */
7682 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7683 return false;
7684 }
7685
7686 /**
7687 * Check whether the called view is a text editor, in which case it
7688 * would make sense to automatically display a soft input window for
7689 * it. Subclasses should override this if they implement
7690 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007691 * a call on that method would return a non-null InputConnection, and
7692 * they are really a first-class editor that the user would normally
7693 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007694 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007695 * <p>The default implementation always returns false. This does
7696 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7697 * will not be called or the user can not otherwise perform edits on your
7698 * view; it is just a hint to the system that this is not the primary
7699 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007700 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007701 * @return Returns true if this view is a text editor, else false.
7702 */
7703 public boolean onCheckIsTextEditor() {
7704 return false;
7705 }
Romain Guy8506ab42009-06-11 17:35:47 -07007706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 /**
7708 * Create a new InputConnection for an InputMethod to interact
7709 * with the view. The default implementation returns null, since it doesn't
7710 * support input methods. You can override this to implement such support.
7711 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 * <p>When implementing this, you probably also want to implement
7714 * {@link #onCheckIsTextEditor()} to indicate you will return a
7715 * non-null InputConnection.
7716 *
7717 * @param outAttrs Fill in with attribute information about the connection.
7718 */
7719 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7720 return null;
7721 }
7722
7723 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007724 * Called by the {@link android.view.inputmethod.InputMethodManager}
7725 * when a view who is not the current
7726 * input connection target is trying to make a call on the manager. The
7727 * default implementation returns false; you can override this to return
7728 * true for certain views if you are performing InputConnection proxying
7729 * to them.
7730 * @param view The View that is making the InputMethodManager call.
7731 * @return Return true to allow the call, false to reject.
7732 */
7733 public boolean checkInputConnectionProxy(View view) {
7734 return false;
7735 }
Romain Guy8506ab42009-06-11 17:35:47 -07007736
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 * Show the context menu for this view. It is not safe to hold on to the
7739 * menu after returning from this method.
7740 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007741 * You should normally not overload this method. Overload
7742 * {@link #onCreateContextMenu(ContextMenu)} or define an
7743 * {@link OnCreateContextMenuListener} to add items to the context menu.
7744 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 * @param menu The context menu to populate
7746 */
7747 public void createContextMenu(ContextMenu menu) {
7748 ContextMenuInfo menuInfo = getContextMenuInfo();
7749
7750 // Sets the current menu info so all items added to menu will have
7751 // my extra info set.
7752 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7753
7754 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007755 ListenerInfo li = mListenerInfo;
7756 if (li != null && li.mOnCreateContextMenuListener != null) {
7757 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 }
7759
7760 // Clear the extra information so subsequent items that aren't mine don't
7761 // have my extra info.
7762 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7763
7764 if (mParent != null) {
7765 mParent.createContextMenu(menu);
7766 }
7767 }
7768
7769 /**
7770 * Views should implement this if they have extra information to associate
7771 * with the context menu. The return result is supplied as a parameter to
7772 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7773 * callback.
7774 *
7775 * @return Extra information about the item for which the context menu
7776 * should be shown. This information will vary across different
7777 * subclasses of View.
7778 */
7779 protected ContextMenuInfo getContextMenuInfo() {
7780 return null;
7781 }
7782
7783 /**
7784 * Views should implement this if the view itself is going to add items to
7785 * the context menu.
7786 *
7787 * @param menu the context menu to populate
7788 */
7789 protected void onCreateContextMenu(ContextMenu menu) {
7790 }
7791
7792 /**
7793 * Implement this method to handle trackball motion events. The
7794 * <em>relative</em> movement of the trackball since the last event
7795 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7796 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7797 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7798 * they will often be fractional values, representing the more fine-grained
7799 * movement information available from a trackball).
7800 *
7801 * @param event The motion event.
7802 * @return True if the event was handled, false otherwise.
7803 */
7804 public boolean onTrackballEvent(MotionEvent event) {
7805 return false;
7806 }
7807
7808 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007809 * Implement this method to handle generic motion events.
7810 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007811 * Generic motion events describe joystick movements, mouse hovers, track pad
7812 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007813 * {@link MotionEvent#getSource() source} of the motion event specifies
7814 * the class of input that was received. Implementations of this method
7815 * must examine the bits in the source before processing the event.
7816 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007817 * </p><p>
7818 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7819 * are delivered to the view under the pointer. All other generic motion events are
7820 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007821 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007822 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007823 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007824 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7825 * // process the joystick movement...
7826 * return true;
7827 * }
7828 * }
7829 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7830 * switch (event.getAction()) {
7831 * case MotionEvent.ACTION_HOVER_MOVE:
7832 * // process the mouse hover movement...
7833 * return true;
7834 * case MotionEvent.ACTION_SCROLL:
7835 * // process the scroll wheel movement...
7836 * return true;
7837 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007838 * }
7839 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007840 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007841 *
7842 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007843 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007844 */
7845 public boolean onGenericMotionEvent(MotionEvent event) {
7846 return false;
7847 }
7848
7849 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007850 * Implement this method to handle hover events.
7851 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007852 * This method is called whenever a pointer is hovering into, over, or out of the
7853 * bounds of a view and the view is not currently being touched.
7854 * Hover events are represented as pointer events with action
7855 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7856 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7857 * </p>
7858 * <ul>
7859 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7860 * when the pointer enters the bounds of the view.</li>
7861 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7862 * when the pointer has already entered the bounds of the view and has moved.</li>
7863 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7864 * when the pointer has exited the bounds of the view or when the pointer is
7865 * about to go down due to a button click, tap, or similar user action that
7866 * causes the view to be touched.</li>
7867 * </ul>
7868 * <p>
7869 * The view should implement this method to return true to indicate that it is
7870 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007871 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007872 * The default implementation calls {@link #setHovered} to update the hovered state
7873 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007874 * is enabled and is clickable. The default implementation also sends hover
7875 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007876 * </p>
7877 *
7878 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007879 * @return True if the view handled the hover event.
7880 *
7881 * @see #isHovered
7882 * @see #setHovered
7883 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007884 */
7885 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007886 // The root view may receive hover (or touch) events that are outside the bounds of
7887 // the window. This code ensures that we only send accessibility events for
7888 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007889 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007890 if (!mSendingHoverAccessibilityEvents) {
7891 if ((action == MotionEvent.ACTION_HOVER_ENTER
7892 || action == MotionEvent.ACTION_HOVER_MOVE)
7893 && !hasHoveredChild()
7894 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007895 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007896 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007897 }
7898 } else {
7899 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007900 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007901 && !pointInView(event.getX(), event.getY()))) {
7902 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007903 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007904 // If the window does not have input focus we take away accessibility
7905 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007906 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007907 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007908 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007909 }
Jeff Browna1b24182011-07-28 13:38:24 -07007910 }
7911
Jeff Brown87b7f802011-06-21 18:35:45 -07007912 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007913 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007914 case MotionEvent.ACTION_HOVER_ENTER:
7915 setHovered(true);
7916 break;
7917 case MotionEvent.ACTION_HOVER_EXIT:
7918 setHovered(false);
7919 break;
7920 }
Jeff Browna1b24182011-07-28 13:38:24 -07007921
7922 // Dispatch the event to onGenericMotionEvent before returning true.
7923 // This is to provide compatibility with existing applications that
7924 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7925 // break because of the new default handling for hoverable views
7926 // in onHoverEvent.
7927 // Note that onGenericMotionEvent will be called by default when
7928 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7929 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007930 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007931 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007932
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007933 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007934 }
7935
7936 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007937 * Returns true if the view should handle {@link #onHoverEvent}
7938 * by calling {@link #setHovered} to change its hovered state.
7939 *
7940 * @return True if the view is hoverable.
7941 */
7942 private boolean isHoverable() {
7943 final int viewFlags = mViewFlags;
7944 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7945 return false;
7946 }
7947
7948 return (viewFlags & CLICKABLE) == CLICKABLE
7949 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7950 }
7951
7952 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007953 * Returns true if the view is currently hovered.
7954 *
7955 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007956 *
7957 * @see #setHovered
7958 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007959 */
Jeff Brown10b62902011-06-20 16:40:37 -07007960 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007961 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007962 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007963 }
7964
7965 /**
7966 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007967 * <p>
7968 * Calling this method also changes the drawable state of the view. This
7969 * enables the view to react to hover by using different drawable resources
7970 * to change its appearance.
7971 * </p><p>
7972 * The {@link #onHoverChanged} method is called when the hovered state changes.
7973 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007974 *
7975 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007976 *
7977 * @see #isHovered
7978 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007979 */
7980 public void setHovered(boolean hovered) {
7981 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007982 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7983 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007984 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007985 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007986 }
7987 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007988 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7989 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007990 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007991 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007992 }
7993 }
7994 }
7995
7996 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007997 * Implement this method to handle hover state changes.
7998 * <p>
7999 * This method is called whenever the hover state changes as a result of a
8000 * call to {@link #setHovered}.
8001 * </p>
8002 *
8003 * @param hovered The current hover state, as returned by {@link #isHovered}.
8004 *
8005 * @see #isHovered
8006 * @see #setHovered
8007 */
8008 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008009 }
8010
8011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008012 * Implement this method to handle touch screen motion events.
8013 *
8014 * @param event The motion event.
8015 * @return True if the event was handled, false otherwise.
8016 */
8017 public boolean onTouchEvent(MotionEvent event) {
8018 final int viewFlags = mViewFlags;
8019
8020 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008021 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008022 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 // A disabled view that is clickable still consumes the touch
8025 // events, it just doesn't respond to them.
8026 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8027 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8028 }
8029
8030 if (mTouchDelegate != null) {
8031 if (mTouchDelegate.onTouchEvent(event)) {
8032 return true;
8033 }
8034 }
8035
8036 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8037 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8038 switch (event.getAction()) {
8039 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008040 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8041 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 // take focus if we don't have it already and we should in
8043 // touch mode.
8044 boolean focusTaken = false;
8045 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8046 focusTaken = requestFocus();
8047 }
8048
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008049 if (prepressed) {
8050 // The button is being released before we actually
8051 // showed it as pressed. Make it show the pressed
8052 // state now (before scheduling the click) to ensure
8053 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008054 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008055 }
Joe Malin32736f02011-01-19 16:14:20 -08008056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008057 if (!mHasPerformedLongPress) {
8058 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008059 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060
8061 // Only perform take click actions if we were in the pressed state
8062 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008063 // Use a Runnable and post this rather than calling
8064 // performClick directly. This lets other visual state
8065 // of the view update before click actions start.
8066 if (mPerformClick == null) {
8067 mPerformClick = new PerformClick();
8068 }
8069 if (!post(mPerformClick)) {
8070 performClick();
8071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 }
8073 }
8074
8075 if (mUnsetPressedState == null) {
8076 mUnsetPressedState = new UnsetPressedState();
8077 }
8078
Adam Powelle14579b2009-12-16 18:39:52 -08008079 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008080 postDelayed(mUnsetPressedState,
8081 ViewConfiguration.getPressedStateDuration());
8082 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 // If the post failed, unpress right now
8084 mUnsetPressedState.run();
8085 }
Adam Powelle14579b2009-12-16 18:39:52 -08008086 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 }
8088 break;
8089
8090 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008091 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008092
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008093 if (performButtonActionOnTouchDown(event)) {
8094 break;
8095 }
8096
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008097 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008098 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008099
8100 // For views inside a scrolling container, delay the pressed feedback for
8101 // a short period in case this is a scroll.
8102 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008103 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008104 if (mPendingCheckForTap == null) {
8105 mPendingCheckForTap = new CheckForTap();
8106 }
8107 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8108 } else {
8109 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008110 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008111 checkForLongClick(0);
8112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 break;
8114
8115 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008116 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008117 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 break;
8119
8120 case MotionEvent.ACTION_MOVE:
8121 final int x = (int) event.getX();
8122 final int y = (int) event.getY();
8123
8124 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008125 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008127 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008128 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008129 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008130 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131
Adam Powell4d6f0662012-02-21 15:11:11 -08008132 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 }
8135 break;
8136 }
8137 return true;
8138 }
8139
8140 return false;
8141 }
8142
8143 /**
Adam Powell10298662011-08-14 18:26:30 -07008144 * @hide
8145 */
8146 public boolean isInScrollingContainer() {
8147 ViewParent p = getParent();
8148 while (p != null && p instanceof ViewGroup) {
8149 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8150 return true;
8151 }
8152 p = p.getParent();
8153 }
8154 return false;
8155 }
8156
8157 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008158 * Remove the longpress detection timer.
8159 */
8160 private void removeLongPressCallback() {
8161 if (mPendingCheckForLongPress != null) {
8162 removeCallbacks(mPendingCheckForLongPress);
8163 }
8164 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008165
8166 /**
8167 * Remove the pending click action
8168 */
8169 private void removePerformClickCallback() {
8170 if (mPerformClick != null) {
8171 removeCallbacks(mPerformClick);
8172 }
8173 }
8174
Adam Powelle14579b2009-12-16 18:39:52 -08008175 /**
Romain Guya440b002010-02-24 15:57:54 -08008176 * Remove the prepress detection timer.
8177 */
8178 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008179 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008180 setPressed(false);
8181 removeCallbacks(mUnsetPressedState);
8182 }
8183 }
8184
8185 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008186 * Remove the tap detection timer.
8187 */
8188 private void removeTapCallback() {
8189 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008190 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008191 removeCallbacks(mPendingCheckForTap);
8192 }
8193 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008194
8195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008196 * Cancels a pending long press. Your subclass can use this if you
8197 * want the context menu to come up if the user presses and holds
8198 * at the same place, but you don't want it to come up if they press
8199 * and then move around enough to cause scrolling.
8200 */
8201 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008202 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008203
8204 /*
8205 * The prepressed state handled by the tap callback is a display
8206 * construct, but the tap callback will post a long press callback
8207 * less its own timeout. Remove it here.
8208 */
8209 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 }
8211
8212 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008213 * Remove the pending callback for sending a
8214 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8215 */
8216 private void removeSendViewScrolledAccessibilityEventCallback() {
8217 if (mSendViewScrolledAccessibilityEvent != null) {
8218 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008219 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008220 }
8221 }
8222
8223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 * Sets the TouchDelegate for this View.
8225 */
8226 public void setTouchDelegate(TouchDelegate delegate) {
8227 mTouchDelegate = delegate;
8228 }
8229
8230 /**
8231 * Gets the TouchDelegate for this View.
8232 */
8233 public TouchDelegate getTouchDelegate() {
8234 return mTouchDelegate;
8235 }
8236
8237 /**
8238 * Set flags controlling behavior of this view.
8239 *
8240 * @param flags Constant indicating the value which should be set
8241 * @param mask Constant indicating the bit range that should be changed
8242 */
8243 void setFlags(int flags, int mask) {
8244 int old = mViewFlags;
8245 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8246
8247 int changed = mViewFlags ^ old;
8248 if (changed == 0) {
8249 return;
8250 }
8251 int privateFlags = mPrivateFlags;
8252
8253 /* Check if the FOCUSABLE bit has changed */
8254 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008255 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008257 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 /* Give up focus if we are no longer focusable */
8259 clearFocus();
8260 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008261 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 /*
8263 * Tell the view system that we are now available to take focus
8264 * if no one else already has it.
8265 */
8266 if (mParent != null) mParent.focusableViewAvailable(this);
8267 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008268 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8269 notifyAccessibilityStateChanged();
8270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008271 }
8272
8273 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8274 if ((changed & VISIBILITY_MASK) != 0) {
8275 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008276 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008277 * it was not visible. Marking it drawn ensures that the invalidation will
8278 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008280 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008281 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282
8283 needGlobalAttributesUpdate(true);
8284
8285 // a view becoming visible is worth notifying the parent
8286 // about in case nothing has focus. even if this specific view
8287 // isn't focusable, it may contain something that is, so let
8288 // the root view try to give this focus if nothing else does.
8289 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8290 mParent.focusableViewAvailable(this);
8291 }
8292 }
8293 }
8294
8295 /* Check if the GONE bit has changed */
8296 if ((changed & GONE) != 0) {
8297 needGlobalAttributesUpdate(false);
8298 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299
Romain Guyecd80ee2009-12-03 17:13:02 -08008300 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8301 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008302 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008303 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008304 if (mParent instanceof View) {
8305 // GONE views noop invalidation, so invalidate the parent
8306 ((View) mParent).invalidate(true);
8307 }
8308 // Mark the view drawn to ensure that it gets invalidated properly the next
8309 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008310 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 }
8312 if (mAttachInfo != null) {
8313 mAttachInfo.mViewVisibilityChanged = true;
8314 }
8315 }
8316
8317 /* Check if the VISIBLE bit has changed */
8318 if ((changed & INVISIBLE) != 0) {
8319 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008320 /*
8321 * If this view is becoming invisible, set the DRAWN flag so that
8322 * the next invalidate() will not be skipped.
8323 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008324 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325
8326 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008327 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 if (getRootView() != this) {
8329 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008330 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 }
8332 }
8333 if (mAttachInfo != null) {
8334 mAttachInfo.mViewVisibilityChanged = true;
8335 }
8336 }
8337
Adam Powell326d8082009-12-09 15:10:07 -08008338 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008339 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008340 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8341 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008342 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008343 } else if (mParent != null) {
8344 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008345 }
Adam Powell326d8082009-12-09 15:10:07 -08008346 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8347 }
8348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008349 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8350 destroyDrawingCache();
8351 }
8352
8353 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8354 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008355 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008356 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
8358
8359 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8360 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008361 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 }
8363
8364 if ((changed & DRAW_MASK) != 0) {
8365 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008366 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008367 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8368 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008370 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 }
8372 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008373 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008374 }
8375 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008376 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 }
8378
8379 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008380 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 mParent.recomputeViewAttributes(this);
8382 }
8383 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008384
8385 if (AccessibilityManager.getInstance(mContext).isEnabled()
8386 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8387 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8388 notifyAccessibilityStateChanged();
8389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 }
8391
8392 /**
8393 * Change the view's z order in the tree, so it's on top of other sibling
8394 * views
8395 */
8396 public void bringToFront() {
8397 if (mParent != null) {
8398 mParent.bringChildToFront(this);
8399 }
8400 }
8401
8402 /**
8403 * This is called in response to an internal scroll in this view (i.e., the
8404 * view scrolled its own contents). This is typically as a result of
8405 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8406 * called.
8407 *
8408 * @param l Current horizontal scroll origin.
8409 * @param t Current vertical scroll origin.
8410 * @param oldl Previous horizontal scroll origin.
8411 * @param oldt Previous vertical scroll origin.
8412 */
8413 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008414 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8415 postSendViewScrolledAccessibilityEventCallback();
8416 }
8417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 mBackgroundSizeChanged = true;
8419
8420 final AttachInfo ai = mAttachInfo;
8421 if (ai != null) {
8422 ai.mViewScrollChanged = true;
8423 }
8424 }
8425
8426 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008427 * Interface definition for a callback to be invoked when the layout bounds of a view
8428 * changes due to layout processing.
8429 */
8430 public interface OnLayoutChangeListener {
8431 /**
8432 * Called when the focus state of a view has changed.
8433 *
8434 * @param v The view whose state has changed.
8435 * @param left The new value of the view's left property.
8436 * @param top The new value of the view's top property.
8437 * @param right The new value of the view's right property.
8438 * @param bottom The new value of the view's bottom property.
8439 * @param oldLeft The previous value of the view's left property.
8440 * @param oldTop The previous value of the view's top property.
8441 * @param oldRight The previous value of the view's right property.
8442 * @param oldBottom The previous value of the view's bottom property.
8443 */
8444 void onLayoutChange(View v, int left, int top, int right, int bottom,
8445 int oldLeft, int oldTop, int oldRight, int oldBottom);
8446 }
8447
8448 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008449 * This is called during layout when the size of this view has changed. If
8450 * you were just added to the view hierarchy, you're called with the old
8451 * values of 0.
8452 *
8453 * @param w Current width of this view.
8454 * @param h Current height of this view.
8455 * @param oldw Old width of this view.
8456 * @param oldh Old height of this view.
8457 */
8458 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8459 }
8460
8461 /**
8462 * Called by draw to draw the child views. This may be overridden
8463 * by derived classes to gain control just before its children are drawn
8464 * (but after its own view has been drawn).
8465 * @param canvas the canvas on which to draw the view
8466 */
8467 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 }
8470
8471 /**
8472 * Gets the parent of this view. Note that the parent is a
8473 * ViewParent and not necessarily a View.
8474 *
8475 * @return Parent of this view.
8476 */
8477 public final ViewParent getParent() {
8478 return mParent;
8479 }
8480
8481 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008482 * Set the horizontal scrolled position of your view. This will cause a call to
8483 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8484 * invalidated.
8485 * @param value the x position to scroll to
8486 */
8487 public void setScrollX(int value) {
8488 scrollTo(value, mScrollY);
8489 }
8490
8491 /**
8492 * Set the vertical scrolled position of your view. This will cause a call to
8493 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8494 * invalidated.
8495 * @param value the y position to scroll to
8496 */
8497 public void setScrollY(int value) {
8498 scrollTo(mScrollX, value);
8499 }
8500
8501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 * Return the scrolled left position of this view. This is the left edge of
8503 * the displayed part of your view. You do not need to draw any pixels
8504 * farther left, since those are outside of the frame of your view on
8505 * screen.
8506 *
8507 * @return The left edge of the displayed part of your view, in pixels.
8508 */
8509 public final int getScrollX() {
8510 return mScrollX;
8511 }
8512
8513 /**
8514 * Return the scrolled top position of this view. This is the top edge of
8515 * the displayed part of your view. You do not need to draw any pixels above
8516 * it, since those are outside of the frame of your view on screen.
8517 *
8518 * @return The top edge of the displayed part of your view, in pixels.
8519 */
8520 public final int getScrollY() {
8521 return mScrollY;
8522 }
8523
8524 /**
8525 * Return the width of the your view.
8526 *
8527 * @return The width of your view, in pixels.
8528 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008529 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 public final int getWidth() {
8531 return mRight - mLeft;
8532 }
8533
8534 /**
8535 * Return the height of your view.
8536 *
8537 * @return The height of your view, in pixels.
8538 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008539 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 public final int getHeight() {
8541 return mBottom - mTop;
8542 }
8543
8544 /**
8545 * Return the visible drawing bounds of your view. Fills in the output
8546 * rectangle with the values from getScrollX(), getScrollY(),
8547 * getWidth(), and getHeight().
8548 *
8549 * @param outRect The (scrolled) drawing bounds of the view.
8550 */
8551 public void getDrawingRect(Rect outRect) {
8552 outRect.left = mScrollX;
8553 outRect.top = mScrollY;
8554 outRect.right = mScrollX + (mRight - mLeft);
8555 outRect.bottom = mScrollY + (mBottom - mTop);
8556 }
8557
8558 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008559 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8560 * raw width component (that is the result is masked by
8561 * {@link #MEASURED_SIZE_MASK}).
8562 *
8563 * @return The raw measured width of this view.
8564 */
8565 public final int getMeasuredWidth() {
8566 return mMeasuredWidth & MEASURED_SIZE_MASK;
8567 }
8568
8569 /**
8570 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008571 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008572 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 * This should be used during measurement and layout calculations only. Use
8574 * {@link #getWidth()} to see how wide a view is after layout.
8575 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008576 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008578 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 return mMeasuredWidth;
8580 }
8581
8582 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008583 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8584 * raw width component (that is the result is masked by
8585 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008587 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 */
8589 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008590 return mMeasuredHeight & MEASURED_SIZE_MASK;
8591 }
8592
8593 /**
8594 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008595 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008596 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8597 * This should be used during measurement and layout calculations only. Use
8598 * {@link #getHeight()} to see how wide a view is after layout.
8599 *
8600 * @return The measured width of this view as a bit mask.
8601 */
8602 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 return mMeasuredHeight;
8604 }
8605
8606 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008607 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8608 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8609 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8610 * and the height component is at the shifted bits
8611 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8612 */
8613 public final int getMeasuredState() {
8614 return (mMeasuredWidth&MEASURED_STATE_MASK)
8615 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8616 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8617 }
8618
8619 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008620 * The transform matrix of this view, which is calculated based on the current
8621 * roation, scale, and pivot properties.
8622 *
8623 * @see #getRotation()
8624 * @see #getScaleX()
8625 * @see #getScaleY()
8626 * @see #getPivotX()
8627 * @see #getPivotY()
8628 * @return The current transform matrix for the view
8629 */
8630 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008631 if (mTransformationInfo != null) {
8632 updateMatrix();
8633 return mTransformationInfo.mMatrix;
8634 }
8635 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008636 }
8637
8638 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008639 * Utility function to determine if the value is far enough away from zero to be
8640 * considered non-zero.
8641 * @param value A floating point value to check for zero-ness
8642 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8643 */
8644 private static boolean nonzero(float value) {
8645 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8646 }
8647
8648 /**
Jeff Brown86671742010-09-30 20:00:15 -07008649 * Returns true if the transform matrix is the identity matrix.
8650 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008651 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008652 * @return True if the transform matrix is the identity matrix, false otherwise.
8653 */
Jeff Brown86671742010-09-30 20:00:15 -07008654 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008655 if (mTransformationInfo != null) {
8656 updateMatrix();
8657 return mTransformationInfo.mMatrixIsIdentity;
8658 }
8659 return true;
8660 }
8661
8662 void ensureTransformationInfo() {
8663 if (mTransformationInfo == null) {
8664 mTransformationInfo = new TransformationInfo();
8665 }
Jeff Brown86671742010-09-30 20:00:15 -07008666 }
8667
8668 /**
8669 * Recomputes the transform matrix if necessary.
8670 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008671 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 final TransformationInfo info = mTransformationInfo;
8673 if (info == null) {
8674 return;
8675 }
8676 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008677 // transform-related properties have changed since the last time someone
8678 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008679
8680 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008681 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008682 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8683 info.mPrevWidth = mRight - mLeft;
8684 info.mPrevHeight = mBottom - mTop;
8685 info.mPivotX = info.mPrevWidth / 2f;
8686 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008687 }
8688 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008689 info.mMatrix.reset();
8690 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8691 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8692 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8693 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008694 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008695 if (info.mCamera == null) {
8696 info.mCamera = new Camera();
8697 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008698 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008699 info.mCamera.save();
8700 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8701 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8702 info.mCamera.getMatrix(info.matrix3D);
8703 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8704 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8705 info.mPivotY + info.mTranslationY);
8706 info.mMatrix.postConcat(info.matrix3D);
8707 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008708 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008709 info.mMatrixDirty = false;
8710 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8711 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008712 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008713 }
8714
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008715 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008716 * Utility method to retrieve the inverse of the current mMatrix property.
8717 * We cache the matrix to avoid recalculating it when transform properties
8718 * have not changed.
8719 *
8720 * @return The inverse of the current matrix of this view.
8721 */
Jeff Brown86671742010-09-30 20:00:15 -07008722 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008723 final TransformationInfo info = mTransformationInfo;
8724 if (info != null) {
8725 updateMatrix();
8726 if (info.mInverseMatrixDirty) {
8727 if (info.mInverseMatrix == null) {
8728 info.mInverseMatrix = new Matrix();
8729 }
8730 info.mMatrix.invert(info.mInverseMatrix);
8731 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008732 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008733 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008734 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008735 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008736 }
8737
8738 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008739 * Gets the distance along the Z axis from the camera to this view.
8740 *
8741 * @see #setCameraDistance(float)
8742 *
8743 * @return The distance along the Z axis.
8744 */
8745 public float getCameraDistance() {
8746 ensureTransformationInfo();
8747 final float dpi = mResources.getDisplayMetrics().densityDpi;
8748 final TransformationInfo info = mTransformationInfo;
8749 if (info.mCamera == null) {
8750 info.mCamera = new Camera();
8751 info.matrix3D = new Matrix();
8752 }
8753 return -(info.mCamera.getLocationZ() * dpi);
8754 }
8755
8756 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008757 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8758 * views are drawn) from the camera to this view. The camera's distance
8759 * affects 3D transformations, for instance rotations around the X and Y
8760 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008761 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008762 * use a camera distance that's greater than the height (X axis rotation) or
8763 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008764 *
Romain Guya5364ee2011-02-24 14:46:04 -08008765 * <p>The distance of the camera from the view plane can have an affect on the
8766 * perspective distortion of the view when it is rotated around the x or y axis.
8767 * For example, a large distance will result in a large viewing angle, and there
8768 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008769 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008770 * also result in some drawing artifacts if the rotated view ends up partially
8771 * behind the camera (which is why the recommendation is to use a distance at
8772 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008773 *
Romain Guya5364ee2011-02-24 14:46:04 -08008774 * <p>The distance is expressed in "depth pixels." The default distance depends
8775 * on the screen density. For instance, on a medium density display, the
8776 * default distance is 1280. On a high density display, the default distance
8777 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008778 *
Romain Guya5364ee2011-02-24 14:46:04 -08008779 * <p>If you want to specify a distance that leads to visually consistent
8780 * results across various densities, use the following formula:</p>
8781 * <pre>
8782 * float scale = context.getResources().getDisplayMetrics().density;
8783 * view.setCameraDistance(distance * scale);
8784 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008785 *
Romain Guya5364ee2011-02-24 14:46:04 -08008786 * <p>The density scale factor of a high density display is 1.5,
8787 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008788 *
Romain Guya5364ee2011-02-24 14:46:04 -08008789 * @param distance The distance in "depth pixels", if negative the opposite
8790 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008791 *
8792 * @see #setRotationX(float)
8793 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008794 */
8795 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008796 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008797
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008798 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008799 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008800 final TransformationInfo info = mTransformationInfo;
8801 if (info.mCamera == null) {
8802 info.mCamera = new Camera();
8803 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008804 }
8805
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008806 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8807 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008808
Chet Haase9d1992d2012-03-13 11:03:25 -07008809 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008810 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008811 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008812 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008813 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008814 // View was rejected last time it was drawn by its parent; this may have changed
8815 invalidateParentIfNeeded();
8816 }
Romain Guya5364ee2011-02-24 14:46:04 -08008817 }
8818
8819 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008820 * The degrees that the view is rotated around the pivot point.
8821 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008822 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008823 * @see #getPivotX()
8824 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008825 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008826 * @return The degrees of rotation.
8827 */
Chet Haasea5531132012-02-02 13:41:44 -08008828 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008830 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 }
8832
8833 /**
Chet Haase897247b2010-09-09 14:54:47 -07008834 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8835 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008836 *
8837 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008838 *
8839 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008840 * @see #getPivotX()
8841 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008842 * @see #setRotationX(float)
8843 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008844 *
8845 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008846 */
8847 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008848 ensureTransformationInfo();
8849 final TransformationInfo info = mTransformationInfo;
8850 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008851 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008852 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 info.mRotation = rotation;
8854 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008855 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008856 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008857 mDisplayList.setRotation(rotation);
8858 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008859 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008860 // View was rejected last time it was drawn by its parent; this may have changed
8861 invalidateParentIfNeeded();
8862 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008863 }
8864 }
8865
8866 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008867 * The degrees that the view is rotated around the vertical axis through the pivot point.
8868 *
8869 * @see #getPivotX()
8870 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008871 * @see #setRotationY(float)
8872 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008873 * @return The degrees of Y rotation.
8874 */
Chet Haasea5531132012-02-02 13:41:44 -08008875 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008876 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008877 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008878 }
8879
8880 /**
Chet Haase897247b2010-09-09 14:54:47 -07008881 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8882 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8883 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008884 *
Romain Guya5364ee2011-02-24 14:46:04 -08008885 * When rotating large views, it is recommended to adjust the camera distance
8886 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008887 *
8888 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008889 *
8890 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008891 * @see #getPivotX()
8892 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008893 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008894 * @see #setRotationX(float)
8895 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008896 *
8897 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008898 */
8899 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008900 ensureTransformationInfo();
8901 final TransformationInfo info = mTransformationInfo;
8902 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008903 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008904 info.mRotationY = rotationY;
8905 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008906 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008907 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008908 mDisplayList.setRotationY(rotationY);
8909 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008910 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008911 // View was rejected last time it was drawn by its parent; this may have changed
8912 invalidateParentIfNeeded();
8913 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008914 }
8915 }
8916
8917 /**
8918 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8919 *
8920 * @see #getPivotX()
8921 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008922 * @see #setRotationX(float)
8923 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008924 * @return The degrees of X rotation.
8925 */
Chet Haasea5531132012-02-02 13:41:44 -08008926 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008927 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008928 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008929 }
8930
8931 /**
Chet Haase897247b2010-09-09 14:54:47 -07008932 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8933 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8934 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008935 *
Romain Guya5364ee2011-02-24 14:46:04 -08008936 * When rotating large views, it is recommended to adjust the camera distance
8937 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008938 *
8939 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008940 *
8941 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008942 * @see #getPivotX()
8943 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008944 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008945 * @see #setRotationY(float)
8946 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008947 *
8948 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008949 */
8950 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008951 ensureTransformationInfo();
8952 final TransformationInfo info = mTransformationInfo;
8953 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008954 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008955 info.mRotationX = rotationX;
8956 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008957 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008958 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008959 mDisplayList.setRotationX(rotationX);
8960 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008961 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008962 // View was rejected last time it was drawn by its parent; this may have changed
8963 invalidateParentIfNeeded();
8964 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008965 }
8966 }
8967
8968 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008969 * The amount that the view is scaled in x around the pivot point, as a proportion of
8970 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8971 *
Joe Onorato93162322010-09-16 15:42:01 -04008972 * <p>By default, this is 1.0f.
8973 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008974 * @see #getPivotX()
8975 * @see #getPivotY()
8976 * @return The scaling factor.
8977 */
Chet Haasea5531132012-02-02 13:41:44 -08008978 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008979 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008980 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008981 }
8982
8983 /**
8984 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8985 * the view's unscaled width. A value of 1 means that no scaling is applied.
8986 *
8987 * @param scaleX The scaling factor.
8988 * @see #getPivotX()
8989 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008990 *
8991 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008992 */
8993 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008994 ensureTransformationInfo();
8995 final TransformationInfo info = mTransformationInfo;
8996 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008997 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008998 info.mScaleX = scaleX;
8999 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009000 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009001 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009002 mDisplayList.setScaleX(scaleX);
9003 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009004 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009005 // View was rejected last time it was drawn by its parent; this may have changed
9006 invalidateParentIfNeeded();
9007 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009008 }
9009 }
9010
9011 /**
9012 * The amount that the view is scaled in y around the pivot point, as a proportion of
9013 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9014 *
Joe Onorato93162322010-09-16 15:42:01 -04009015 * <p>By default, this is 1.0f.
9016 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 * @see #getPivotX()
9018 * @see #getPivotY()
9019 * @return The scaling factor.
9020 */
Chet Haasea5531132012-02-02 13:41:44 -08009021 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009022 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009024 }
9025
9026 /**
9027 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9028 * the view's unscaled width. A value of 1 means that no scaling is applied.
9029 *
9030 * @param scaleY The scaling factor.
9031 * @see #getPivotX()
9032 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009033 *
9034 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009035 */
9036 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009037 ensureTransformationInfo();
9038 final TransformationInfo info = mTransformationInfo;
9039 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009040 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009041 info.mScaleY = scaleY;
9042 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009043 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009044 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009045 mDisplayList.setScaleY(scaleY);
9046 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009047 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009048 // View was rejected last time it was drawn by its parent; this may have changed
9049 invalidateParentIfNeeded();
9050 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009051 }
9052 }
9053
9054 /**
9055 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9056 * and {@link #setScaleX(float) scaled}.
9057 *
9058 * @see #getRotation()
9059 * @see #getScaleX()
9060 * @see #getScaleY()
9061 * @see #getPivotY()
9062 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009063 *
9064 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009065 */
Chet Haasea5531132012-02-02 13:41:44 -08009066 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009067 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009068 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009069 }
9070
9071 /**
9072 * Sets the x location of the point around which the view is
9073 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009074 * By default, the pivot point is centered on the object.
9075 * Setting this property disables this behavior and causes the view to use only the
9076 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009077 *
9078 * @param pivotX The x location of the pivot point.
9079 * @see #getRotation()
9080 * @see #getScaleX()
9081 * @see #getScaleY()
9082 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009083 *
9084 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009085 */
9086 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009087 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009088 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009089 final TransformationInfo info = mTransformationInfo;
9090 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009091 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009092 info.mPivotX = pivotX;
9093 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009094 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009095 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009096 mDisplayList.setPivotX(pivotX);
9097 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009098 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009099 // View was rejected last time it was drawn by its parent; this may have changed
9100 invalidateParentIfNeeded();
9101 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009102 }
9103 }
9104
9105 /**
9106 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9107 * and {@link #setScaleY(float) scaled}.
9108 *
9109 * @see #getRotation()
9110 * @see #getScaleX()
9111 * @see #getScaleY()
9112 * @see #getPivotY()
9113 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009114 *
9115 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009116 */
Chet Haasea5531132012-02-02 13:41:44 -08009117 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009118 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009119 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009120 }
9121
9122 /**
9123 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009124 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9125 * Setting this property disables this behavior and causes the view to use only the
9126 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009127 *
9128 * @param pivotY The y location of the pivot point.
9129 * @see #getRotation()
9130 * @see #getScaleX()
9131 * @see #getScaleY()
9132 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009133 *
9134 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009135 */
9136 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009137 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009138 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009139 final TransformationInfo info = mTransformationInfo;
9140 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009141 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009142 info.mPivotY = pivotY;
9143 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009144 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009145 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009146 mDisplayList.setPivotY(pivotY);
9147 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009148 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009149 // View was rejected last time it was drawn by its parent; this may have changed
9150 invalidateParentIfNeeded();
9151 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009152 }
9153 }
9154
9155 /**
9156 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9157 * completely transparent and 1 means the view is completely opaque.
9158 *
Joe Onorato93162322010-09-16 15:42:01 -04009159 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009160 * @return The opacity of the view.
9161 */
Chet Haasea5531132012-02-02 13:41:44 -08009162 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009163 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009164 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 }
9166
9167 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009168 * Returns whether this View has content which overlaps. This function, intended to be
9169 * overridden by specific View types, is an optimization when alpha is set on a view. If
9170 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9171 * and then composited it into place, which can be expensive. If the view has no overlapping
9172 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9173 * An example of overlapping rendering is a TextView with a background image, such as a
9174 * Button. An example of non-overlapping rendering is a TextView with no background, or
9175 * an ImageView with only the foreground image. The default implementation returns true;
9176 * subclasses should override if they have cases which can be optimized.
9177 *
9178 * @return true if the content in this view might overlap, false otherwise.
9179 */
9180 public boolean hasOverlappingRendering() {
9181 return true;
9182 }
9183
9184 /**
Romain Guy171c5922011-01-06 10:04:23 -08009185 * <p>Sets 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.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009187 *
Romain Guy171c5922011-01-06 10:04:23 -08009188 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9189 * responsible for applying the opacity itself. Otherwise, calling this method is
9190 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009191 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009192 *
Chet Haasea5531132012-02-02 13:41:44 -08009193 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9194 * performance implications. It is generally best to use the alpha property sparingly and
9195 * transiently, as in the case of fading animations.</p>
9196 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009197 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009198 *
Joe Malin32736f02011-01-19 16:14:20 -08009199 * @see #setLayerType(int, android.graphics.Paint)
9200 *
Chet Haase73066682010-11-29 15:55:32 -08009201 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009202 */
9203 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009204 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009205 if (mTransformationInfo.mAlpha != alpha) {
9206 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009207 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009208 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009209 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009210 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009211 invalidate(true);
9212 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009213 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009214 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009215 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009216 mDisplayList.setAlpha(alpha);
9217 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009218 }
Chet Haaseed032702010-10-01 14:05:54 -07009219 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009220 }
9221
9222 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009223 * Faster version of setAlpha() which performs the same steps except there are
9224 * no calls to invalidate(). The caller of this function should perform proper invalidation
9225 * on the parent and this object. The return value indicates whether the subclass handles
9226 * alpha (the return value for onSetAlpha()).
9227 *
9228 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009229 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9230 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009231 */
9232 boolean setAlphaNoInvalidation(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;
9236 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9237 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009238 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009239 return true;
9240 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009241 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009242 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009243 mDisplayList.setAlpha(alpha);
9244 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009245 }
Chet Haasea00f3862011-02-22 06:34:40 -08009246 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009247 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009248 }
9249
9250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251 * Top position of this view relative to its parent.
9252 *
9253 * @return The top of this view, in pixels.
9254 */
9255 @ViewDebug.CapturedViewProperty
9256 public final int getTop() {
9257 return mTop;
9258 }
9259
9260 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009261 * Sets the top position of this view relative to its parent. This method is meant to be called
9262 * by the layout system and should not generally be called otherwise, because the property
9263 * may be changed at any time by the layout.
9264 *
9265 * @param top The top of this view, in pixels.
9266 */
9267 public final void setTop(int top) {
9268 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009269 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009270 final boolean matrixIsIdentity = mTransformationInfo == null
9271 || mTransformationInfo.mMatrixIsIdentity;
9272 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009273 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009274 int minTop;
9275 int yLoc;
9276 if (top < mTop) {
9277 minTop = top;
9278 yLoc = top - mTop;
9279 } else {
9280 minTop = mTop;
9281 yLoc = 0;
9282 }
Chet Haasee9140a72011-02-16 16:23:29 -08009283 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009284 }
9285 } else {
9286 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009287 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009288 }
9289
Chet Haaseed032702010-10-01 14:05:54 -07009290 int width = mRight - mLeft;
9291 int oldHeight = mBottom - mTop;
9292
Chet Haase21cd1382010-09-01 17:42:29 -07009293 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009294 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009295 mDisplayList.setTop(mTop);
9296 }
Chet Haase21cd1382010-09-01 17:42:29 -07009297
Chet Haaseed032702010-10-01 14:05:54 -07009298 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9299
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009300 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009301 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009302 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009303 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009304 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009305 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009306 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009307 }
Chet Haase55dbb652010-12-21 20:15:08 -08009308 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009309 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009310 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009311 // View was rejected last time it was drawn by its parent; this may have changed
9312 invalidateParentIfNeeded();
9313 }
Chet Haase21cd1382010-09-01 17:42:29 -07009314 }
9315 }
9316
9317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 * Bottom position of this view relative to its parent.
9319 *
9320 * @return The bottom of this view, in pixels.
9321 */
9322 @ViewDebug.CapturedViewProperty
9323 public final int getBottom() {
9324 return mBottom;
9325 }
9326
9327 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009328 * True if this view has changed since the last time being drawn.
9329 *
9330 * @return The dirty state of this view.
9331 */
9332 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009333 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009334 }
9335
9336 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009337 * Sets the bottom position of this view relative to its parent. This method is meant to be
9338 * called by the layout system and should not generally be called otherwise, because the
9339 * property may be changed at any time by the layout.
9340 *
9341 * @param bottom The bottom of this view, in pixels.
9342 */
9343 public final void setBottom(int bottom) {
9344 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009345 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009346 final boolean matrixIsIdentity = mTransformationInfo == null
9347 || mTransformationInfo.mMatrixIsIdentity;
9348 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009349 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009350 int maxBottom;
9351 if (bottom < mBottom) {
9352 maxBottom = mBottom;
9353 } else {
9354 maxBottom = bottom;
9355 }
Chet Haasee9140a72011-02-16 16:23:29 -08009356 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009357 }
9358 } else {
9359 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009360 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009361 }
9362
Chet Haaseed032702010-10-01 14:05:54 -07009363 int width = mRight - mLeft;
9364 int oldHeight = mBottom - mTop;
9365
Chet Haase21cd1382010-09-01 17:42:29 -07009366 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009367 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009368 mDisplayList.setBottom(mBottom);
9369 }
Chet Haase21cd1382010-09-01 17:42:29 -07009370
Chet Haaseed032702010-10-01 14:05:54 -07009371 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9372
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009373 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009374 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009375 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009376 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009377 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009378 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009379 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009380 }
Chet Haase55dbb652010-12-21 20:15:08 -08009381 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009382 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009383 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009384 // View was rejected last time it was drawn by its parent; this may have changed
9385 invalidateParentIfNeeded();
9386 }
Chet Haase21cd1382010-09-01 17:42:29 -07009387 }
9388 }
9389
9390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 * Left position of this view relative to its parent.
9392 *
9393 * @return The left edge of this view, in pixels.
9394 */
9395 @ViewDebug.CapturedViewProperty
9396 public final int getLeft() {
9397 return mLeft;
9398 }
9399
9400 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009401 * Sets the left position of this view relative to its parent. This method is meant to be called
9402 * by the layout system and should not generally be called otherwise, because the property
9403 * may be changed at any time by the layout.
9404 *
9405 * @param left The bottom of this view, in pixels.
9406 */
9407 public final void setLeft(int left) {
9408 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009409 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009410 final boolean matrixIsIdentity = mTransformationInfo == null
9411 || mTransformationInfo.mMatrixIsIdentity;
9412 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009413 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009414 int minLeft;
9415 int xLoc;
9416 if (left < mLeft) {
9417 minLeft = left;
9418 xLoc = left - mLeft;
9419 } else {
9420 minLeft = mLeft;
9421 xLoc = 0;
9422 }
Chet Haasee9140a72011-02-16 16:23:29 -08009423 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009424 }
9425 } else {
9426 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009427 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009428 }
9429
Chet Haaseed032702010-10-01 14:05:54 -07009430 int oldWidth = mRight - mLeft;
9431 int height = mBottom - mTop;
9432
Chet Haase21cd1382010-09-01 17:42:29 -07009433 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009434 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009435 mDisplayList.setLeft(left);
9436 }
Chet Haase21cd1382010-09-01 17:42:29 -07009437
Chet Haaseed032702010-10-01 14:05:54 -07009438 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9439
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009440 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009441 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009442 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009443 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009444 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009445 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009446 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009447 }
Chet Haase55dbb652010-12-21 20:15:08 -08009448 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009449 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009450 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009451 // View was rejected last time it was drawn by its parent; this may have changed
9452 invalidateParentIfNeeded();
9453 }
Chet Haase21cd1382010-09-01 17:42:29 -07009454 }
9455 }
9456
9457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 * Right position of this view relative to its parent.
9459 *
9460 * @return The right edge of this view, in pixels.
9461 */
9462 @ViewDebug.CapturedViewProperty
9463 public final int getRight() {
9464 return mRight;
9465 }
9466
9467 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009468 * Sets the right position of this view relative to its parent. This method is meant to be called
9469 * by the layout system and should not generally be called otherwise, because the property
9470 * may be changed at any time by the layout.
9471 *
9472 * @param right The bottom of this view, in pixels.
9473 */
9474 public final void setRight(int right) {
9475 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009476 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009477 final boolean matrixIsIdentity = mTransformationInfo == null
9478 || mTransformationInfo.mMatrixIsIdentity;
9479 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009480 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009481 int maxRight;
9482 if (right < mRight) {
9483 maxRight = mRight;
9484 } else {
9485 maxRight = right;
9486 }
Chet Haasee9140a72011-02-16 16:23:29 -08009487 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009488 }
9489 } else {
9490 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009491 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009492 }
9493
Chet Haaseed032702010-10-01 14:05:54 -07009494 int oldWidth = mRight - mLeft;
9495 int height = mBottom - mTop;
9496
Chet Haase21cd1382010-09-01 17:42:29 -07009497 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009498 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009499 mDisplayList.setRight(mRight);
9500 }
Chet Haase21cd1382010-09-01 17:42:29 -07009501
Chet Haaseed032702010-10-01 14:05:54 -07009502 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9503
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009504 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009505 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009506 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009507 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009508 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009509 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009510 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009511 }
Chet Haase55dbb652010-12-21 20:15:08 -08009512 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009513 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009514 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009515 // View was rejected last time it was drawn by its parent; this may have changed
9516 invalidateParentIfNeeded();
9517 }
Chet Haase21cd1382010-09-01 17:42:29 -07009518 }
9519 }
9520
9521 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009522 * The visual x position of this view, in pixels. This is equivalent to the
9523 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009524 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009525 *
Chet Haasedf030d22010-07-30 17:22:38 -07009526 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009527 */
Chet Haasea5531132012-02-02 13:41:44 -08009528 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009529 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009530 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009531 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009532
Chet Haasedf030d22010-07-30 17:22:38 -07009533 /**
9534 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9535 * {@link #setTranslationX(float) translationX} property to be the difference between
9536 * the x value passed in and the current {@link #getLeft() left} property.
9537 *
9538 * @param x The visual x position of this view, in pixels.
9539 */
9540 public void setX(float x) {
9541 setTranslationX(x - mLeft);
9542 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009543
Chet Haasedf030d22010-07-30 17:22:38 -07009544 /**
9545 * The visual y position of this view, in pixels. This is equivalent to the
9546 * {@link #setTranslationY(float) translationY} property plus the current
9547 * {@link #getTop() top} property.
9548 *
9549 * @return The visual y position of this view, in pixels.
9550 */
Chet Haasea5531132012-02-02 13:41:44 -08009551 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009552 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009553 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009554 }
9555
9556 /**
9557 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9558 * {@link #setTranslationY(float) translationY} property to be the difference between
9559 * the y value passed in and the current {@link #getTop() top} property.
9560 *
9561 * @param y The visual y position of this view, in pixels.
9562 */
9563 public void setY(float y) {
9564 setTranslationY(y - mTop);
9565 }
9566
9567
9568 /**
9569 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9570 * This position is post-layout, in addition to wherever the object's
9571 * layout placed it.
9572 *
9573 * @return The horizontal position of this view relative to its left position, in pixels.
9574 */
Chet Haasea5531132012-02-02 13:41:44 -08009575 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009576 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009577 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009578 }
9579
9580 /**
9581 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9582 * This effectively positions the object post-layout, in addition to wherever the object's
9583 * layout placed it.
9584 *
9585 * @param translationX The horizontal position of this view relative to its left position,
9586 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009587 *
9588 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009589 */
9590 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009591 ensureTransformationInfo();
9592 final TransformationInfo info = mTransformationInfo;
9593 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009594 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009595 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009596 info.mTranslationX = translationX;
9597 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009598 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009599 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009600 mDisplayList.setTranslationX(translationX);
9601 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009602 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009603 // View was rejected last time it was drawn by its parent; this may have changed
9604 invalidateParentIfNeeded();
9605 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009606 }
9607 }
9608
9609 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009610 * The horizontal location of this view relative to its {@link #getTop() top} position.
9611 * This position is post-layout, in addition to wherever the object's
9612 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009613 *
Chet Haasedf030d22010-07-30 17:22:38 -07009614 * @return The vertical position of this view relative to its top position,
9615 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009616 */
Chet Haasea5531132012-02-02 13:41:44 -08009617 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009618 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009619 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009620 }
9621
9622 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009623 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9624 * This effectively positions the object post-layout, in addition to wherever the object's
9625 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009626 *
Chet Haasedf030d22010-07-30 17:22:38 -07009627 * @param translationY The vertical position of this view relative to its top position,
9628 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009629 *
9630 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009631 */
Chet Haasedf030d22010-07-30 17:22:38 -07009632 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009633 ensureTransformationInfo();
9634 final TransformationInfo info = mTransformationInfo;
9635 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009636 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009637 info.mTranslationY = translationY;
9638 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009639 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009640 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009641 mDisplayList.setTranslationY(translationY);
9642 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009643 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009644 // View was rejected last time it was drawn by its parent; this may have changed
9645 invalidateParentIfNeeded();
9646 }
Chet Haasedf030d22010-07-30 17:22:38 -07009647 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009648 }
9649
9650 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651 * Hit rectangle in parent's coordinates
9652 *
9653 * @param outRect The hit rectangle of the view.
9654 */
9655 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009656 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009657 final TransformationInfo info = mTransformationInfo;
9658 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009659 outRect.set(mLeft, mTop, mRight, mBottom);
9660 } else {
9661 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009662 tmpRect.set(-info.mPivotX, -info.mPivotY,
9663 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9664 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009665 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9666 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009667 }
9668 }
9669
9670 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009671 * Determines whether the given point, in local coordinates is inside the view.
9672 */
9673 /*package*/ final boolean pointInView(float localX, float localY) {
9674 return localX >= 0 && localX < (mRight - mLeft)
9675 && localY >= 0 && localY < (mBottom - mTop);
9676 }
9677
9678 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009679 * Utility method to determine whether the given point, in local coordinates,
9680 * is inside the view, where the area of the view is expanded by the slop factor.
9681 * This method is called while processing touch-move events to determine if the event
9682 * is still within the view.
9683 */
9684 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009685 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009686 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 }
9688
9689 /**
9690 * When a view has focus and the user navigates away from it, the next view is searched for
9691 * starting from the rectangle filled in by this method.
9692 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009693 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009694 * of the view. However, if your view maintains some idea of internal selection,
9695 * such as a cursor, or a selected row or column, you should override this method and
9696 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697 *
9698 * @param r The rectangle to fill in, in this view's coordinates.
9699 */
9700 public void getFocusedRect(Rect r) {
9701 getDrawingRect(r);
9702 }
9703
9704 /**
9705 * If some part of this view is not clipped by any of its parents, then
9706 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009707 * coordinates (without taking possible View rotations into account), offset
9708 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9709 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 *
9711 * @param r If true is returned, r holds the global coordinates of the
9712 * visible portion of this view.
9713 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9714 * between this view and its root. globalOffet may be null.
9715 * @return true if r is non-empty (i.e. part of the view is visible at the
9716 * root level.
9717 */
9718 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9719 int width = mRight - mLeft;
9720 int height = mBottom - mTop;
9721 if (width > 0 && height > 0) {
9722 r.set(0, 0, width, height);
9723 if (globalOffset != null) {
9724 globalOffset.set(-mScrollX, -mScrollY);
9725 }
9726 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9727 }
9728 return false;
9729 }
9730
9731 public final boolean getGlobalVisibleRect(Rect r) {
9732 return getGlobalVisibleRect(r, null);
9733 }
9734
9735 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009736 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009737 if (getGlobalVisibleRect(r, offset)) {
9738 r.offset(-offset.x, -offset.y); // make r local
9739 return true;
9740 }
9741 return false;
9742 }
9743
9744 /**
9745 * Offset this view's vertical location by the specified number of pixels.
9746 *
9747 * @param offset the number of pixels to offset the view by
9748 */
9749 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009750 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009751 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009752 final boolean matrixIsIdentity = mTransformationInfo == null
9753 || mTransformationInfo.mMatrixIsIdentity;
9754 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009755 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009756 invalidateViewProperty(false, false);
9757 } else {
9758 final ViewParent p = mParent;
9759 if (p != null && mAttachInfo != null) {
9760 final Rect r = mAttachInfo.mTmpInvalRect;
9761 int minTop;
9762 int maxBottom;
9763 int yLoc;
9764 if (offset < 0) {
9765 minTop = mTop + offset;
9766 maxBottom = mBottom;
9767 yLoc = offset;
9768 } else {
9769 minTop = mTop;
9770 maxBottom = mBottom + offset;
9771 yLoc = 0;
9772 }
9773 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9774 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009775 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009776 }
9777 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009778 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009779 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009780
Chet Haasec3aa3612010-06-17 08:50:37 -07009781 mTop += offset;
9782 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009783 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009784 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009785 invalidateViewProperty(false, false);
9786 } else {
9787 if (!matrixIsIdentity) {
9788 invalidateViewProperty(false, true);
9789 }
9790 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009791 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 }
9794
9795 /**
9796 * Offset this view's horizontal location by the specified amount of pixels.
9797 *
9798 * @param offset the numer of pixels to offset the view by
9799 */
9800 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009801 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009802 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009803 final boolean matrixIsIdentity = mTransformationInfo == null
9804 || mTransformationInfo.mMatrixIsIdentity;
9805 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009806 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009807 invalidateViewProperty(false, false);
9808 } else {
9809 final ViewParent p = mParent;
9810 if (p != null && mAttachInfo != null) {
9811 final Rect r = mAttachInfo.mTmpInvalRect;
9812 int minLeft;
9813 int maxRight;
9814 if (offset < 0) {
9815 minLeft = mLeft + offset;
9816 maxRight = mRight;
9817 } else {
9818 minLeft = mLeft;
9819 maxRight = mRight + offset;
9820 }
9821 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9822 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009823 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009824 }
9825 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009826 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009827 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009828
Chet Haasec3aa3612010-06-17 08:50:37 -07009829 mLeft += offset;
9830 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009831 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009832 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009833 invalidateViewProperty(false, false);
9834 } else {
9835 if (!matrixIsIdentity) {
9836 invalidateViewProperty(false, true);
9837 }
9838 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009839 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 }
9842
9843 /**
9844 * Get the LayoutParams associated with this view. All views should have
9845 * layout parameters. These supply parameters to the <i>parent</i> of this
9846 * view specifying how it should be arranged. There are many subclasses of
9847 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9848 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009849 *
9850 * This method may return null if this View is not attached to a parent
9851 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9852 * was not invoked successfully. When a View is attached to a parent
9853 * ViewGroup, this method must not return null.
9854 *
9855 * @return The LayoutParams associated with this view, or null if no
9856 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009858 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 public ViewGroup.LayoutParams getLayoutParams() {
9860 return mLayoutParams;
9861 }
9862
9863 /**
9864 * Set the layout parameters associated with this view. These supply
9865 * parameters to the <i>parent</i> of this view specifying how it should be
9866 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9867 * correspond to the different subclasses of ViewGroup that are responsible
9868 * for arranging their children.
9869 *
Romain Guy01c174b2011-02-22 11:51:06 -08009870 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009871 */
9872 public void setLayoutParams(ViewGroup.LayoutParams params) {
9873 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009874 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 }
9876 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009877 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009878 if (mParent instanceof ViewGroup) {
9879 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 requestLayout();
9882 }
9883
9884 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009885 * Resolve the layout parameters depending on the resolved layout direction
9886 */
9887 private void resolveLayoutParams() {
9888 if (mLayoutParams != null) {
9889 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9890 }
9891 }
9892
9893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 * Set the scrolled position of your view. This will cause a call to
9895 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9896 * invalidated.
9897 * @param x the x position to scroll to
9898 * @param y the y position to scroll to
9899 */
9900 public void scrollTo(int x, int y) {
9901 if (mScrollX != x || mScrollY != y) {
9902 int oldX = mScrollX;
9903 int oldY = mScrollY;
9904 mScrollX = x;
9905 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009906 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009907 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009908 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009909 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911 }
9912 }
9913
9914 /**
9915 * Move the scrolled position of your view. This will cause a call to
9916 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9917 * invalidated.
9918 * @param x the amount of pixels to scroll by horizontally
9919 * @param y the amount of pixels to scroll by vertically
9920 */
9921 public void scrollBy(int x, int y) {
9922 scrollTo(mScrollX + x, mScrollY + y);
9923 }
9924
9925 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009926 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9927 * animation to fade the scrollbars out after a default delay. If a subclass
9928 * provides animated scrolling, the start delay should equal the duration
9929 * of the scrolling animation.</p>
9930 *
9931 * <p>The animation starts only if at least one of the scrollbars is
9932 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9933 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9934 * this method returns true, and false otherwise. If the animation is
9935 * started, this method calls {@link #invalidate()}; in that case the
9936 * caller should not call {@link #invalidate()}.</p>
9937 *
9938 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009939 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009940 *
9941 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9942 * and {@link #scrollTo(int, int)}.</p>
9943 *
9944 * @return true if the animation is played, false otherwise
9945 *
9946 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009947 * @see #scrollBy(int, int)
9948 * @see #scrollTo(int, int)
9949 * @see #isHorizontalScrollBarEnabled()
9950 * @see #isVerticalScrollBarEnabled()
9951 * @see #setHorizontalScrollBarEnabled(boolean)
9952 * @see #setVerticalScrollBarEnabled(boolean)
9953 */
9954 protected boolean awakenScrollBars() {
9955 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009956 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009957 }
9958
9959 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009960 * Trigger the scrollbars to draw.
9961 * This method differs from awakenScrollBars() only in its default duration.
9962 * initialAwakenScrollBars() will show the scroll bars for longer than
9963 * usual to give the user more of a chance to notice them.
9964 *
9965 * @return true if the animation is played, false otherwise.
9966 */
9967 private boolean initialAwakenScrollBars() {
9968 return mScrollCache != null &&
9969 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9970 }
9971
9972 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009973 * <p>
9974 * Trigger the scrollbars to draw. When invoked this method starts an
9975 * animation to fade the scrollbars out after a fixed delay. If a subclass
9976 * provides animated scrolling, the start delay should equal the duration of
9977 * the scrolling animation.
9978 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009979 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009980 * <p>
9981 * The animation starts only if at least one of the scrollbars is enabled,
9982 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9983 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9984 * this method returns true, and false otherwise. If the animation is
9985 * started, this method calls {@link #invalidate()}; in that case the caller
9986 * should not call {@link #invalidate()}.
9987 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009988 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009989 * <p>
9990 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009991 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009992 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009993 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009994 * @param startDelay the delay, in milliseconds, after which the animation
9995 * should start; when the delay is 0, the animation starts
9996 * immediately
9997 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009998 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009999 * @see #scrollBy(int, int)
10000 * @see #scrollTo(int, int)
10001 * @see #isHorizontalScrollBarEnabled()
10002 * @see #isVerticalScrollBarEnabled()
10003 * @see #setHorizontalScrollBarEnabled(boolean)
10004 * @see #setVerticalScrollBarEnabled(boolean)
10005 */
10006 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010007 return awakenScrollBars(startDelay, true);
10008 }
Joe Malin32736f02011-01-19 16:14:20 -080010009
Mike Cleron290947b2009-09-29 18:34:32 -070010010 /**
10011 * <p>
10012 * Trigger the scrollbars to draw. When invoked this method starts an
10013 * animation to fade the scrollbars out after a fixed delay. If a subclass
10014 * provides animated scrolling, the start delay should equal the duration of
10015 * the scrolling animation.
10016 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010017 *
Mike Cleron290947b2009-09-29 18:34:32 -070010018 * <p>
10019 * The animation starts only if at least one of the scrollbars is enabled,
10020 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10021 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10022 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010023 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010024 * is set to true; in that case the caller
10025 * should not call {@link #invalidate()}.
10026 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010027 *
Mike Cleron290947b2009-09-29 18:34:32 -070010028 * <p>
10029 * This method should be invoked everytime a subclass directly updates the
10030 * scroll parameters.
10031 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010032 *
Mike Cleron290947b2009-09-29 18:34:32 -070010033 * @param startDelay the delay, in milliseconds, after which the animation
10034 * should start; when the delay is 0, the animation starts
10035 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010036 *
Mike Cleron290947b2009-09-29 18:34:32 -070010037 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010038 *
Mike Cleron290947b2009-09-29 18:34:32 -070010039 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010040 *
Mike Cleron290947b2009-09-29 18:34:32 -070010041 * @see #scrollBy(int, int)
10042 * @see #scrollTo(int, int)
10043 * @see #isHorizontalScrollBarEnabled()
10044 * @see #isVerticalScrollBarEnabled()
10045 * @see #setHorizontalScrollBarEnabled(boolean)
10046 * @see #setVerticalScrollBarEnabled(boolean)
10047 */
10048 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010049 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010050
Mike Cleronf116bf82009-09-27 19:14:12 -070010051 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10052 return false;
10053 }
10054
10055 if (scrollCache.scrollBar == null) {
10056 scrollCache.scrollBar = new ScrollBarDrawable();
10057 }
10058
10059 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10060
Mike Cleron290947b2009-09-29 18:34:32 -070010061 if (invalidate) {
10062 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010063 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010064 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010065
10066 if (scrollCache.state == ScrollabilityCache.OFF) {
10067 // FIXME: this is copied from WindowManagerService.
10068 // We should get this value from the system when it
10069 // is possible to do so.
10070 final int KEY_REPEAT_FIRST_DELAY = 750;
10071 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10072 }
10073
10074 // Tell mScrollCache when we should start fading. This may
10075 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010076 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010077 scrollCache.fadeStartTime = fadeStartTime;
10078 scrollCache.state = ScrollabilityCache.ON;
10079
10080 // Schedule our fader to run, unscheduling any old ones first
10081 if (mAttachInfo != null) {
10082 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10083 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10084 }
10085
10086 return true;
10087 }
10088
10089 return false;
10090 }
10091
10092 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010093 * Do not invalidate views which are not visible and which are not running an animation. They
10094 * will not get drawn and they should not set dirty flags as if they will be drawn
10095 */
10096 private boolean skipInvalidate() {
10097 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10098 (!(mParent instanceof ViewGroup) ||
10099 !((ViewGroup) mParent).isViewTransitioning(this));
10100 }
10101 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010102 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010103 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10104 * in the future. This must be called from a UI thread. To call from a non-UI
10105 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010106 *
10107 * WARNING: This method is destructive to dirty.
10108 * @param dirty the rectangle representing the bounds of the dirty region
10109 */
10110 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010111 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010112 return;
10113 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010114 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10115 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10116 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10117 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10118 mPrivateFlags |= PFLAG_INVALIDATED;
10119 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010120 final ViewParent p = mParent;
10121 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010122 //noinspection PointlessBooleanExpression,ConstantConditions
10123 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10124 if (p != null && ai != null && ai.mHardwareAccelerated) {
10125 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010126 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010127 p.invalidateChild(this, null);
10128 return;
10129 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 if (p != null && ai != null) {
10132 final int scrollX = mScrollX;
10133 final int scrollY = mScrollY;
10134 final Rect r = ai.mTmpInvalRect;
10135 r.set(dirty.left - scrollX, dirty.top - scrollY,
10136 dirty.right - scrollX, dirty.bottom - scrollY);
10137 mParent.invalidateChild(this, r);
10138 }
10139 }
10140 }
10141
10142 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010143 * 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 -080010144 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010145 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10146 * will be called at some point in the future. This must be called from
10147 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 * @param l the left position of the dirty region
10149 * @param t the top position of the dirty region
10150 * @param r the right position of the dirty region
10151 * @param b the bottom position of the dirty region
10152 */
10153 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010154 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010155 return;
10156 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010157 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10158 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10159 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10160 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10161 mPrivateFlags |= PFLAG_INVALIDATED;
10162 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010163 final ViewParent p = mParent;
10164 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010165 //noinspection PointlessBooleanExpression,ConstantConditions
10166 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10167 if (p != null && ai != null && ai.mHardwareAccelerated) {
10168 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010169 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010170 p.invalidateChild(this, null);
10171 return;
10172 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 if (p != null && ai != null && l < r && t < b) {
10175 final int scrollX = mScrollX;
10176 final int scrollY = mScrollY;
10177 final Rect tmpr = ai.mTmpInvalRect;
10178 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10179 p.invalidateChild(this, tmpr);
10180 }
10181 }
10182 }
10183
10184 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010185 * Invalidate the whole view. If the view is visible,
10186 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10187 * the future. This must be called from a UI thread. To call from a non-UI thread,
10188 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010189 */
10190 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010191 invalidate(true);
10192 }
Joe Malin32736f02011-01-19 16:14:20 -080010193
Chet Haaseed032702010-10-01 14:05:54 -070010194 /**
10195 * This is where the invalidate() work actually happens. A full invalidate()
10196 * causes the drawing cache to be invalidated, but this function can be called with
10197 * invalidateCache set to false to skip that invalidation step for cases that do not
10198 * need it (for example, a component that remains at the same dimensions with the same
10199 * content).
10200 *
10201 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10202 * well. This is usually true for a full invalidate, but may be set to false if the
10203 * View's contents or dimensions have not changed.
10204 */
Romain Guy849d0a32011-02-01 17:20:48 -080010205 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010206 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010207 return;
10208 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010209 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10210 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10211 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010212 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010213 mPrivateFlags &= ~PFLAG_DRAWN;
10214 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010215 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010216 mPrivateFlags |= PFLAG_INVALIDATED;
10217 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010220 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010221 //noinspection PointlessBooleanExpression,ConstantConditions
10222 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10223 if (p != null && ai != null && ai.mHardwareAccelerated) {
10224 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010225 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010226 p.invalidateChild(this, null);
10227 return;
10228 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010229 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 if (p != null && ai != null) {
10232 final Rect r = ai.mTmpInvalRect;
10233 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10234 // Don't call invalidate -- we don't want to internally scroll
10235 // our own bounds
10236 p.invalidateChild(this, r);
10237 }
10238 }
10239 }
10240
10241 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010242 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10243 * set any flags or handle all of the cases handled by the default invalidation methods.
10244 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10245 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10246 * walk up the hierarchy, transforming the dirty rect as necessary.
10247 *
10248 * The method also handles normal invalidation logic if display list properties are not
10249 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10250 * backup approach, to handle these cases used in the various property-setting methods.
10251 *
10252 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10253 * are not being used in this view
10254 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10255 * list properties are not being used in this view
10256 */
10257 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010258 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010259 if (invalidateParent) {
10260 invalidateParentCaches();
10261 }
10262 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010263 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010264 }
10265 invalidate(false);
10266 } else {
10267 final AttachInfo ai = mAttachInfo;
10268 final ViewParent p = mParent;
10269 if (p != null && ai != null) {
10270 final Rect r = ai.mTmpInvalRect;
10271 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10272 if (mParent instanceof ViewGroup) {
10273 ((ViewGroup) mParent).invalidateChildFast(this, r);
10274 } else {
10275 mParent.invalidateChild(this, r);
10276 }
10277 }
10278 }
10279 }
10280
10281 /**
10282 * Utility method to transform a given Rect by the current matrix of this view.
10283 */
10284 void transformRect(final Rect rect) {
10285 if (!getMatrix().isIdentity()) {
10286 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10287 boundingRect.set(rect);
10288 getMatrix().mapRect(boundingRect);
10289 rect.set((int) (boundingRect.left - 0.5f),
10290 (int) (boundingRect.top - 0.5f),
10291 (int) (boundingRect.right + 0.5f),
10292 (int) (boundingRect.bottom + 0.5f));
10293 }
10294 }
10295
10296 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010297 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010298 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10299 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010300 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10301 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010302 *
10303 * @hide
10304 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010305 protected void invalidateParentCaches() {
10306 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010307 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010308 }
10309 }
Joe Malin32736f02011-01-19 16:14:20 -080010310
Romain Guy0fd89bf2011-01-26 15:41:30 -080010311 /**
10312 * Used to indicate that the parent of this view should be invalidated. This functionality
10313 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10314 * which is necessary when various parent-managed properties of the view change, such as
10315 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10316 * an invalidation event to the parent.
10317 *
10318 * @hide
10319 */
10320 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010321 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010322 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010323 }
10324 }
10325
10326 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010327 * Indicates whether this View is opaque. An opaque View guarantees that it will
10328 * draw all the pixels overlapping its bounds using a fully opaque color.
10329 *
10330 * Subclasses of View should override this method whenever possible to indicate
10331 * whether an instance is opaque. Opaque Views are treated in a special way by
10332 * the View hierarchy, possibly allowing it to perform optimizations during
10333 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010334 *
Romain Guy24443ea2009-05-11 11:56:30 -070010335 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010336 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010337 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010338 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010339 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010340 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010341 }
10342
Adam Powell20232d02010-12-08 21:08:53 -080010343 /**
10344 * @hide
10345 */
10346 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010347 // Opaque if:
10348 // - Has a background
10349 // - Background is opaque
10350 // - Doesn't have scrollbars or scrollbars are inside overlay
10351
Philip Milne6c8ea062012-04-03 17:38:43 -070010352 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010353 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010354 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010355 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010356 }
10357
10358 final int flags = mViewFlags;
10359 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10360 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010361 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010362 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010363 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010364 }
10365 }
10366
10367 /**
10368 * @hide
10369 */
10370 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010371 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010372 }
10373
10374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 * @return A handler associated with the thread running the View. This
10376 * handler can be used to pump events in the UI events queue.
10377 */
10378 public Handler getHandler() {
10379 if (mAttachInfo != null) {
10380 return mAttachInfo.mHandler;
10381 }
10382 return null;
10383 }
10384
10385 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010386 * Gets the view root associated with the View.
10387 * @return The view root, or null if none.
10388 * @hide
10389 */
10390 public ViewRootImpl getViewRootImpl() {
10391 if (mAttachInfo != null) {
10392 return mAttachInfo.mViewRootImpl;
10393 }
10394 return null;
10395 }
10396
10397 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010398 * <p>Causes the Runnable to be added to the message queue.
10399 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010400 *
Romain Guye63a4f32011-08-11 11:33:31 -070010401 * <p>This method can be invoked from outside of the UI thread
10402 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010403 *
10404 * @param action The Runnable that will be executed.
10405 *
10406 * @return Returns true if the Runnable was successfully placed in to the
10407 * message queue. Returns false on failure, usually because the
10408 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010409 *
10410 * @see #postDelayed
10411 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010412 */
10413 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010414 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010415 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010416 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010417 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010418 // Assume that post will succeed later
10419 ViewRootImpl.getRunQueue().post(action);
10420 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 }
10422
10423 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010424 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010426 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010427 *
Romain Guye63a4f32011-08-11 11:33:31 -070010428 * <p>This method can be invoked from outside of the UI thread
10429 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 *
10431 * @param action The Runnable that will be executed.
10432 * @param delayMillis The delay (in milliseconds) until the Runnable
10433 * will be executed.
10434 *
10435 * @return true if the Runnable was successfully placed in to the
10436 * message queue. Returns false on failure, usually because the
10437 * looper processing the message queue is exiting. Note that a
10438 * result of true does not mean the Runnable will be processed --
10439 * if the looper is quit before the delivery time of the message
10440 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010441 *
10442 * @see #post
10443 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010444 */
10445 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010446 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010447 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010448 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010449 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010450 // Assume that post will succeed later
10451 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10452 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 }
10454
10455 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010456 * <p>Causes the Runnable to execute on the next animation time step.
10457 * The runnable will be run on the user interface thread.</p>
10458 *
10459 * <p>This method can be invoked from outside of the UI thread
10460 * only when this View is attached to a window.</p>
10461 *
10462 * @param action The Runnable that will be executed.
10463 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010464 * @see #postOnAnimationDelayed
10465 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010466 */
10467 public void postOnAnimation(Runnable action) {
10468 final AttachInfo attachInfo = mAttachInfo;
10469 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010470 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10471 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010472 } else {
10473 // Assume that post will succeed later
10474 ViewRootImpl.getRunQueue().post(action);
10475 }
10476 }
10477
10478 /**
10479 * <p>Causes the Runnable to execute on the next animation time step,
10480 * after the specified amount of time elapses.
10481 * The runnable will be run on the user interface thread.</p>
10482 *
10483 * <p>This method can be invoked from outside of the UI thread
10484 * only when this View is attached to a window.</p>
10485 *
10486 * @param action The Runnable that will be executed.
10487 * @param delayMillis The delay (in milliseconds) until the Runnable
10488 * will be executed.
10489 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010490 * @see #postOnAnimation
10491 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010492 */
10493 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10494 final AttachInfo attachInfo = mAttachInfo;
10495 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010496 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10497 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010498 } else {
10499 // Assume that post will succeed later
10500 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10501 }
10502 }
10503
10504 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010505 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010506 *
Romain Guye63a4f32011-08-11 11:33:31 -070010507 * <p>This method can be invoked from outside of the UI thread
10508 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 *
10510 * @param action The Runnable to remove from the message handling queue
10511 *
10512 * @return true if this view could ask the Handler to remove the Runnable,
10513 * false otherwise. When the returned value is true, the Runnable
10514 * may or may not have been actually removed from the message queue
10515 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010516 *
10517 * @see #post
10518 * @see #postDelayed
10519 * @see #postOnAnimation
10520 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010521 */
10522 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010523 if (action != null) {
10524 final AttachInfo attachInfo = mAttachInfo;
10525 if (attachInfo != null) {
10526 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010527 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10528 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010529 } else {
10530 // Assume that post will succeed later
10531 ViewRootImpl.getRunQueue().removeCallbacks(action);
10532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 return true;
10535 }
10536
10537 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010538 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10539 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010540 *
Romain Guye63a4f32011-08-11 11:33:31 -070010541 * <p>This method can be invoked from outside of the UI thread
10542 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010543 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010545 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 */
10547 public void postInvalidate() {
10548 postInvalidateDelayed(0);
10549 }
10550
10551 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010552 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10553 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010554 *
Romain Guye63a4f32011-08-11 11:33:31 -070010555 * <p>This method can be invoked from outside of the UI thread
10556 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 *
10558 * @param left The left coordinate of the rectangle to invalidate.
10559 * @param top The top coordinate of the rectangle to invalidate.
10560 * @param right The right coordinate of the rectangle to invalidate.
10561 * @param bottom The bottom coordinate of the rectangle to invalidate.
10562 *
10563 * @see #invalidate(int, int, int, int)
10564 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010565 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 */
10567 public void postInvalidate(int left, int top, int right, int bottom) {
10568 postInvalidateDelayed(0, left, top, right, bottom);
10569 }
10570
10571 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010572 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10573 * loop. Waits for the specified amount of time.</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 delayMilliseconds the duration in milliseconds to delay the
10579 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010580 *
10581 * @see #invalidate()
10582 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 */
10584 public void postInvalidateDelayed(long delayMilliseconds) {
10585 // We try only with the AttachInfo because there's no point in invalidating
10586 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010587 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010588 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010589 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591 }
10592
10593 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010594 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10595 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010596 *
Romain Guye63a4f32011-08-11 11:33:31 -070010597 * <p>This method can be invoked from outside of the UI thread
10598 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010599 *
10600 * @param delayMilliseconds the duration in milliseconds to delay the
10601 * invalidation by
10602 * @param left The left coordinate of the rectangle to invalidate.
10603 * @param top The top coordinate of the rectangle to invalidate.
10604 * @param right The right coordinate of the rectangle to invalidate.
10605 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010606 *
10607 * @see #invalidate(int, int, int, int)
10608 * @see #invalidate(Rect)
10609 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 */
10611 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10612 int right, int bottom) {
10613
10614 // We try only with the AttachInfo because there's no point in invalidating
10615 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010616 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010617 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10619 info.target = this;
10620 info.left = left;
10621 info.top = top;
10622 info.right = right;
10623 info.bottom = bottom;
10624
Jeff Browna175a5b2012-02-15 19:18:31 -080010625 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 }
10627 }
10628
10629 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010630 * <p>Cause an invalidate to happen on the next animation time step, typically the
10631 * next display frame.</p>
10632 *
10633 * <p>This method can be invoked from outside of the UI thread
10634 * only when this View is attached to a window.</p>
10635 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010636 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010637 */
10638 public void postInvalidateOnAnimation() {
10639 // We try only with the AttachInfo because there's no point in invalidating
10640 // if we are not attached to our window
10641 final AttachInfo attachInfo = mAttachInfo;
10642 if (attachInfo != null) {
10643 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10644 }
10645 }
10646
10647 /**
10648 * <p>Cause an invalidate of the specified area to happen on the next animation
10649 * time step, typically the next display frame.</p>
10650 *
10651 * <p>This method can be invoked from outside of the UI thread
10652 * only when this View is attached to a window.</p>
10653 *
10654 * @param left The left coordinate of the rectangle to invalidate.
10655 * @param top The top coordinate of the rectangle to invalidate.
10656 * @param right The right coordinate of the rectangle to invalidate.
10657 * @param bottom The bottom coordinate of the rectangle to invalidate.
10658 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010659 * @see #invalidate(int, int, int, int)
10660 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010661 */
10662 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10663 // We try only with the AttachInfo because there's no point in invalidating
10664 // if we are not attached to our window
10665 final AttachInfo attachInfo = mAttachInfo;
10666 if (attachInfo != null) {
10667 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10668 info.target = this;
10669 info.left = left;
10670 info.top = top;
10671 info.right = right;
10672 info.bottom = bottom;
10673
10674 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10675 }
10676 }
10677
10678 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010679 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10680 * This event is sent at most once every
10681 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10682 */
10683 private void postSendViewScrolledAccessibilityEventCallback() {
10684 if (mSendViewScrolledAccessibilityEvent == null) {
10685 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10686 }
10687 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10688 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10689 postDelayed(mSendViewScrolledAccessibilityEvent,
10690 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10691 }
10692 }
10693
10694 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 * Called by a parent to request that a child update its values for mScrollX
10696 * and mScrollY if necessary. This will typically be done if the child is
10697 * animating a scroll using a {@link android.widget.Scroller Scroller}
10698 * object.
10699 */
10700 public void computeScroll() {
10701 }
10702
10703 /**
10704 * <p>Indicate whether the horizontal edges are faded when the view is
10705 * scrolled horizontally.</p>
10706 *
10707 * @return true if the horizontal edges should are faded on scroll, false
10708 * otherwise
10709 *
10710 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010711 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010712 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 */
10714 public boolean isHorizontalFadingEdgeEnabled() {
10715 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10716 }
10717
10718 /**
10719 * <p>Define whether the horizontal edges should be faded when this view
10720 * is scrolled horizontally.</p>
10721 *
10722 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10723 * be faded when the view is scrolled
10724 * horizontally
10725 *
10726 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010727 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010728 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 */
10730 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10731 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10732 if (horizontalFadingEdgeEnabled) {
10733 initScrollCache();
10734 }
10735
10736 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10737 }
10738 }
10739
10740 /**
10741 * <p>Indicate whether the vertical edges are faded when the view is
10742 * scrolled horizontally.</p>
10743 *
10744 * @return true if the vertical edges should are faded on scroll, false
10745 * otherwise
10746 *
10747 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010748 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010749 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 */
10751 public boolean isVerticalFadingEdgeEnabled() {
10752 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10753 }
10754
10755 /**
10756 * <p>Define whether the vertical edges should be faded when this view
10757 * is scrolled vertically.</p>
10758 *
10759 * @param verticalFadingEdgeEnabled true if the vertical edges should
10760 * be faded when the view is scrolled
10761 * vertically
10762 *
10763 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010764 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010765 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 */
10767 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10768 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10769 if (verticalFadingEdgeEnabled) {
10770 initScrollCache();
10771 }
10772
10773 mViewFlags ^= FADING_EDGE_VERTICAL;
10774 }
10775 }
10776
10777 /**
10778 * Returns the strength, or intensity, of the top faded edge. The strength is
10779 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10780 * returns 0.0 or 1.0 but no value in between.
10781 *
10782 * Subclasses should override this method to provide a smoother fade transition
10783 * when scrolling occurs.
10784 *
10785 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10786 */
10787 protected float getTopFadingEdgeStrength() {
10788 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10789 }
10790
10791 /**
10792 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10793 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10794 * returns 0.0 or 1.0 but no value in between.
10795 *
10796 * Subclasses should override this method to provide a smoother fade transition
10797 * when scrolling occurs.
10798 *
10799 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10800 */
10801 protected float getBottomFadingEdgeStrength() {
10802 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10803 computeVerticalScrollRange() ? 1.0f : 0.0f;
10804 }
10805
10806 /**
10807 * Returns the strength, or intensity, of the left faded edge. The strength is
10808 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10809 * returns 0.0 or 1.0 but no value in between.
10810 *
10811 * Subclasses should override this method to provide a smoother fade transition
10812 * when scrolling occurs.
10813 *
10814 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10815 */
10816 protected float getLeftFadingEdgeStrength() {
10817 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10818 }
10819
10820 /**
10821 * Returns the strength, or intensity, of the right faded edge. The strength is
10822 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10823 * returns 0.0 or 1.0 but no value in between.
10824 *
10825 * Subclasses should override this method to provide a smoother fade transition
10826 * when scrolling occurs.
10827 *
10828 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10829 */
10830 protected float getRightFadingEdgeStrength() {
10831 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10832 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10833 }
10834
10835 /**
10836 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10837 * scrollbar is not drawn by default.</p>
10838 *
10839 * @return true if the horizontal scrollbar should be painted, false
10840 * otherwise
10841 *
10842 * @see #setHorizontalScrollBarEnabled(boolean)
10843 */
10844 public boolean isHorizontalScrollBarEnabled() {
10845 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10846 }
10847
10848 /**
10849 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10850 * scrollbar is not drawn by default.</p>
10851 *
10852 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10853 * be painted
10854 *
10855 * @see #isHorizontalScrollBarEnabled()
10856 */
10857 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10858 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10859 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010860 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010861 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 }
10863 }
10864
10865 /**
10866 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10867 * scrollbar is not drawn by default.</p>
10868 *
10869 * @return true if the vertical scrollbar should be painted, false
10870 * otherwise
10871 *
10872 * @see #setVerticalScrollBarEnabled(boolean)
10873 */
10874 public boolean isVerticalScrollBarEnabled() {
10875 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10876 }
10877
10878 /**
10879 * <p>Define whether the vertical scrollbar should be drawn or not. The
10880 * scrollbar is not drawn by default.</p>
10881 *
10882 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10883 * be painted
10884 *
10885 * @see #isVerticalScrollBarEnabled()
10886 */
10887 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10888 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10889 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010890 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010891 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 }
10893 }
10894
Adam Powell20232d02010-12-08 21:08:53 -080010895 /**
10896 * @hide
10897 */
10898 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010899 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010900 }
Joe Malin32736f02011-01-19 16:14:20 -080010901
Mike Cleronfe81d382009-09-28 14:22:16 -070010902 /**
10903 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010904 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010905 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010906 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010907 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010908 */
10909 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10910 initScrollCache();
10911 final ScrollabilityCache scrollabilityCache = mScrollCache;
10912 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010913 if (fadeScrollbars) {
10914 scrollabilityCache.state = ScrollabilityCache.OFF;
10915 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010916 scrollabilityCache.state = ScrollabilityCache.ON;
10917 }
10918 }
Joe Malin32736f02011-01-19 16:14:20 -080010919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 /**
Joe Malin32736f02011-01-19 16:14:20 -080010921 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010922 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010923 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010924 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010925 *
10926 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010927 */
10928 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010929 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010930 }
Joe Malin32736f02011-01-19 16:14:20 -080010931
Mike Cleron52f0a642009-09-28 18:21:37 -070010932 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010933 *
10934 * Returns the delay before scrollbars fade.
10935 *
10936 * @return the delay before scrollbars fade
10937 *
10938 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10939 */
10940 public int getScrollBarDefaultDelayBeforeFade() {
10941 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10942 mScrollCache.scrollBarDefaultDelayBeforeFade;
10943 }
10944
10945 /**
10946 * Define the delay before scrollbars fade.
10947 *
10948 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10949 *
10950 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10951 */
10952 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10953 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10954 }
10955
10956 /**
10957 *
10958 * Returns the scrollbar fade duration.
10959 *
10960 * @return the scrollbar fade duration
10961 *
10962 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10963 */
10964 public int getScrollBarFadeDuration() {
10965 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10966 mScrollCache.scrollBarFadeDuration;
10967 }
10968
10969 /**
10970 * Define the scrollbar fade duration.
10971 *
10972 * @param scrollBarFadeDuration - the scrollbar fade duration
10973 *
10974 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10975 */
10976 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10977 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10978 }
10979
10980 /**
10981 *
10982 * Returns the scrollbar size.
10983 *
10984 * @return the scrollbar size
10985 *
10986 * @attr ref android.R.styleable#View_scrollbarSize
10987 */
10988 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010989 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010990 mScrollCache.scrollBarSize;
10991 }
10992
10993 /**
10994 * Define the scrollbar size.
10995 *
10996 * @param scrollBarSize - the scrollbar size
10997 *
10998 * @attr ref android.R.styleable#View_scrollbarSize
10999 */
11000 public void setScrollBarSize(int scrollBarSize) {
11001 getScrollCache().scrollBarSize = scrollBarSize;
11002 }
11003
11004 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011005 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11006 * inset. When inset, they add to the padding of the view. And the scrollbars
11007 * can be drawn inside the padding area or on the edge of the view. For example,
11008 * if a view has a background drawable and you want to draw the scrollbars
11009 * inside the padding specified by the drawable, you can use
11010 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11011 * appear at the edge of the view, ignoring the padding, then you can use
11012 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11013 * @param style the style of the scrollbars. Should be one of
11014 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11015 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11016 * @see #SCROLLBARS_INSIDE_OVERLAY
11017 * @see #SCROLLBARS_INSIDE_INSET
11018 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11019 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011020 *
11021 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 */
11023 public void setScrollBarStyle(int style) {
11024 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11025 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011026 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011027 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011028 }
11029 }
11030
11031 /**
11032 * <p>Returns the current scrollbar style.</p>
11033 * @return the current scrollbar style
11034 * @see #SCROLLBARS_INSIDE_OVERLAY
11035 * @see #SCROLLBARS_INSIDE_INSET
11036 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11037 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011038 *
11039 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011041 @ViewDebug.ExportedProperty(mapping = {
11042 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11043 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11044 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11045 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11046 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011047 public int getScrollBarStyle() {
11048 return mViewFlags & SCROLLBARS_STYLE_MASK;
11049 }
11050
11051 /**
11052 * <p>Compute the horizontal range that the horizontal scrollbar
11053 * represents.</p>
11054 *
11055 * <p>The range is expressed in arbitrary units that must be the same as the
11056 * units used by {@link #computeHorizontalScrollExtent()} and
11057 * {@link #computeHorizontalScrollOffset()}.</p>
11058 *
11059 * <p>The default range is the drawing width of this view.</p>
11060 *
11061 * @return the total horizontal range represented by the horizontal
11062 * scrollbar
11063 *
11064 * @see #computeHorizontalScrollExtent()
11065 * @see #computeHorizontalScrollOffset()
11066 * @see android.widget.ScrollBarDrawable
11067 */
11068 protected int computeHorizontalScrollRange() {
11069 return getWidth();
11070 }
11071
11072 /**
11073 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11074 * within the horizontal range. This value is used to compute the position
11075 * of the thumb within the scrollbar's track.</p>
11076 *
11077 * <p>The range is expressed in arbitrary units that must be the same as the
11078 * units used by {@link #computeHorizontalScrollRange()} and
11079 * {@link #computeHorizontalScrollExtent()}.</p>
11080 *
11081 * <p>The default offset is the scroll offset of this view.</p>
11082 *
11083 * @return the horizontal offset of the scrollbar's thumb
11084 *
11085 * @see #computeHorizontalScrollRange()
11086 * @see #computeHorizontalScrollExtent()
11087 * @see android.widget.ScrollBarDrawable
11088 */
11089 protected int computeHorizontalScrollOffset() {
11090 return mScrollX;
11091 }
11092
11093 /**
11094 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11095 * within the horizontal range. This value is used to compute the length
11096 * of the thumb within the scrollbar's track.</p>
11097 *
11098 * <p>The range is expressed in arbitrary units that must be the same as the
11099 * units used by {@link #computeHorizontalScrollRange()} and
11100 * {@link #computeHorizontalScrollOffset()}.</p>
11101 *
11102 * <p>The default extent is the drawing width of this view.</p>
11103 *
11104 * @return the horizontal extent of the scrollbar's thumb
11105 *
11106 * @see #computeHorizontalScrollRange()
11107 * @see #computeHorizontalScrollOffset()
11108 * @see android.widget.ScrollBarDrawable
11109 */
11110 protected int computeHorizontalScrollExtent() {
11111 return getWidth();
11112 }
11113
11114 /**
11115 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11116 *
11117 * <p>The range is expressed in arbitrary units that must be the same as the
11118 * units used by {@link #computeVerticalScrollExtent()} and
11119 * {@link #computeVerticalScrollOffset()}.</p>
11120 *
11121 * @return the total vertical range represented by the vertical scrollbar
11122 *
11123 * <p>The default range is the drawing height of this view.</p>
11124 *
11125 * @see #computeVerticalScrollExtent()
11126 * @see #computeVerticalScrollOffset()
11127 * @see android.widget.ScrollBarDrawable
11128 */
11129 protected int computeVerticalScrollRange() {
11130 return getHeight();
11131 }
11132
11133 /**
11134 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11135 * within the horizontal range. This value is used to compute the position
11136 * of the thumb within the scrollbar's track.</p>
11137 *
11138 * <p>The range is expressed in arbitrary units that must be the same as the
11139 * units used by {@link #computeVerticalScrollRange()} and
11140 * {@link #computeVerticalScrollExtent()}.</p>
11141 *
11142 * <p>The default offset is the scroll offset of this view.</p>
11143 *
11144 * @return the vertical offset of the scrollbar's thumb
11145 *
11146 * @see #computeVerticalScrollRange()
11147 * @see #computeVerticalScrollExtent()
11148 * @see android.widget.ScrollBarDrawable
11149 */
11150 protected int computeVerticalScrollOffset() {
11151 return mScrollY;
11152 }
11153
11154 /**
11155 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11156 * within the vertical range. This value is used to compute the length
11157 * of the thumb within the scrollbar's track.</p>
11158 *
11159 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011160 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 * {@link #computeVerticalScrollOffset()}.</p>
11162 *
11163 * <p>The default extent is the drawing height of this view.</p>
11164 *
11165 * @return the vertical extent of the scrollbar's thumb
11166 *
11167 * @see #computeVerticalScrollRange()
11168 * @see #computeVerticalScrollOffset()
11169 * @see android.widget.ScrollBarDrawable
11170 */
11171 protected int computeVerticalScrollExtent() {
11172 return getHeight();
11173 }
11174
11175 /**
Adam Powell69159442011-06-13 17:53:06 -070011176 * Check if this view can be scrolled horizontally in a certain direction.
11177 *
11178 * @param direction Negative to check scrolling left, positive to check scrolling right.
11179 * @return true if this view can be scrolled in the specified direction, false otherwise.
11180 */
11181 public boolean canScrollHorizontally(int direction) {
11182 final int offset = computeHorizontalScrollOffset();
11183 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11184 if (range == 0) return false;
11185 if (direction < 0) {
11186 return offset > 0;
11187 } else {
11188 return offset < range - 1;
11189 }
11190 }
11191
11192 /**
11193 * Check if this view can be scrolled vertically in a certain direction.
11194 *
11195 * @param direction Negative to check scrolling up, positive to check scrolling down.
11196 * @return true if this view can be scrolled in the specified direction, false otherwise.
11197 */
11198 public boolean canScrollVertically(int direction) {
11199 final int offset = computeVerticalScrollOffset();
11200 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11201 if (range == 0) return false;
11202 if (direction < 0) {
11203 return offset > 0;
11204 } else {
11205 return offset < range - 1;
11206 }
11207 }
11208
11209 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11211 * scrollbars are painted only if they have been awakened first.</p>
11212 *
11213 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011214 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011215 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011217 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 // scrollbars are drawn only when the animation is running
11219 final ScrollabilityCache cache = mScrollCache;
11220 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011221
Mike Cleronf116bf82009-09-27 19:14:12 -070011222 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011223
Mike Cleronf116bf82009-09-27 19:14:12 -070011224 if (state == ScrollabilityCache.OFF) {
11225 return;
11226 }
Joe Malin32736f02011-01-19 16:14:20 -080011227
Mike Cleronf116bf82009-09-27 19:14:12 -070011228 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011229
Mike Cleronf116bf82009-09-27 19:14:12 -070011230 if (state == ScrollabilityCache.FADING) {
11231 // We're fading -- get our fade interpolation
11232 if (cache.interpolatorValues == null) {
11233 cache.interpolatorValues = new float[1];
11234 }
Joe Malin32736f02011-01-19 16:14:20 -080011235
Mike Cleronf116bf82009-09-27 19:14:12 -070011236 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011237
Mike Cleronf116bf82009-09-27 19:14:12 -070011238 // Stops the animation if we're done
11239 if (cache.scrollBarInterpolator.timeToValues(values) ==
11240 Interpolator.Result.FREEZE_END) {
11241 cache.state = ScrollabilityCache.OFF;
11242 } else {
11243 cache.scrollBar.setAlpha(Math.round(values[0]));
11244 }
Joe Malin32736f02011-01-19 16:14:20 -080011245
11246 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011247 // drawing. We only want this when we're fading so that
11248 // we prevent excessive redraws
11249 invalidate = true;
11250 } else {
11251 // We're just on -- but we may have been fading before so
11252 // reset alpha
11253 cache.scrollBar.setAlpha(255);
11254 }
11255
Joe Malin32736f02011-01-19 16:14:20 -080011256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 final int viewFlags = mViewFlags;
11258
11259 final boolean drawHorizontalScrollBar =
11260 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11261 final boolean drawVerticalScrollBar =
11262 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11263 && !isVerticalScrollBarHidden();
11264
11265 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11266 final int width = mRight - mLeft;
11267 final int height = mBottom - mTop;
11268
11269 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011270
Mike Reede8853fc2009-09-04 14:01:48 -040011271 final int scrollX = mScrollX;
11272 final int scrollY = mScrollY;
11273 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11274
Mike Cleronf116bf82009-09-27 19:14:12 -070011275 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011278 int size = scrollBar.getSize(false);
11279 if (size <= 0) {
11280 size = cache.scrollBarSize;
11281 }
11282
Mike Cleronf116bf82009-09-27 19:14:12 -070011283 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011284 computeHorizontalScrollOffset(),
11285 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011286 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011287 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011288 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011289 left = scrollX + (mPaddingLeft & inside);
11290 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11291 bottom = top + size;
11292 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11293 if (invalidate) {
11294 invalidate(left, top, right, bottom);
11295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 }
11297
11298 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011299 int size = scrollBar.getSize(true);
11300 if (size <= 0) {
11301 size = cache.scrollBarSize;
11302 }
11303
Mike Reede8853fc2009-09-04 14:01:48 -040011304 scrollBar.setParameters(computeVerticalScrollRange(),
11305 computeVerticalScrollOffset(),
11306 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011307 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11308 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11309 verticalScrollbarPosition = isLayoutRtl() ?
11310 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11311 }
11312 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011313 default:
Adam Powell20232d02010-12-08 21:08:53 -080011314 case SCROLLBAR_POSITION_RIGHT:
11315 left = scrollX + width - size - (mUserPaddingRight & inside);
11316 break;
11317 case SCROLLBAR_POSITION_LEFT:
11318 left = scrollX + (mUserPaddingLeft & inside);
11319 break;
11320 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011321 top = scrollY + (mPaddingTop & inside);
11322 right = left + size;
11323 bottom = scrollY + height - (mUserPaddingBottom & inside);
11324 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11325 if (invalidate) {
11326 invalidate(left, top, right, bottom);
11327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 }
11329 }
11330 }
11331 }
Romain Guy8506ab42009-06-11 17:35:47 -070011332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011334 * 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 -080011335 * FastScroller is visible.
11336 * @return whether to temporarily hide the vertical scrollbar
11337 * @hide
11338 */
11339 protected boolean isVerticalScrollBarHidden() {
11340 return false;
11341 }
11342
11343 /**
11344 * <p>Draw the horizontal scrollbar if
11345 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11346 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 * @param canvas the canvas on which to draw the scrollbar
11348 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349 *
11350 * @see #isHorizontalScrollBarEnabled()
11351 * @see #computeHorizontalScrollRange()
11352 * @see #computeHorizontalScrollExtent()
11353 * @see #computeHorizontalScrollOffset()
11354 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011355 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011356 */
Romain Guy8fb95422010-08-17 18:38:51 -070011357 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11358 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011359 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011360 scrollBar.draw(canvas);
11361 }
Mike Reede8853fc2009-09-04 14:01:48 -040011362
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011363 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011364 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11365 * returns true.</p>
11366 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011367 * @param canvas the canvas on which to draw the scrollbar
11368 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369 *
11370 * @see #isVerticalScrollBarEnabled()
11371 * @see #computeVerticalScrollRange()
11372 * @see #computeVerticalScrollExtent()
11373 * @see #computeVerticalScrollOffset()
11374 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011375 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 */
Romain Guy8fb95422010-08-17 18:38:51 -070011377 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11378 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011379 scrollBar.setBounds(l, t, r, b);
11380 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 }
11382
11383 /**
11384 * Implement this to do your drawing.
11385 *
11386 * @param canvas the canvas on which the background will be drawn
11387 */
11388 protected void onDraw(Canvas canvas) {
11389 }
11390
11391 /*
11392 * Caller is responsible for calling requestLayout if necessary.
11393 * (This allows addViewInLayout to not request a new layout.)
11394 */
11395 void assignParent(ViewParent parent) {
11396 if (mParent == null) {
11397 mParent = parent;
11398 } else if (parent == null) {
11399 mParent = null;
11400 } else {
11401 throw new RuntimeException("view " + this + " being added, but"
11402 + " it already has a parent");
11403 }
11404 }
11405
11406 /**
11407 * This is called when the view is attached to a window. At this point it
11408 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011409 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11410 * however it may be called any time before the first onDraw -- including
11411 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011412 *
11413 * @see #onDetachedFromWindow()
11414 */
11415 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011416 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011417 mParent.requestTransparentRegion(this);
11418 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011419
Dianne Hackborn4702a852012-08-17 15:18:29 -070011420 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011421 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011422 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011423 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011424
Chet Haasea9b61ac2010-12-20 07:40:25 -080011425 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011426
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011427 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011428
Svetoslav Ganov42138042012-03-20 11:51:39 -070011429 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011430 if (isFocused()) {
11431 InputMethodManager imm = InputMethodManager.peekInstance();
11432 imm.focusIn(this);
11433 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011434
11435 if (mAttachInfo != null && mDisplayList != null) {
11436 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11437 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011438 }
Cibu Johny86666632010-02-22 13:01:02 -080011439
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011440 void resolveRtlProperties() {
11441 // Order is important here: LayoutDirection MUST be resolved first...
11442 resolveLayoutDirection();
11443 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11444 resolvePadding();
11445 resolveLayoutParams();
11446 resolveTextDirection();
11447 resolveTextAlignment();
Fabrice Di Meglio0af4b8b2012-06-11 18:30:05 -070011448 resolveDrawables();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011449 }
11450
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011451 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011452 * @see #onScreenStateChanged(int)
11453 */
11454 void dispatchScreenStateChanged(int screenState) {
11455 onScreenStateChanged(screenState);
11456 }
11457
11458 /**
11459 * This method is called whenever the state of the screen this view is
11460 * attached to changes. A state change will usually occurs when the screen
11461 * turns on or off (whether it happens automatically or the user does it
11462 * manually.)
11463 *
11464 * @param screenState The new state of the screen. Can be either
11465 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11466 */
11467 public void onScreenStateChanged(int screenState) {
11468 }
11469
11470 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011471 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11472 */
11473 private boolean hasRtlSupport() {
11474 return mContext.getApplicationInfo().hasRtlSupport();
11475 }
11476
11477 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011478 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11479 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011480 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011481 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011482 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011483 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011484 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011485
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011486 if (hasRtlSupport()) {
11487 // Set resolved depending on layout direction
11488 switch (getLayoutDirection()) {
11489 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011490 // We cannot resolve yet. LTR is by default and let the resolution happen again
11491 // later to get the correct resolved value
11492 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011493
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011494 ViewGroup viewGroup = ((ViewGroup) mParent);
11495
11496 // We cannot resolve yet on the parent too. LTR is by default and let the
11497 // resolution happen again later
11498 if (!viewGroup.canResolveLayoutDirection()) return;
11499
11500 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011501 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011502 }
11503 break;
11504 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011505 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011506 break;
11507 case LAYOUT_DIRECTION_LOCALE:
11508 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011509 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011510 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011511 break;
11512 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011513 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011514 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011515 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011516
11517 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011518 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011519 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011520 }
11521
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011522 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011523 * Called when layout direction has been resolved.
11524 *
11525 * The default implementation does nothing.
11526 */
11527 public void onResolvedLayoutDirectionChanged() {
11528 }
11529
11530 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011531 * Return if padding has been resolved
11532 */
11533 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011534 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011535 }
11536
11537 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011538 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011539 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011540 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011541 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11542 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11543 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11544 // left / right padding are used if defined. If they are not defined and start / end
11545 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11546 // resolve them as left / right (layout direction is not taken into account).
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011547 if (mUserPaddingLeftInitial == UNDEFINED_PADDING &&
11548 mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011549 mUserPaddingLeft = mUserPaddingStart;
11550 }
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011551 if (mUserPaddingRightInitial == UNDEFINED_PADDING
11552 && mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011553 mUserPaddingRight = mUserPaddingEnd;
11554 }
11555
11556 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11557
11558 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11559 mUserPaddingBottom);
11560 } else {
11561 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11562 // If start / end padding are defined, they will be resolved (hence overriding) to
11563 // left / right or right / left depending on the resolved layout direction.
11564 // If start / end padding are not defined, use the left / right ones.
11565 int resolvedLayoutDirection = getResolvedLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011566 // Set user padding to initial values ...
11567 mUserPaddingLeft = (mUserPaddingLeftInitial == UNDEFINED_PADDING) ?
11568 0 : mUserPaddingLeftInitial;
11569 mUserPaddingRight = (mUserPaddingRightInitial == UNDEFINED_PADDING) ?
11570 0 : mUserPaddingRightInitial;
11571 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011572 switch (resolvedLayoutDirection) {
11573 case LAYOUT_DIRECTION_RTL:
11574 if (mUserPaddingStart != UNDEFINED_PADDING) {
11575 mUserPaddingRight = mUserPaddingStart;
11576 }
11577 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11578 mUserPaddingLeft = mUserPaddingEnd;
11579 }
11580 break;
11581 case LAYOUT_DIRECTION_LTR:
11582 default:
11583 if (mUserPaddingStart != UNDEFINED_PADDING) {
11584 mUserPaddingLeft = mUserPaddingStart;
11585 }
11586 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11587 mUserPaddingRight = mUserPaddingEnd;
11588 }
11589 }
11590
11591 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11592
11593 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11594 mUserPaddingBottom);
11595 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011596 }
11597
Dianne Hackborn4702a852012-08-17 15:18:29 -070011598 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011599 }
11600
11601 /**
11602 * Resolve padding depending on the layout direction. Subclasses that care about
11603 * padding resolution should override this method. The default implementation does
11604 * nothing.
11605 *
11606 * @param layoutDirection the direction of the layout
11607 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070011608 * @see #LAYOUT_DIRECTION_LTR
11609 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011610 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011611 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011612 }
11613
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011614 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011615 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011616 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011617 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011618 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011619 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011620 switch (getLayoutDirection()) {
11621 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011622 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011623 default:
11624 return true;
11625 }
11626 }
11627
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011628 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011629 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11630 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011631 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011632 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011633 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011634 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011635 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011636 // Reset also the text direction
11637 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011638 }
11639
11640 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011641 * Called during reset of resolved layout direction.
11642 *
11643 * Subclasses need to override this method to clear cached information that depends on the
11644 * resolved layout direction, or to inform child views that inherit their layout direction.
11645 *
11646 * The default implementation does nothing.
11647 */
11648 public void onResolvedLayoutDirectionReset() {
11649 }
11650
11651 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011652 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011653 *
11654 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011655 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011656 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011657 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070011658 return (LAYOUT_DIRECTION_RTL == TextUtils.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011659 }
11660
11661 /**
11662 * This is called when the view is detached from a window. At this point it
11663 * no longer has a surface for drawing.
11664 *
11665 * @see #onAttachedToWindow()
11666 */
11667 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011668 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011669
Romain Guya440b002010-02-24 15:57:54 -080011670 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011671 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011672 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011673 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011675 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011676
Romain Guya998dff2012-03-23 18:58:36 -070011677 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011678
11679 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011680 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011681 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011682 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011683 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011684 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011685 // Should never happen
11686 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011687 }
11688
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011689 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011690
11691 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011692 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011693 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011694 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 }
11696
11697 /**
11698 * @return The number of times this view has been attached to a window
11699 */
11700 protected int getWindowAttachCount() {
11701 return mWindowAttachCount;
11702 }
11703
11704 /**
11705 * Retrieve a unique token identifying the window this view is attached to.
11706 * @return Return the window's token for use in
11707 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11708 */
11709 public IBinder getWindowToken() {
11710 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11711 }
11712
11713 /**
11714 * Retrieve a unique token identifying the top-level "real" window of
11715 * the window that this view is attached to. That is, this is like
11716 * {@link #getWindowToken}, except if the window this view in is a panel
11717 * window (attached to another containing window), then the token of
11718 * the containing window is returned instead.
11719 *
11720 * @return Returns the associated window token, either
11721 * {@link #getWindowToken()} or the containing window's token.
11722 */
11723 public IBinder getApplicationWindowToken() {
11724 AttachInfo ai = mAttachInfo;
11725 if (ai != null) {
11726 IBinder appWindowToken = ai.mPanelParentWindowToken;
11727 if (appWindowToken == null) {
11728 appWindowToken = ai.mWindowToken;
11729 }
11730 return appWindowToken;
11731 }
11732 return null;
11733 }
11734
11735 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011736 * Gets the logical display to which the view's window has been attached.
11737 *
11738 * @return The logical display, or null if the view is not currently attached to a window.
11739 */
11740 public Display getDisplay() {
11741 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11742 }
11743
11744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745 * Retrieve private session object this view hierarchy is using to
11746 * communicate with the window manager.
11747 * @return the session object to communicate with the window manager
11748 */
11749 /*package*/ IWindowSession getWindowSession() {
11750 return mAttachInfo != null ? mAttachInfo.mSession : null;
11751 }
11752
11753 /**
11754 * @param info the {@link android.view.View.AttachInfo} to associated with
11755 * this view
11756 */
11757 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11758 //System.out.println("Attached! " + this);
11759 mAttachInfo = info;
11760 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011761 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011762 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011763 if (mFloatingTreeObserver != null) {
11764 info.mTreeObserver.merge(mFloatingTreeObserver);
11765 mFloatingTreeObserver = null;
11766 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011767 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011768 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011769 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011770 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011771 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011772 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011773
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011774 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011775 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011776 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011777 if (listeners != null && listeners.size() > 0) {
11778 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11779 // perform the dispatching. The iterator is a safe guard against listeners that
11780 // could mutate the list by calling the various add/remove methods. This prevents
11781 // the array from being modified while we iterate it.
11782 for (OnAttachStateChangeListener listener : listeners) {
11783 listener.onViewAttachedToWindow(this);
11784 }
11785 }
11786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011787 int vis = info.mWindowVisibility;
11788 if (vis != GONE) {
11789 onWindowVisibilityChanged(vis);
11790 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011791 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011792 // If nobody has evaluated the drawable state yet, then do it now.
11793 refreshDrawableState();
11794 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011795 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011796 }
11797
11798 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011799 AttachInfo info = mAttachInfo;
11800 if (info != null) {
11801 int vis = info.mWindowVisibility;
11802 if (vis != GONE) {
11803 onWindowVisibilityChanged(GONE);
11804 }
11805 }
11806
11807 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011808
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011809 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011810 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011811 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011812 if (listeners != null && listeners.size() > 0) {
11813 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11814 // perform the dispatching. The iterator is a safe guard against listeners that
11815 // could mutate the list by calling the various add/remove methods. This prevents
11816 // the array from being modified while we iterate it.
11817 for (OnAttachStateChangeListener listener : listeners) {
11818 listener.onViewDetachedFromWindow(this);
11819 }
11820 }
11821
Dianne Hackborn4702a852012-08-17 15:18:29 -070011822 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011823 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011824 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011827 mAttachInfo = null;
11828 }
11829
11830 /**
11831 * Store this view hierarchy's frozen state into the given container.
11832 *
11833 * @param container The SparseArray in which to save the view's state.
11834 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011835 * @see #restoreHierarchyState(android.util.SparseArray)
11836 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11837 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011838 */
11839 public void saveHierarchyState(SparseArray<Parcelable> container) {
11840 dispatchSaveInstanceState(container);
11841 }
11842
11843 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011844 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11845 * this view and its children. May be overridden to modify how freezing happens to a
11846 * 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 -080011847 *
11848 * @param container The SparseArray in which to save the view's state.
11849 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011850 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11851 * @see #saveHierarchyState(android.util.SparseArray)
11852 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 */
11854 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11855 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011856 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011858 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 throw new IllegalStateException(
11860 "Derived class did not call super.onSaveInstanceState()");
11861 }
11862 if (state != null) {
11863 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11864 // + ": " + state);
11865 container.put(mID, state);
11866 }
11867 }
11868 }
11869
11870 /**
11871 * Hook allowing a view to generate a representation of its internal state
11872 * that can later be used to create a new instance with that same state.
11873 * This state should only contain information that is not persistent or can
11874 * not be reconstructed later. For example, you will never store your
11875 * current position on screen because that will be computed again when a
11876 * new instance of the view is placed in its view hierarchy.
11877 * <p>
11878 * Some examples of things you may store here: the current cursor position
11879 * in a text view (but usually not the text itself since that is stored in a
11880 * content provider or other persistent storage), the currently selected
11881 * item in a list view.
11882 *
11883 * @return Returns a Parcelable object containing the view's current dynamic
11884 * state, or null if there is nothing interesting to save. The
11885 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011886 * @see #onRestoreInstanceState(android.os.Parcelable)
11887 * @see #saveHierarchyState(android.util.SparseArray)
11888 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011889 * @see #setSaveEnabled(boolean)
11890 */
11891 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011892 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011893 return BaseSavedState.EMPTY_STATE;
11894 }
11895
11896 /**
11897 * Restore this view hierarchy's frozen state from the given container.
11898 *
11899 * @param container The SparseArray which holds previously frozen states.
11900 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011901 * @see #saveHierarchyState(android.util.SparseArray)
11902 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11903 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 */
11905 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11906 dispatchRestoreInstanceState(container);
11907 }
11908
11909 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011910 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11911 * state for this view and its children. May be overridden to modify how restoring
11912 * happens to a view's children; for example, some views may want to not store state
11913 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011914 *
11915 * @param container The SparseArray which holds previously saved state.
11916 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011917 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11918 * @see #restoreHierarchyState(android.util.SparseArray)
11919 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 */
11921 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11922 if (mID != NO_ID) {
11923 Parcelable state = container.get(mID);
11924 if (state != null) {
11925 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11926 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011927 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011928 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011929 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 throw new IllegalStateException(
11931 "Derived class did not call super.onRestoreInstanceState()");
11932 }
11933 }
11934 }
11935 }
11936
11937 /**
11938 * Hook allowing a view to re-apply a representation of its internal state that had previously
11939 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11940 * null state.
11941 *
11942 * @param state The frozen state that had previously been returned by
11943 * {@link #onSaveInstanceState}.
11944 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011945 * @see #onSaveInstanceState()
11946 * @see #restoreHierarchyState(android.util.SparseArray)
11947 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948 */
11949 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011950 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011952 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11953 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011954 + "when two views of different type have the same id in the same hierarchy. "
11955 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011956 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011957 }
11958 }
11959
11960 /**
11961 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11962 *
11963 * @return the drawing start time in milliseconds
11964 */
11965 public long getDrawingTime() {
11966 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11967 }
11968
11969 /**
11970 * <p>Enables or disables the duplication of the parent's state into this view. When
11971 * duplication is enabled, this view gets its drawable state from its parent rather
11972 * than from its own internal properties.</p>
11973 *
11974 * <p>Note: in the current implementation, setting this property to true after the
11975 * view was added to a ViewGroup might have no effect at all. This property should
11976 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11977 *
11978 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11979 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011980 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011981 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11982 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011983 *
11984 * @param enabled True to enable duplication of the parent's drawable state, false
11985 * to disable it.
11986 *
11987 * @see #getDrawableState()
11988 * @see #isDuplicateParentStateEnabled()
11989 */
11990 public void setDuplicateParentStateEnabled(boolean enabled) {
11991 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11992 }
11993
11994 /**
11995 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11996 *
11997 * @return True if this view's drawable state is duplicated from the parent,
11998 * false otherwise
11999 *
12000 * @see #getDrawableState()
12001 * @see #setDuplicateParentStateEnabled(boolean)
12002 */
12003 public boolean isDuplicateParentStateEnabled() {
12004 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12005 }
12006
12007 /**
Romain Guy171c5922011-01-06 10:04:23 -080012008 * <p>Specifies the type of layer backing this view. The layer can be
12009 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12010 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012011 *
Romain Guy171c5922011-01-06 10:04:23 -080012012 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12013 * instance that controls how the layer is composed on screen. The following
12014 * properties of the paint are taken into account when composing the layer:</p>
12015 * <ul>
12016 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12017 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12018 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12019 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012020 *
Romain Guy171c5922011-01-06 10:04:23 -080012021 * <p>If this view has an alpha value set to < 1.0 by calling
12022 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12023 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12024 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012025 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012026 *
Romain Guy171c5922011-01-06 10:04:23 -080012027 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12028 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12029 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012030 *
Chet Haased15ebf22012-09-05 11:40:29 -070012031 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012032 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12033 * {@link #LAYER_TYPE_HARDWARE}
12034 * @param paint The paint used to compose the layer. This argument is optional
12035 * and can be null. It is ignored when the layer type is
12036 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012037 *
12038 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012039 * @see #LAYER_TYPE_NONE
12040 * @see #LAYER_TYPE_SOFTWARE
12041 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012042 * @see #setAlpha(float)
12043 *
Romain Guy171c5922011-01-06 10:04:23 -080012044 * @attr ref android.R.styleable#View_layerType
12045 */
12046 public void setLayerType(int layerType, Paint paint) {
12047 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012048 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012049 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12050 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012051
Romain Guyd6cd5722011-01-17 14:42:41 -080012052 if (layerType == mLayerType) {
12053 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12054 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012055 invalidateParentCaches();
12056 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012057 }
12058 return;
12059 }
Romain Guy171c5922011-01-06 10:04:23 -080012060
12061 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012062 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012063 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012064 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012065 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012066 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012067 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012068 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012069 default:
12070 break;
Romain Guy171c5922011-01-06 10:04:23 -080012071 }
12072
12073 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012074 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12075 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12076 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012077
Romain Guy0fd89bf2011-01-26 15:41:30 -080012078 invalidateParentCaches();
12079 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012080 }
12081
12082 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012083 * Updates the {@link Paint} object used with the current layer (used only if the current
12084 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12085 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12086 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12087 * ensure that the view gets redrawn immediately.
12088 *
12089 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12090 * instance that controls how the layer is composed on screen. The following
12091 * properties of the paint are taken into account when composing the layer:</p>
12092 * <ul>
12093 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12094 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12095 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12096 * </ul>
12097 *
12098 * <p>If this view has an alpha value set to < 1.0 by calling
12099 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12100 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12101 * equivalent to setting a hardware layer on this view and providing a paint with
12102 * the desired alpha value.</p>
12103 *
12104 * @param paint The paint used to compose the layer. This argument is optional
12105 * and can be null. It is ignored when the layer type is
12106 * {@link #LAYER_TYPE_NONE}
12107 *
12108 * @see #setLayerType(int, android.graphics.Paint)
12109 */
12110 public void setLayerPaint(Paint paint) {
12111 int layerType = getLayerType();
12112 if (layerType != LAYER_TYPE_NONE) {
12113 mLayerPaint = paint == null ? new Paint() : paint;
12114 if (layerType == LAYER_TYPE_HARDWARE) {
12115 HardwareLayer layer = getHardwareLayer();
12116 if (layer != null) {
12117 layer.setLayerPaint(paint);
12118 }
12119 invalidateViewProperty(false, false);
12120 } else {
12121 invalidate();
12122 }
12123 }
12124 }
12125
12126 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012127 * Indicates whether this view has a static layer. A view with layer type
12128 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12129 * dynamic.
12130 */
12131 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012132 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012133 }
12134
12135 /**
Romain Guy171c5922011-01-06 10:04:23 -080012136 * Indicates what type of layer is currently associated with this view. By default
12137 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12138 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12139 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012140 *
Romain Guy171c5922011-01-06 10:04:23 -080012141 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12142 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012143 *
12144 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012145 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012146 * @see #LAYER_TYPE_NONE
12147 * @see #LAYER_TYPE_SOFTWARE
12148 * @see #LAYER_TYPE_HARDWARE
12149 */
12150 public int getLayerType() {
12151 return mLayerType;
12152 }
Joe Malin32736f02011-01-19 16:14:20 -080012153
Romain Guy6c319ca2011-01-11 14:29:25 -080012154 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012155 * Forces this view's layer to be created and this view to be rendered
12156 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12157 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012158 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012159 * This method can for instance be used to render a view into its layer before
12160 * starting an animation. If this view is complex, rendering into the layer
12161 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012162 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012163 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012164 *
12165 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012166 */
12167 public void buildLayer() {
12168 if (mLayerType == LAYER_TYPE_NONE) return;
12169
12170 if (mAttachInfo == null) {
12171 throw new IllegalStateException("This view must be attached to a window first");
12172 }
12173
12174 switch (mLayerType) {
12175 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012176 if (mAttachInfo.mHardwareRenderer != null &&
12177 mAttachInfo.mHardwareRenderer.isEnabled() &&
12178 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012179 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012180 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012181 break;
12182 case LAYER_TYPE_SOFTWARE:
12183 buildDrawingCache(true);
12184 break;
12185 }
12186 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012187
Romain Guyf1ae1062011-03-02 18:16:04 -080012188 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012189 * <p>Returns a hardware layer that can be used to draw this view again
12190 * without executing its draw method.</p>
12191 *
12192 * @return A HardwareLayer ready to render, or null if an error occurred.
12193 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012194 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012195 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12196 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012197 return null;
12198 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012199
Romain Guy9c4b79a2011-11-10 19:23:58 -080012200 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012201
12202 final int width = mRight - mLeft;
12203 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012204
Romain Guy6c319ca2011-01-11 14:29:25 -080012205 if (width == 0 || height == 0) {
12206 return null;
12207 }
12208
Dianne Hackborn4702a852012-08-17 15:18:29 -070012209 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012210 if (mHardwareLayer == null) {
12211 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12212 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012213 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012214 } else {
12215 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012216 if (mHardwareLayer.resize(width, height)) {
12217 mLocalDirtyRect.set(0, 0, width, height);
12218 }
Romain Guy846a5332012-07-11 17:44:57 -070012219 }
12220
12221 // This should not be necessary but applications that change
12222 // the parameters of their background drawable without calling
12223 // this.setBackground(Drawable) can leave the view in a bad state
12224 // (for instance isOpaque() returns true, but the background is
12225 // not opaque.)
12226 computeOpaqueFlags();
12227
12228 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012229 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012230 mHardwareLayer.setOpaque(opaque);
12231 mLocalDirtyRect.set(0, 0, width, height);
12232 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012233 }
12234
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012235 // The layer is not valid if the underlying GPU resources cannot be allocated
12236 if (!mHardwareLayer.isValid()) {
12237 return null;
12238 }
Chet Haased15ebf22012-09-05 11:40:29 -070012239 mHardwareLayer.setLayerPaint(mLayerPaint);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012240
Chet Haasea1cff502012-02-21 13:43:44 -080012241 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012242 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012243 }
12244
12245 return mHardwareLayer;
12246 }
Romain Guy171c5922011-01-06 10:04:23 -080012247
Romain Guy589b0bb2011-10-10 13:57:47 -070012248 /**
12249 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012250 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012251 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012252 *
12253 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012254 * @see #LAYER_TYPE_HARDWARE
12255 */
Romain Guya998dff2012-03-23 18:58:36 -070012256 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012257 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012258 AttachInfo info = mAttachInfo;
12259 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012260 info.mHardwareRenderer.isEnabled() &&
12261 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012262 mHardwareLayer.destroy();
12263 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012264
Romain Guy9c4b79a2011-11-10 19:23:58 -080012265 invalidate(true);
12266 invalidateParentCaches();
12267 }
Romain Guy65b345f2011-07-27 18:51:50 -070012268 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012269 }
Romain Guy65b345f2011-07-27 18:51:50 -070012270 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012271 }
12272
Romain Guy171c5922011-01-06 10:04:23 -080012273 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012274 * Destroys all hardware rendering resources. This method is invoked
12275 * when the system needs to reclaim resources. Upon execution of this
12276 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012277 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012278 * Note: you <strong>must</strong> call
12279 * <code>super.destroyHardwareResources()</code> when overriding
12280 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012281 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012282 * @hide
12283 */
12284 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012285 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012286 }
12287
12288 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012289 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12290 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12291 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12292 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12293 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12294 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012295 *
Romain Guy171c5922011-01-06 10:04:23 -080012296 * <p>Enabling the drawing cache is similar to
12297 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012298 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12299 * drawing cache has no effect on rendering because the system uses a different mechanism
12300 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12301 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12302 * for information on how to enable software and hardware layers.</p>
12303 *
12304 * <p>This API can be used to manually generate
12305 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12306 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012307 *
12308 * @param enabled true to enable the drawing cache, false otherwise
12309 *
12310 * @see #isDrawingCacheEnabled()
12311 * @see #getDrawingCache()
12312 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012313 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 */
12315 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012316 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12318 }
12319
12320 /**
12321 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12322 *
12323 * @return true if the drawing cache is enabled
12324 *
12325 * @see #setDrawingCacheEnabled(boolean)
12326 * @see #getDrawingCache()
12327 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012328 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012329 public boolean isDrawingCacheEnabled() {
12330 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12331 }
12332
12333 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012334 * Debugging utility which recursively outputs the dirty state of a view and its
12335 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012336 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012337 * @hide
12338 */
Romain Guy676b1732011-02-14 14:45:33 -080012339 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012340 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012341 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12342 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12343 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12344 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012345 if (clear) {
12346 mPrivateFlags &= clearMask;
12347 }
12348 if (this instanceof ViewGroup) {
12349 ViewGroup parent = (ViewGroup) this;
12350 final int count = parent.getChildCount();
12351 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012352 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012353 child.outputDirtyFlags(indent + " ", clear, clearMask);
12354 }
12355 }
12356 }
12357
12358 /**
12359 * This method is used by ViewGroup to cause its children to restore or recreate their
12360 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12361 * to recreate its own display list, which would happen if it went through the normal
12362 * draw/dispatchDraw mechanisms.
12363 *
12364 * @hide
12365 */
12366 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012367
12368 /**
12369 * A view that is not attached or hardware accelerated cannot create a display list.
12370 * This method checks these conditions and returns the appropriate result.
12371 *
12372 * @return true if view has the ability to create a display list, false otherwise.
12373 *
12374 * @hide
12375 */
12376 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012377 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012378 }
Joe Malin32736f02011-01-19 16:14:20 -080012379
Chet Haasedaf98e92011-01-10 14:10:36 -080012380 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012381 * @return The HardwareRenderer associated with that view or null if hardware rendering
12382 * is not supported or this this has not been attached to a window.
12383 *
12384 * @hide
12385 */
12386 public HardwareRenderer getHardwareRenderer() {
12387 if (mAttachInfo != null) {
12388 return mAttachInfo.mHardwareRenderer;
12389 }
12390 return null;
12391 }
12392
12393 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012394 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12395 * Otherwise, the same display list will be returned (after having been rendered into
12396 * along the way, depending on the invalidation state of the view).
12397 *
12398 * @param displayList The previous version of this displayList, could be null.
12399 * @param isLayer Whether the requester of the display list is a layer. If so,
12400 * the view will avoid creating a layer inside the resulting display list.
12401 * @return A new or reused DisplayList object.
12402 */
12403 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12404 if (!canHaveDisplayList()) {
12405 return null;
12406 }
12407
Dianne Hackborn4702a852012-08-17 15:18:29 -070012408 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012409 displayList == null || !displayList.isValid() ||
12410 (!isLayer && mRecreateDisplayList))) {
12411 // Don't need to recreate the display list, just need to tell our
12412 // children to restore/recreate theirs
12413 if (displayList != null && displayList.isValid() &&
12414 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012415 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12416 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012417 dispatchGetDisplayList();
12418
12419 return displayList;
12420 }
12421
12422 if (!isLayer) {
12423 // If we got here, we're recreating it. Mark it as such to ensure that
12424 // we copy in child display lists into ours in drawChild()
12425 mRecreateDisplayList = true;
12426 }
12427 if (displayList == null) {
12428 final String name = getClass().getSimpleName();
12429 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12430 // If we're creating a new display list, make sure our parent gets invalidated
12431 // since they will need to recreate their display list to account for this
12432 // new child display list.
12433 invalidateParentCaches();
12434 }
12435
12436 boolean caching = false;
12437 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012438 int width = mRight - mLeft;
12439 int height = mBottom - mTop;
12440
12441 try {
12442 canvas.setViewport(width, height);
12443 // The dirty rect should always be null for a display list
12444 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012445 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012446 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012447 if (layerType == LAYER_TYPE_HARDWARE) {
12448 final HardwareLayer layer = getHardwareLayer();
12449 if (layer != null && layer.isValid()) {
12450 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12451 } else {
12452 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12453 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12454 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12455 }
12456 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012457 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012458 buildDrawingCache(true);
12459 Bitmap cache = getDrawingCache(true);
12460 if (cache != null) {
12461 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12462 caching = true;
12463 }
Chet Haasea1cff502012-02-21 13:43:44 -080012464 }
Chet Haasea1cff502012-02-21 13:43:44 -080012465 } else {
12466
12467 computeScroll();
12468
Chet Haasea1cff502012-02-21 13:43:44 -080012469 canvas.translate(-mScrollX, -mScrollY);
12470 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012471 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12472 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012473 }
12474
12475 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012476 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012477 dispatchDraw(canvas);
12478 } else {
12479 draw(canvas);
12480 }
12481 }
12482 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012483 canvas.onPostDraw();
12484
12485 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012486 displayList.setCaching(caching);
12487 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012488 displayList.setLeftTopRightBottom(0, 0, width, height);
12489 } else {
12490 setDisplayListProperties(displayList);
12491 }
12492 }
12493 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012494 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12495 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012496 }
12497
12498 return displayList;
12499 }
12500
12501 /**
12502 * Get the DisplayList for the HardwareLayer
12503 *
12504 * @param layer The HardwareLayer whose DisplayList we want
12505 * @return A DisplayList fopr the specified HardwareLayer
12506 */
12507 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12508 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12509 layer.setDisplayList(displayList);
12510 return displayList;
12511 }
12512
12513
12514 /**
Romain Guyb051e892010-09-28 19:09:36 -070012515 * <p>Returns a display list that can be used to draw this view again
12516 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012517 *
Romain Guyb051e892010-09-28 19:09:36 -070012518 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012519 *
12520 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012521 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012522 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012523 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012524 return mDisplayList;
12525 }
12526
Romain Guy38c2ece2012-05-24 14:20:56 -070012527 private void clearDisplayList() {
12528 if (mDisplayList != null) {
12529 mDisplayList.invalidate();
12530 mDisplayList.clear();
12531 }
12532 }
12533
Romain Guyb051e892010-09-28 19:09:36 -070012534 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012535 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012536 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012537 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012538 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012539 * @see #getDrawingCache(boolean)
12540 */
12541 public Bitmap getDrawingCache() {
12542 return getDrawingCache(false);
12543 }
12544
12545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012546 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12547 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12548 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12549 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12550 * request the drawing cache by calling this method and draw it on screen if the
12551 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012552 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012553 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12554 * this method will create a bitmap of the same size as this view. Because this bitmap
12555 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12556 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12557 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12558 * size than the view. This implies that your application must be able to handle this
12559 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012560 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012561 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12562 * the current density of the screen when the application is in compatibility
12563 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012565 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012567 * @see #setDrawingCacheEnabled(boolean)
12568 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012569 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012570 * @see #destroyDrawingCache()
12571 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012572 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12574 return null;
12575 }
12576 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012577 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012578 }
Romain Guy02890fd2010-08-06 17:58:44 -070012579 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012580 }
12581
12582 /**
12583 * <p>Frees the resources used by the drawing cache. If you call
12584 * {@link #buildDrawingCache()} manually without calling
12585 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12586 * should cleanup the cache with this method afterwards.</p>
12587 *
12588 * @see #setDrawingCacheEnabled(boolean)
12589 * @see #buildDrawingCache()
12590 * @see #getDrawingCache()
12591 */
12592 public void destroyDrawingCache() {
12593 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012594 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 mDrawingCache = null;
12596 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012597 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012598 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012599 mUnscaledDrawingCache = null;
12600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012601 }
12602
12603 /**
12604 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012605 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012606 * view will always be drawn on top of a solid color.
12607 *
12608 * @param color The background color to use for the drawing cache's bitmap
12609 *
12610 * @see #setDrawingCacheEnabled(boolean)
12611 * @see #buildDrawingCache()
12612 * @see #getDrawingCache()
12613 */
12614 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012615 if (color != mDrawingCacheBackgroundColor) {
12616 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012617 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619 }
12620
12621 /**
12622 * @see #setDrawingCacheBackgroundColor(int)
12623 *
12624 * @return The background color to used for the drawing cache's bitmap
12625 */
12626 public int getDrawingCacheBackgroundColor() {
12627 return mDrawingCacheBackgroundColor;
12628 }
12629
12630 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012631 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012632 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012633 * @see #buildDrawingCache(boolean)
12634 */
12635 public void buildDrawingCache() {
12636 buildDrawingCache(false);
12637 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012638
Romain Guyfbd8f692009-06-26 14:51:58 -070012639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12641 *
12642 * <p>If you call {@link #buildDrawingCache()} manually without calling
12643 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12644 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012645 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012646 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12647 * this method will create a bitmap of the same size as this view. Because this bitmap
12648 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12649 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12650 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12651 * size than the view. This implies that your application must be able to handle this
12652 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012653 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012654 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12655 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012656 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012657 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 *
12659 * @see #getDrawingCache()
12660 * @see #destroyDrawingCache()
12661 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012662 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012663 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012664 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012665 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012666
Romain Guy8506ab42009-06-11 17:35:47 -070012667 int width = mRight - mLeft;
12668 int height = mBottom - mTop;
12669
12670 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012671 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012672
Romain Guye1123222009-06-29 14:24:56 -070012673 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012674 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12675 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012677
12678 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012679 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012680 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012681
Chris Craik10e9d1d2012-09-06 14:42:44 -070012682 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12683 final int drawingCacheSize =
12684 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12685 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012686 if (width > 0 && height > 0) {
12687 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12688 + projectedBitmapSize + " bytes, only "
12689 + drawingCacheSize + " available");
12690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012692 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 return;
12694 }
12695
12696 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012697 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012698
12699 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 Bitmap.Config quality;
12701 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012702 // Never pick ARGB_4444 because it looks awful
12703 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12705 case DRAWING_CACHE_QUALITY_AUTO:
12706 quality = Bitmap.Config.ARGB_8888;
12707 break;
12708 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012709 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 break;
12711 case DRAWING_CACHE_QUALITY_HIGH:
12712 quality = Bitmap.Config.ARGB_8888;
12713 break;
12714 default:
12715 quality = Bitmap.Config.ARGB_8888;
12716 break;
12717 }
12718 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012719 // Optimization for translucent windows
12720 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012721 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722 }
12723
12724 // Try to cleanup memory
12725 if (bitmap != null) bitmap.recycle();
12726
12727 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012728 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12729 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012730 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012731 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012732 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012733 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012734 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012735 }
Adam Powell26153a32010-11-08 15:22:27 -080012736 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012737 } catch (OutOfMemoryError e) {
12738 // If there is not enough memory to create the bitmap cache, just
12739 // ignore the issue as bitmap caches are not required to draw the
12740 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012741 if (autoScale) {
12742 mDrawingCache = null;
12743 } else {
12744 mUnscaledDrawingCache = null;
12745 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012746 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747 return;
12748 }
12749
12750 clear = drawingCacheBackgroundColor != 0;
12751 }
12752
12753 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012754 if (attachInfo != null) {
12755 canvas = attachInfo.mCanvas;
12756 if (canvas == null) {
12757 canvas = new Canvas();
12758 }
12759 canvas.setBitmap(bitmap);
12760 // Temporarily clobber the cached Canvas in case one of our children
12761 // is also using a drawing cache. Without this, the children would
12762 // steal the canvas by attaching their own bitmap to it and bad, bad
12763 // thing would happen (invisible views, corrupted drawings, etc.)
12764 attachInfo.mCanvas = null;
12765 } else {
12766 // This case should hopefully never or seldom happen
12767 canvas = new Canvas(bitmap);
12768 }
12769
12770 if (clear) {
12771 bitmap.eraseColor(drawingCacheBackgroundColor);
12772 }
12773
12774 computeScroll();
12775 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012776
Romain Guye1123222009-06-29 14:24:56 -070012777 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012778 final float scale = attachInfo.mApplicationScale;
12779 canvas.scale(scale, scale);
12780 }
Joe Malin32736f02011-01-19 16:14:20 -080012781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 canvas.translate(-mScrollX, -mScrollY);
12783
Dianne Hackborn4702a852012-08-17 15:18:29 -070012784 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012785 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12786 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012787 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012789
12790 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012791 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12792 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012793 dispatchDraw(canvas);
12794 } else {
12795 draw(canvas);
12796 }
12797
12798 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012799 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012800
12801 if (attachInfo != null) {
12802 // Restore the cached Canvas for our siblings
12803 attachInfo.mCanvas = canvas;
12804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805 }
12806 }
12807
12808 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012809 * Create a snapshot of the view into a bitmap. We should probably make
12810 * some form of this public, but should think about the API.
12811 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012812 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012813 int width = mRight - mLeft;
12814 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012815
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012816 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012817 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012818 width = (int) ((width * scale) + 0.5f);
12819 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012820
Dianne Hackborndde331c2012-08-03 14:01:57 -070012821 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12822 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012823 if (bitmap == null) {
12824 throw new OutOfMemoryError();
12825 }
12826
Romain Guyc529d8d2011-09-06 15:01:39 -070012827 Resources resources = getResources();
12828 if (resources != null) {
12829 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12830 }
Joe Malin32736f02011-01-19 16:14:20 -080012831
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012832 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012833 if (attachInfo != null) {
12834 canvas = attachInfo.mCanvas;
12835 if (canvas == null) {
12836 canvas = new Canvas();
12837 }
12838 canvas.setBitmap(bitmap);
12839 // Temporarily clobber the cached Canvas in case one of our children
12840 // is also using a drawing cache. Without this, the children would
12841 // steal the canvas by attaching their own bitmap to it and bad, bad
12842 // things would happen (invisible views, corrupted drawings, etc.)
12843 attachInfo.mCanvas = null;
12844 } else {
12845 // This case should hopefully never or seldom happen
12846 canvas = new Canvas(bitmap);
12847 }
12848
Romain Guy5bcdff42009-05-14 21:27:18 -070012849 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012850 bitmap.eraseColor(backgroundColor);
12851 }
12852
12853 computeScroll();
12854 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012855 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012856 canvas.translate(-mScrollX, -mScrollY);
12857
Romain Guy5bcdff42009-05-14 21:27:18 -070012858 // Temporarily remove the dirty mask
12859 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012860 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012861
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012862 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012863 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012864 dispatchDraw(canvas);
12865 } else {
12866 draw(canvas);
12867 }
12868
Romain Guy5bcdff42009-05-14 21:27:18 -070012869 mPrivateFlags = flags;
12870
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012871 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012872 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012873
12874 if (attachInfo != null) {
12875 // Restore the cached Canvas for our siblings
12876 attachInfo.mCanvas = canvas;
12877 }
Romain Guy8506ab42009-06-11 17:35:47 -070012878
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012879 return bitmap;
12880 }
12881
12882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012883 * Indicates whether this View is currently in edit mode. A View is usually
12884 * in edit mode when displayed within a developer tool. For instance, if
12885 * this View is being drawn by a visual user interface builder, this method
12886 * should return true.
12887 *
12888 * Subclasses should check the return value of this method to provide
12889 * different behaviors if their normal behavior might interfere with the
12890 * host environment. For instance: the class spawns a thread in its
12891 * constructor, the drawing code relies on device-specific features, etc.
12892 *
12893 * This method is usually checked in the drawing code of custom widgets.
12894 *
12895 * @return True if this View is in edit mode, false otherwise.
12896 */
12897 public boolean isInEditMode() {
12898 return false;
12899 }
12900
12901 /**
12902 * If the View draws content inside its padding and enables fading edges,
12903 * it needs to support padding offsets. Padding offsets are added to the
12904 * fading edges to extend the length of the fade so that it covers pixels
12905 * drawn inside the padding.
12906 *
12907 * Subclasses of this class should override this method if they need
12908 * to draw content inside the padding.
12909 *
12910 * @return True if padding offset must be applied, false otherwise.
12911 *
12912 * @see #getLeftPaddingOffset()
12913 * @see #getRightPaddingOffset()
12914 * @see #getTopPaddingOffset()
12915 * @see #getBottomPaddingOffset()
12916 *
12917 * @since CURRENT
12918 */
12919 protected boolean isPaddingOffsetRequired() {
12920 return false;
12921 }
12922
12923 /**
12924 * Amount by which to extend the left fading region. Called only when
12925 * {@link #isPaddingOffsetRequired()} returns true.
12926 *
12927 * @return The left padding offset in pixels.
12928 *
12929 * @see #isPaddingOffsetRequired()
12930 *
12931 * @since CURRENT
12932 */
12933 protected int getLeftPaddingOffset() {
12934 return 0;
12935 }
12936
12937 /**
12938 * Amount by which to extend the right fading region. Called only when
12939 * {@link #isPaddingOffsetRequired()} returns true.
12940 *
12941 * @return The right padding offset in pixels.
12942 *
12943 * @see #isPaddingOffsetRequired()
12944 *
12945 * @since CURRENT
12946 */
12947 protected int getRightPaddingOffset() {
12948 return 0;
12949 }
12950
12951 /**
12952 * Amount by which to extend the top fading region. Called only when
12953 * {@link #isPaddingOffsetRequired()} returns true.
12954 *
12955 * @return The top padding offset in pixels.
12956 *
12957 * @see #isPaddingOffsetRequired()
12958 *
12959 * @since CURRENT
12960 */
12961 protected int getTopPaddingOffset() {
12962 return 0;
12963 }
12964
12965 /**
12966 * Amount by which to extend the bottom fading region. Called only when
12967 * {@link #isPaddingOffsetRequired()} returns true.
12968 *
12969 * @return The bottom padding offset in pixels.
12970 *
12971 * @see #isPaddingOffsetRequired()
12972 *
12973 * @since CURRENT
12974 */
12975 protected int getBottomPaddingOffset() {
12976 return 0;
12977 }
12978
12979 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012980 * @hide
12981 * @param offsetRequired
12982 */
12983 protected int getFadeTop(boolean offsetRequired) {
12984 int top = mPaddingTop;
12985 if (offsetRequired) top += getTopPaddingOffset();
12986 return top;
12987 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012988
Romain Guyf2fc4602011-07-19 15:20:03 -070012989 /**
12990 * @hide
12991 * @param offsetRequired
12992 */
12993 protected int getFadeHeight(boolean offsetRequired) {
12994 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012995 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012996 return mBottom - mTop - mPaddingBottom - padding;
12997 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012998
Romain Guyf2fc4602011-07-19 15:20:03 -070012999 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013000 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013001 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013002 *
Romain Guy2bffd262010-09-12 17:40:02 -070013003 * <p>Even if this method returns true, it does not mean that every call
13004 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13005 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013006 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013007 * window is hardware accelerated,
13008 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13009 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013010 *
Romain Guy2bffd262010-09-12 17:40:02 -070013011 * @return True if the view is attached to a window and the window is
13012 * hardware accelerated; false in any other case.
13013 */
13014 public boolean isHardwareAccelerated() {
13015 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13016 }
Joe Malin32736f02011-01-19 16:14:20 -080013017
Romain Guy2bffd262010-09-12 17:40:02 -070013018 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013019 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13020 * case of an active Animation being run on the view.
13021 */
13022 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13023 Animation a, boolean scalingRequired) {
13024 Transformation invalidationTransform;
13025 final int flags = parent.mGroupFlags;
13026 final boolean initialized = a.isInitialized();
13027 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013028 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013029 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013030 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013031 onAnimationStart();
13032 }
13033
13034 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13035 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13036 if (parent.mInvalidationTransformation == null) {
13037 parent.mInvalidationTransformation = new Transformation();
13038 }
13039 invalidationTransform = parent.mInvalidationTransformation;
13040 a.getTransformation(drawingTime, invalidationTransform, 1f);
13041 } else {
13042 invalidationTransform = parent.mChildTransformation;
13043 }
Romain Guy393a52c2012-05-22 20:21:08 -070013044
Chet Haasebcca79a2012-02-14 08:45:14 -080013045 if (more) {
13046 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013047 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13048 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13049 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13050 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013051 // The child need to draw an animation, potentially offscreen, so
13052 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013053 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013054 parent.invalidate(mLeft, mTop, mRight, mBottom);
13055 }
13056 } else {
13057 if (parent.mInvalidateRegion == null) {
13058 parent.mInvalidateRegion = new RectF();
13059 }
13060 final RectF region = parent.mInvalidateRegion;
13061 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13062 invalidationTransform);
13063
13064 // The child need to draw an animation, potentially offscreen, so
13065 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013066 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013067
13068 final int left = mLeft + (int) region.left;
13069 final int top = mTop + (int) region.top;
13070 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13071 top + (int) (region.height() + .5f));
13072 }
13073 }
13074 return more;
13075 }
13076
Chet Haasea1cff502012-02-21 13:43:44 -080013077 /**
13078 * This method is called by getDisplayList() when a display list is created or re-rendered.
13079 * It sets or resets the current value of all properties on that display list (resetting is
13080 * necessary when a display list is being re-created, because we need to make sure that
13081 * previously-set transform values
13082 */
13083 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013084 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013085 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013086 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013087 if (mParent instanceof ViewGroup) {
13088 displayList.setClipChildren(
13089 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13090 }
Chet Haase9420abd2012-03-29 16:28:32 -070013091 float alpha = 1;
13092 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13093 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13094 ViewGroup parentVG = (ViewGroup) mParent;
13095 final boolean hasTransform =
13096 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13097 if (hasTransform) {
13098 Transformation transform = parentVG.mChildTransformation;
13099 final int transformType = parentVG.mChildTransformation.getTransformationType();
13100 if (transformType != Transformation.TYPE_IDENTITY) {
13101 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13102 alpha = transform.getAlpha();
13103 }
13104 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13105 displayList.setStaticMatrix(transform.getMatrix());
13106 }
13107 }
13108 }
Chet Haasea1cff502012-02-21 13:43:44 -080013109 }
13110 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013111 alpha *= mTransformationInfo.mAlpha;
13112 if (alpha < 1) {
13113 final int multipliedAlpha = (int) (255 * alpha);
13114 if (onSetAlpha(multipliedAlpha)) {
13115 alpha = 1;
13116 }
13117 }
13118 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013119 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13120 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13121 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13122 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013123 if (mTransformationInfo.mCamera == null) {
13124 mTransformationInfo.mCamera = new Camera();
13125 mTransformationInfo.matrix3D = new Matrix();
13126 }
13127 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013128 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013129 displayList.setPivotX(getPivotX());
13130 displayList.setPivotY(getPivotY());
13131 }
Chet Haase9420abd2012-03-29 16:28:32 -070013132 } else if (alpha < 1) {
13133 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013134 }
13135 }
13136 }
13137
Chet Haasebcca79a2012-02-14 08:45:14 -080013138 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013139 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13140 * This draw() method is an implementation detail and is not intended to be overridden or
13141 * to be called from anywhere else other than ViewGroup.drawChild().
13142 */
13143 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013144 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013145 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013146 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013147 final int flags = parent.mGroupFlags;
13148
Chet Haasea1cff502012-02-21 13:43:44 -080013149 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013150 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013151 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013152 }
13153
13154 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013155 boolean concatMatrix = false;
13156
13157 boolean scalingRequired = false;
13158 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013159 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013160
13161 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013162 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13163 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013164 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013165 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013166 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13167 } else {
13168 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13169 }
13170
Chet Haasebcca79a2012-02-14 08:45:14 -080013171 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013172 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013173 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013174 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013175 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013176 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013177 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013178 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013179 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013180 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013181 mDisplayList != null) {
13182 // No longer animating: clear out old animation matrix
13183 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013184 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013185 }
13186 if (!useDisplayListProperties &&
13187 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13188 final boolean hasTransform =
13189 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13190 if (hasTransform) {
13191 final int transformType = parent.mChildTransformation.getTransformationType();
13192 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13193 parent.mChildTransformation : null;
13194 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13195 }
Chet Haase64a48c12012-02-13 16:33:29 -080013196 }
13197 }
13198
13199 concatMatrix |= !childHasIdentityMatrix;
13200
13201 // Sets the flag as early as possible to allow draw() implementations
13202 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013203 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013204
Romain Guyfbb43212012-08-30 15:19:27 -070013205 if (!concatMatrix &&
13206 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13207 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013208 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013209 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13210 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013211 return more;
13212 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013213 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013214
13215 if (hardwareAccelerated) {
13216 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13217 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013218 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13219 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013220 }
13221
Chet Haase64a48c12012-02-13 16:33:29 -080013222 DisplayList displayList = null;
13223 Bitmap cache = null;
13224 boolean hasDisplayList = false;
13225 if (caching) {
13226 if (!hardwareAccelerated) {
13227 if (layerType != LAYER_TYPE_NONE) {
13228 layerType = LAYER_TYPE_SOFTWARE;
13229 buildDrawingCache(true);
13230 }
13231 cache = getDrawingCache(true);
13232 } else {
13233 switch (layerType) {
13234 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013235 if (useDisplayListProperties) {
13236 hasDisplayList = canHaveDisplayList();
13237 } else {
13238 buildDrawingCache(true);
13239 cache = getDrawingCache(true);
13240 }
Chet Haase64a48c12012-02-13 16:33:29 -080013241 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013242 case LAYER_TYPE_HARDWARE:
13243 if (useDisplayListProperties) {
13244 hasDisplayList = canHaveDisplayList();
13245 }
13246 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013247 case LAYER_TYPE_NONE:
13248 // Delay getting the display list until animation-driven alpha values are
13249 // set up and possibly passed on to the view
13250 hasDisplayList = canHaveDisplayList();
13251 break;
13252 }
13253 }
13254 }
Chet Haasea1cff502012-02-21 13:43:44 -080013255 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013256 if (useDisplayListProperties) {
13257 displayList = getDisplayList();
13258 if (!displayList.isValid()) {
13259 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13260 // to getDisplayList(), the display list will be marked invalid and we should not
13261 // try to use it again.
13262 displayList = null;
13263 hasDisplayList = false;
13264 useDisplayListProperties = false;
13265 }
13266 }
Chet Haase64a48c12012-02-13 16:33:29 -080013267
Chet Haase526057b2012-07-12 17:50:41 -070013268 int sx = 0;
13269 int sy = 0;
13270 if (!hasDisplayList) {
13271 computeScroll();
13272 sx = mScrollX;
13273 sy = mScrollY;
13274 }
13275
Chet Haase64a48c12012-02-13 16:33:29 -080013276 final boolean hasNoCache = cache == null || hasDisplayList;
13277 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13278 layerType != LAYER_TYPE_HARDWARE;
13279
Chet Haasea1cff502012-02-21 13:43:44 -080013280 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013281 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013282 restoreTo = canvas.save();
13283 }
Chet Haase64a48c12012-02-13 16:33:29 -080013284 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013285 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013286 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013287 if (!useDisplayListProperties) {
13288 canvas.translate(mLeft, mTop);
13289 }
Chet Haase64a48c12012-02-13 16:33:29 -080013290 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013291 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013292 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013293 restoreTo = canvas.save();
13294 }
Chet Haase64a48c12012-02-13 16:33:29 -080013295 // mAttachInfo cannot be null, otherwise scalingRequired == false
13296 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13297 canvas.scale(scale, scale);
13298 }
13299 }
13300
Chet Haasea1cff502012-02-21 13:43:44 -080013301 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013302 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013303 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013304 if (transformToApply != null || !childHasIdentityMatrix) {
13305 int transX = 0;
13306 int transY = 0;
13307
13308 if (offsetForScroll) {
13309 transX = -sx;
13310 transY = -sy;
13311 }
13312
13313 if (transformToApply != null) {
13314 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013315 if (useDisplayListProperties) {
13316 displayList.setAnimationMatrix(transformToApply.getMatrix());
13317 } else {
13318 // Undo the scroll translation, apply the transformation matrix,
13319 // then redo the scroll translate to get the correct result.
13320 canvas.translate(-transX, -transY);
13321 canvas.concat(transformToApply.getMatrix());
13322 canvas.translate(transX, transY);
13323 }
Chet Haasea1cff502012-02-21 13:43:44 -080013324 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013325 }
13326
13327 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013328 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013329 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013330 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013331 }
13332 }
13333
Chet Haasea1cff502012-02-21 13:43:44 -080013334 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013335 canvas.translate(-transX, -transY);
13336 canvas.concat(getMatrix());
13337 canvas.translate(transX, transY);
13338 }
13339 }
13340
Chet Haase21433372012-06-05 07:54:09 -070013341 // Deal with alpha if it is or used to be <1
13342 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013343 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013344 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013345 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013346 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013347 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013348 }
Chet Haasea1cff502012-02-21 13:43:44 -080013349 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013350 if (hasNoCache) {
13351 final int multipliedAlpha = (int) (255 * alpha);
13352 if (!onSetAlpha(multipliedAlpha)) {
13353 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013354 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013355 layerType != LAYER_TYPE_NONE) {
13356 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13357 }
Chet Haase9420abd2012-03-29 16:28:32 -070013358 if (useDisplayListProperties) {
13359 displayList.setAlpha(alpha * getAlpha());
13360 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013361 final int scrollX = hasDisplayList ? 0 : sx;
13362 final int scrollY = hasDisplayList ? 0 : sy;
13363 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13364 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013365 }
13366 } else {
13367 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013368 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013369 }
13370 }
13371 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013372 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013373 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013374 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013375 }
13376
Chet Haasea1cff502012-02-21 13:43:44 -080013377 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13378 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013379 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013380 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013381 } else {
13382 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013383 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013384 } else {
13385 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13386 }
13387 }
13388 }
13389
Chet Haase9420abd2012-03-29 16:28:32 -070013390 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013391 displayList = getDisplayList();
13392 if (!displayList.isValid()) {
13393 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13394 // to getDisplayList(), the display list will be marked invalid and we should not
13395 // try to use it again.
13396 displayList = null;
13397 hasDisplayList = false;
13398 }
13399 }
13400
13401 if (hasNoCache) {
13402 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013403 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013404 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013405 if (layer != null && layer.isValid()) {
13406 mLayerPaint.setAlpha((int) (alpha * 255));
13407 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13408 layerRendered = true;
13409 } else {
13410 final int scrollX = hasDisplayList ? 0 : sx;
13411 final int scrollY = hasDisplayList ? 0 : sy;
13412 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013413 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013414 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13415 }
13416 }
13417
13418 if (!layerRendered) {
13419 if (!hasDisplayList) {
13420 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013421 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13422 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013423 dispatchDraw(canvas);
13424 } else {
13425 draw(canvas);
13426 }
13427 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013428 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013429 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013430 }
13431 }
13432 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013433 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013434 Paint cachePaint;
13435
13436 if (layerType == LAYER_TYPE_NONE) {
13437 cachePaint = parent.mCachePaint;
13438 if (cachePaint == null) {
13439 cachePaint = new Paint();
13440 cachePaint.setDither(false);
13441 parent.mCachePaint = cachePaint;
13442 }
Chet Haase9420abd2012-03-29 16:28:32 -070013443 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013444 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013445 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13446 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013447 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013448 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013449 }
13450 } else {
13451 cachePaint = mLayerPaint;
13452 cachePaint.setAlpha((int) (alpha * 255));
13453 }
13454 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13455 }
13456
Chet Haasea1cff502012-02-21 13:43:44 -080013457 if (restoreTo >= 0) {
13458 canvas.restoreToCount(restoreTo);
13459 }
Chet Haase64a48c12012-02-13 16:33:29 -080013460
13461 if (a != null && !more) {
13462 if (!hardwareAccelerated && !a.getFillAfter()) {
13463 onSetAlpha(255);
13464 }
13465 parent.finishAnimatingView(this, a);
13466 }
13467
13468 if (more && hardwareAccelerated) {
13469 // invalidation is the trigger to recreate display lists, so if we're using
13470 // display lists to render, force an invalidate to allow the animation to
13471 // continue drawing another frame
13472 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013473 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013474 // alpha animations should cause the child to recreate its display list
13475 invalidate(true);
13476 }
13477 }
13478
13479 mRecreateDisplayList = false;
13480
13481 return more;
13482 }
13483
13484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013485 * Manually render this view (and all of its children) to the given Canvas.
13486 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013487 * called. When implementing a view, implement
13488 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13489 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 *
13491 * @param canvas The Canvas to which the View is rendered.
13492 */
13493 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013494 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013495 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013496 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013497 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013499 /*
13500 * Draw traversal performs several drawing steps which must be executed
13501 * in the appropriate order:
13502 *
13503 * 1. Draw the background
13504 * 2. If necessary, save the canvas' layers to prepare for fading
13505 * 3. Draw view's content
13506 * 4. Draw children
13507 * 5. If necessary, draw the fading edges and restore layers
13508 * 6. Draw decorations (scrollbars for instance)
13509 */
13510
13511 // Step 1, draw the background, if needed
13512 int saveCount;
13513
Romain Guy24443ea2009-05-11 11:56:30 -070013514 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013515 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013516 if (background != null) {
13517 final int scrollX = mScrollX;
13518 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013519
Romain Guy24443ea2009-05-11 11:56:30 -070013520 if (mBackgroundSizeChanged) {
13521 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13522 mBackgroundSizeChanged = false;
13523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013524
Romain Guy24443ea2009-05-11 11:56:30 -070013525 if ((scrollX | scrollY) == 0) {
13526 background.draw(canvas);
13527 } else {
13528 canvas.translate(scrollX, scrollY);
13529 background.draw(canvas);
13530 canvas.translate(-scrollX, -scrollY);
13531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 }
13533 }
13534
13535 // skip step 2 & 5 if possible (common case)
13536 final int viewFlags = mViewFlags;
13537 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13538 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13539 if (!verticalEdges && !horizontalEdges) {
13540 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013541 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542
13543 // Step 4, draw the children
13544 dispatchDraw(canvas);
13545
13546 // Step 6, draw decorations (scrollbars)
13547 onDrawScrollBars(canvas);
13548
13549 // we're done...
13550 return;
13551 }
13552
13553 /*
13554 * Here we do the full fledged routine...
13555 * (this is an uncommon case where speed matters less,
13556 * this is why we repeat some of the tests that have been
13557 * done above)
13558 */
13559
13560 boolean drawTop = false;
13561 boolean drawBottom = false;
13562 boolean drawLeft = false;
13563 boolean drawRight = false;
13564
13565 float topFadeStrength = 0.0f;
13566 float bottomFadeStrength = 0.0f;
13567 float leftFadeStrength = 0.0f;
13568 float rightFadeStrength = 0.0f;
13569
13570 // Step 2, save the canvas' layers
13571 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013572
13573 final boolean offsetRequired = isPaddingOffsetRequired();
13574 if (offsetRequired) {
13575 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013576 }
13577
13578 int left = mScrollX + paddingLeft;
13579 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013580 int top = mScrollY + getFadeTop(offsetRequired);
13581 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013582
13583 if (offsetRequired) {
13584 right += getRightPaddingOffset();
13585 bottom += getBottomPaddingOffset();
13586 }
13587
13588 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013589 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013590 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013591
13592 // clip the fade length if top and bottom fades overlap
13593 // overlapping fades produce odd-looking artifacts
13594 if (verticalEdges && (top + length > bottom - length)) {
13595 length = (bottom - top) / 2;
13596 }
13597
13598 // also clip horizontal fades if necessary
13599 if (horizontalEdges && (left + length > right - length)) {
13600 length = (right - left) / 2;
13601 }
13602
13603 if (verticalEdges) {
13604 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013605 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013606 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013607 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 }
13609
13610 if (horizontalEdges) {
13611 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013612 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013613 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013614 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013615 }
13616
13617 saveCount = canvas.getSaveCount();
13618
13619 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013620 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013621 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13622
13623 if (drawTop) {
13624 canvas.saveLayer(left, top, right, top + length, null, flags);
13625 }
13626
13627 if (drawBottom) {
13628 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13629 }
13630
13631 if (drawLeft) {
13632 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13633 }
13634
13635 if (drawRight) {
13636 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13637 }
13638 } else {
13639 scrollabilityCache.setFadeColor(solidColor);
13640 }
13641
13642 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013643 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644
13645 // Step 4, draw the children
13646 dispatchDraw(canvas);
13647
13648 // Step 5, draw the fade effect and restore layers
13649 final Paint p = scrollabilityCache.paint;
13650 final Matrix matrix = scrollabilityCache.matrix;
13651 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013652
13653 if (drawTop) {
13654 matrix.setScale(1, fadeHeight * topFadeStrength);
13655 matrix.postTranslate(left, top);
13656 fade.setLocalMatrix(matrix);
13657 canvas.drawRect(left, top, right, top + length, p);
13658 }
13659
13660 if (drawBottom) {
13661 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13662 matrix.postRotate(180);
13663 matrix.postTranslate(left, bottom);
13664 fade.setLocalMatrix(matrix);
13665 canvas.drawRect(left, bottom - length, right, bottom, p);
13666 }
13667
13668 if (drawLeft) {
13669 matrix.setScale(1, fadeHeight * leftFadeStrength);
13670 matrix.postRotate(-90);
13671 matrix.postTranslate(left, top);
13672 fade.setLocalMatrix(matrix);
13673 canvas.drawRect(left, top, left + length, bottom, p);
13674 }
13675
13676 if (drawRight) {
13677 matrix.setScale(1, fadeHeight * rightFadeStrength);
13678 matrix.postRotate(90);
13679 matrix.postTranslate(right, top);
13680 fade.setLocalMatrix(matrix);
13681 canvas.drawRect(right - length, top, right, bottom, p);
13682 }
13683
13684 canvas.restoreToCount(saveCount);
13685
13686 // Step 6, draw decorations (scrollbars)
13687 onDrawScrollBars(canvas);
13688 }
13689
13690 /**
13691 * Override this if your view is known to always be drawn on top of a solid color background,
13692 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13693 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13694 * should be set to 0xFF.
13695 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013696 * @see #setVerticalFadingEdgeEnabled(boolean)
13697 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013698 *
13699 * @return The known solid color background for this view, or 0 if the color may vary
13700 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013701 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702 public int getSolidColor() {
13703 return 0;
13704 }
13705
13706 /**
13707 * Build a human readable string representation of the specified view flags.
13708 *
13709 * @param flags the view flags to convert to a string
13710 * @return a String representing the supplied flags
13711 */
13712 private static String printFlags(int flags) {
13713 String output = "";
13714 int numFlags = 0;
13715 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13716 output += "TAKES_FOCUS";
13717 numFlags++;
13718 }
13719
13720 switch (flags & VISIBILITY_MASK) {
13721 case INVISIBLE:
13722 if (numFlags > 0) {
13723 output += " ";
13724 }
13725 output += "INVISIBLE";
13726 // USELESS HERE numFlags++;
13727 break;
13728 case GONE:
13729 if (numFlags > 0) {
13730 output += " ";
13731 }
13732 output += "GONE";
13733 // USELESS HERE numFlags++;
13734 break;
13735 default:
13736 break;
13737 }
13738 return output;
13739 }
13740
13741 /**
13742 * Build a human readable string representation of the specified private
13743 * view flags.
13744 *
13745 * @param privateFlags the private view flags to convert to a string
13746 * @return a String representing the supplied flags
13747 */
13748 private static String printPrivateFlags(int privateFlags) {
13749 String output = "";
13750 int numFlags = 0;
13751
Dianne Hackborn4702a852012-08-17 15:18:29 -070013752 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 output += "WANTS_FOCUS";
13754 numFlags++;
13755 }
13756
Dianne Hackborn4702a852012-08-17 15:18:29 -070013757 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 if (numFlags > 0) {
13759 output += " ";
13760 }
13761 output += "FOCUSED";
13762 numFlags++;
13763 }
13764
Dianne Hackborn4702a852012-08-17 15:18:29 -070013765 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013766 if (numFlags > 0) {
13767 output += " ";
13768 }
13769 output += "SELECTED";
13770 numFlags++;
13771 }
13772
Dianne Hackborn4702a852012-08-17 15:18:29 -070013773 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013774 if (numFlags > 0) {
13775 output += " ";
13776 }
13777 output += "IS_ROOT_NAMESPACE";
13778 numFlags++;
13779 }
13780
Dianne Hackborn4702a852012-08-17 15:18:29 -070013781 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013782 if (numFlags > 0) {
13783 output += " ";
13784 }
13785 output += "HAS_BOUNDS";
13786 numFlags++;
13787 }
13788
Dianne Hackborn4702a852012-08-17 15:18:29 -070013789 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013790 if (numFlags > 0) {
13791 output += " ";
13792 }
13793 output += "DRAWN";
13794 // USELESS HERE numFlags++;
13795 }
13796 return output;
13797 }
13798
13799 /**
13800 * <p>Indicates whether or not this view's layout will be requested during
13801 * the next hierarchy layout pass.</p>
13802 *
13803 * @return true if the layout will be forced during next layout pass
13804 */
13805 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013806 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013807 }
13808
13809 /**
13810 * Assign a size and position to a view and all of its
13811 * descendants
13812 *
13813 * <p>This is the second phase of the layout mechanism.
13814 * (The first is measuring). In this phase, each parent calls
13815 * layout on all of its children to position them.
13816 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013817 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013818 *
Chet Haase9c087442011-01-12 16:20:16 -080013819 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820 * Derived classes with children should override
13821 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013822 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013823 *
13824 * @param l Left position, relative to parent
13825 * @param t Top position, relative to parent
13826 * @param r Right position, relative to parent
13827 * @param b Bottom position, relative to parent
13828 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013829 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013830 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013831 int oldL = mLeft;
13832 int oldT = mTop;
13833 int oldB = mBottom;
13834 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013836 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013837 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013838 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013839
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013840 ListenerInfo li = mListenerInfo;
13841 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013842 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013843 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013844 int numListeners = listenersCopy.size();
13845 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013846 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013847 }
13848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013849 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013850 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013851 }
13852
13853 /**
13854 * Called from layout when this view should
13855 * assign a size and position to each of its children.
13856 *
13857 * Derived classes with children should override
13858 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013859 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013860 * @param changed This is a new size or position for this view
13861 * @param left Left position, relative to parent
13862 * @param top Top position, relative to parent
13863 * @param right Right position, relative to parent
13864 * @param bottom Bottom position, relative to parent
13865 */
13866 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13867 }
13868
13869 /**
13870 * Assign a size and position to this view.
13871 *
13872 * This is called from layout.
13873 *
13874 * @param left Left position, relative to parent
13875 * @param top Top position, relative to parent
13876 * @param right Right position, relative to parent
13877 * @param bottom Bottom position, relative to parent
13878 * @return true if the new size and position are different than the
13879 * previous ones
13880 * {@hide}
13881 */
13882 protected boolean setFrame(int left, int top, int right, int bottom) {
13883 boolean changed = false;
13884
13885 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013886 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 + right + "," + bottom + ")");
13888 }
13889
13890 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13891 changed = true;
13892
13893 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013894 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013896 int oldWidth = mRight - mLeft;
13897 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013898 int newWidth = right - left;
13899 int newHeight = bottom - top;
13900 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13901
13902 // Invalidate our old position
13903 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904
13905 mLeft = left;
13906 mTop = top;
13907 mRight = right;
13908 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013909 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013910 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912
Dianne Hackborn4702a852012-08-17 15:18:29 -070013913 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013915
Chet Haase75755e22011-07-18 17:48:25 -070013916 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013917 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013918 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013919 if (mTransformationInfo != null) {
13920 mTransformationInfo.mMatrixDirty = true;
13921 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013923 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13924 }
13925
13926 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13927 // If we are visible, force the DRAWN bit to on so that
13928 // this invalidate will go through (at least to our parent).
13929 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013930 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013931 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013932 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013933 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013934 // parent display list may need to be recreated based on a change in the bounds
13935 // of any child
13936 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 }
13938
13939 // Reset drawn bit to original value (invalidate turns it off)
13940 mPrivateFlags |= drawn;
13941
13942 mBackgroundSizeChanged = true;
13943 }
13944 return changed;
13945 }
13946
13947 /**
13948 * Finalize inflating a view from XML. This is called as the last phase
13949 * of inflation, after all child views have been added.
13950 *
13951 * <p>Even if the subclass overrides onFinishInflate, they should always be
13952 * sure to call the super method, so that we get called.
13953 */
13954 protected void onFinishInflate() {
13955 }
13956
13957 /**
13958 * Returns the resources associated with this view.
13959 *
13960 * @return Resources object.
13961 */
13962 public Resources getResources() {
13963 return mResources;
13964 }
13965
13966 /**
13967 * Invalidates the specified Drawable.
13968 *
13969 * @param drawable the drawable to invalidate
13970 */
13971 public void invalidateDrawable(Drawable drawable) {
13972 if (verifyDrawable(drawable)) {
13973 final Rect dirty = drawable.getBounds();
13974 final int scrollX = mScrollX;
13975 final int scrollY = mScrollY;
13976
13977 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13978 dirty.right + scrollX, dirty.bottom + scrollY);
13979 }
13980 }
13981
13982 /**
13983 * Schedules an action on a drawable to occur at a specified time.
13984 *
13985 * @param who the recipient of the action
13986 * @param what the action to run on the drawable
13987 * @param when the time at which the action must occur. Uses the
13988 * {@link SystemClock#uptimeMillis} timebase.
13989 */
13990 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013991 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013992 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013993 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013994 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13995 Choreographer.CALLBACK_ANIMATION, what, who,
13996 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013997 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013998 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014000 }
14001 }
14002
14003 /**
14004 * Cancels a scheduled action on a drawable.
14005 *
14006 * @param who the recipient of the action
14007 * @param what the action to cancel
14008 */
14009 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014010 if (verifyDrawable(who) && what != null) {
14011 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014012 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14013 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014014 } else {
14015 ViewRootImpl.getRunQueue().removeCallbacks(what);
14016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017 }
14018 }
14019
14020 /**
14021 * Unschedule any events associated with the given Drawable. This can be
14022 * used when selecting a new Drawable into a view, so that the previous
14023 * one is completely unscheduled.
14024 *
14025 * @param who The Drawable to unschedule.
14026 *
14027 * @see #drawableStateChanged
14028 */
14029 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014030 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014031 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14032 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014033 }
14034 }
14035
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014036 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014037 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14038 * that the View directionality can and will be resolved before its Drawables.
14039 *
14040 * Will call {@link View#onResolveDrawables} when resolution is done.
14041 */
14042 public void resolveDrawables() {
14043 if (mBackground != null) {
14044 mBackground.setLayoutDirection(getResolvedLayoutDirection());
14045 }
14046 onResolveDrawables(getResolvedLayoutDirection());
14047 }
14048
14049 /**
14050 * Called when layout direction has been resolved.
14051 *
14052 * The default implementation does nothing.
14053 *
14054 * @param layoutDirection The resolved layout direction.
14055 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014056 * @see #LAYOUT_DIRECTION_LTR
14057 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014058 */
14059 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014060 }
14061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014062 /**
14063 * If your view subclass is displaying its own Drawable objects, it should
14064 * override this function and return true for any Drawable it is
14065 * displaying. This allows animations for those drawables to be
14066 * scheduled.
14067 *
14068 * <p>Be sure to call through to the super class when overriding this
14069 * function.
14070 *
14071 * @param who The Drawable to verify. Return true if it is one you are
14072 * displaying, else return the result of calling through to the
14073 * super class.
14074 *
14075 * @return boolean If true than the Drawable is being displayed in the
14076 * view; else false and it is not allowed to animate.
14077 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014078 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14079 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014080 */
14081 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014082 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 }
14084
14085 /**
14086 * This function is called whenever the state of the view changes in such
14087 * a way that it impacts the state of drawables being shown.
14088 *
14089 * <p>Be sure to call through to the superclass when overriding this
14090 * function.
14091 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014092 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014093 */
14094 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014095 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014096 if (d != null && d.isStateful()) {
14097 d.setState(getDrawableState());
14098 }
14099 }
14100
14101 /**
14102 * Call this to force a view to update its drawable state. This will cause
14103 * drawableStateChanged to be called on this view. Views that are interested
14104 * in the new state should call getDrawableState.
14105 *
14106 * @see #drawableStateChanged
14107 * @see #getDrawableState
14108 */
14109 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014110 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014111 drawableStateChanged();
14112
14113 ViewParent parent = mParent;
14114 if (parent != null) {
14115 parent.childDrawableStateChanged(this);
14116 }
14117 }
14118
14119 /**
14120 * Return an array of resource IDs of the drawable states representing the
14121 * current state of the view.
14122 *
14123 * @return The current drawable state
14124 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014125 * @see Drawable#setState(int[])
14126 * @see #drawableStateChanged()
14127 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014128 */
14129 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014130 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014131 return mDrawableState;
14132 } else {
14133 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014134 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014135 return mDrawableState;
14136 }
14137 }
14138
14139 /**
14140 * Generate the new {@link android.graphics.drawable.Drawable} state for
14141 * this view. This is called by the view
14142 * system when the cached Drawable state is determined to be invalid. To
14143 * retrieve the current state, you should use {@link #getDrawableState}.
14144 *
14145 * @param extraSpace if non-zero, this is the number of extra entries you
14146 * would like in the returned array in which you can place your own
14147 * states.
14148 *
14149 * @return Returns an array holding the current {@link Drawable} state of
14150 * the view.
14151 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014152 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014153 */
14154 protected int[] onCreateDrawableState(int extraSpace) {
14155 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14156 mParent instanceof View) {
14157 return ((View) mParent).onCreateDrawableState(extraSpace);
14158 }
14159
14160 int[] drawableState;
14161
14162 int privateFlags = mPrivateFlags;
14163
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014164 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014165 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014166 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14167 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014168 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014169 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014170 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014171 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14172 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014173 // This is set if HW acceleration is requested, even if the current
14174 // process doesn't allow it. This is just to allow app preview
14175 // windows to better match their app.
14176 viewStateIndex |= VIEW_STATE_ACCELERATED;
14177 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014178 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179
Christopher Tate3d4bf172011-03-28 16:16:46 -070014180 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014181 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14182 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014184 drawableState = VIEW_STATE_SETS[viewStateIndex];
14185
14186 //noinspection ConstantIfStatement
14187 if (false) {
14188 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14189 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014190 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14192 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014193 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014194 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014195 + ": " + Arrays.toString(drawableState));
14196 }
14197
14198 if (extraSpace == 0) {
14199 return drawableState;
14200 }
14201
14202 final int[] fullState;
14203 if (drawableState != null) {
14204 fullState = new int[drawableState.length + extraSpace];
14205 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14206 } else {
14207 fullState = new int[extraSpace];
14208 }
14209
14210 return fullState;
14211 }
14212
14213 /**
14214 * Merge your own state values in <var>additionalState</var> into the base
14215 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014216 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 *
14218 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014219 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220 * own additional state values.
14221 *
14222 * @param additionalState The additional state values you would like
14223 * added to <var>baseState</var>; this array is not modified.
14224 *
14225 * @return As a convenience, the <var>baseState</var> array you originally
14226 * passed into the function is returned.
14227 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014228 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 */
14230 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14231 final int N = baseState.length;
14232 int i = N - 1;
14233 while (i >= 0 && baseState[i] == 0) {
14234 i--;
14235 }
14236 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14237 return baseState;
14238 }
14239
14240 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014241 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14242 * on all Drawable objects associated with this view.
14243 */
14244 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014245 if (mBackground != null) {
14246 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014247 }
14248 }
14249
14250 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014251 * Sets the background color for this view.
14252 * @param color the color of the background
14253 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014254 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014256 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014257 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014258 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014259 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014260 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 }
14263
14264 /**
14265 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014266 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014267 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014269 * @attr ref android.R.styleable#View_background
14270 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014271 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014272 public void setBackgroundResource(int resid) {
14273 if (resid != 0 && resid == mBackgroundResource) {
14274 return;
14275 }
14276
14277 Drawable d= null;
14278 if (resid != 0) {
14279 d = mResources.getDrawable(resid);
14280 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014281 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014282
14283 mBackgroundResource = resid;
14284 }
14285
14286 /**
14287 * Set the background to a given Drawable, or remove the background. If the
14288 * background has padding, this View's padding is set to the background's
14289 * padding. However, when a background is removed, this View's padding isn't
14290 * touched. If setting the padding is desired, please use
14291 * {@link #setPadding(int, int, int, int)}.
14292 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014293 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014294 * background
14295 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014296 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014297 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014298 setBackgroundDrawable(background);
14299 }
14300
14301 /**
14302 * @deprecated use {@link #setBackground(Drawable)} instead
14303 */
14304 @Deprecated
14305 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014306 computeOpaqueFlags();
14307
Philip Milne6c8ea062012-04-03 17:38:43 -070014308 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014309 return;
14310 }
14311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014312 boolean requestLayout = false;
14313
14314 mBackgroundResource = 0;
14315
14316 /*
14317 * Regardless of whether we're setting a new background or not, we want
14318 * to clear the previous drawable.
14319 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014320 if (mBackground != null) {
14321 mBackground.setCallback(null);
14322 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014323 }
14324
Philip Milne6c8ea062012-04-03 17:38:43 -070014325 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 Rect padding = sThreadLocal.get();
14327 if (padding == null) {
14328 padding = new Rect();
14329 sThreadLocal.set(padding);
14330 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014331 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014332 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014333 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014334 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014335 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014336 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014337 mUserPaddingLeftInitial = padding.right;
14338 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014339 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014340 break;
14341 case LAYOUT_DIRECTION_LTR:
14342 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014343 mUserPaddingLeftInitial = padding.left;
14344 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014345 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014347 }
14348
14349 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14350 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014351 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14352 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014353 requestLayout = true;
14354 }
14355
Philip Milne6c8ea062012-04-03 17:38:43 -070014356 background.setCallback(this);
14357 if (background.isStateful()) {
14358 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014359 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014360 background.setVisible(getVisibility() == VISIBLE, false);
14361 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362
Dianne Hackborn4702a852012-08-17 15:18:29 -070014363 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14364 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14365 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014366 requestLayout = true;
14367 }
14368 } else {
14369 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014370 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014371
Dianne Hackborn4702a852012-08-17 15:18:29 -070014372 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014373 /*
14374 * This view ONLY drew the background before and we're removing
14375 * the background, so now it won't draw anything
14376 * (hence we SKIP_DRAW)
14377 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014378 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14379 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014380 }
14381
14382 /*
14383 * When the background is set, we try to apply its padding to this
14384 * View. When the background is removed, we don't touch this View's
14385 * padding. This is noted in the Javadocs. Hence, we don't need to
14386 * requestLayout(), the invalidate() below is sufficient.
14387 */
14388
14389 // The old background's minimum size could have affected this
14390 // View's layout, so let's requestLayout
14391 requestLayout = true;
14392 }
14393
Romain Guy8f1344f52009-05-15 16:03:59 -070014394 computeOpaqueFlags();
14395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014396 if (requestLayout) {
14397 requestLayout();
14398 }
14399
14400 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014401 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014402 }
14403
14404 /**
14405 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014406 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014408 *
14409 * @see #setBackground(Drawable)
14410 *
14411 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412 */
14413 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014414 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014415 }
14416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 /**
14418 * Sets the padding. The view may add on the space required to display
14419 * the scrollbars, depending on the style and visibility of the scrollbars.
14420 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14421 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14422 * from the values set in this call.
14423 *
14424 * @attr ref android.R.styleable#View_padding
14425 * @attr ref android.R.styleable#View_paddingBottom
14426 * @attr ref android.R.styleable#View_paddingLeft
14427 * @attr ref android.R.styleable#View_paddingRight
14428 * @attr ref android.R.styleable#View_paddingTop
14429 * @param left the left padding in pixels
14430 * @param top the top padding in pixels
14431 * @param right the right padding in pixels
14432 * @param bottom the bottom padding in pixels
14433 */
14434 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014435 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014436 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014437
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014438 mUserPaddingStart = UNDEFINED_PADDING;
14439 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014440
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014441 mUserPaddingLeftInitial = left;
14442 mUserPaddingRightInitial = right;
14443
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014444 internalSetPadding(left, top, right, bottom);
14445 }
14446
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014447 /**
14448 * @hide
14449 */
14450 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014451 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014452 mUserPaddingRight = right;
14453 mUserPaddingBottom = bottom;
14454
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014455 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014456 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014457
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014458 // Common case is there are no scroll bars.
14459 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014460 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014461 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014462 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014463 switch (mVerticalScrollbarPosition) {
14464 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014465 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014466 left += offset;
14467 } else {
14468 right += offset;
14469 }
14470 break;
Adam Powell20232d02010-12-08 21:08:53 -080014471 case SCROLLBAR_POSITION_RIGHT:
14472 right += offset;
14473 break;
14474 case SCROLLBAR_POSITION_LEFT:
14475 left += offset;
14476 break;
14477 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014478 }
Adam Powell20232d02010-12-08 21:08:53 -080014479 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014480 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14481 ? 0 : getHorizontalScrollbarHeight();
14482 }
14483 }
Romain Guy8506ab42009-06-11 17:35:47 -070014484
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014485 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 changed = true;
14487 mPaddingLeft = left;
14488 }
14489 if (mPaddingTop != top) {
14490 changed = true;
14491 mPaddingTop = top;
14492 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014493 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014494 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014495 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014496 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014497 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014499 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014500 }
14501
14502 if (changed) {
14503 requestLayout();
14504 }
14505 }
14506
14507 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014508 * Sets the relative padding. The view may add on the space required to display
14509 * the scrollbars, depending on the style and visibility of the scrollbars.
14510 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14511 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14512 * from the values set in this call.
14513 *
14514 * @attr ref android.R.styleable#View_padding
14515 * @attr ref android.R.styleable#View_paddingBottom
14516 * @attr ref android.R.styleable#View_paddingStart
14517 * @attr ref android.R.styleable#View_paddingEnd
14518 * @attr ref android.R.styleable#View_paddingTop
14519 * @param start the start padding in pixels
14520 * @param top the top padding in pixels
14521 * @param end the end padding in pixels
14522 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014523 */
14524 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014525 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014526 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014527
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014528 mUserPaddingStart = start;
14529 mUserPaddingEnd = end;
14530
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014531 switch(getResolvedLayoutDirection()) {
14532 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014533 mUserPaddingLeftInitial = end;
14534 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014535 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014536 break;
14537 case LAYOUT_DIRECTION_LTR:
14538 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014539 mUserPaddingLeftInitial = start;
14540 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014541 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014542 }
14543 }
14544
14545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 * Returns the top padding of this view.
14547 *
14548 * @return the top padding in pixels
14549 */
14550 public int getPaddingTop() {
14551 return mPaddingTop;
14552 }
14553
14554 /**
14555 * Returns the bottom padding of this view. If there are inset and enabled
14556 * scrollbars, this value may include the space required to display the
14557 * scrollbars as well.
14558 *
14559 * @return the bottom padding in pixels
14560 */
14561 public int getPaddingBottom() {
14562 return mPaddingBottom;
14563 }
14564
14565 /**
14566 * Returns the left padding of this view. If there are inset and enabled
14567 * scrollbars, this value may include the space required to display the
14568 * scrollbars as well.
14569 *
14570 * @return the left padding in pixels
14571 */
14572 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014573 if (!isPaddingResolved()) {
14574 resolvePadding();
14575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014576 return mPaddingLeft;
14577 }
14578
14579 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014580 * Returns the start padding of this view depending on its resolved layout direction.
14581 * If there are inset and enabled scrollbars, this value may include the space
14582 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014583 *
14584 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014585 */
14586 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014587 if (!isPaddingResolved()) {
14588 resolvePadding();
14589 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014590 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14591 mPaddingRight : mPaddingLeft;
14592 }
14593
14594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014595 * Returns the right padding of this view. If there are inset and enabled
14596 * scrollbars, this value may include the space required to display the
14597 * scrollbars as well.
14598 *
14599 * @return the right padding in pixels
14600 */
14601 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014602 if (!isPaddingResolved()) {
14603 resolvePadding();
14604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014605 return mPaddingRight;
14606 }
14607
14608 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014609 * Returns the end padding of this view depending on its resolved layout direction.
14610 * If there are inset and enabled scrollbars, this value may include the space
14611 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014612 *
14613 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014614 */
14615 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014616 if (!isPaddingResolved()) {
14617 resolvePadding();
14618 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014619 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14620 mPaddingLeft : mPaddingRight;
14621 }
14622
14623 /**
14624 * Return if the padding as been set thru relative values
14625 * {@link #setPaddingRelative(int, int, int, int)} or thru
14626 * @attr ref android.R.styleable#View_paddingStart or
14627 * @attr ref android.R.styleable#View_paddingEnd
14628 *
14629 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014630 */
14631 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014632 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014633 }
14634
14635 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014636 * @hide
14637 */
Philip Milne7a23b492012-04-24 22:12:36 -070014638 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014639 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014640 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014641 }
14642 return mLayoutInsets;
14643 }
14644
14645 /**
14646 * @hide
14647 */
14648 public void setLayoutInsets(Insets layoutInsets) {
14649 mLayoutInsets = layoutInsets;
14650 }
14651
14652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014653 * Changes the selection state of this view. A view can be selected or not.
14654 * Note that selection is not the same as focus. Views are typically
14655 * selected in the context of an AdapterView like ListView or GridView;
14656 * the selected view is the view that is highlighted.
14657 *
14658 * @param selected true if the view must be selected, false otherwise
14659 */
14660 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014661 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14662 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014663 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014664 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014665 refreshDrawableState();
14666 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014667 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14668 notifyAccessibilityStateChanged();
14669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014670 }
14671 }
14672
14673 /**
14674 * Dispatch setSelected to all of this View's children.
14675 *
14676 * @see #setSelected(boolean)
14677 *
14678 * @param selected The new selected state
14679 */
14680 protected void dispatchSetSelected(boolean selected) {
14681 }
14682
14683 /**
14684 * Indicates the selection state of this view.
14685 *
14686 * @return true if the view is selected, false otherwise
14687 */
14688 @ViewDebug.ExportedProperty
14689 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014690 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014691 }
14692
14693 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014694 * Changes the activated state of this view. A view can be activated or not.
14695 * Note that activation is not the same as selection. Selection is
14696 * a transient property, representing the view (hierarchy) the user is
14697 * currently interacting with. Activation is a longer-term state that the
14698 * user can move views in and out of. For example, in a list view with
14699 * single or multiple selection enabled, the views in the current selection
14700 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14701 * here.) The activated state is propagated down to children of the view it
14702 * is set on.
14703 *
14704 * @param activated true if the view must be activated, false otherwise
14705 */
14706 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014707 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14708 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014709 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014710 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014711 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014712 }
14713 }
14714
14715 /**
14716 * Dispatch setActivated to all of this View's children.
14717 *
14718 * @see #setActivated(boolean)
14719 *
14720 * @param activated The new activated state
14721 */
14722 protected void dispatchSetActivated(boolean activated) {
14723 }
14724
14725 /**
14726 * Indicates the activation state of this view.
14727 *
14728 * @return true if the view is activated, false otherwise
14729 */
14730 @ViewDebug.ExportedProperty
14731 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014732 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014733 }
14734
14735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014736 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14737 * observer can be used to get notifications when global events, like
14738 * layout, happen.
14739 *
14740 * The returned ViewTreeObserver observer is not guaranteed to remain
14741 * valid for the lifetime of this View. If the caller of this method keeps
14742 * a long-lived reference to ViewTreeObserver, it should always check for
14743 * the return value of {@link ViewTreeObserver#isAlive()}.
14744 *
14745 * @return The ViewTreeObserver for this view's hierarchy.
14746 */
14747 public ViewTreeObserver getViewTreeObserver() {
14748 if (mAttachInfo != null) {
14749 return mAttachInfo.mTreeObserver;
14750 }
14751 if (mFloatingTreeObserver == null) {
14752 mFloatingTreeObserver = new ViewTreeObserver();
14753 }
14754 return mFloatingTreeObserver;
14755 }
14756
14757 /**
14758 * <p>Finds the topmost view in the current view hierarchy.</p>
14759 *
14760 * @return the topmost view containing this view
14761 */
14762 public View getRootView() {
14763 if (mAttachInfo != null) {
14764 final View v = mAttachInfo.mRootView;
14765 if (v != null) {
14766 return v;
14767 }
14768 }
Romain Guy8506ab42009-06-11 17:35:47 -070014769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770 View parent = this;
14771
14772 while (parent.mParent != null && parent.mParent instanceof View) {
14773 parent = (View) parent.mParent;
14774 }
14775
14776 return parent;
14777 }
14778
14779 /**
14780 * <p>Computes the coordinates of this view on the screen. The argument
14781 * must be an array of two integers. After the method returns, the array
14782 * contains the x and y location in that order.</p>
14783 *
14784 * @param location an array of two integers in which to hold the coordinates
14785 */
14786 public void getLocationOnScreen(int[] location) {
14787 getLocationInWindow(location);
14788
14789 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014790 if (info != null) {
14791 location[0] += info.mWindowLeft;
14792 location[1] += info.mWindowTop;
14793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014794 }
14795
14796 /**
14797 * <p>Computes the coordinates of this view in its window. The argument
14798 * must be an array of two integers. After the method returns, the array
14799 * contains the x and y location in that order.</p>
14800 *
14801 * @param location an array of two integers in which to hold the coordinates
14802 */
14803 public void getLocationInWindow(int[] location) {
14804 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014805 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014806 }
14807
Gilles Debunne6583ce52011-12-06 18:09:02 -080014808 if (mAttachInfo == null) {
14809 // When the view is not attached to a window, this method does not make sense
14810 location[0] = location[1] = 0;
14811 return;
14812 }
14813
Gilles Debunnecea45132011-11-24 02:19:27 +010014814 float[] position = mAttachInfo.mTmpTransformLocation;
14815 position[0] = position[1] = 0.0f;
14816
14817 if (!hasIdentityMatrix()) {
14818 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014820
Gilles Debunnecea45132011-11-24 02:19:27 +010014821 position[0] += mLeft;
14822 position[1] += mTop;
14823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014824 ViewParent viewParent = mParent;
14825 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014826 final View view = (View) viewParent;
14827
14828 position[0] -= view.mScrollX;
14829 position[1] -= view.mScrollY;
14830
14831 if (!view.hasIdentityMatrix()) {
14832 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014833 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014834
14835 position[0] += view.mLeft;
14836 position[1] += view.mTop;
14837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014839 }
Romain Guy8506ab42009-06-11 17:35:47 -070014840
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014841 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014842 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014843 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14844 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014845 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014846
14847 location[0] = (int) (position[0] + 0.5f);
14848 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014849 }
14850
14851 /**
14852 * {@hide}
14853 * @param id the id of the view to be found
14854 * @return the view of the specified id, null if cannot be found
14855 */
14856 protected View findViewTraversal(int id) {
14857 if (id == mID) {
14858 return this;
14859 }
14860 return null;
14861 }
14862
14863 /**
14864 * {@hide}
14865 * @param tag the tag of the view to be found
14866 * @return the view of specified tag, null if cannot be found
14867 */
14868 protected View findViewWithTagTraversal(Object tag) {
14869 if (tag != null && tag.equals(mTag)) {
14870 return this;
14871 }
14872 return null;
14873 }
14874
14875 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014876 * {@hide}
14877 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014878 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014879 * @return The first view that matches the predicate or null.
14880 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014881 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014882 if (predicate.apply(this)) {
14883 return this;
14884 }
14885 return null;
14886 }
14887
14888 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014889 * Look for a child view with the given id. If this view has the given
14890 * id, return this view.
14891 *
14892 * @param id The id to search for.
14893 * @return The view that has the given id in the hierarchy or null
14894 */
14895 public final View findViewById(int id) {
14896 if (id < 0) {
14897 return null;
14898 }
14899 return findViewTraversal(id);
14900 }
14901
14902 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014903 * Finds a view by its unuque and stable accessibility id.
14904 *
14905 * @param accessibilityId The searched accessibility id.
14906 * @return The found view.
14907 */
14908 final View findViewByAccessibilityId(int accessibilityId) {
14909 if (accessibilityId < 0) {
14910 return null;
14911 }
14912 return findViewByAccessibilityIdTraversal(accessibilityId);
14913 }
14914
14915 /**
14916 * Performs the traversal to find a view by its unuque and stable accessibility id.
14917 *
14918 * <strong>Note:</strong>This method does not stop at the root namespace
14919 * boundary since the user can touch the screen at an arbitrary location
14920 * potentially crossing the root namespace bounday which will send an
14921 * accessibility event to accessibility services and they should be able
14922 * to obtain the event source. Also accessibility ids are guaranteed to be
14923 * unique in the window.
14924 *
14925 * @param accessibilityId The accessibility id.
14926 * @return The found view.
14927 */
14928 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14929 if (getAccessibilityViewId() == accessibilityId) {
14930 return this;
14931 }
14932 return null;
14933 }
14934
14935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014936 * Look for a child view with the given tag. If this view has the given
14937 * tag, return this view.
14938 *
14939 * @param tag The tag to search for, using "tag.equals(getTag())".
14940 * @return The View that has the given tag in the hierarchy or null
14941 */
14942 public final View findViewWithTag(Object tag) {
14943 if (tag == null) {
14944 return null;
14945 }
14946 return findViewWithTagTraversal(tag);
14947 }
14948
14949 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014950 * {@hide}
14951 * Look for a child view that matches the specified predicate.
14952 * If this view matches the predicate, return this view.
14953 *
14954 * @param predicate The predicate to evaluate.
14955 * @return The first view that matches the predicate or null.
14956 */
14957 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014958 return findViewByPredicateTraversal(predicate, null);
14959 }
14960
14961 /**
14962 * {@hide}
14963 * Look for a child view that matches the specified predicate,
14964 * starting with the specified view and its descendents and then
14965 * recusively searching the ancestors and siblings of that view
14966 * until this view is reached.
14967 *
14968 * This method is useful in cases where the predicate does not match
14969 * a single unique view (perhaps multiple views use the same id)
14970 * and we are trying to find the view that is "closest" in scope to the
14971 * starting view.
14972 *
14973 * @param start The view to start from.
14974 * @param predicate The predicate to evaluate.
14975 * @return The first view that matches the predicate or null.
14976 */
14977 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14978 View childToSkip = null;
14979 for (;;) {
14980 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14981 if (view != null || start == this) {
14982 return view;
14983 }
14984
14985 ViewParent parent = start.getParent();
14986 if (parent == null || !(parent instanceof View)) {
14987 return null;
14988 }
14989
14990 childToSkip = start;
14991 start = (View) parent;
14992 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014993 }
14994
14995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996 * Sets the identifier for this view. The identifier does not have to be
14997 * unique in this view's hierarchy. The identifier should be a positive
14998 * number.
14999 *
15000 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015001 * @see #getId()
15002 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015003 *
15004 * @param id a number used to identify the view
15005 *
15006 * @attr ref android.R.styleable#View_id
15007 */
15008 public void setId(int id) {
15009 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015010 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15011 mID = generateViewId();
15012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015013 }
15014
15015 /**
15016 * {@hide}
15017 *
15018 * @param isRoot true if the view belongs to the root namespace, false
15019 * otherwise
15020 */
15021 public void setIsRootNamespace(boolean isRoot) {
15022 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015023 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015024 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015025 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015026 }
15027 }
15028
15029 /**
15030 * {@hide}
15031 *
15032 * @return true if the view belongs to the root namespace, false otherwise
15033 */
15034 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015035 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015036 }
15037
15038 /**
15039 * Returns this view's identifier.
15040 *
15041 * @return a positive integer used to identify the view or {@link #NO_ID}
15042 * if the view has no ID
15043 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015044 * @see #setId(int)
15045 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015046 * @attr ref android.R.styleable#View_id
15047 */
15048 @ViewDebug.CapturedViewProperty
15049 public int getId() {
15050 return mID;
15051 }
15052
15053 /**
15054 * Returns this view's tag.
15055 *
15056 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015057 *
15058 * @see #setTag(Object)
15059 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015060 */
15061 @ViewDebug.ExportedProperty
15062 public Object getTag() {
15063 return mTag;
15064 }
15065
15066 /**
15067 * Sets the tag associated with this view. A tag can be used to mark
15068 * a view in its hierarchy and does not have to be unique within the
15069 * hierarchy. Tags can also be used to store data within a view without
15070 * resorting to another data structure.
15071 *
15072 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015073 *
15074 * @see #getTag()
15075 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015076 */
15077 public void setTag(final Object tag) {
15078 mTag = tag;
15079 }
15080
15081 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015082 * Returns the tag associated with this view and the specified key.
15083 *
15084 * @param key The key identifying the tag
15085 *
15086 * @return the Object stored in this view as a tag
15087 *
15088 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015089 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015090 */
15091 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015092 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015093 return null;
15094 }
15095
15096 /**
15097 * Sets a tag associated with this view and a key. A tag can be used
15098 * to mark a view in its hierarchy and does not have to be unique within
15099 * the hierarchy. Tags can also be used to store data within a view
15100 * without resorting to another data structure.
15101 *
15102 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015103 * application to ensure it is unique (see the <a
15104 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15105 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015106 * the Android framework or not associated with any package will cause
15107 * an {@link IllegalArgumentException} to be thrown.
15108 *
15109 * @param key The key identifying the tag
15110 * @param tag An Object to tag the view with
15111 *
15112 * @throws IllegalArgumentException If they specified key is not valid
15113 *
15114 * @see #setTag(Object)
15115 * @see #getTag(int)
15116 */
15117 public void setTag(int key, final Object tag) {
15118 // If the package id is 0x00 or 0x01, it's either an undefined package
15119 // or a framework id
15120 if ((key >>> 24) < 2) {
15121 throw new IllegalArgumentException("The key must be an application-specific "
15122 + "resource id.");
15123 }
15124
Adam Powell2b2f6d62011-09-23 11:15:39 -070015125 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015126 }
15127
15128 /**
15129 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15130 * framework id.
15131 *
15132 * @hide
15133 */
15134 public void setTagInternal(int key, Object tag) {
15135 if ((key >>> 24) != 0x1) {
15136 throw new IllegalArgumentException("The key must be a framework-specific "
15137 + "resource id.");
15138 }
15139
Adam Powell2b2f6d62011-09-23 11:15:39 -070015140 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015141 }
15142
Adam Powell2b2f6d62011-09-23 11:15:39 -070015143 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015144 if (mKeyedTags == null) {
15145 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015146 }
15147
Adam Powell7db82ac2011-09-22 19:44:04 -070015148 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015149 }
15150
15151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015152 * Prints information about this view in the log output, with the tag
15153 * {@link #VIEW_LOG_TAG}.
15154 *
15155 * @hide
15156 */
15157 public void debug() {
15158 debug(0);
15159 }
15160
15161 /**
15162 * Prints information about this view in the log output, with the tag
15163 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15164 * indentation defined by the <code>depth</code>.
15165 *
15166 * @param depth the indentation level
15167 *
15168 * @hide
15169 */
15170 protected void debug(int depth) {
15171 String output = debugIndent(depth - 1);
15172
15173 output += "+ " + this;
15174 int id = getId();
15175 if (id != -1) {
15176 output += " (id=" + id + ")";
15177 }
15178 Object tag = getTag();
15179 if (tag != null) {
15180 output += " (tag=" + tag + ")";
15181 }
15182 Log.d(VIEW_LOG_TAG, output);
15183
Dianne Hackborn4702a852012-08-17 15:18:29 -070015184 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015185 output = debugIndent(depth) + " FOCUSED";
15186 Log.d(VIEW_LOG_TAG, output);
15187 }
15188
15189 output = debugIndent(depth);
15190 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15191 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15192 + "} ";
15193 Log.d(VIEW_LOG_TAG, output);
15194
15195 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15196 || mPaddingBottom != 0) {
15197 output = debugIndent(depth);
15198 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15199 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15200 Log.d(VIEW_LOG_TAG, output);
15201 }
15202
15203 output = debugIndent(depth);
15204 output += "mMeasureWidth=" + mMeasuredWidth +
15205 " mMeasureHeight=" + mMeasuredHeight;
15206 Log.d(VIEW_LOG_TAG, output);
15207
15208 output = debugIndent(depth);
15209 if (mLayoutParams == null) {
15210 output += "BAD! no layout params";
15211 } else {
15212 output = mLayoutParams.debug(output);
15213 }
15214 Log.d(VIEW_LOG_TAG, output);
15215
15216 output = debugIndent(depth);
15217 output += "flags={";
15218 output += View.printFlags(mViewFlags);
15219 output += "}";
15220 Log.d(VIEW_LOG_TAG, output);
15221
15222 output = debugIndent(depth);
15223 output += "privateFlags={";
15224 output += View.printPrivateFlags(mPrivateFlags);
15225 output += "}";
15226 Log.d(VIEW_LOG_TAG, output);
15227 }
15228
15229 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015230 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015231 *
15232 * @param depth the indentation level
15233 * @return a String containing (depth * 2 + 3) * 2 white spaces
15234 *
15235 * @hide
15236 */
15237 protected static String debugIndent(int depth) {
15238 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15239 for (int i = 0; i < (depth * 2) + 3; i++) {
15240 spaces.append(' ').append(' ');
15241 }
15242 return spaces.toString();
15243 }
15244
15245 /**
15246 * <p>Return the offset of the widget's text baseline from the widget's top
15247 * boundary. If this widget does not support baseline alignment, this
15248 * method returns -1. </p>
15249 *
15250 * @return the offset of the baseline within the widget's bounds or -1
15251 * if baseline alignment is not supported
15252 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015253 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015254 public int getBaseline() {
15255 return -1;
15256 }
15257
15258 /**
Chet Haase97140572012-09-13 14:56:47 -070015259 * Returns whether the view hierarchy is currently undergoing a layout pass. This
15260 * information is useful to avoid situations such as calling {@link #requestLayout()} during
15261 * a layout pass.
15262 *
15263 * @return whether the view hierarchy is currently undergoing a layout pass
15264 */
15265 public boolean isInLayout() {
15266 ViewRootImpl viewRoot = getViewRootImpl();
15267 return (viewRoot != null && viewRoot.isInLayout());
15268 }
15269
15270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015271 * Call this when something has changed which has invalidated the
15272 * layout of this view. This will schedule a layout pass of the view
Chet Haase97140572012-09-13 14:56:47 -070015273 * tree. This should not be called while the view hierarchy is currently in a layout
15274 * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
15275 * end of the current layout pass (and then layout will run again) or after the current
15276 * frame is drawn and the next layout occurs.
15277 *
15278 * <p>Subclasses which override this method should call the superclass method to
15279 * handle possible request-during-layout errors correctly.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015280 */
15281 public void requestLayout() {
Chet Haase97140572012-09-13 14:56:47 -070015282 ViewRootImpl viewRoot = getViewRootImpl();
15283 if (viewRoot != null && viewRoot.isInLayout()) {
15284 viewRoot.requestLayoutDuringLayout(this);
15285 return;
15286 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070015287 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15288 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015289
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015290 if (mParent != null && !mParent.isLayoutRequested()) {
15291 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015292 }
15293 }
15294
15295 /**
15296 * Forces this view to be laid out during the next layout pass.
15297 * This method does not call requestLayout() or forceLayout()
15298 * on the parent.
15299 */
15300 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015301 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15302 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 }
15304
15305 /**
15306 * <p>
15307 * This is called to find out how big a view should be. The parent
15308 * supplies constraint information in the width and height parameters.
15309 * </p>
15310 *
15311 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015312 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015313 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015314 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015315 * </p>
15316 *
15317 *
15318 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15319 * parent
15320 * @param heightMeasureSpec Vertical space requirements as imposed by the
15321 * parent
15322 *
15323 * @see #onMeasure(int, int)
15324 */
15325 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015326 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015327 widthMeasureSpec != mOldWidthMeasureSpec ||
15328 heightMeasureSpec != mOldHeightMeasureSpec) {
15329
15330 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015331 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015332
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015333 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015334 resolvePadding();
15335 }
15336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015337 // measure ourselves, this should set the measured dimension flag back
15338 onMeasure(widthMeasureSpec, heightMeasureSpec);
15339
15340 // flag not set, setMeasuredDimension() was not invoked, we raise
15341 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015342 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015343 throw new IllegalStateException("onMeasure() did not set the"
15344 + " measured dimension by calling"
15345 + " setMeasuredDimension()");
15346 }
15347
Dianne Hackborn4702a852012-08-17 15:18:29 -070015348 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015349 }
15350
15351 mOldWidthMeasureSpec = widthMeasureSpec;
15352 mOldHeightMeasureSpec = heightMeasureSpec;
15353 }
15354
15355 /**
15356 * <p>
15357 * Measure the view and its content to determine the measured width and the
15358 * measured height. This method is invoked by {@link #measure(int, int)} and
15359 * should be overriden by subclasses to provide accurate and efficient
15360 * measurement of their contents.
15361 * </p>
15362 *
15363 * <p>
15364 * <strong>CONTRACT:</strong> When overriding this method, you
15365 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15366 * measured width and height of this view. Failure to do so will trigger an
15367 * <code>IllegalStateException</code>, thrown by
15368 * {@link #measure(int, int)}. Calling the superclass'
15369 * {@link #onMeasure(int, int)} is a valid use.
15370 * </p>
15371 *
15372 * <p>
15373 * The base class implementation of measure defaults to the background size,
15374 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15375 * override {@link #onMeasure(int, int)} to provide better measurements of
15376 * their content.
15377 * </p>
15378 *
15379 * <p>
15380 * If this method is overridden, it is the subclass's responsibility to make
15381 * sure the measured height and width are at least the view's minimum height
15382 * and width ({@link #getSuggestedMinimumHeight()} and
15383 * {@link #getSuggestedMinimumWidth()}).
15384 * </p>
15385 *
15386 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15387 * The requirements are encoded with
15388 * {@link android.view.View.MeasureSpec}.
15389 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15390 * The requirements are encoded with
15391 * {@link android.view.View.MeasureSpec}.
15392 *
15393 * @see #getMeasuredWidth()
15394 * @see #getMeasuredHeight()
15395 * @see #setMeasuredDimension(int, int)
15396 * @see #getSuggestedMinimumHeight()
15397 * @see #getSuggestedMinimumWidth()
15398 * @see android.view.View.MeasureSpec#getMode(int)
15399 * @see android.view.View.MeasureSpec#getSize(int)
15400 */
15401 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15402 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15403 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15404 }
15405
15406 /**
15407 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15408 * measured width and measured height. Failing to do so will trigger an
15409 * exception at measurement time.</p>
15410 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015411 * @param measuredWidth The measured width of this view. May be a complex
15412 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15413 * {@link #MEASURED_STATE_TOO_SMALL}.
15414 * @param measuredHeight The measured height of this view. May be a complex
15415 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15416 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417 */
15418 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15419 mMeasuredWidth = measuredWidth;
15420 mMeasuredHeight = measuredHeight;
15421
Dianne Hackborn4702a852012-08-17 15:18:29 -070015422 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015423 }
15424
15425 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015426 * Merge two states as returned by {@link #getMeasuredState()}.
15427 * @param curState The current state as returned from a view or the result
15428 * of combining multiple views.
15429 * @param newState The new view state to combine.
15430 * @return Returns a new integer reflecting the combination of the two
15431 * states.
15432 */
15433 public static int combineMeasuredStates(int curState, int newState) {
15434 return curState | newState;
15435 }
15436
15437 /**
15438 * Version of {@link #resolveSizeAndState(int, int, int)}
15439 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15440 */
15441 public static int resolveSize(int size, int measureSpec) {
15442 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15443 }
15444
15445 /**
15446 * Utility to reconcile a desired size and state, with constraints imposed
15447 * by a MeasureSpec. Will take the desired size, unless a different size
15448 * is imposed by the constraints. The returned value is a compound integer,
15449 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15450 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15451 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 *
15453 * @param size How big the view wants to be
15454 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015455 * @return Size information bit mask as defined by
15456 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015457 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015458 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015459 int result = size;
15460 int specMode = MeasureSpec.getMode(measureSpec);
15461 int specSize = MeasureSpec.getSize(measureSpec);
15462 switch (specMode) {
15463 case MeasureSpec.UNSPECIFIED:
15464 result = size;
15465 break;
15466 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015467 if (specSize < size) {
15468 result = specSize | MEASURED_STATE_TOO_SMALL;
15469 } else {
15470 result = size;
15471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472 break;
15473 case MeasureSpec.EXACTLY:
15474 result = specSize;
15475 break;
15476 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015477 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015478 }
15479
15480 /**
15481 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015482 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015483 * by the MeasureSpec.
15484 *
15485 * @param size Default size for this view
15486 * @param measureSpec Constraints imposed by the parent
15487 * @return The size this view should be.
15488 */
15489 public static int getDefaultSize(int size, int measureSpec) {
15490 int result = size;
15491 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015492 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015493
15494 switch (specMode) {
15495 case MeasureSpec.UNSPECIFIED:
15496 result = size;
15497 break;
15498 case MeasureSpec.AT_MOST:
15499 case MeasureSpec.EXACTLY:
15500 result = specSize;
15501 break;
15502 }
15503 return result;
15504 }
15505
15506 /**
15507 * Returns the suggested minimum height that the view should use. This
15508 * returns the maximum of the view's minimum height
15509 * and the background's minimum height
15510 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15511 * <p>
15512 * When being used in {@link #onMeasure(int, int)}, the caller should still
15513 * ensure the returned height is within the requirements of the parent.
15514 *
15515 * @return The suggested minimum height of the view.
15516 */
15517 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015518 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015520 }
15521
15522 /**
15523 * Returns the suggested minimum width that the view should use. This
15524 * returns the maximum of the view's minimum width)
15525 * and the background's minimum width
15526 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15527 * <p>
15528 * When being used in {@link #onMeasure(int, int)}, the caller should still
15529 * ensure the returned width is within the requirements of the parent.
15530 *
15531 * @return The suggested minimum width of the view.
15532 */
15533 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015534 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015536
Philip Milne6c8ea062012-04-03 17:38:43 -070015537 /**
15538 * Returns the minimum height of the view.
15539 *
15540 * @return the minimum height the view will try to be.
15541 *
15542 * @see #setMinimumHeight(int)
15543 *
15544 * @attr ref android.R.styleable#View_minHeight
15545 */
15546 public int getMinimumHeight() {
15547 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015548 }
15549
15550 /**
15551 * Sets the minimum height of the view. It is not guaranteed the view will
15552 * be able to achieve this minimum height (for example, if its parent layout
15553 * constrains it with less available height).
15554 *
15555 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015556 *
15557 * @see #getMinimumHeight()
15558 *
15559 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015560 */
15561 public void setMinimumHeight(int minHeight) {
15562 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015563 requestLayout();
15564 }
15565
15566 /**
15567 * Returns the minimum width of the view.
15568 *
15569 * @return the minimum width the view will try to be.
15570 *
15571 * @see #setMinimumWidth(int)
15572 *
15573 * @attr ref android.R.styleable#View_minWidth
15574 */
15575 public int getMinimumWidth() {
15576 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015577 }
15578
15579 /**
15580 * Sets the minimum width of the view. It is not guaranteed the view will
15581 * be able to achieve this minimum width (for example, if its parent layout
15582 * constrains it with less available width).
15583 *
15584 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015585 *
15586 * @see #getMinimumWidth()
15587 *
15588 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015589 */
15590 public void setMinimumWidth(int minWidth) {
15591 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015592 requestLayout();
15593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015594 }
15595
15596 /**
15597 * Get the animation currently associated with this view.
15598 *
15599 * @return The animation that is currently playing or
15600 * scheduled to play for this view.
15601 */
15602 public Animation getAnimation() {
15603 return mCurrentAnimation;
15604 }
15605
15606 /**
15607 * Start the specified animation now.
15608 *
15609 * @param animation the animation to start now
15610 */
15611 public void startAnimation(Animation animation) {
15612 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15613 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015614 invalidateParentCaches();
15615 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015616 }
15617
15618 /**
15619 * Cancels any animations for this view.
15620 */
15621 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015622 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015623 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015625 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015626 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015627 }
15628
15629 /**
15630 * Sets the next animation to play for this view.
15631 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015632 * {@link #startAnimation(android.view.animation.Animation)} instead.
15633 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015634 * control over the start time and invalidation, but you
15635 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015636 * 2) the view's parent (which controls animations on its children)
15637 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015638 * start.
15639 *
15640 * @param animation The next animation, or null.
15641 */
15642 public void setAnimation(Animation animation) {
15643 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015645 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015646 // If the screen is off assume the animation start time is now instead of
15647 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15648 // would cause the animation to start when the screen turns back on
15649 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15650 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15651 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015653 animation.reset();
15654 }
15655 }
15656
15657 /**
15658 * Invoked by a parent ViewGroup to notify the start of the animation
15659 * currently associated with this view. If you override this method,
15660 * always call super.onAnimationStart();
15661 *
15662 * @see #setAnimation(android.view.animation.Animation)
15663 * @see #getAnimation()
15664 */
15665 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015666 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015667 }
15668
15669 /**
15670 * Invoked by a parent ViewGroup to notify the end of the animation
15671 * currently associated with this view. If you override this method,
15672 * always call super.onAnimationEnd();
15673 *
15674 * @see #setAnimation(android.view.animation.Animation)
15675 * @see #getAnimation()
15676 */
15677 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015678 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015679 }
15680
15681 /**
15682 * Invoked if there is a Transform that involves alpha. Subclass that can
15683 * draw themselves with the specified alpha should return true, and then
15684 * respect that alpha when their onDraw() is called. If this returns false
15685 * then the view may be redirected to draw into an offscreen buffer to
15686 * fulfill the request, which will look fine, but may be slower than if the
15687 * subclass handles it internally. The default implementation returns false.
15688 *
15689 * @param alpha The alpha (0..255) to apply to the view's drawing
15690 * @return true if the view can draw with the specified alpha.
15691 */
15692 protected boolean onSetAlpha(int alpha) {
15693 return false;
15694 }
15695
15696 /**
15697 * This is used by the RootView to perform an optimization when
15698 * the view hierarchy contains one or several SurfaceView.
15699 * SurfaceView is always considered transparent, but its children are not,
15700 * therefore all View objects remove themselves from the global transparent
15701 * region (passed as a parameter to this function).
15702 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015703 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015704 *
15705 * @return Returns true if the effective visibility of the view at this
15706 * point is opaque, regardless of the transparent region; returns false
15707 * if it is possible for underlying windows to be seen behind the view.
15708 *
15709 * {@hide}
15710 */
15711 public boolean gatherTransparentRegion(Region region) {
15712 final AttachInfo attachInfo = mAttachInfo;
15713 if (region != null && attachInfo != null) {
15714 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015715 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015716 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15717 // remove it from the transparent region.
15718 final int[] location = attachInfo.mTransparentLocation;
15719 getLocationInWindow(location);
15720 region.op(location[0], location[1], location[0] + mRight - mLeft,
15721 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015722 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015723 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15724 // exists, so we remove the background drawable's non-transparent
15725 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015726 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015727 }
15728 }
15729 return true;
15730 }
15731
15732 /**
15733 * Play a sound effect for this view.
15734 *
15735 * <p>The framework will play sound effects for some built in actions, such as
15736 * clicking, but you may wish to play these effects in your widget,
15737 * for instance, for internal navigation.
15738 *
15739 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15740 * {@link #isSoundEffectsEnabled()} is true.
15741 *
15742 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15743 */
15744 public void playSoundEffect(int soundConstant) {
15745 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15746 return;
15747 }
15748 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15749 }
15750
15751 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015752 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015753 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015754 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015755 *
15756 * <p>The framework will provide haptic feedback for some built in actions,
15757 * such as long presses, but you may wish to provide feedback for your
15758 * own widget.
15759 *
15760 * <p>The feedback will only be performed if
15761 * {@link #isHapticFeedbackEnabled()} is true.
15762 *
15763 * @param feedbackConstant One of the constants defined in
15764 * {@link HapticFeedbackConstants}
15765 */
15766 public boolean performHapticFeedback(int feedbackConstant) {
15767 return performHapticFeedback(feedbackConstant, 0);
15768 }
15769
15770 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015771 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015772 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015773 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015774 *
15775 * @param feedbackConstant One of the constants defined in
15776 * {@link HapticFeedbackConstants}
15777 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15778 */
15779 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15780 if (mAttachInfo == null) {
15781 return false;
15782 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015783 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015784 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015785 && !isHapticFeedbackEnabled()) {
15786 return false;
15787 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015788 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15789 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015790 }
15791
15792 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015793 * Request that the visibility of the status bar or other screen/window
15794 * decorations be changed.
15795 *
15796 * <p>This method is used to put the over device UI into temporary modes
15797 * where the user's attention is focused more on the application content,
15798 * by dimming or hiding surrounding system affordances. This is typically
15799 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15800 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15801 * to be placed behind the action bar (and with these flags other system
15802 * affordances) so that smooth transitions between hiding and showing them
15803 * can be done.
15804 *
15805 * <p>Two representative examples of the use of system UI visibility is
15806 * implementing a content browsing application (like a magazine reader)
15807 * and a video playing application.
15808 *
15809 * <p>The first code shows a typical implementation of a View in a content
15810 * browsing application. In this implementation, the application goes
15811 * into a content-oriented mode by hiding the status bar and action bar,
15812 * and putting the navigation elements into lights out mode. The user can
15813 * then interact with content while in this mode. Such an application should
15814 * provide an easy way for the user to toggle out of the mode (such as to
15815 * check information in the status bar or access notifications). In the
15816 * implementation here, this is done simply by tapping on the content.
15817 *
15818 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15819 * content}
15820 *
15821 * <p>This second code sample shows a typical implementation of a View
15822 * in a video playing application. In this situation, while the video is
15823 * playing the application would like to go into a complete full-screen mode,
15824 * to use as much of the display as possible for the video. When in this state
15825 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015826 * touching on the screen to pop the UI out of full screen mode. See
15827 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015828 *
15829 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15830 * content}
15831 *
15832 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15833 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15834 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15835 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015836 */
15837 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015838 if (visibility != mSystemUiVisibility) {
15839 mSystemUiVisibility = visibility;
15840 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15841 mParent.recomputeViewAttributes(this);
15842 }
Joe Onorato664644d2011-01-23 17:53:23 -080015843 }
15844 }
15845
15846 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015847 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15848 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15849 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15850 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15851 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015852 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015853 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015854 return mSystemUiVisibility;
15855 }
15856
Scott Mainec6331b2011-05-24 16:55:56 -070015857 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015858 * Returns the current system UI visibility that is currently set for
15859 * the entire window. This is the combination of the
15860 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15861 * views in the window.
15862 */
15863 public int getWindowSystemUiVisibility() {
15864 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15865 }
15866
15867 /**
15868 * Override to find out when the window's requested system UI visibility
15869 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15870 * This is different from the callbacks recieved through
15871 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15872 * in that this is only telling you about the local request of the window,
15873 * not the actual values applied by the system.
15874 */
15875 public void onWindowSystemUiVisibilityChanged(int visible) {
15876 }
15877
15878 /**
15879 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15880 * the view hierarchy.
15881 */
15882 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15883 onWindowSystemUiVisibilityChanged(visible);
15884 }
15885
15886 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015887 * Set a listener to receive callbacks when the visibility of the system bar changes.
15888 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15889 */
Joe Onorato664644d2011-01-23 17:53:23 -080015890 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015891 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015892 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15893 mParent.recomputeViewAttributes(this);
15894 }
15895 }
15896
15897 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015898 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15899 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015900 */
15901 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015902 ListenerInfo li = mListenerInfo;
15903 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15904 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015905 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015906 }
15907 }
15908
Dianne Hackborncf675782012-05-10 15:07:24 -070015909 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015910 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15911 if (val != mSystemUiVisibility) {
15912 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015913 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015914 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015915 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015916 }
15917
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015918 /** @hide */
15919 public void setDisabledSystemUiVisibility(int flags) {
15920 if (mAttachInfo != null) {
15921 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15922 mAttachInfo.mDisabledSystemUiVisibility = flags;
15923 if (mParent != null) {
15924 mParent.recomputeViewAttributes(this);
15925 }
15926 }
15927 }
15928 }
15929
Joe Onorato664644d2011-01-23 17:53:23 -080015930 /**
Joe Malin32736f02011-01-19 16:14:20 -080015931 * Creates an image that the system displays during the drag and drop
15932 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15933 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15934 * appearance as the given View. The default also positions the center of the drag shadow
15935 * directly under the touch point. If no View is provided (the constructor with no parameters
15936 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15937 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15938 * default is an invisible drag shadow.
15939 * <p>
15940 * You are not required to use the View you provide to the constructor as the basis of the
15941 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15942 * anything you want as the drag shadow.
15943 * </p>
15944 * <p>
15945 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15946 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15947 * size and position of the drag shadow. It uses this data to construct a
15948 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15949 * so that your application can draw the shadow image in the Canvas.
15950 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015951 *
15952 * <div class="special reference">
15953 * <h3>Developer Guides</h3>
15954 * <p>For a guide to implementing drag and drop features, read the
15955 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15956 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015957 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015958 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015959 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015960
15961 /**
Joe Malin32736f02011-01-19 16:14:20 -080015962 * Constructs a shadow image builder based on a View. By default, the resulting drag
15963 * shadow will have the same appearance and dimensions as the View, with the touch point
15964 * over the center of the View.
15965 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015966 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015967 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015968 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015969 }
15970
Christopher Tate17ed60c2011-01-18 12:50:26 -080015971 /**
15972 * Construct a shadow builder object with no associated View. This
15973 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15974 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15975 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015976 * reference to any View object. If they are not overridden, then the result is an
15977 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015978 */
15979 public DragShadowBuilder() {
15980 mView = new WeakReference<View>(null);
15981 }
15982
15983 /**
15984 * Returns the View object that had been passed to the
15985 * {@link #View.DragShadowBuilder(View)}
15986 * constructor. If that View parameter was {@code null} or if the
15987 * {@link #View.DragShadowBuilder()}
15988 * constructor was used to instantiate the builder object, this method will return
15989 * null.
15990 *
15991 * @return The View object associate with this builder object.
15992 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015993 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015994 final public View getView() {
15995 return mView.get();
15996 }
15997
Christopher Tate2c095f32010-10-04 14:13:40 -070015998 /**
Joe Malin32736f02011-01-19 16:14:20 -080015999 * Provides the metrics for the shadow image. These include the dimensions of
16000 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016001 * be centered under the touch location while dragging.
16002 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016003 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016004 * same as the dimensions of the View itself and centers the shadow under
16005 * the touch point.
16006 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016007 *
Joe Malin32736f02011-01-19 16:14:20 -080016008 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16009 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16010 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16011 * image.
16012 *
16013 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16014 * shadow image that should be underneath the touch point during the drag and drop
16015 * operation. Your application must set {@link android.graphics.Point#x} to the
16016 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016017 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016018 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016019 final View view = mView.get();
16020 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016021 shadowSize.set(view.getWidth(), view.getHeight());
16022 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016023 } else {
16024 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16025 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016026 }
16027
16028 /**
Joe Malin32736f02011-01-19 16:14:20 -080016029 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16030 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016031 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016032 *
Joe Malin32736f02011-01-19 16:14:20 -080016033 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016034 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016035 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016036 final View view = mView.get();
16037 if (view != null) {
16038 view.draw(canvas);
16039 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016040 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016041 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016042 }
16043 }
16044
16045 /**
Joe Malin32736f02011-01-19 16:14:20 -080016046 * Starts a drag and drop operation. When your application calls this method, it passes a
16047 * {@link android.view.View.DragShadowBuilder} object to the system. The
16048 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16049 * to get metrics for the drag shadow, and then calls the object's
16050 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16051 * <p>
16052 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16053 * drag events to all the View objects in your application that are currently visible. It does
16054 * this either by calling the View object's drag listener (an implementation of
16055 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16056 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16057 * Both are passed a {@link android.view.DragEvent} object that has a
16058 * {@link android.view.DragEvent#getAction()} value of
16059 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16060 * </p>
16061 * <p>
16062 * Your application can invoke startDrag() on any attached View object. The View object does not
16063 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16064 * be related to the View the user selected for dragging.
16065 * </p>
16066 * @param data A {@link android.content.ClipData} object pointing to the data to be
16067 * transferred by the drag and drop operation.
16068 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16069 * drag shadow.
16070 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16071 * drop operation. This Object is put into every DragEvent object sent by the system during the
16072 * current drag.
16073 * <p>
16074 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16075 * to the target Views. For example, it can contain flags that differentiate between a
16076 * a copy operation and a move operation.
16077 * </p>
16078 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16079 * so the parameter should be set to 0.
16080 * @return {@code true} if the method completes successfully, or
16081 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16082 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016083 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016084 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016085 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016086 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016087 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016088 }
16089 boolean okay = false;
16090
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016091 Point shadowSize = new Point();
16092 Point shadowTouchPoint = new Point();
16093 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016094
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016095 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16096 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16097 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016098 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016099
Chris Tatea32dcf72010-10-14 12:13:50 -070016100 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016101 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16102 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016103 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016104 Surface surface = new Surface();
16105 try {
16106 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016107 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016108 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016109 + " surface=" + surface);
16110 if (token != null) {
16111 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016112 try {
Chris Tate6b391282010-10-14 15:48:59 -070016113 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016114 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016115 } finally {
16116 surface.unlockCanvasAndPost(canvas);
16117 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016118
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016119 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016120
16121 // Cache the local state object for delivery with DragEvents
16122 root.setLocalDragState(myLocalState);
16123
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016124 // repurpose 'shadowSize' for the last touch point
16125 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016126
Christopher Tatea53146c2010-09-07 11:57:52 -070016127 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016128 shadowSize.x, shadowSize.y,
16129 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016130 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016131
16132 // Off and running! Release our local surface instance; the drag
16133 // shadow surface is now managed by the system process.
16134 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016135 }
16136 } catch (Exception e) {
16137 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16138 surface.destroy();
16139 }
16140
16141 return okay;
16142 }
16143
Christopher Tatea53146c2010-09-07 11:57:52 -070016144 /**
Joe Malin32736f02011-01-19 16:14:20 -080016145 * Handles drag events sent by the system following a call to
16146 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16147 *<p>
16148 * When the system calls this method, it passes a
16149 * {@link android.view.DragEvent} object. A call to
16150 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16151 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16152 * operation.
16153 * @param event The {@link android.view.DragEvent} sent by the system.
16154 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16155 * in DragEvent, indicating the type of drag event represented by this object.
16156 * @return {@code true} if the method was successful, otherwise {@code false}.
16157 * <p>
16158 * The method should return {@code true} in response to an action type of
16159 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16160 * operation.
16161 * </p>
16162 * <p>
16163 * The method should also return {@code true} in response to an action type of
16164 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16165 * {@code false} if it didn't.
16166 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016167 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016168 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016169 return false;
16170 }
16171
16172 /**
Joe Malin32736f02011-01-19 16:14:20 -080016173 * Detects if this View is enabled and has a drag event listener.
16174 * If both are true, then it calls the drag event listener with the
16175 * {@link android.view.DragEvent} it received. If the drag event listener returns
16176 * {@code true}, then dispatchDragEvent() returns {@code true}.
16177 * <p>
16178 * For all other cases, the method calls the
16179 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16180 * method and returns its result.
16181 * </p>
16182 * <p>
16183 * This ensures that a drag event is always consumed, even if the View does not have a drag
16184 * event listener. However, if the View has a listener and the listener returns true, then
16185 * onDragEvent() is not called.
16186 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016187 */
16188 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016189 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016190 ListenerInfo li = mListenerInfo;
16191 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16192 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016193 return true;
16194 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016195 return onDragEvent(event);
16196 }
16197
Christopher Tate3d4bf172011-03-28 16:16:46 -070016198 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016199 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016200 }
16201
Christopher Tatea53146c2010-09-07 11:57:52 -070016202 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016203 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16204 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016205 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016206 */
16207 public void onCloseSystemDialogs(String reason) {
16208 }
Joe Malin32736f02011-01-19 16:14:20 -080016209
Dianne Hackbornffa42482009-09-23 22:20:11 -070016210 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016211 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016212 * update a Region being computed for
16213 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016214 * that any non-transparent parts of the Drawable are removed from the
16215 * given transparent region.
16216 *
16217 * @param dr The Drawable whose transparency is to be applied to the region.
16218 * @param region A Region holding the current transparency information,
16219 * where any parts of the region that are set are considered to be
16220 * transparent. On return, this region will be modified to have the
16221 * transparency information reduced by the corresponding parts of the
16222 * Drawable that are not transparent.
16223 * {@hide}
16224 */
16225 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16226 if (DBG) {
16227 Log.i("View", "Getting transparent region for: " + this);
16228 }
16229 final Region r = dr.getTransparentRegion();
16230 final Rect db = dr.getBounds();
16231 final AttachInfo attachInfo = mAttachInfo;
16232 if (r != null && attachInfo != null) {
16233 final int w = getRight()-getLeft();
16234 final int h = getBottom()-getTop();
16235 if (db.left > 0) {
16236 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16237 r.op(0, 0, db.left, h, Region.Op.UNION);
16238 }
16239 if (db.right < w) {
16240 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16241 r.op(db.right, 0, w, h, Region.Op.UNION);
16242 }
16243 if (db.top > 0) {
16244 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16245 r.op(0, 0, w, db.top, Region.Op.UNION);
16246 }
16247 if (db.bottom < h) {
16248 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16249 r.op(0, db.bottom, w, h, Region.Op.UNION);
16250 }
16251 final int[] location = attachInfo.mTransparentLocation;
16252 getLocationInWindow(location);
16253 r.translate(location[0], location[1]);
16254 region.op(r, Region.Op.INTERSECT);
16255 } else {
16256 region.op(db, Region.Op.DIFFERENCE);
16257 }
16258 }
16259
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016260 private void checkForLongClick(int delayOffset) {
16261 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16262 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016263
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016264 if (mPendingCheckForLongPress == null) {
16265 mPendingCheckForLongPress = new CheckForLongPress();
16266 }
16267 mPendingCheckForLongPress.rememberWindowAttachCount();
16268 postDelayed(mPendingCheckForLongPress,
16269 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016271 }
16272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016273 /**
16274 * Inflate a view from an XML resource. This convenience method wraps the {@link
16275 * LayoutInflater} class, which provides a full range of options for view inflation.
16276 *
16277 * @param context The Context object for your activity or application.
16278 * @param resource The resource ID to inflate
16279 * @param root A view group that will be the parent. Used to properly inflate the
16280 * layout_* parameters.
16281 * @see LayoutInflater
16282 */
16283 public static View inflate(Context context, int resource, ViewGroup root) {
16284 LayoutInflater factory = LayoutInflater.from(context);
16285 return factory.inflate(resource, root);
16286 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016288 /**
Adam Powell637d3372010-08-25 14:37:03 -070016289 * Scroll the view with standard behavior for scrolling beyond the normal
16290 * content boundaries. Views that call this method should override
16291 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16292 * results of an over-scroll operation.
16293 *
16294 * Views can use this method to handle any touch or fling-based scrolling.
16295 *
16296 * @param deltaX Change in X in pixels
16297 * @param deltaY Change in Y in pixels
16298 * @param scrollX Current X scroll value in pixels before applying deltaX
16299 * @param scrollY Current Y scroll value in pixels before applying deltaY
16300 * @param scrollRangeX Maximum content scroll range along the X axis
16301 * @param scrollRangeY Maximum content scroll range along the Y axis
16302 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16303 * along the X axis.
16304 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16305 * along the Y axis.
16306 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16307 * @return true if scrolling was clamped to an over-scroll boundary along either
16308 * axis, false otherwise.
16309 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016310 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016311 protected boolean overScrollBy(int deltaX, int deltaY,
16312 int scrollX, int scrollY,
16313 int scrollRangeX, int scrollRangeY,
16314 int maxOverScrollX, int maxOverScrollY,
16315 boolean isTouchEvent) {
16316 final int overScrollMode = mOverScrollMode;
16317 final boolean canScrollHorizontal =
16318 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16319 final boolean canScrollVertical =
16320 computeVerticalScrollRange() > computeVerticalScrollExtent();
16321 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16322 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16323 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16324 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16325
16326 int newScrollX = scrollX + deltaX;
16327 if (!overScrollHorizontal) {
16328 maxOverScrollX = 0;
16329 }
16330
16331 int newScrollY = scrollY + deltaY;
16332 if (!overScrollVertical) {
16333 maxOverScrollY = 0;
16334 }
16335
16336 // Clamp values if at the limits and record
16337 final int left = -maxOverScrollX;
16338 final int right = maxOverScrollX + scrollRangeX;
16339 final int top = -maxOverScrollY;
16340 final int bottom = maxOverScrollY + scrollRangeY;
16341
16342 boolean clampedX = false;
16343 if (newScrollX > right) {
16344 newScrollX = right;
16345 clampedX = true;
16346 } else if (newScrollX < left) {
16347 newScrollX = left;
16348 clampedX = true;
16349 }
16350
16351 boolean clampedY = false;
16352 if (newScrollY > bottom) {
16353 newScrollY = bottom;
16354 clampedY = true;
16355 } else if (newScrollY < top) {
16356 newScrollY = top;
16357 clampedY = true;
16358 }
16359
16360 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16361
16362 return clampedX || clampedY;
16363 }
16364
16365 /**
16366 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16367 * respond to the results of an over-scroll operation.
16368 *
16369 * @param scrollX New X scroll value in pixels
16370 * @param scrollY New Y scroll value in pixels
16371 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16372 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16373 */
16374 protected void onOverScrolled(int scrollX, int scrollY,
16375 boolean clampedX, boolean clampedY) {
16376 // Intentionally empty.
16377 }
16378
16379 /**
16380 * Returns the over-scroll mode for this view. The result will be
16381 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16382 * (allow over-scrolling only if the view content is larger than the container),
16383 * or {@link #OVER_SCROLL_NEVER}.
16384 *
16385 * @return This view's over-scroll mode.
16386 */
16387 public int getOverScrollMode() {
16388 return mOverScrollMode;
16389 }
16390
16391 /**
16392 * Set the over-scroll mode for this view. Valid over-scroll modes are
16393 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16394 * (allow over-scrolling only if the view content is larger than the container),
16395 * or {@link #OVER_SCROLL_NEVER}.
16396 *
16397 * Setting the over-scroll mode of a view will have an effect only if the
16398 * view is capable of scrolling.
16399 *
16400 * @param overScrollMode The new over-scroll mode for this view.
16401 */
16402 public void setOverScrollMode(int overScrollMode) {
16403 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16404 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16405 overScrollMode != OVER_SCROLL_NEVER) {
16406 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16407 }
16408 mOverScrollMode = overScrollMode;
16409 }
16410
16411 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016412 * Gets a scale factor that determines the distance the view should scroll
16413 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16414 * @return The vertical scroll scale factor.
16415 * @hide
16416 */
16417 protected float getVerticalScrollFactor() {
16418 if (mVerticalScrollFactor == 0) {
16419 TypedValue outValue = new TypedValue();
16420 if (!mContext.getTheme().resolveAttribute(
16421 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16422 throw new IllegalStateException(
16423 "Expected theme to define listPreferredItemHeight.");
16424 }
16425 mVerticalScrollFactor = outValue.getDimension(
16426 mContext.getResources().getDisplayMetrics());
16427 }
16428 return mVerticalScrollFactor;
16429 }
16430
16431 /**
16432 * Gets a scale factor that determines the distance the view should scroll
16433 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16434 * @return The horizontal scroll scale factor.
16435 * @hide
16436 */
16437 protected float getHorizontalScrollFactor() {
16438 // TODO: Should use something else.
16439 return getVerticalScrollFactor();
16440 }
16441
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016442 /**
16443 * Return the value specifying the text direction or policy that was set with
16444 * {@link #setTextDirection(int)}.
16445 *
16446 * @return the defined text direction. It can be one of:
16447 *
16448 * {@link #TEXT_DIRECTION_INHERIT},
16449 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16450 * {@link #TEXT_DIRECTION_ANY_RTL},
16451 * {@link #TEXT_DIRECTION_LTR},
16452 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016453 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016454 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016455 @ViewDebug.ExportedProperty(category = "text", mapping = {
16456 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16457 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16458 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16459 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16460 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16461 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16462 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016463 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016464 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016465 }
16466
16467 /**
16468 * Set the text direction.
16469 *
16470 * @param textDirection the direction to set. Should be one of:
16471 *
16472 * {@link #TEXT_DIRECTION_INHERIT},
16473 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16474 * {@link #TEXT_DIRECTION_ANY_RTL},
16475 * {@link #TEXT_DIRECTION_LTR},
16476 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016477 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016478 */
16479 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016480 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016481 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016482 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016483 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016484 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016485 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016486 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016487 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016488 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016489 }
16490 }
16491
16492 /**
16493 * Return the resolved text direction.
16494 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016495 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16496 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16497 * up the parent chain of the view. if there is no parent, then it will return the default
16498 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16499 *
16500 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016501 *
Doug Feltcb3791202011-07-07 11:57:48 -070016502 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16503 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016504 * {@link #TEXT_DIRECTION_LTR},
16505 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016506 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016507 */
16508 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016509 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016510 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016511 resolveTextDirection();
16512 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016513 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016514 }
16515
16516 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016517 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16518 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016519 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016520 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016521 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016522 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016523
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016524 if (hasRtlSupport()) {
16525 // Set resolved text direction flag depending on text direction flag
16526 final int textDirection = getTextDirection();
16527 switch(textDirection) {
16528 case TEXT_DIRECTION_INHERIT:
16529 if (canResolveTextDirection()) {
16530 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016531
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016532 // Set current resolved direction to the same value as the parent's one
16533 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16534 switch (parentResolvedDirection) {
16535 case TEXT_DIRECTION_FIRST_STRONG:
16536 case TEXT_DIRECTION_ANY_RTL:
16537 case TEXT_DIRECTION_LTR:
16538 case TEXT_DIRECTION_RTL:
16539 case TEXT_DIRECTION_LOCALE:
16540 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016541 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016542 break;
16543 default:
16544 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016545 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016546 }
16547 } else {
16548 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016549 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016550 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016551 break;
16552 case TEXT_DIRECTION_FIRST_STRONG:
16553 case TEXT_DIRECTION_ANY_RTL:
16554 case TEXT_DIRECTION_LTR:
16555 case TEXT_DIRECTION_RTL:
16556 case TEXT_DIRECTION_LOCALE:
16557 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016558 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016559 break;
16560 default:
16561 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016562 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016563 }
16564 } else {
16565 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016566 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016567 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016568
16569 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016570 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016571 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016572 }
16573
16574 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016575 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016576 * resolution should override this method.
16577 *
16578 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016579 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016580 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016581 }
16582
16583 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016584 * Check if text direction resolution can be done.
16585 *
16586 * @return true if text direction resolution can be done otherwise return false.
16587 */
16588 public boolean canResolveTextDirection() {
16589 switch (getTextDirection()) {
16590 case TEXT_DIRECTION_INHERIT:
16591 return (mParent != null) && (mParent instanceof ViewGroup);
16592 default:
16593 return true;
16594 }
16595 }
16596
16597 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016598 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016599 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016600 * reset is done.
16601 */
16602 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016603 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016604 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016605 }
16606
16607 /**
16608 * Called when text direction is reset. Subclasses that care about text direction reset should
16609 * override this method and do a reset of the text direction of their children. The default
16610 * implementation does nothing.
16611 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016612 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016613 }
16614
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016615 /**
16616 * Return the value specifying the text alignment or policy that was set with
16617 * {@link #setTextAlignment(int)}.
16618 *
16619 * @return the defined text alignment. It can be one of:
16620 *
16621 * {@link #TEXT_ALIGNMENT_INHERIT},
16622 * {@link #TEXT_ALIGNMENT_GRAVITY},
16623 * {@link #TEXT_ALIGNMENT_CENTER},
16624 * {@link #TEXT_ALIGNMENT_TEXT_START},
16625 * {@link #TEXT_ALIGNMENT_TEXT_END},
16626 * {@link #TEXT_ALIGNMENT_VIEW_START},
16627 * {@link #TEXT_ALIGNMENT_VIEW_END}
16628 */
16629 @ViewDebug.ExportedProperty(category = "text", mapping = {
16630 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16631 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16632 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16633 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16634 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16635 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16636 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16637 })
16638 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016639 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016640 }
16641
16642 /**
16643 * Set the text alignment.
16644 *
16645 * @param textAlignment The text alignment to set. Should be one of
16646 *
16647 * {@link #TEXT_ALIGNMENT_INHERIT},
16648 * {@link #TEXT_ALIGNMENT_GRAVITY},
16649 * {@link #TEXT_ALIGNMENT_CENTER},
16650 * {@link #TEXT_ALIGNMENT_TEXT_START},
16651 * {@link #TEXT_ALIGNMENT_TEXT_END},
16652 * {@link #TEXT_ALIGNMENT_VIEW_START},
16653 * {@link #TEXT_ALIGNMENT_VIEW_END}
16654 *
16655 * @attr ref android.R.styleable#View_textAlignment
16656 */
16657 public void setTextAlignment(int textAlignment) {
16658 if (textAlignment != getTextAlignment()) {
16659 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016660 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016661 resetResolvedTextAlignment();
16662 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016663 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016664 // Refresh
16665 requestLayout();
16666 invalidate(true);
16667 }
16668 }
16669
16670 /**
16671 * Return the resolved text alignment.
16672 *
16673 * The resolved text alignment. This needs resolution if the value is
16674 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16675 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16676 *
16677 * @return the resolved text alignment. Returns one of:
16678 *
16679 * {@link #TEXT_ALIGNMENT_GRAVITY},
16680 * {@link #TEXT_ALIGNMENT_CENTER},
16681 * {@link #TEXT_ALIGNMENT_TEXT_START},
16682 * {@link #TEXT_ALIGNMENT_TEXT_END},
16683 * {@link #TEXT_ALIGNMENT_VIEW_START},
16684 * {@link #TEXT_ALIGNMENT_VIEW_END}
16685 */
16686 @ViewDebug.ExportedProperty(category = "text", mapping = {
16687 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16688 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16689 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16690 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16691 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16692 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16693 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16694 })
16695 public int getResolvedTextAlignment() {
16696 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016697 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016698 resolveTextAlignment();
16699 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016700 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016701 }
16702
16703 /**
16704 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16705 * resolution is done.
16706 */
16707 public void resolveTextAlignment() {
16708 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016709 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016710
16711 if (hasRtlSupport()) {
16712 // Set resolved text alignment flag depending on text alignment flag
16713 final int textAlignment = getTextAlignment();
16714 switch (textAlignment) {
16715 case TEXT_ALIGNMENT_INHERIT:
16716 // Check if we can resolve the text alignment
16717 if (canResolveLayoutDirection() && mParent instanceof View) {
16718 View view = (View) mParent;
16719
16720 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16721 switch (parentResolvedTextAlignment) {
16722 case TEXT_ALIGNMENT_GRAVITY:
16723 case TEXT_ALIGNMENT_TEXT_START:
16724 case TEXT_ALIGNMENT_TEXT_END:
16725 case TEXT_ALIGNMENT_CENTER:
16726 case TEXT_ALIGNMENT_VIEW_START:
16727 case TEXT_ALIGNMENT_VIEW_END:
16728 // Resolved text alignment is the same as the parent resolved
16729 // text alignment
16730 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016731 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016732 break;
16733 default:
16734 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016735 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016736 }
16737 }
16738 else {
16739 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016740 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016741 }
16742 break;
16743 case TEXT_ALIGNMENT_GRAVITY:
16744 case TEXT_ALIGNMENT_TEXT_START:
16745 case TEXT_ALIGNMENT_TEXT_END:
16746 case TEXT_ALIGNMENT_CENTER:
16747 case TEXT_ALIGNMENT_VIEW_START:
16748 case TEXT_ALIGNMENT_VIEW_END:
16749 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016750 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016751 break;
16752 default:
16753 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016754 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016755 }
16756 } else {
16757 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016758 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016759 }
16760
16761 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016762 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016763 onResolvedTextAlignmentChanged();
16764 }
16765
16766 /**
16767 * Check if text alignment resolution can be done.
16768 *
16769 * @return true if text alignment resolution can be done otherwise return false.
16770 */
16771 public boolean canResolveTextAlignment() {
16772 switch (getTextAlignment()) {
16773 case TEXT_DIRECTION_INHERIT:
16774 return (mParent != null);
16775 default:
16776 return true;
16777 }
16778 }
16779
16780 /**
16781 * Called when text alignment has been resolved. Subclasses that care about text alignment
16782 * resolution should override this method.
16783 *
16784 * The default implementation does nothing.
16785 */
16786 public void onResolvedTextAlignmentChanged() {
16787 }
16788
16789 /**
16790 * Reset resolved text alignment. Text alignment can be resolved with a call to
16791 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16792 * reset is done.
16793 */
16794 public void resetResolvedTextAlignment() {
16795 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016796 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016797 onResolvedTextAlignmentReset();
16798 }
16799
16800 /**
16801 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16802 * override this method and do a reset of the text alignment of their children. The default
16803 * implementation does nothing.
16804 */
16805 public void onResolvedTextAlignmentReset() {
16806 }
16807
Adam Powella9108a22012-07-18 11:18:09 -070016808 /**
16809 * Generate a value suitable for use in {@link #setId(int)}.
16810 * This value will not collide with ID values generated at build time by aapt for R.id.
16811 *
16812 * @return a generated ID value
16813 */
16814 public static int generateViewId() {
16815 for (;;) {
16816 final int result = sNextGeneratedId.get();
16817 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16818 int newValue = result + 1;
16819 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16820 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16821 return result;
16822 }
16823 }
16824 }
16825
Chet Haaseb39f0512011-05-24 14:36:40 -070016826 //
16827 // Properties
16828 //
16829 /**
16830 * A Property wrapper around the <code>alpha</code> functionality handled by the
16831 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16832 */
Chet Haased47f1532011-12-16 11:18:52 -080016833 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016834 @Override
16835 public void setValue(View object, float value) {
16836 object.setAlpha(value);
16837 }
16838
16839 @Override
16840 public Float get(View object) {
16841 return object.getAlpha();
16842 }
16843 };
16844
16845 /**
16846 * A Property wrapper around the <code>translationX</code> functionality handled by the
16847 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16848 */
Chet Haased47f1532011-12-16 11:18:52 -080016849 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016850 @Override
16851 public void setValue(View object, float value) {
16852 object.setTranslationX(value);
16853 }
16854
16855 @Override
16856 public Float get(View object) {
16857 return object.getTranslationX();
16858 }
16859 };
16860
16861 /**
16862 * A Property wrapper around the <code>translationY</code> functionality handled by the
16863 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16864 */
Chet Haased47f1532011-12-16 11:18:52 -080016865 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016866 @Override
16867 public void setValue(View object, float value) {
16868 object.setTranslationY(value);
16869 }
16870
16871 @Override
16872 public Float get(View object) {
16873 return object.getTranslationY();
16874 }
16875 };
16876
16877 /**
16878 * A Property wrapper around the <code>x</code> functionality handled by the
16879 * {@link View#setX(float)} and {@link View#getX()} methods.
16880 */
Chet Haased47f1532011-12-16 11:18:52 -080016881 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016882 @Override
16883 public void setValue(View object, float value) {
16884 object.setX(value);
16885 }
16886
16887 @Override
16888 public Float get(View object) {
16889 return object.getX();
16890 }
16891 };
16892
16893 /**
16894 * A Property wrapper around the <code>y</code> functionality handled by the
16895 * {@link View#setY(float)} and {@link View#getY()} methods.
16896 */
Chet Haased47f1532011-12-16 11:18:52 -080016897 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016898 @Override
16899 public void setValue(View object, float value) {
16900 object.setY(value);
16901 }
16902
16903 @Override
16904 public Float get(View object) {
16905 return object.getY();
16906 }
16907 };
16908
16909 /**
16910 * A Property wrapper around the <code>rotation</code> functionality handled by the
16911 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16912 */
Chet Haased47f1532011-12-16 11:18:52 -080016913 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016914 @Override
16915 public void setValue(View object, float value) {
16916 object.setRotation(value);
16917 }
16918
16919 @Override
16920 public Float get(View object) {
16921 return object.getRotation();
16922 }
16923 };
16924
16925 /**
16926 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16927 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16928 */
Chet Haased47f1532011-12-16 11:18:52 -080016929 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016930 @Override
16931 public void setValue(View object, float value) {
16932 object.setRotationX(value);
16933 }
16934
16935 @Override
16936 public Float get(View object) {
16937 return object.getRotationX();
16938 }
16939 };
16940
16941 /**
16942 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16943 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16944 */
Chet Haased47f1532011-12-16 11:18:52 -080016945 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016946 @Override
16947 public void setValue(View object, float value) {
16948 object.setRotationY(value);
16949 }
16950
16951 @Override
16952 public Float get(View object) {
16953 return object.getRotationY();
16954 }
16955 };
16956
16957 /**
16958 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16959 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16960 */
Chet Haased47f1532011-12-16 11:18:52 -080016961 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016962 @Override
16963 public void setValue(View object, float value) {
16964 object.setScaleX(value);
16965 }
16966
16967 @Override
16968 public Float get(View object) {
16969 return object.getScaleX();
16970 }
16971 };
16972
16973 /**
16974 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16975 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16976 */
Chet Haased47f1532011-12-16 11:18:52 -080016977 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016978 @Override
16979 public void setValue(View object, float value) {
16980 object.setScaleY(value);
16981 }
16982
16983 @Override
16984 public Float get(View object) {
16985 return object.getScaleY();
16986 }
16987 };
16988
Jeff Brown33bbfd22011-02-24 20:55:35 -080016989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016990 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16991 * Each MeasureSpec represents a requirement for either the width or the height.
16992 * A MeasureSpec is comprised of a size and a mode. There are three possible
16993 * modes:
16994 * <dl>
16995 * <dt>UNSPECIFIED</dt>
16996 * <dd>
16997 * The parent has not imposed any constraint on the child. It can be whatever size
16998 * it wants.
16999 * </dd>
17000 *
17001 * <dt>EXACTLY</dt>
17002 * <dd>
17003 * The parent has determined an exact size for the child. The child is going to be
17004 * given those bounds regardless of how big it wants to be.
17005 * </dd>
17006 *
17007 * <dt>AT_MOST</dt>
17008 * <dd>
17009 * The child can be as large as it wants up to the specified size.
17010 * </dd>
17011 * </dl>
17012 *
17013 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17014 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17015 */
17016 public static class MeasureSpec {
17017 private static final int MODE_SHIFT = 30;
17018 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17019
17020 /**
17021 * Measure specification mode: The parent has not imposed any constraint
17022 * on the child. It can be whatever size it wants.
17023 */
17024 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17025
17026 /**
17027 * Measure specification mode: The parent has determined an exact size
17028 * for the child. The child is going to be given those bounds regardless
17029 * of how big it wants to be.
17030 */
17031 public static final int EXACTLY = 1 << MODE_SHIFT;
17032
17033 /**
17034 * Measure specification mode: The child can be as large as it wants up
17035 * to the specified size.
17036 */
17037 public static final int AT_MOST = 2 << MODE_SHIFT;
17038
17039 /**
17040 * Creates a measure specification based on the supplied size and mode.
17041 *
17042 * The mode must always be one of the following:
17043 * <ul>
17044 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17045 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17046 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17047 * </ul>
17048 *
17049 * @param size the size of the measure specification
17050 * @param mode the mode of the measure specification
17051 * @return the measure specification based on size and mode
17052 */
17053 public static int makeMeasureSpec(int size, int mode) {
17054 return size + mode;
17055 }
17056
17057 /**
17058 * Extracts the mode from the supplied measure specification.
17059 *
17060 * @param measureSpec the measure specification to extract the mode from
17061 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17062 * {@link android.view.View.MeasureSpec#AT_MOST} or
17063 * {@link android.view.View.MeasureSpec#EXACTLY}
17064 */
17065 public static int getMode(int measureSpec) {
17066 return (measureSpec & MODE_MASK);
17067 }
17068
17069 /**
17070 * Extracts the size from the supplied measure specification.
17071 *
17072 * @param measureSpec the measure specification to extract the size from
17073 * @return the size in pixels defined in the supplied measure specification
17074 */
17075 public static int getSize(int measureSpec) {
17076 return (measureSpec & ~MODE_MASK);
17077 }
17078
17079 /**
17080 * Returns a String representation of the specified measure
17081 * specification.
17082 *
17083 * @param measureSpec the measure specification to convert to a String
17084 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17085 */
17086 public static String toString(int measureSpec) {
17087 int mode = getMode(measureSpec);
17088 int size = getSize(measureSpec);
17089
17090 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17091
17092 if (mode == UNSPECIFIED)
17093 sb.append("UNSPECIFIED ");
17094 else if (mode == EXACTLY)
17095 sb.append("EXACTLY ");
17096 else if (mode == AT_MOST)
17097 sb.append("AT_MOST ");
17098 else
17099 sb.append(mode).append(" ");
17100
17101 sb.append(size);
17102 return sb.toString();
17103 }
17104 }
17105
17106 class CheckForLongPress implements Runnable {
17107
17108 private int mOriginalWindowAttachCount;
17109
17110 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017111 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017112 && mOriginalWindowAttachCount == mWindowAttachCount) {
17113 if (performLongClick()) {
17114 mHasPerformedLongPress = true;
17115 }
17116 }
17117 }
17118
17119 public void rememberWindowAttachCount() {
17120 mOriginalWindowAttachCount = mWindowAttachCount;
17121 }
17122 }
Joe Malin32736f02011-01-19 16:14:20 -080017123
Adam Powelle14579b2009-12-16 18:39:52 -080017124 private final class CheckForTap implements Runnable {
17125 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017126 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017127 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017128 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017129 }
17130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017131
Adam Powella35d7682010-03-12 14:48:13 -080017132 private final class PerformClick implements Runnable {
17133 public void run() {
17134 performClick();
17135 }
17136 }
17137
Dianne Hackborn63042d62011-01-26 18:56:29 -080017138 /** @hide */
17139 public void hackTurnOffWindowResizeAnim(boolean off) {
17140 mAttachInfo.mTurnOffWindowResizeAnim = off;
17141 }
Joe Malin32736f02011-01-19 16:14:20 -080017142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017143 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017144 * This method returns a ViewPropertyAnimator object, which can be used to animate
17145 * specific properties on this View.
17146 *
17147 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17148 */
17149 public ViewPropertyAnimator animate() {
17150 if (mAnimator == null) {
17151 mAnimator = new ViewPropertyAnimator(this);
17152 }
17153 return mAnimator;
17154 }
17155
17156 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017157 * Interface definition for a callback to be invoked when a hardware key event is
17158 * dispatched to this view. The callback will be invoked before the key event is
17159 * given to the view. This is only useful for hardware keyboards; a software input
17160 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017161 */
17162 public interface OnKeyListener {
17163 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017164 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017165 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017166 * <p>Key presses in software keyboards will generally NOT trigger this method,
17167 * although some may elect to do so in some situations. Do not assume a
17168 * software input method has to be key-based; even if it is, it may use key presses
17169 * in a different way than you expect, so there is no way to reliably catch soft
17170 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017171 *
17172 * @param v The view the key has been dispatched to.
17173 * @param keyCode The code for the physical key that was pressed
17174 * @param event The KeyEvent object containing full information about
17175 * the event.
17176 * @return True if the listener has consumed the event, false otherwise.
17177 */
17178 boolean onKey(View v, int keyCode, KeyEvent event);
17179 }
17180
17181 /**
17182 * Interface definition for a callback to be invoked when a touch event is
17183 * dispatched to this view. The callback will be invoked before the touch
17184 * event is given to the view.
17185 */
17186 public interface OnTouchListener {
17187 /**
17188 * Called when a touch event is dispatched to a view. This allows listeners to
17189 * get a chance to respond before the target view.
17190 *
17191 * @param v The view the touch event has been dispatched to.
17192 * @param event The MotionEvent object containing full information about
17193 * the event.
17194 * @return True if the listener has consumed the event, false otherwise.
17195 */
17196 boolean onTouch(View v, MotionEvent event);
17197 }
17198
17199 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017200 * Interface definition for a callback to be invoked when a hover event is
17201 * dispatched to this view. The callback will be invoked before the hover
17202 * event is given to the view.
17203 */
17204 public interface OnHoverListener {
17205 /**
17206 * Called when a hover event is dispatched to a view. This allows listeners to
17207 * get a chance to respond before the target view.
17208 *
17209 * @param v The view the hover event has been dispatched to.
17210 * @param event The MotionEvent object containing full information about
17211 * the event.
17212 * @return True if the listener has consumed the event, false otherwise.
17213 */
17214 boolean onHover(View v, MotionEvent event);
17215 }
17216
17217 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017218 * Interface definition for a callback to be invoked when a generic motion event is
17219 * dispatched to this view. The callback will be invoked before the generic motion
17220 * event is given to the view.
17221 */
17222 public interface OnGenericMotionListener {
17223 /**
17224 * Called when a generic motion event is dispatched to a view. This allows listeners to
17225 * get a chance to respond before the target view.
17226 *
17227 * @param v The view the generic motion event has been dispatched to.
17228 * @param event The MotionEvent object containing full information about
17229 * the event.
17230 * @return True if the listener has consumed the event, false otherwise.
17231 */
17232 boolean onGenericMotion(View v, MotionEvent event);
17233 }
17234
17235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017236 * Interface definition for a callback to be invoked when a view has been clicked and held.
17237 */
17238 public interface OnLongClickListener {
17239 /**
17240 * Called when a view has been clicked and held.
17241 *
17242 * @param v The view that was clicked and held.
17243 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017244 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017245 */
17246 boolean onLongClick(View v);
17247 }
17248
17249 /**
Chris Tate32affef2010-10-18 15:29:21 -070017250 * Interface definition for a callback to be invoked when a drag is being dispatched
17251 * to this view. The callback will be invoked before the hosting view's own
17252 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17253 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017254 *
17255 * <div class="special reference">
17256 * <h3>Developer Guides</h3>
17257 * <p>For a guide to implementing drag and drop features, read the
17258 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17259 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017260 */
17261 public interface OnDragListener {
17262 /**
17263 * Called when a drag event is dispatched to a view. This allows listeners
17264 * to get a chance to override base View behavior.
17265 *
Joe Malin32736f02011-01-19 16:14:20 -080017266 * @param v The View that received the drag event.
17267 * @param event The {@link android.view.DragEvent} object for the drag event.
17268 * @return {@code true} if the drag event was handled successfully, or {@code false}
17269 * if the drag event was not handled. Note that {@code false} will trigger the View
17270 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017271 */
17272 boolean onDrag(View v, DragEvent event);
17273 }
17274
17275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017276 * Interface definition for a callback to be invoked when the focus state of
17277 * a view changed.
17278 */
17279 public interface OnFocusChangeListener {
17280 /**
17281 * Called when the focus state of a view has changed.
17282 *
17283 * @param v The view whose state has changed.
17284 * @param hasFocus The new focus state of v.
17285 */
17286 void onFocusChange(View v, boolean hasFocus);
17287 }
17288
17289 /**
17290 * Interface definition for a callback to be invoked when a view is clicked.
17291 */
17292 public interface OnClickListener {
17293 /**
17294 * Called when a view has been clicked.
17295 *
17296 * @param v The view that was clicked.
17297 */
17298 void onClick(View v);
17299 }
17300
17301 /**
17302 * Interface definition for a callback to be invoked when the context menu
17303 * for this view is being built.
17304 */
17305 public interface OnCreateContextMenuListener {
17306 /**
17307 * Called when the context menu for this view is being built. It is not
17308 * safe to hold onto the menu after this method returns.
17309 *
17310 * @param menu The context menu that is being built
17311 * @param v The view for which the context menu is being built
17312 * @param menuInfo Extra information about the item for which the
17313 * context menu should be shown. This information will vary
17314 * depending on the class of v.
17315 */
17316 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17317 }
17318
Joe Onorato664644d2011-01-23 17:53:23 -080017319 /**
17320 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017321 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017322 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017323 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017324 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017325 */
17326 public interface OnSystemUiVisibilityChangeListener {
17327 /**
17328 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017329 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017330 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017331 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17332 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17333 * This tells you the <strong>global</strong> state of these UI visibility
17334 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017335 */
17336 public void onSystemUiVisibilityChange(int visibility);
17337 }
17338
Adam Powell4afd62b2011-02-18 15:02:18 -080017339 /**
17340 * Interface definition for a callback to be invoked when this view is attached
17341 * or detached from its window.
17342 */
17343 public interface OnAttachStateChangeListener {
17344 /**
17345 * Called when the view is attached to a window.
17346 * @param v The view that was attached
17347 */
17348 public void onViewAttachedToWindow(View v);
17349 /**
17350 * Called when the view is detached from a window.
17351 * @param v The view that was detached
17352 */
17353 public void onViewDetachedFromWindow(View v);
17354 }
17355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356 private final class UnsetPressedState implements Runnable {
17357 public void run() {
17358 setPressed(false);
17359 }
17360 }
17361
17362 /**
17363 * Base class for derived classes that want to save and restore their own
17364 * state in {@link android.view.View#onSaveInstanceState()}.
17365 */
17366 public static class BaseSavedState extends AbsSavedState {
17367 /**
17368 * Constructor used when reading from a parcel. Reads the state of the superclass.
17369 *
17370 * @param source
17371 */
17372 public BaseSavedState(Parcel source) {
17373 super(source);
17374 }
17375
17376 /**
17377 * Constructor called by derived classes when creating their SavedState objects
17378 *
17379 * @param superState The state of the superclass of this view
17380 */
17381 public BaseSavedState(Parcelable superState) {
17382 super(superState);
17383 }
17384
17385 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17386 new Parcelable.Creator<BaseSavedState>() {
17387 public BaseSavedState createFromParcel(Parcel in) {
17388 return new BaseSavedState(in);
17389 }
17390
17391 public BaseSavedState[] newArray(int size) {
17392 return new BaseSavedState[size];
17393 }
17394 };
17395 }
17396
17397 /**
17398 * A set of information given to a view when it is attached to its parent
17399 * window.
17400 */
17401 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 interface Callbacks {
17403 void playSoundEffect(int effectId);
17404 boolean performHapticFeedback(int effectId, boolean always);
17405 }
17406
17407 /**
17408 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17409 * to a Handler. This class contains the target (View) to invalidate and
17410 * the coordinates of the dirty rectangle.
17411 *
17412 * For performance purposes, this class also implements a pool of up to
17413 * POOL_LIMIT objects that get reused. This reduces memory allocations
17414 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017415 */
Romain Guyd928d682009-03-31 17:52:16 -070017416 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017417 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017418 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17419 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017420 public InvalidateInfo newInstance() {
17421 return new InvalidateInfo();
17422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017423
Romain Guyd928d682009-03-31 17:52:16 -070017424 public void onAcquired(InvalidateInfo element) {
17425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017426
Romain Guyd928d682009-03-31 17:52:16 -070017427 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017428 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017429 }
17430 }, POOL_LIMIT)
17431 );
17432
17433 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017434 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017435
17436 View target;
17437
17438 int left;
17439 int top;
17440 int right;
17441 int bottom;
17442
Romain Guyd928d682009-03-31 17:52:16 -070017443 public void setNextPoolable(InvalidateInfo element) {
17444 mNext = element;
17445 }
17446
17447 public InvalidateInfo getNextPoolable() {
17448 return mNext;
17449 }
17450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017451 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017452 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017453 }
17454
17455 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017456 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017457 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017458
17459 public boolean isPooled() {
17460 return mIsPooled;
17461 }
17462
17463 public void setPooled(boolean isPooled) {
17464 mIsPooled = isPooled;
17465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017466 }
17467
17468 final IWindowSession mSession;
17469
17470 final IWindow mWindow;
17471
17472 final IBinder mWindowToken;
17473
Jeff Brown98365d72012-08-19 20:30:52 -070017474 final Display mDisplay;
17475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017476 final Callbacks mRootCallbacks;
17477
Romain Guy59a12ca2011-06-09 17:48:21 -070017478 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017480 /**
17481 * The top view of the hierarchy.
17482 */
17483 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017485 IBinder mPanelParentWindowToken;
17486 Surface mSurface;
17487
Romain Guyb051e892010-09-28 19:09:36 -070017488 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017489 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017490 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017491
Romain Guy7e4e5612012-03-05 14:37:29 -080017492 boolean mScreenOn;
17493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017494 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017495 * Scale factor used by the compatibility mode
17496 */
17497 float mApplicationScale;
17498
17499 /**
17500 * Indicates whether the application is in compatibility mode
17501 */
17502 boolean mScalingRequired;
17503
17504 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017505 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017506 */
17507 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017508
Dianne Hackborn63042d62011-01-26 18:56:29 -080017509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017510 * Left position of this view's window
17511 */
17512 int mWindowLeft;
17513
17514 /**
17515 * Top position of this view's window
17516 */
17517 int mWindowTop;
17518
17519 /**
Adam Powell26153a32010-11-08 15:22:27 -080017520 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017521 */
Adam Powell26153a32010-11-08 15:22:27 -080017522 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017523
17524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017525 * For windows that are full-screen but using insets to layout inside
17526 * of the screen decorations, these are the current insets for the
17527 * content of the window.
17528 */
17529 final Rect mContentInsets = new Rect();
17530
17531 /**
17532 * For windows that are full-screen but using insets to layout inside
17533 * of the screen decorations, these are the current insets for the
17534 * actual visible parts of the window.
17535 */
17536 final Rect mVisibleInsets = new Rect();
17537
17538 /**
17539 * The internal insets given by this window. This value is
17540 * supplied by the client (through
17541 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17542 * be given to the window manager when changed to be used in laying
17543 * out windows behind it.
17544 */
17545 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17546 = new ViewTreeObserver.InternalInsetsInfo();
17547
17548 /**
17549 * All views in the window's hierarchy that serve as scroll containers,
17550 * used to determine if the window can be resized or must be panned
17551 * to adjust for a soft input area.
17552 */
17553 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17554
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017555 final KeyEvent.DispatcherState mKeyDispatchState
17556 = new KeyEvent.DispatcherState();
17557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017558 /**
17559 * Indicates whether the view's window currently has the focus.
17560 */
17561 boolean mHasWindowFocus;
17562
17563 /**
17564 * The current visibility of the window.
17565 */
17566 int mWindowVisibility;
17567
17568 /**
17569 * Indicates the time at which drawing started to occur.
17570 */
17571 long mDrawingTime;
17572
17573 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017574 * Indicates whether or not ignoring the DIRTY_MASK flags.
17575 */
17576 boolean mIgnoreDirtyState;
17577
17578 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017579 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17580 * to avoid clearing that flag prematurely.
17581 */
17582 boolean mSetIgnoreDirtyState = false;
17583
17584 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017585 * Indicates whether the view's window is currently in touch mode.
17586 */
17587 boolean mInTouchMode;
17588
17589 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017590 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017591 * the next time it performs a traversal
17592 */
17593 boolean mRecomputeGlobalAttributes;
17594
17595 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017596 * Always report new attributes at next traversal.
17597 */
17598 boolean mForceReportNewAttributes;
17599
17600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017601 * Set during a traveral if any views want to keep the screen on.
17602 */
17603 boolean mKeepScreenOn;
17604
17605 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017606 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17607 */
17608 int mSystemUiVisibility;
17609
17610 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017611 * Hack to force certain system UI visibility flags to be cleared.
17612 */
17613 int mDisabledSystemUiVisibility;
17614
17615 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017616 * Last global system UI visibility reported by the window manager.
17617 */
17618 int mGlobalSystemUiVisibility;
17619
17620 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017621 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17622 * attached.
17623 */
17624 boolean mHasSystemUiListeners;
17625
17626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017627 * Set if the visibility of any views has changed.
17628 */
17629 boolean mViewVisibilityChanged;
17630
17631 /**
17632 * Set to true if a view has been scrolled.
17633 */
17634 boolean mViewScrollChanged;
17635
17636 /**
17637 * Global to the view hierarchy used as a temporary for dealing with
17638 * x/y points in the transparent region computations.
17639 */
17640 final int[] mTransparentLocation = new int[2];
17641
17642 /**
17643 * Global to the view hierarchy used as a temporary for dealing with
17644 * x/y points in the ViewGroup.invalidateChild implementation.
17645 */
17646 final int[] mInvalidateChildLocation = new int[2];
17647
Chet Haasec3aa3612010-06-17 08:50:37 -070017648
17649 /**
17650 * Global to the view hierarchy used as a temporary for dealing with
17651 * x/y location when view is transformed.
17652 */
17653 final float[] mTmpTransformLocation = new float[2];
17654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017655 /**
17656 * The view tree observer used to dispatch global events like
17657 * layout, pre-draw, touch mode change, etc.
17658 */
17659 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17660
17661 /**
17662 * A Canvas used by the view hierarchy to perform bitmap caching.
17663 */
17664 Canvas mCanvas;
17665
17666 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017667 * The view root impl.
17668 */
17669 final ViewRootImpl mViewRootImpl;
17670
17671 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017672 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017673 * handler can be used to pump events in the UI events queue.
17674 */
17675 final Handler mHandler;
17676
17677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017678 * Temporary for use in computing invalidate rectangles while
17679 * calling up the hierarchy.
17680 */
17681 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017682
17683 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017684 * Temporary for use in computing hit areas with transformed views
17685 */
17686 final RectF mTmpTransformRect = new RectF();
17687
17688 /**
Chet Haase599913d2012-07-23 16:22:05 -070017689 * Temporary for use in transforming invalidation rect
17690 */
17691 final Matrix mTmpMatrix = new Matrix();
17692
17693 /**
17694 * Temporary for use in transforming invalidation rect
17695 */
17696 final Transformation mTmpTransformation = new Transformation();
17697
17698 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017699 * Temporary list for use in collecting focusable descendents of a view.
17700 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017701 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017703 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017704 * The id of the window for accessibility purposes.
17705 */
17706 int mAccessibilityWindowId = View.NO_ID;
17707
17708 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017709 * Whether to ingore not exposed for accessibility Views when
17710 * reporting the view tree to accessibility services.
17711 */
17712 boolean mIncludeNotImportantViews;
17713
17714 /**
17715 * The drawable for highlighting accessibility focus.
17716 */
17717 Drawable mAccessibilityFocusDrawable;
17718
17719 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017720 * Show where the margins, bounds and layout bounds are for each view.
17721 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017722 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017723
17724 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017725 * Point used to compute visible regions.
17726 */
17727 final Point mPoint = new Point();
17728
17729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017730 * Creates a new set of attachment information with the specified
17731 * events handler and thread.
17732 *
17733 * @param handler the events handler the view must use
17734 */
Jeff Brown98365d72012-08-19 20:30:52 -070017735 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017736 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017737 mSession = session;
17738 mWindow = window;
17739 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017740 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017741 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017742 mHandler = handler;
17743 mRootCallbacks = effectPlayer;
17744 }
17745 }
17746
17747 /**
17748 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17749 * is supported. This avoids keeping too many unused fields in most
17750 * instances of View.</p>
17751 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017752 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017753
Mike Cleronf116bf82009-09-27 19:14:12 -070017754 /**
17755 * Scrollbars are not visible
17756 */
17757 public static final int OFF = 0;
17758
17759 /**
17760 * Scrollbars are visible
17761 */
17762 public static final int ON = 1;
17763
17764 /**
17765 * Scrollbars are fading away
17766 */
17767 public static final int FADING = 2;
17768
17769 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017771 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017772 public int scrollBarDefaultDelayBeforeFade;
17773 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017774
17775 public int scrollBarSize;
17776 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017777 public float[] interpolatorValues;
17778 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017779
17780 public final Paint paint;
17781 public final Matrix matrix;
17782 public Shader shader;
17783
Mike Cleronf116bf82009-09-27 19:14:12 -070017784 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17785
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017786 private static final float[] OPAQUE = { 255 };
17787 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017788
Mike Cleronf116bf82009-09-27 19:14:12 -070017789 /**
17790 * When fading should start. This time moves into the future every time
17791 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17792 */
17793 public long fadeStartTime;
17794
17795
17796 /**
17797 * The current state of the scrollbars: ON, OFF, or FADING
17798 */
17799 public int state = OFF;
17800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017801 private int mLastColor;
17802
Mike Cleronf116bf82009-09-27 19:14:12 -070017803 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017804 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17805 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017806 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17807 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017808
17809 paint = new Paint();
17810 matrix = new Matrix();
17811 // use use a height of 1, and then wack the matrix each time we
17812 // actually use it.
17813 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017814 paint.setShader(shader);
17815 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017816
Mike Cleronf116bf82009-09-27 19:14:12 -070017817 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017818 }
Romain Guy8506ab42009-06-11 17:35:47 -070017819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017820 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017821 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017822 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017823
Romain Guyd679b572012-08-29 21:49:00 -070017824 if (color != 0) {
17825 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17826 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17827 paint.setShader(shader);
17828 // Restore the default transfer mode (src_over)
17829 paint.setXfermode(null);
17830 } else {
17831 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17832 paint.setShader(shader);
17833 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017835 }
17836 }
Joe Malin32736f02011-01-19 16:14:20 -080017837
Mike Cleronf116bf82009-09-27 19:14:12 -070017838 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017839 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017840 if (now >= fadeStartTime) {
17841
17842 // the animation fades the scrollbars out by changing
17843 // the opacity (alpha) from fully opaque to fully
17844 // transparent
17845 int nextFrame = (int) now;
17846 int framesCount = 0;
17847
17848 Interpolator interpolator = scrollBarInterpolator;
17849
17850 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017851 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017852
17853 // End transparent
17854 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017855 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017856
17857 state = FADING;
17858
17859 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017860 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017861 }
17862 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017863 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017864
Svetoslav Ganova0156172011-06-26 17:55:44 -070017865 /**
17866 * Resuable callback for sending
17867 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17868 */
17869 private class SendViewScrolledAccessibilityEvent implements Runnable {
17870 public volatile boolean mIsPending;
17871
17872 public void run() {
17873 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17874 mIsPending = false;
17875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017876 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017877
17878 /**
17879 * <p>
17880 * This class represents a delegate that can be registered in a {@link View}
17881 * to enhance accessibility support via composition rather via inheritance.
17882 * It is specifically targeted to widget developers that extend basic View
17883 * classes i.e. classes in package android.view, that would like their
17884 * applications to be backwards compatible.
17885 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017886 * <div class="special reference">
17887 * <h3>Developer Guides</h3>
17888 * <p>For more information about making applications accessible, read the
17889 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17890 * developer guide.</p>
17891 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017892 * <p>
17893 * A scenario in which a developer would like to use an accessibility delegate
17894 * is overriding a method introduced in a later API version then the minimal API
17895 * version supported by the application. For example, the method
17896 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17897 * in API version 4 when the accessibility APIs were first introduced. If a
17898 * developer would like his application to run on API version 4 devices (assuming
17899 * all other APIs used by the application are version 4 or lower) and take advantage
17900 * of this method, instead of overriding the method which would break the application's
17901 * backwards compatibility, he can override the corresponding method in this
17902 * delegate and register the delegate in the target View if the API version of
17903 * the system is high enough i.e. the API version is same or higher to the API
17904 * version that introduced
17905 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17906 * </p>
17907 * <p>
17908 * Here is an example implementation:
17909 * </p>
17910 * <code><pre><p>
17911 * if (Build.VERSION.SDK_INT >= 14) {
17912 * // If the API version is equal of higher than the version in
17913 * // which onInitializeAccessibilityNodeInfo was introduced we
17914 * // register a delegate with a customized implementation.
17915 * View view = findViewById(R.id.view_id);
17916 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17917 * public void onInitializeAccessibilityNodeInfo(View host,
17918 * AccessibilityNodeInfo info) {
17919 * // Let the default implementation populate the info.
17920 * super.onInitializeAccessibilityNodeInfo(host, info);
17921 * // Set some other information.
17922 * info.setEnabled(host.isEnabled());
17923 * }
17924 * });
17925 * }
17926 * </code></pre></p>
17927 * <p>
17928 * This delegate contains methods that correspond to the accessibility methods
17929 * in View. If a delegate has been specified the implementation in View hands
17930 * off handling to the corresponding method in this delegate. The default
17931 * implementation the delegate methods behaves exactly as the corresponding
17932 * method in View for the case of no accessibility delegate been set. Hence,
17933 * to customize the behavior of a View method, clients can override only the
17934 * corresponding delegate method without altering the behavior of the rest
17935 * accessibility related methods of the host view.
17936 * </p>
17937 */
17938 public static class AccessibilityDelegate {
17939
17940 /**
17941 * Sends an accessibility event of the given type. If accessibility is not
17942 * enabled this method has no effect.
17943 * <p>
17944 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17945 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17946 * been set.
17947 * </p>
17948 *
17949 * @param host The View hosting the delegate.
17950 * @param eventType The type of the event to send.
17951 *
17952 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17953 */
17954 public void sendAccessibilityEvent(View host, int eventType) {
17955 host.sendAccessibilityEventInternal(eventType);
17956 }
17957
17958 /**
alanv8eeefef2012-05-07 16:57:53 -070017959 * Performs the specified accessibility action on the view. For
17960 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17961 * <p>
17962 * The default implementation behaves as
17963 * {@link View#performAccessibilityAction(int, Bundle)
17964 * View#performAccessibilityAction(int, Bundle)} for the case of
17965 * no accessibility delegate been set.
17966 * </p>
17967 *
17968 * @param action The action to perform.
17969 * @return Whether the action was performed.
17970 *
17971 * @see View#performAccessibilityAction(int, Bundle)
17972 * View#performAccessibilityAction(int, Bundle)
17973 */
17974 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17975 return host.performAccessibilityActionInternal(action, args);
17976 }
17977
17978 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017979 * Sends an accessibility event. This method behaves exactly as
17980 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17981 * empty {@link AccessibilityEvent} and does not perform a check whether
17982 * accessibility is enabled.
17983 * <p>
17984 * The default implementation behaves as
17985 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17986 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17987 * the case of no accessibility delegate been set.
17988 * </p>
17989 *
17990 * @param host The View hosting the delegate.
17991 * @param event The event to send.
17992 *
17993 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17994 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17995 */
17996 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17997 host.sendAccessibilityEventUncheckedInternal(event);
17998 }
17999
18000 /**
18001 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18002 * to its children for adding their text content to the event.
18003 * <p>
18004 * The default implementation behaves as
18005 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18006 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18007 * the case of no accessibility delegate been set.
18008 * </p>
18009 *
18010 * @param host The View hosting the delegate.
18011 * @param event The event.
18012 * @return True if the event population was completed.
18013 *
18014 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18015 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18016 */
18017 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18018 return host.dispatchPopulateAccessibilityEventInternal(event);
18019 }
18020
18021 /**
18022 * Gives a chance to the host View to populate the accessibility event with its
18023 * text content.
18024 * <p>
18025 * The default implementation behaves as
18026 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18027 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18028 * the case of no accessibility delegate been set.
18029 * </p>
18030 *
18031 * @param host The View hosting the delegate.
18032 * @param event The accessibility event which to populate.
18033 *
18034 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18035 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18036 */
18037 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18038 host.onPopulateAccessibilityEventInternal(event);
18039 }
18040
18041 /**
18042 * Initializes an {@link AccessibilityEvent} with information about the
18043 * the host View which is the event source.
18044 * <p>
18045 * The default implementation behaves as
18046 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18047 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18048 * the case of no accessibility delegate been set.
18049 * </p>
18050 *
18051 * @param host The View hosting the delegate.
18052 * @param event The event to initialize.
18053 *
18054 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18055 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18056 */
18057 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18058 host.onInitializeAccessibilityEventInternal(event);
18059 }
18060
18061 /**
18062 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18063 * <p>
18064 * The default implementation behaves as
18065 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18066 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18067 * the case of no accessibility delegate been set.
18068 * </p>
18069 *
18070 * @param host The View hosting the delegate.
18071 * @param info The instance to initialize.
18072 *
18073 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18074 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18075 */
18076 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18077 host.onInitializeAccessibilityNodeInfoInternal(info);
18078 }
18079
18080 /**
18081 * Called when a child of the host View has requested sending an
18082 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18083 * to augment the event.
18084 * <p>
18085 * The default implementation behaves as
18086 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18087 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18088 * the case of no accessibility delegate been set.
18089 * </p>
18090 *
18091 * @param host The View hosting the delegate.
18092 * @param child The child which requests sending the event.
18093 * @param event The event to be sent.
18094 * @return True if the event should be sent
18095 *
18096 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18097 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18098 */
18099 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18100 AccessibilityEvent event) {
18101 return host.onRequestSendAccessibilityEventInternal(child, event);
18102 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018103
18104 /**
18105 * Gets the provider for managing a virtual view hierarchy rooted at this View
18106 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18107 * that explore the window content.
18108 * <p>
18109 * The default implementation behaves as
18110 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18111 * the case of no accessibility delegate been set.
18112 * </p>
18113 *
18114 * @return The provider.
18115 *
18116 * @see AccessibilityNodeProvider
18117 */
18118 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18119 return null;
18120 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018121 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018122
18123 private class MatchIdPredicate implements Predicate<View> {
18124 public int mId;
18125
18126 @Override
18127 public boolean apply(View view) {
18128 return (view.mID == mId);
18129 }
18130 }
18131
18132 private class MatchLabelForPredicate implements Predicate<View> {
18133 private int mLabeledId;
18134
18135 @Override
18136 public boolean apply(View view) {
18137 return (view.mLabelForId == mLabeledId);
18138 }
18139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018140}