blob: b1f5e9e58d362e2b75df02c54fae48a0dc8647d8 [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 Brown98365d72012-08-19 20:30:52 -070042import android.hardware.display.DisplayManager;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070052import android.util.FloatProperty;
53import android.util.LocaleUtil;
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 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001506 * The next available accessiiblity id.
1507 */
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 /**
2056 * The default whether the view is important for accessiblity.
2057 */
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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002763 *
2764 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002766 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002767 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768
2769 /**
2770 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002771 *
2772 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002774 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002775 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002777 /**
Adam Powell20232d02010-12-08 21:08:53 -08002778 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002779 *
2780 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002781 */
2782 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002783 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002784
2785 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002786 * Cache the paddingStart set by the user to append to the scrollbar's size.
2787 *
2788 */
2789 @ViewDebug.ExportedProperty(category = "padding")
2790 int mUserPaddingStart;
2791
2792 /**
2793 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2794 *
2795 */
2796 @ViewDebug.ExportedProperty(category = "padding")
2797 int mUserPaddingEnd;
2798
2799 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002800 * Whether a left padding has been defined during layout inflation.
2801 *
2802 * @hide
2803 */
2804 boolean mUserPaddingLeftDefined = false;
2805
2806 /**
2807 * Whether a right padding has been defined during layout inflation.
2808 *
2809 * @hide
2810 */
2811 boolean mUserPaddingRightDefined = false;
2812
2813 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002814 * Default undefined padding
2815 */
2816 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2817
2818 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002819 * @hide
2820 */
2821 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2822 /**
2823 * @hide
2824 */
2825 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826
Philip Milne6c8ea062012-04-03 17:38:43 -07002827 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828
2829 private int mBackgroundResource;
2830 private boolean mBackgroundSizeChanged;
2831
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002832 static class ListenerInfo {
2833 /**
2834 * Listener used to dispatch focus change events.
2835 * This field should be made private, so it is hidden from the SDK.
2836 * {@hide}
2837 */
2838 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002840 /**
2841 * Listeners for layout change events.
2842 */
2843 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002844
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002845 /**
2846 * Listeners for attach events.
2847 */
2848 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002849
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002850 /**
2851 * Listener used to dispatch click events.
2852 * This field should be made private, so it is hidden from the SDK.
2853 * {@hide}
2854 */
2855 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002857 /**
2858 * Listener used to dispatch long click events.
2859 * This field should be made private, so it is hidden from the SDK.
2860 * {@hide}
2861 */
2862 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002864 /**
2865 * Listener used to build the context menu.
2866 * This field should be made private, so it is hidden from the SDK.
2867 * {@hide}
2868 */
2869 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002871 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002873 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002875 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002876
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002877 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002878
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002879 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002880
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002881 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2882 }
2883
2884 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 /**
2887 * The application environment this view lives in.
2888 * This field should be made private, so it is hidden from the SDK.
2889 * {@hide}
2890 */
2891 protected Context mContext;
2892
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002893 private final Resources mResources;
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 private ScrollabilityCache mScrollCache;
2896
2897 private int[] mDrawableState = null;
2898
Romain Guy0211a0a2011-02-14 16:34:59 -08002899 /**
2900 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002901 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002902 * @hide
2903 */
2904 public boolean mCachingFailed;
2905
Romain Guy02890fd2010-08-06 17:58:44 -07002906 private Bitmap mDrawingCache;
2907 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002908 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002909 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910
2911 /**
2912 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2913 * the user may specify which view to go to next.
2914 */
2915 private int mNextFocusLeftId = View.NO_ID;
2916
2917 /**
2918 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2919 * the user may specify which view to go to next.
2920 */
2921 private int mNextFocusRightId = View.NO_ID;
2922
2923 /**
2924 * When this view has focus and the next focus is {@link #FOCUS_UP},
2925 * the user may specify which view to go to next.
2926 */
2927 private int mNextFocusUpId = View.NO_ID;
2928
2929 /**
2930 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2931 * the user may specify which view to go to next.
2932 */
2933 private int mNextFocusDownId = View.NO_ID;
2934
Jeff Brown4e6319b2010-12-13 10:36:51 -08002935 /**
2936 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2937 * the user may specify which view to go to next.
2938 */
2939 int mNextFocusForwardId = View.NO_ID;
2940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002942 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002943 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002944 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 private UnsetPressedState mUnsetPressedState;
2947
2948 /**
2949 * Whether the long press's action has been invoked. The tap's action is invoked on the
2950 * up event while a long press is invoked as soon as the long press duration is reached, so
2951 * a long press could be performed before the tap is checked, in which case the tap's action
2952 * should not be invoked.
2953 */
2954 private boolean mHasPerformedLongPress;
2955
2956 /**
2957 * The minimum height of the view. We'll try our best to have the height
2958 * of this view to at least this amount.
2959 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002960 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 private int mMinHeight;
2962
2963 /**
2964 * The minimum width of the view. We'll try our best to have the width
2965 * of this view to at least this amount.
2966 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002967 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 private int mMinWidth;
2969
2970 /**
2971 * The delegate to handle touch events that are physically in this view
2972 * but should be handled by another view.
2973 */
2974 private TouchDelegate mTouchDelegate = null;
2975
2976 /**
2977 * Solid color to use as a background when creating the drawing cache. Enables
2978 * the cache to use 16 bit bitmaps instead of 32 bit.
2979 */
2980 private int mDrawingCacheBackgroundColor = 0;
2981
2982 /**
2983 * Special tree observer used when mAttachInfo is null.
2984 */
2985 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002986
Adam Powelle14579b2009-12-16 18:39:52 -08002987 /**
2988 * Cache the touch slop from the context that created the view.
2989 */
2990 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002993 * Object that handles automatic animation of view properties.
2994 */
2995 private ViewPropertyAnimator mAnimator = null;
2996
2997 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002998 * Flag indicating that a drag can cross window boundaries. When
2999 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3000 * with this flag set, all visible applications will be able to participate
3001 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003002 *
3003 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003004 */
3005 public static final int DRAG_FLAG_GLOBAL = 1;
3006
3007 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003008 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3009 */
3010 private float mVerticalScrollFactor;
3011
3012 /**
Adam Powell20232d02010-12-08 21:08:53 -08003013 * Position of the vertical scroll bar.
3014 */
3015 private int mVerticalScrollbarPosition;
3016
3017 /**
3018 * Position the scroll bar at the default position as determined by the system.
3019 */
3020 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3021
3022 /**
3023 * Position the scroll bar along the left edge.
3024 */
3025 public static final int SCROLLBAR_POSITION_LEFT = 1;
3026
3027 /**
3028 * Position the scroll bar along the right edge.
3029 */
3030 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3031
3032 /**
Romain Guy171c5922011-01-06 10:04:23 -08003033 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003034 *
3035 * @see #getLayerType()
3036 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003037 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003038 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003039 */
3040 public static final int LAYER_TYPE_NONE = 0;
3041
3042 /**
3043 * <p>Indicates that the view has a software layer. A software layer is backed
3044 * by a bitmap and causes the view to be rendered using Android's software
3045 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003046 *
Romain Guy171c5922011-01-06 10:04:23 -08003047 * <p>Software layers have various usages:</p>
3048 * <p>When the application is not using hardware acceleration, a software layer
3049 * is useful to apply a specific color filter and/or blending mode and/or
3050 * translucency to a view and all its children.</p>
3051 * <p>When the application is using hardware acceleration, a software layer
3052 * is useful to render drawing primitives not supported by the hardware
3053 * accelerated pipeline. It can also be used to cache a complex view tree
3054 * into a texture and reduce the complexity of drawing operations. For instance,
3055 * when animating a complex view tree with a translation, a software layer can
3056 * be used to render the view tree only once.</p>
3057 * <p>Software layers should be avoided when the affected view tree updates
3058 * often. Every update will require to re-render the software layer, which can
3059 * potentially be slow (particularly when hardware acceleration is turned on
3060 * since the layer will have to be uploaded into a hardware texture after every
3061 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003062 *
3063 * @see #getLayerType()
3064 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003065 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003066 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003067 */
3068 public static final int LAYER_TYPE_SOFTWARE = 1;
3069
3070 /**
3071 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3072 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3073 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3074 * rendering pipeline, but only if hardware acceleration is turned on for the
3075 * view hierarchy. When hardware acceleration is turned off, hardware layers
3076 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003077 *
Romain Guy171c5922011-01-06 10:04:23 -08003078 * <p>A hardware layer is useful to apply a specific color filter and/or
3079 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003080 * <p>A hardware layer can be used to cache a complex view tree into a
3081 * texture and reduce the complexity of drawing operations. For instance,
3082 * when animating a complex view tree with a translation, a hardware layer can
3083 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003084 * <p>A hardware layer can also be used to increase the rendering quality when
3085 * rotation transformations are applied on a view. It can also be used to
3086 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003087 *
3088 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003089 * @see #setLayerType(int, android.graphics.Paint)
3090 * @see #LAYER_TYPE_NONE
3091 * @see #LAYER_TYPE_SOFTWARE
3092 */
3093 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003094
Romain Guy3aaff3a2011-01-12 14:18:47 -08003095 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3096 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3097 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3098 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3099 })
Romain Guy171c5922011-01-06 10:04:23 -08003100 int mLayerType = LAYER_TYPE_NONE;
3101 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003102 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003103
3104 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003105 * Set to true when the view is sending hover accessibility events because it
3106 * is the innermost hovered view.
3107 */
3108 private boolean mSendingHoverAccessibilityEvents;
3109
Dianne Hackborn4702a852012-08-17 15:18:29 -07003110 /**
3111 * Delegate for injecting accessiblity functionality.
3112 */
3113 AccessibilityDelegate mAccessibilityDelegate;
3114
3115 /**
3116 * Consistency verifier for debugging purposes.
3117 * @hide
3118 */
3119 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3120 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3121 new InputEventConsistencyVerifier(this, 0) : null;
3122
Adam Powella9108a22012-07-18 11:18:09 -07003123 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3124
Jeff Brown87b7f802011-06-21 18:35:45 -07003125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * Simple constructor to use when creating a view from code.
3127 *
3128 * @param context The Context the view is running in, through which it can
3129 * access the current theme, resources, etc.
3130 */
3131 public View(Context context) {
3132 mContext = context;
3133 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003134 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003135 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003136 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3137 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3138 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3139 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003140 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003141 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003142 mUserPaddingStart = UNDEFINED_PADDING;
3143 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 }
3145
3146 /**
3147 * Constructor that is called when inflating a view from XML. This is called
3148 * when a view is being constructed from an XML file, supplying attributes
3149 * that were specified in the XML file. This version uses a default style of
3150 * 0, so the only attribute values applied are those in the Context's Theme
3151 * and the given AttributeSet.
3152 *
3153 * <p>
3154 * The method onFinishInflate() will be called after all children have been
3155 * added.
3156 *
3157 * @param context The Context the view is running in, through which it can
3158 * access the current theme, resources, etc.
3159 * @param attrs The attributes of the XML tag that is inflating the view.
3160 * @see #View(Context, AttributeSet, int)
3161 */
3162 public View(Context context, AttributeSet attrs) {
3163 this(context, attrs, 0);
3164 }
3165
3166 /**
3167 * Perform inflation from XML and apply a class-specific base style. This
3168 * constructor of View allows subclasses to use their own base style when
3169 * they are inflating. For example, a Button class's constructor would call
3170 * this version of the super class constructor and supply
3171 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3172 * the theme's button style to modify all of the base view attributes (in
3173 * particular its background) as well as the Button class's attributes.
3174 *
3175 * @param context The Context the view is running in, through which it can
3176 * access the current theme, resources, etc.
3177 * @param attrs The attributes of the XML tag that is inflating the view.
3178 * @param defStyle The default style to apply to this view. If 0, no style
3179 * will be applied (beyond what is included in the theme). This may
3180 * either be an attribute resource, whose value will be retrieved
3181 * from the current theme, or an explicit style resource.
3182 * @see #View(Context, AttributeSet)
3183 */
3184 public View(Context context, AttributeSet attrs, int defStyle) {
3185 this(context);
3186
3187 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3188 defStyle, 0);
3189
3190 Drawable background = null;
3191
3192 int leftPadding = -1;
3193 int topPadding = -1;
3194 int rightPadding = -1;
3195 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003196 int startPadding = UNDEFINED_PADDING;
3197 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198
3199 int padding = -1;
3200
3201 int viewFlagValues = 0;
3202 int viewFlagMasks = 0;
3203
3204 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 int x = 0;
3207 int y = 0;
3208
Chet Haase73066682010-11-29 15:55:32 -08003209 float tx = 0;
3210 float ty = 0;
3211 float rotation = 0;
3212 float rotationX = 0;
3213 float rotationY = 0;
3214 float sx = 1f;
3215 float sy = 1f;
3216 boolean transformSet = false;
3217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003219 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003220 boolean initializeScrollbars = false;
3221
3222 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 final int N = a.getIndexCount();
3225 for (int i = 0; i < N; i++) {
3226 int attr = a.getIndex(i);
3227 switch (attr) {
3228 case com.android.internal.R.styleable.View_background:
3229 background = a.getDrawable(attr);
3230 break;
3231 case com.android.internal.R.styleable.View_padding:
3232 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003233 mUserPaddingLeftDefined = true;
3234 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 break;
3236 case com.android.internal.R.styleable.View_paddingLeft:
3237 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003238 mUserPaddingLeftDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 break;
3240 case com.android.internal.R.styleable.View_paddingTop:
3241 topPadding = a.getDimensionPixelSize(attr, -1);
3242 break;
3243 case com.android.internal.R.styleable.View_paddingRight:
3244 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003245 mUserPaddingRightDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 break;
3247 case com.android.internal.R.styleable.View_paddingBottom:
3248 bottomPadding = a.getDimensionPixelSize(attr, -1);
3249 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003250 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003251 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003252 break;
3253 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003254 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003255 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 case com.android.internal.R.styleable.View_scrollX:
3257 x = a.getDimensionPixelOffset(attr, 0);
3258 break;
3259 case com.android.internal.R.styleable.View_scrollY:
3260 y = a.getDimensionPixelOffset(attr, 0);
3261 break;
Chet Haase73066682010-11-29 15:55:32 -08003262 case com.android.internal.R.styleable.View_alpha:
3263 setAlpha(a.getFloat(attr, 1f));
3264 break;
3265 case com.android.internal.R.styleable.View_transformPivotX:
3266 setPivotX(a.getDimensionPixelOffset(attr, 0));
3267 break;
3268 case com.android.internal.R.styleable.View_transformPivotY:
3269 setPivotY(a.getDimensionPixelOffset(attr, 0));
3270 break;
3271 case com.android.internal.R.styleable.View_translationX:
3272 tx = a.getDimensionPixelOffset(attr, 0);
3273 transformSet = true;
3274 break;
3275 case com.android.internal.R.styleable.View_translationY:
3276 ty = a.getDimensionPixelOffset(attr, 0);
3277 transformSet = true;
3278 break;
3279 case com.android.internal.R.styleable.View_rotation:
3280 rotation = a.getFloat(attr, 0);
3281 transformSet = true;
3282 break;
3283 case com.android.internal.R.styleable.View_rotationX:
3284 rotationX = a.getFloat(attr, 0);
3285 transformSet = true;
3286 break;
3287 case com.android.internal.R.styleable.View_rotationY:
3288 rotationY = a.getFloat(attr, 0);
3289 transformSet = true;
3290 break;
3291 case com.android.internal.R.styleable.View_scaleX:
3292 sx = a.getFloat(attr, 1f);
3293 transformSet = true;
3294 break;
3295 case com.android.internal.R.styleable.View_scaleY:
3296 sy = a.getFloat(attr, 1f);
3297 transformSet = true;
3298 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 case com.android.internal.R.styleable.View_id:
3300 mID = a.getResourceId(attr, NO_ID);
3301 break;
3302 case com.android.internal.R.styleable.View_tag:
3303 mTag = a.getText(attr);
3304 break;
3305 case com.android.internal.R.styleable.View_fitsSystemWindows:
3306 if (a.getBoolean(attr, false)) {
3307 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3308 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3309 }
3310 break;
3311 case com.android.internal.R.styleable.View_focusable:
3312 if (a.getBoolean(attr, false)) {
3313 viewFlagValues |= FOCUSABLE;
3314 viewFlagMasks |= FOCUSABLE_MASK;
3315 }
3316 break;
3317 case com.android.internal.R.styleable.View_focusableInTouchMode:
3318 if (a.getBoolean(attr, false)) {
3319 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3320 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3321 }
3322 break;
3323 case com.android.internal.R.styleable.View_clickable:
3324 if (a.getBoolean(attr, false)) {
3325 viewFlagValues |= CLICKABLE;
3326 viewFlagMasks |= CLICKABLE;
3327 }
3328 break;
3329 case com.android.internal.R.styleable.View_longClickable:
3330 if (a.getBoolean(attr, false)) {
3331 viewFlagValues |= LONG_CLICKABLE;
3332 viewFlagMasks |= LONG_CLICKABLE;
3333 }
3334 break;
3335 case com.android.internal.R.styleable.View_saveEnabled:
3336 if (!a.getBoolean(attr, true)) {
3337 viewFlagValues |= SAVE_DISABLED;
3338 viewFlagMasks |= SAVE_DISABLED_MASK;
3339 }
3340 break;
3341 case com.android.internal.R.styleable.View_duplicateParentState:
3342 if (a.getBoolean(attr, false)) {
3343 viewFlagValues |= DUPLICATE_PARENT_STATE;
3344 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3345 }
3346 break;
3347 case com.android.internal.R.styleable.View_visibility:
3348 final int visibility = a.getInt(attr, 0);
3349 if (visibility != 0) {
3350 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3351 viewFlagMasks |= VISIBILITY_MASK;
3352 }
3353 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003354 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003355 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003356 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003357 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003358 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003359 final int value = (layoutDirection != -1) ?
3360 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003361 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003362 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 case com.android.internal.R.styleable.View_drawingCacheQuality:
3364 final int cacheQuality = a.getInt(attr, 0);
3365 if (cacheQuality != 0) {
3366 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3367 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3368 }
3369 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003370 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003371 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003372 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3374 if (!a.getBoolean(attr, true)) {
3375 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3376 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3377 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003378 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3380 if (!a.getBoolean(attr, true)) {
3381 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3382 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3383 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003384 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 case R.styleable.View_scrollbars:
3386 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3387 if (scrollbars != SCROLLBARS_NONE) {
3388 viewFlagValues |= scrollbars;
3389 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003390 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
3392 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003393 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003395 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003396 // Ignore the attribute starting with ICS
3397 break;
3398 }
3399 // With builds < ICS, fall through and apply fading edges
3400 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3402 if (fadingEdge != FADING_EDGE_NONE) {
3403 viewFlagValues |= fadingEdge;
3404 viewFlagMasks |= FADING_EDGE_MASK;
3405 initializeFadingEdge(a);
3406 }
3407 break;
3408 case R.styleable.View_scrollbarStyle:
3409 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3410 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3411 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3412 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3413 }
3414 break;
3415 case R.styleable.View_isScrollContainer:
3416 setScrollContainer = true;
3417 if (a.getBoolean(attr, false)) {
3418 setScrollContainer(true);
3419 }
3420 break;
3421 case com.android.internal.R.styleable.View_keepScreenOn:
3422 if (a.getBoolean(attr, false)) {
3423 viewFlagValues |= KEEP_SCREEN_ON;
3424 viewFlagMasks |= KEEP_SCREEN_ON;
3425 }
3426 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003427 case R.styleable.View_filterTouchesWhenObscured:
3428 if (a.getBoolean(attr, false)) {
3429 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3430 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3431 }
3432 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 case R.styleable.View_nextFocusLeft:
3434 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3435 break;
3436 case R.styleable.View_nextFocusRight:
3437 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3438 break;
3439 case R.styleable.View_nextFocusUp:
3440 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3441 break;
3442 case R.styleable.View_nextFocusDown:
3443 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3444 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003445 case R.styleable.View_nextFocusForward:
3446 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3447 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 case R.styleable.View_minWidth:
3449 mMinWidth = a.getDimensionPixelSize(attr, 0);
3450 break;
3451 case R.styleable.View_minHeight:
3452 mMinHeight = a.getDimensionPixelSize(attr, 0);
3453 break;
Romain Guy9a817362009-05-01 10:57:14 -07003454 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003455 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003456 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003457 + "be used within a restricted context");
3458 }
3459
Romain Guy9a817362009-05-01 10:57:14 -07003460 final String handlerName = a.getString(attr);
3461 if (handlerName != null) {
3462 setOnClickListener(new OnClickListener() {
3463 private Method mHandler;
3464
3465 public void onClick(View v) {
3466 if (mHandler == null) {
3467 try {
3468 mHandler = getContext().getClass().getMethod(handlerName,
3469 View.class);
3470 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003471 int id = getId();
3472 String idText = id == NO_ID ? "" : " with id '"
3473 + getContext().getResources().getResourceEntryName(
3474 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003475 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003476 handlerName + "(View) in the activity "
3477 + getContext().getClass() + " for onClick handler"
3478 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003479 }
3480 }
3481
3482 try {
3483 mHandler.invoke(getContext(), View.this);
3484 } catch (IllegalAccessException e) {
3485 throw new IllegalStateException("Could not execute non "
3486 + "public method of the activity", e);
3487 } catch (InvocationTargetException e) {
3488 throw new IllegalStateException("Could not execute "
3489 + "method of the activity", e);
3490 }
3491 }
3492 });
3493 }
3494 break;
Adam Powell637d3372010-08-25 14:37:03 -07003495 case R.styleable.View_overScrollMode:
3496 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3497 break;
Adam Powell20232d02010-12-08 21:08:53 -08003498 case R.styleable.View_verticalScrollbarPosition:
3499 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3500 break;
Romain Guy171c5922011-01-06 10:04:23 -08003501 case R.styleable.View_layerType:
3502 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3503 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003504 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003505 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003506 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003507 // Set the text direction flags depending on the value of the attribute
3508 final int textDirection = a.getInt(attr, -1);
3509 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003510 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003511 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003512 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003513 case R.styleable.View_textAlignment:
3514 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003515 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003516 // Set the text alignment flag depending on the value of the attribute
3517 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003518 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003519 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003520 case R.styleable.View_importantForAccessibility:
3521 setImportantForAccessibility(a.getInt(attr,
3522 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003523 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 }
3525 }
3526
Adam Powell637d3372010-08-25 14:37:03 -07003527 setOverScrollMode(overScrollMode);
3528
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003529 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3530 // the resolved layout direction). Those cached values will be used later during padding
3531 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003532 mUserPaddingStart = startPadding;
3533 mUserPaddingEnd = endPadding;
3534
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003535 if (background != null) {
3536 setBackground(background);
3537 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 if (padding >= 0) {
3540 leftPadding = padding;
3541 topPadding = padding;
3542 rightPadding = padding;
3543 bottomPadding = padding;
3544 }
3545
3546 // If the user specified the padding (either with android:padding or
3547 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3548 // use the default padding or the padding from the background drawable
3549 // (stored at this point in mPadding*)
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003550 internalSetPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 topPadding >= 0 ? topPadding : mPaddingTop,
3552 rightPadding >= 0 ? rightPadding : mPaddingRight,
3553 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3554
3555 if (viewFlagMasks != 0) {
3556 setFlags(viewFlagValues, viewFlagMasks);
3557 }
3558
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003559 if (initializeScrollbars) {
3560 initializeScrollbars(a);
3561 }
3562
3563 a.recycle();
3564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 // Needs to be called after mViewFlags is set
3566 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3567 recomputePadding();
3568 }
3569
3570 if (x != 0 || y != 0) {
3571 scrollTo(x, y);
3572 }
3573
Chet Haase73066682010-11-29 15:55:32 -08003574 if (transformSet) {
3575 setTranslationX(tx);
3576 setTranslationY(ty);
3577 setRotation(rotation);
3578 setRotationX(rotationX);
3579 setRotationY(rotationY);
3580 setScaleX(sx);
3581 setScaleY(sy);
3582 }
3583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3585 setScrollContainer(true);
3586 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003587
3588 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 }
3590
3591 /**
3592 * Non-public constructor for use in testing
3593 */
3594 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003595 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 }
3597
Dianne Hackborn4702a852012-08-17 15:18:29 -07003598 public String toString() {
3599 StringBuilder out = new StringBuilder(128);
3600 out.append(getClass().getName());
3601 out.append('{');
3602 out.append(Integer.toHexString(System.identityHashCode(this)));
3603 out.append(' ');
3604 switch (mViewFlags&VISIBILITY_MASK) {
3605 case VISIBLE: out.append('V'); break;
3606 case INVISIBLE: out.append('I'); break;
3607 case GONE: out.append('G'); break;
3608 default: out.append('.'); break;
3609 }
3610 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3611 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3612 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3613 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3614 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3615 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3616 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3617 out.append(' ');
3618 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3619 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3620 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3621 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3622 out.append('p');
3623 } else {
3624 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3625 }
3626 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3627 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3628 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3629 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3630 out.append(' ');
3631 out.append(mLeft);
3632 out.append(',');
3633 out.append(mTop);
3634 out.append('-');
3635 out.append(mRight);
3636 out.append(',');
3637 out.append(mBottom);
3638 final int id = getId();
3639 if (id != NO_ID) {
3640 out.append(" #");
3641 out.append(Integer.toHexString(id));
3642 final Resources r = mResources;
3643 if (id != 0 && r != null) {
3644 try {
3645 String pkgname;
3646 switch (id&0xff000000) {
3647 case 0x7f000000:
3648 pkgname="app";
3649 break;
3650 case 0x01000000:
3651 pkgname="android";
3652 break;
3653 default:
3654 pkgname = r.getResourcePackageName(id);
3655 break;
3656 }
3657 String typename = r.getResourceTypeName(id);
3658 String entryname = r.getResourceEntryName(id);
3659 out.append(" ");
3660 out.append(pkgname);
3661 out.append(":");
3662 out.append(typename);
3663 out.append("/");
3664 out.append(entryname);
3665 } catch (Resources.NotFoundException e) {
3666 }
3667 }
3668 }
3669 out.append("}");
3670 return out.toString();
3671 }
3672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 /**
3674 * <p>
3675 * Initializes the fading edges from a given set of styled attributes. This
3676 * method should be called by subclasses that need fading edges and when an
3677 * instance of these subclasses is created programmatically rather than
3678 * being inflated from XML. This method is automatically called when the XML
3679 * is inflated.
3680 * </p>
3681 *
3682 * @param a the styled attributes set to initialize the fading edges from
3683 */
3684 protected void initializeFadingEdge(TypedArray a) {
3685 initScrollCache();
3686
3687 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3688 R.styleable.View_fadingEdgeLength,
3689 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3690 }
3691
3692 /**
3693 * Returns the size of the vertical faded edges used to indicate that more
3694 * content in this view is visible.
3695 *
3696 * @return The size in pixels of the vertical faded edge or 0 if vertical
3697 * faded edges are not enabled for this view.
3698 * @attr ref android.R.styleable#View_fadingEdgeLength
3699 */
3700 public int getVerticalFadingEdgeLength() {
3701 if (isVerticalFadingEdgeEnabled()) {
3702 ScrollabilityCache cache = mScrollCache;
3703 if (cache != null) {
3704 return cache.fadingEdgeLength;
3705 }
3706 }
3707 return 0;
3708 }
3709
3710 /**
3711 * Set the size of the faded edge used to indicate that more content in this
3712 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003713 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3714 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3715 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 *
3717 * @param length The size in pixels of the faded edge used to indicate that more
3718 * content in this view is visible.
3719 */
3720 public void setFadingEdgeLength(int length) {
3721 initScrollCache();
3722 mScrollCache.fadingEdgeLength = length;
3723 }
3724
3725 /**
3726 * Returns the size of the horizontal faded edges used to indicate that more
3727 * content in this view is visible.
3728 *
3729 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3730 * faded edges are not enabled for this view.
3731 * @attr ref android.R.styleable#View_fadingEdgeLength
3732 */
3733 public int getHorizontalFadingEdgeLength() {
3734 if (isHorizontalFadingEdgeEnabled()) {
3735 ScrollabilityCache cache = mScrollCache;
3736 if (cache != null) {
3737 return cache.fadingEdgeLength;
3738 }
3739 }
3740 return 0;
3741 }
3742
3743 /**
3744 * Returns the width of the vertical scrollbar.
3745 *
3746 * @return The width in pixels of the vertical scrollbar or 0 if there
3747 * is no vertical scrollbar.
3748 */
3749 public int getVerticalScrollbarWidth() {
3750 ScrollabilityCache cache = mScrollCache;
3751 if (cache != null) {
3752 ScrollBarDrawable scrollBar = cache.scrollBar;
3753 if (scrollBar != null) {
3754 int size = scrollBar.getSize(true);
3755 if (size <= 0) {
3756 size = cache.scrollBarSize;
3757 }
3758 return size;
3759 }
3760 return 0;
3761 }
3762 return 0;
3763 }
3764
3765 /**
3766 * Returns the height of the horizontal scrollbar.
3767 *
3768 * @return The height in pixels of the horizontal scrollbar or 0 if
3769 * there is no horizontal scrollbar.
3770 */
3771 protected int getHorizontalScrollbarHeight() {
3772 ScrollabilityCache cache = mScrollCache;
3773 if (cache != null) {
3774 ScrollBarDrawable scrollBar = cache.scrollBar;
3775 if (scrollBar != null) {
3776 int size = scrollBar.getSize(false);
3777 if (size <= 0) {
3778 size = cache.scrollBarSize;
3779 }
3780 return size;
3781 }
3782 return 0;
3783 }
3784 return 0;
3785 }
3786
3787 /**
3788 * <p>
3789 * Initializes the scrollbars from a given set of styled attributes. This
3790 * method should be called by subclasses that need scrollbars and when an
3791 * instance of these subclasses is created programmatically rather than
3792 * being inflated from XML. This method is automatically called when the XML
3793 * is inflated.
3794 * </p>
3795 *
3796 * @param a the styled attributes set to initialize the scrollbars from
3797 */
3798 protected void initializeScrollbars(TypedArray a) {
3799 initScrollCache();
3800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003802
Mike Cleronf116bf82009-09-27 19:14:12 -07003803 if (scrollabilityCache.scrollBar == null) {
3804 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3805 }
Joe Malin32736f02011-01-19 16:14:20 -08003806
Romain Guy8bda2482010-03-02 11:42:11 -08003807 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808
Mike Cleronf116bf82009-09-27 19:14:12 -07003809 if (!fadeScrollbars) {
3810 scrollabilityCache.state = ScrollabilityCache.ON;
3811 }
3812 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003813
3814
Mike Cleronf116bf82009-09-27 19:14:12 -07003815 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3816 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3817 .getScrollBarFadeDuration());
3818 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3819 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3820 ViewConfiguration.getScrollDefaultDelay());
3821
Joe Malin32736f02011-01-19 16:14:20 -08003822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3824 com.android.internal.R.styleable.View_scrollbarSize,
3825 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3826
3827 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3828 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3829
3830 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3831 if (thumb != null) {
3832 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3833 }
3834
3835 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3836 false);
3837 if (alwaysDraw) {
3838 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3839 }
3840
3841 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3842 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3843
3844 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3845 if (thumb != null) {
3846 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3847 }
3848
3849 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3850 false);
3851 if (alwaysDraw) {
3852 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3853 }
3854
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003855 // Apply layout direction to the new Drawables if needed
3856 final int layoutDirection = getResolvedLayoutDirection();
3857 if (track != null) {
3858 track.setLayoutDirection(layoutDirection);
3859 }
3860 if (thumb != null) {
3861 thumb.setLayoutDirection(layoutDirection);
3862 }
3863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003865 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
3867
3868 /**
3869 * <p>
3870 * Initalizes the scrollability cache if necessary.
3871 * </p>
3872 */
3873 private void initScrollCache() {
3874 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003875 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 }
3877 }
3878
Philip Milne6c8ea062012-04-03 17:38:43 -07003879 private ScrollabilityCache getScrollCache() {
3880 initScrollCache();
3881 return mScrollCache;
3882 }
3883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 /**
Adam Powell20232d02010-12-08 21:08:53 -08003885 * Set the position of the vertical scroll bar. Should be one of
3886 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3887 * {@link #SCROLLBAR_POSITION_RIGHT}.
3888 *
3889 * @param position Where the vertical scroll bar should be positioned.
3890 */
3891 public void setVerticalScrollbarPosition(int position) {
3892 if (mVerticalScrollbarPosition != position) {
3893 mVerticalScrollbarPosition = position;
3894 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003895 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003896 }
3897 }
3898
3899 /**
3900 * @return The position where the vertical scroll bar will show, if applicable.
3901 * @see #setVerticalScrollbarPosition(int)
3902 */
3903 public int getVerticalScrollbarPosition() {
3904 return mVerticalScrollbarPosition;
3905 }
3906
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003907 ListenerInfo getListenerInfo() {
3908 if (mListenerInfo != null) {
3909 return mListenerInfo;
3910 }
3911 mListenerInfo = new ListenerInfo();
3912 return mListenerInfo;
3913 }
3914
Adam Powell20232d02010-12-08 21:08:53 -08003915 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 * Register a callback to be invoked when focus of this view changed.
3917 *
3918 * @param l The callback that will run.
3919 */
3920 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003921 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923
3924 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003925 * Add a listener that will be called when the bounds of the view change due to
3926 * layout processing.
3927 *
3928 * @param listener The listener that will be called when layout bounds change.
3929 */
3930 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003931 ListenerInfo li = getListenerInfo();
3932 if (li.mOnLayoutChangeListeners == null) {
3933 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003934 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003935 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3936 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003937 }
Chet Haase21cd1382010-09-01 17:42:29 -07003938 }
3939
3940 /**
3941 * Remove a listener for layout changes.
3942 *
3943 * @param listener The listener for layout bounds change.
3944 */
3945 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003946 ListenerInfo li = mListenerInfo;
3947 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003948 return;
3949 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003950 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003951 }
3952
3953 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003954 * Add a listener for attach state changes.
3955 *
3956 * This listener will be called whenever this view is attached or detached
3957 * from a window. Remove the listener using
3958 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3959 *
3960 * @param listener Listener to attach
3961 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3962 */
3963 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003964 ListenerInfo li = getListenerInfo();
3965 if (li.mOnAttachStateChangeListeners == null) {
3966 li.mOnAttachStateChangeListeners
3967 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003968 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003969 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003970 }
3971
3972 /**
3973 * Remove a listener for attach state changes. The listener will receive no further
3974 * notification of window attach/detach events.
3975 *
3976 * @param listener Listener to remove
3977 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3978 */
3979 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo li = mListenerInfo;
3981 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003982 return;
3983 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003984 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003985 }
3986
3987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 * Returns the focus-change callback registered for this view.
3989 *
3990 * @return The callback, or null if one is not registered.
3991 */
3992 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003993 ListenerInfo li = mListenerInfo;
3994 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996
3997 /**
3998 * Register a callback to be invoked when this view is clicked. If this view is not
3999 * clickable, it becomes clickable.
4000 *
4001 * @param l The callback that will run
4002 *
4003 * @see #setClickable(boolean)
4004 */
4005 public void setOnClickListener(OnClickListener l) {
4006 if (!isClickable()) {
4007 setClickable(true);
4008 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004009 getListenerInfo().mOnClickListener = l;
4010 }
4011
4012 /**
4013 * Return whether this view has an attached OnClickListener. Returns
4014 * true if there is a listener, false if there is none.
4015 */
4016 public boolean hasOnClickListeners() {
4017 ListenerInfo li = mListenerInfo;
4018 return (li != null && li.mOnClickListener != 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 and held. If this view is not
4023 * long clickable, it becomes long clickable.
4024 *
4025 * @param l The callback that will run
4026 *
4027 * @see #setLongClickable(boolean)
4028 */
4029 public void setOnLongClickListener(OnLongClickListener l) {
4030 if (!isLongClickable()) {
4031 setLongClickable(true);
4032 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004033 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 }
4035
4036 /**
4037 * Register a callback to be invoked when the context menu for this view is
4038 * being built. If this view is not long clickable, it becomes long clickable.
4039 *
4040 * @param l The callback that will run
4041 *
4042 */
4043 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4044 if (!isLongClickable()) {
4045 setLongClickable(true);
4046 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004047 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 }
4049
4050 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004051 * Call this view's OnClickListener, if it is defined. Performs all normal
4052 * actions associated with clicking: reporting accessibility event, playing
4053 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 *
4055 * @return True there was an assigned OnClickListener that was called, false
4056 * otherwise is returned.
4057 */
4058 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004059 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4060
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004061 ListenerInfo li = mListenerInfo;
4062 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 return true;
4066 }
4067
4068 return false;
4069 }
4070
4071 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004072 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4073 * this only calls the listener, and does not do any associated clicking
4074 * actions like reporting an accessibility event.
4075 *
4076 * @return True there was an assigned OnClickListener that was called, false
4077 * otherwise is returned.
4078 */
4079 public boolean callOnClick() {
4080 ListenerInfo li = mListenerInfo;
4081 if (li != null && li.mOnClickListener != null) {
4082 li.mOnClickListener.onClick(this);
4083 return true;
4084 }
4085 return false;
4086 }
4087
4088 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004089 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4090 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004092 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 */
4094 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004095 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004098 ListenerInfo li = mListenerInfo;
4099 if (li != null && li.mOnLongClickListener != null) {
4100 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 }
4102 if (!handled) {
4103 handled = showContextMenu();
4104 }
4105 if (handled) {
4106 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4107 }
4108 return handled;
4109 }
4110
4111 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004112 * Performs button-related actions during a touch down event.
4113 *
4114 * @param event The event.
4115 * @return True if the down was consumed.
4116 *
4117 * @hide
4118 */
4119 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4120 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4121 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4122 return true;
4123 }
4124 }
4125 return false;
4126 }
4127
4128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 * Bring up the context menu for this view.
4130 *
4131 * @return Whether a context menu was displayed.
4132 */
4133 public boolean showContextMenu() {
4134 return getParent().showContextMenuForChild(this);
4135 }
4136
4137 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004138 * Bring up the context menu for this view, referring to the item under the specified point.
4139 *
4140 * @param x The referenced x coordinate.
4141 * @param y The referenced y coordinate.
4142 * @param metaState The keyboard modifiers that were pressed.
4143 * @return Whether a context menu was displayed.
4144 *
4145 * @hide
4146 */
4147 public boolean showContextMenu(float x, float y, int metaState) {
4148 return showContextMenu();
4149 }
4150
4151 /**
Adam Powell6e346362010-07-23 10:18:23 -07004152 * Start an action mode.
4153 *
4154 * @param callback Callback that will control the lifecycle of the action mode
4155 * @return The new action mode if it is started, null otherwise
4156 *
4157 * @see ActionMode
4158 */
4159 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004160 ViewParent parent = getParent();
4161 if (parent == null) return null;
4162 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004163 }
4164
4165 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004166 * Register a callback to be invoked when a hardware key is pressed in this view.
4167 * Key presses in software input methods will generally not trigger the methods of
4168 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 * @param l the key listener to attach to this view
4170 */
4171 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004172 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 }
4174
4175 /**
4176 * Register a callback to be invoked when a touch event is sent to this view.
4177 * @param l the touch listener to attach to this view
4178 */
4179 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004180 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182
4183 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004184 * Register a callback to be invoked when a generic motion event is sent to this view.
4185 * @param l the generic motion listener to attach to this view
4186 */
4187 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004188 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004189 }
4190
4191 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004192 * Register a callback to be invoked when a hover event is sent to this view.
4193 * @param l the hover listener to attach to this view
4194 */
4195 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004196 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004197 }
4198
4199 /**
Joe Malin32736f02011-01-19 16:14:20 -08004200 * Register a drag event listener callback object for this View. The parameter is
4201 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4202 * View, the system calls the
4203 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4204 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004205 */
4206 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004207 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004208 }
4209
4210 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004211 * Give this view focus. This will cause
4212 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 *
4214 * Note: this does not check whether this {@link View} should get focus, it just
4215 * gives it focus no matter what. It should only be called internally by framework
4216 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4217 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004218 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4219 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 * focus moved when requestFocus() is called. It may not always
4221 * apply, in which case use the default View.FOCUS_DOWN.
4222 * @param previouslyFocusedRect The rectangle of the view that had focus
4223 * prior in this View's coordinate system.
4224 */
4225 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4226 if (DBG) {
4227 System.out.println(this + " requestFocus()");
4228 }
4229
Dianne Hackborn4702a852012-08-17 15:18:29 -07004230 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4231 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232
4233 if (mParent != null) {
4234 mParent.requestChildFocus(this, this);
4235 }
4236
4237 onFocusChanged(true, direction, previouslyFocusedRect);
4238 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004239
4240 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4241 notifyAccessibilityStateChanged();
4242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 }
4244 }
4245
4246 /**
4247 * Request that a rectangle of this view be visible on the screen,
4248 * scrolling if necessary just enough.
4249 *
4250 * <p>A View should call this if it maintains some notion of which part
4251 * of its content is interesting. For example, a text editing view
4252 * should call this when its cursor moves.
4253 *
4254 * @param rectangle The rectangle.
4255 * @return Whether any parent scrolled.
4256 */
4257 public boolean requestRectangleOnScreen(Rect rectangle) {
4258 return requestRectangleOnScreen(rectangle, false);
4259 }
4260
4261 /**
4262 * Request that a rectangle of this view be visible on the screen,
4263 * scrolling if necessary just enough.
4264 *
4265 * <p>A View should call this if it maintains some notion of which part
4266 * of its content is interesting. For example, a text editing view
4267 * should call this when its cursor moves.
4268 *
4269 * <p>When <code>immediate</code> is set to true, scrolling will not be
4270 * animated.
4271 *
4272 * @param rectangle The rectangle.
4273 * @param immediate True to forbid animated scrolling, false otherwise
4274 * @return Whether any parent scrolled.
4275 */
4276 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4277 View child = this;
4278 ViewParent parent = mParent;
4279 boolean scrolled = false;
4280 while (parent != null) {
4281 scrolled |= parent.requestChildRectangleOnScreen(child,
4282 rectangle, immediate);
4283
4284 // offset rect so next call has the rectangle in the
4285 // coordinate system of its direct child.
4286 rectangle.offset(child.getLeft(), child.getTop());
4287 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4288
4289 if (!(parent instanceof View)) {
4290 break;
4291 }
Romain Guy8506ab42009-06-11 17:35:47 -07004292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 child = (View) parent;
4294 parent = child.getParent();
4295 }
4296 return scrolled;
4297 }
4298
4299 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004300 * Called when this view wants to give up focus. If focus is cleared
4301 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4302 * <p>
4303 * <strong>Note:</strong> When a View clears focus the framework is trying
4304 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004305 * View is the first from the top that can take focus, then all callbacks
4306 * related to clearing focus will be invoked after wich the framework will
4307 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004308 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 */
4310 public void clearFocus() {
4311 if (DBG) {
4312 System.out.println(this + " clearFocus()");
4313 }
4314
Dianne Hackborn4702a852012-08-17 15:18:29 -07004315 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4316 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317
4318 if (mParent != null) {
4319 mParent.clearChildFocus(this);
4320 }
4321
4322 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004325
4326 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004327
4328 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4329 notifyAccessibilityStateChanged();
4330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
4332 }
4333
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004334 void ensureInputFocusOnFirstFocusable() {
4335 View root = getRootView();
4336 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004337 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
4339 }
4340
4341 /**
4342 * Called internally by the view system when a new view is getting focus.
4343 * This is what clears the old focus.
4344 */
4345 void unFocus() {
4346 if (DBG) {
4347 System.out.println(this + " unFocus()");
4348 }
4349
Dianne Hackborn4702a852012-08-17 15:18:29 -07004350 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4351 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352
4353 onFocusChanged(false, 0, null);
4354 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004355
4356 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4357 notifyAccessibilityStateChanged();
4358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 }
4360 }
4361
4362 /**
4363 * Returns true if this view has focus iteself, or is the ancestor of the
4364 * view that has focus.
4365 *
4366 * @return True if this view has or contains focus, false otherwise.
4367 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004368 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004370 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
4372
4373 /**
4374 * Returns true if this view is focusable or if it contains a reachable View
4375 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4376 * is a View whose parents do not block descendants focus.
4377 *
4378 * Only {@link #VISIBLE} views are considered focusable.
4379 *
4380 * @return True if the view is focusable or if the view contains a focusable
4381 * View, false otherwise.
4382 *
4383 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4384 */
4385 public boolean hasFocusable() {
4386 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4387 }
4388
4389 /**
4390 * Called by the view system when the focus state of this view changes.
4391 * When the focus change event is caused by directional navigation, direction
4392 * and previouslyFocusedRect provide insight into where the focus is coming from.
4393 * When overriding, be sure to call up through to the super class so that
4394 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 * @param gainFocus True if the View has focus; false otherwise.
4397 * @param direction The direction focus has moved when requestFocus()
4398 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004399 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4400 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4401 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4403 * system, of the previously focused view. If applicable, this will be
4404 * passed in as finer grained information about where the focus is coming
4405 * from (in addition to direction). Will be <code>null</code> otherwise.
4406 */
4407 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004408 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004409 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4410 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004411 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004412 }
4413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 InputMethodManager imm = InputMethodManager.peekInstance();
4415 if (!gainFocus) {
4416 if (isPressed()) {
4417 setPressed(false);
4418 }
4419 if (imm != null && mAttachInfo != null
4420 && mAttachInfo.mHasWindowFocus) {
4421 imm.focusOut(this);
4422 }
Romain Guya2431d02009-04-30 16:30:00 -07004423 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 } else if (imm != null && mAttachInfo != null
4425 && mAttachInfo.mHasWindowFocus) {
4426 imm.focusIn(this);
4427 }
Romain Guy8506ab42009-06-11 17:35:47 -07004428
Romain Guy0fd89bf2011-01-26 15:41:30 -08004429 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004430 ListenerInfo li = mListenerInfo;
4431 if (li != null && li.mOnFocusChangeListener != null) {
4432 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 }
Joe Malin32736f02011-01-19 16:14:20 -08004434
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004435 if (mAttachInfo != null) {
4436 mAttachInfo.mKeyDispatchState.reset(this);
4437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 }
4439
4440 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004441 * Sends an accessibility event of the given type. If accessiiblity is
4442 * not enabled this method has no effect. The default implementation calls
4443 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4444 * to populate information about the event source (this View), then calls
4445 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4446 * populate the text content of the event source including its descendants,
4447 * and last calls
4448 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4449 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004450 * <p>
4451 * If an {@link AccessibilityDelegate} has been specified via calling
4452 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4453 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4454 * responsible for handling this call.
4455 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004456 *
Scott Mainb303d832011-10-12 16:45:18 -07004457 * @param eventType The type of the event to send, as defined by several types from
4458 * {@link android.view.accessibility.AccessibilityEvent}, such as
4459 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4460 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004461 *
4462 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4463 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4464 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004465 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004466 */
4467 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004468 if (mAccessibilityDelegate != null) {
4469 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4470 } else {
4471 sendAccessibilityEventInternal(eventType);
4472 }
4473 }
4474
4475 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004476 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4477 * {@link AccessibilityEvent} to make an announcement which is related to some
4478 * sort of a context change for which none of the events representing UI transitions
4479 * is a good fit. For example, announcing a new page in a book. If accessibility
4480 * is not enabled this method does nothing.
4481 *
4482 * @param text The announcement text.
4483 */
4484 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004485 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004486 AccessibilityEvent event = AccessibilityEvent.obtain(
4487 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004488 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004489 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004490 event.setContentDescription(null);
4491 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004492 }
4493 }
4494
4495 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004496 * @see #sendAccessibilityEvent(int)
4497 *
4498 * Note: Called from the default {@link AccessibilityDelegate}.
4499 */
4500 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004501 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4502 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4503 }
4504 }
4505
4506 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004507 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4508 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004509 * perform a check whether accessibility is enabled.
4510 * <p>
4511 * If an {@link AccessibilityDelegate} has been specified via calling
4512 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4513 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4514 * is responsible for handling this call.
4515 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004516 *
4517 * @param event The event to send.
4518 *
4519 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004520 */
4521 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004522 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004523 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004524 } else {
4525 sendAccessibilityEventUncheckedInternal(event);
4526 }
4527 }
4528
4529 /**
4530 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4531 *
4532 * Note: Called from the default {@link AccessibilityDelegate}.
4533 */
4534 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004535 if (!isShown()) {
4536 return;
4537 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004538 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004539 // Only a subset of accessibility events populates text content.
4540 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4541 dispatchPopulateAccessibilityEvent(event);
4542 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004543 // In the beginning we called #isShown(), so we know that getParent() is not null.
4544 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004545 }
4546
4547 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004548 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4549 * to its children for adding their text content to the event. Note that the
4550 * event text is populated in a separate dispatch path since we add to the
4551 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004552 * A typical implementation will call
4553 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4554 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4555 * on each child. Override this method if custom population of the event text
4556 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004557 * <p>
4558 * If an {@link AccessibilityDelegate} has been specified via calling
4559 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4560 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4561 * is responsible for handling this call.
4562 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004563 * <p>
4564 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4565 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4566 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004567 *
4568 * @param event The event.
4569 *
4570 * @return True if the event population was completed.
4571 */
4572 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004573 if (mAccessibilityDelegate != null) {
4574 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4575 } else {
4576 return dispatchPopulateAccessibilityEventInternal(event);
4577 }
4578 }
4579
4580 /**
4581 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4582 *
4583 * Note: Called from the default {@link AccessibilityDelegate}.
4584 */
4585 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004586 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004587 return false;
4588 }
4589
4590 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004591 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004592 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004593 * text content. While this method is free to modify event
4594 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004595 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4596 * <p>
4597 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004598 * to the text added by the super implementation:
4599 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004600 * super.onPopulateAccessibilityEvent(event);
4601 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4602 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4603 * mCurrentDate.getTimeInMillis(), flags);
4604 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004605 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004606 * <p>
4607 * If an {@link AccessibilityDelegate} has been specified via calling
4608 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4609 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4610 * is responsible for handling this call.
4611 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004612 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4613 * information to the event, in case the default implementation has basic information to add.
4614 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004615 *
4616 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004617 *
4618 * @see #sendAccessibilityEvent(int)
4619 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004620 */
4621 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004622 if (mAccessibilityDelegate != null) {
4623 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4624 } else {
4625 onPopulateAccessibilityEventInternal(event);
4626 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004627 }
4628
4629 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004630 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4631 *
4632 * Note: Called from the default {@link AccessibilityDelegate}.
4633 */
4634 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4635
4636 }
4637
4638 /**
4639 * Initializes an {@link AccessibilityEvent} with information about
4640 * this View which is the event source. In other words, the source of
4641 * an accessibility event is the view whose state change triggered firing
4642 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004643 * <p>
4644 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004645 * to properties set by the super implementation:
4646 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4647 * super.onInitializeAccessibilityEvent(event);
4648 * event.setPassword(true);
4649 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004650 * <p>
4651 * If an {@link AccessibilityDelegate} has been specified via calling
4652 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4653 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4654 * is responsible for handling this call.
4655 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004656 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4657 * information to the event, in case the default implementation has basic information to add.
4658 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004660 *
4661 * @see #sendAccessibilityEvent(int)
4662 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4663 */
4664 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004665 if (mAccessibilityDelegate != null) {
4666 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4667 } else {
4668 onInitializeAccessibilityEventInternal(event);
4669 }
4670 }
4671
4672 /**
4673 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4674 *
4675 * Note: Called from the default {@link AccessibilityDelegate}.
4676 */
4677 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004678 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004679 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004680 event.setPackageName(getContext().getPackageName());
4681 event.setEnabled(isEnabled());
4682 event.setContentDescription(mContentDescription);
4683
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004684 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004685 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004686 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4687 FOCUSABLES_ALL);
4688 event.setItemCount(focusablesTempList.size());
4689 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4690 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004691 }
4692 }
4693
4694 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004695 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4696 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4697 * This method is responsible for obtaining an accessibility node info from a
4698 * pool of reusable instances and calling
4699 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4700 * initialize the former.
4701 * <p>
4702 * Note: The client is responsible for recycling the obtained instance by calling
4703 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4704 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004705 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004706 * @return A populated {@link AccessibilityNodeInfo}.
4707 *
4708 * @see AccessibilityNodeInfo
4709 */
4710 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004711 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4712 if (provider != null) {
4713 return provider.createAccessibilityNodeInfo(View.NO_ID);
4714 } else {
4715 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4716 onInitializeAccessibilityNodeInfo(info);
4717 return info;
4718 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004719 }
4720
4721 /**
4722 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4723 * The base implementation sets:
4724 * <ul>
4725 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004726 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4727 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004728 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4729 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4730 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4731 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4732 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4733 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4734 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4735 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4736 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4737 * </ul>
4738 * <p>
4739 * Subclasses should override this method, call the super implementation,
4740 * and set additional attributes.
4741 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004742 * <p>
4743 * If an {@link AccessibilityDelegate} has been specified via calling
4744 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4745 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4746 * is responsible for handling this call.
4747 * </p>
4748 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004749 * @param info The instance to initialize.
4750 */
4751 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004752 if (mAccessibilityDelegate != null) {
4753 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4754 } else {
4755 onInitializeAccessibilityNodeInfoInternal(info);
4756 }
4757 }
4758
4759 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004760 * Gets the location of this view in screen coordintates.
4761 *
4762 * @param outRect The output location
4763 */
4764 private void getBoundsOnScreen(Rect outRect) {
4765 if (mAttachInfo == null) {
4766 return;
4767 }
4768
4769 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004770 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004771
4772 if (!hasIdentityMatrix()) {
4773 getMatrix().mapRect(position);
4774 }
4775
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004776 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004777
4778 ViewParent parent = mParent;
4779 while (parent instanceof View) {
4780 View parentView = (View) parent;
4781
4782 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4783
4784 if (!parentView.hasIdentityMatrix()) {
4785 parentView.getMatrix().mapRect(position);
4786 }
4787
4788 position.offset(parentView.mLeft, parentView.mTop);
4789
4790 parent = parentView.mParent;
4791 }
4792
4793 if (parent instanceof ViewRootImpl) {
4794 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4795 position.offset(0, -viewRootImpl.mCurScrollY);
4796 }
4797
4798 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4799
4800 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4801 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4802 }
4803
4804 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004805 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4806 *
4807 * Note: Called from the default {@link AccessibilityDelegate}.
4808 */
4809 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004810 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004811
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004812 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004813 info.setBoundsInParent(bounds);
4814
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004815 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004816 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004817
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004818 ViewParent parent = getParentForAccessibility();
4819 if (parent instanceof View) {
4820 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004821 }
4822
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004823 info.setVisibleToUser(isVisibleToUser());
4824
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004825 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004826 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004827 info.setContentDescription(getContentDescription());
4828
4829 info.setEnabled(isEnabled());
4830 info.setClickable(isClickable());
4831 info.setFocusable(isFocusable());
4832 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004833 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004834 info.setSelected(isSelected());
4835 info.setLongClickable(isLongClickable());
4836
4837 // TODO: These make sense only if we are in an AdapterView but all
4838 // views can be selected. Maybe from accessiiblity perspective
4839 // we should report as selectable view in an AdapterView.
4840 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4841 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4842
4843 if (isFocusable()) {
4844 if (isFocused()) {
4845 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4846 } else {
4847 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4848 }
4849 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004850
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004851 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004852 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004853 } else {
4854 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4855 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004856
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004857 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004858 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4859 }
4860
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004861 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004862 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4863 }
4864
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004865 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004866 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4867 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4868 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004869 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4870 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004871 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004872 }
4873
4874 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004875 * Computes whether this view is visible to the user. Such a view is
4876 * attached, visible, all its predecessors are visible, it is not clipped
4877 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004878 *
4879 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004880 *
4881 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004882 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07004883 protected boolean isVisibleToUser() {
4884 return isVisibleToUser(null);
4885 }
4886
4887 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07004888 * Computes whether the given portion of this view is visible to the user.
4889 * Such a view is attached, visible, all its predecessors are visible,
4890 * has an alpha greater than zero, and the specified portion is not
4891 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07004892 *
4893 * @param boundInView the portion of the view to test; coordinates should be relative; may be
4894 * <code>null</code>, and the entire view will be tested in this case.
4895 * When <code>true</code> is returned by the function, the actual visible
4896 * region will be stored in this parameter; that is, if boundInView is fully
4897 * contained within the view, no modification will be made, otherwise regions
4898 * outside of the visible area of the view will be clipped.
4899 *
4900 * @return Whether the specified portion of the view is visible on the screen.
4901 *
4902 * @hide
4903 */
4904 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07004905 if (mAttachInfo != null) {
4906 Rect visibleRect = mAttachInfo.mTmpInvalRect;
4907 Point offset = mAttachInfo.mPoint;
4908 // The first two checks are made also made by isShown() which
4909 // however traverses the tree up to the parent to catch that.
4910 // Therefore, we do some fail fast check to minimize the up
4911 // tree traversal.
4912 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
4913 && getAlpha() > 0
4914 && isShown()
4915 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004916 if (isVisible && boundInView != null) {
4917 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07004918 // isVisible is always true here, use a simple assignment
4919 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07004920 }
4921 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07004922 }
4923
4924 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004925 }
4926
4927 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004928 * Sets a delegate for implementing accessibility support via compositon as
4929 * opposed to inheritance. The delegate's primary use is for implementing
4930 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4931 *
4932 * @param delegate The delegate instance.
4933 *
4934 * @see AccessibilityDelegate
4935 */
4936 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4937 mAccessibilityDelegate = delegate;
4938 }
4939
4940 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004941 * Gets the provider for managing a virtual view hierarchy rooted at this View
4942 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4943 * that explore the window content.
4944 * <p>
4945 * If this method returns an instance, this instance is responsible for managing
4946 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4947 * View including the one representing the View itself. Similarly the returned
4948 * instance is responsible for performing accessibility actions on any virtual
4949 * view or the root view itself.
4950 * </p>
4951 * <p>
4952 * If an {@link AccessibilityDelegate} has been specified via calling
4953 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4954 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4955 * is responsible for handling this call.
4956 * </p>
4957 *
4958 * @return The provider.
4959 *
4960 * @see AccessibilityNodeProvider
4961 */
4962 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4963 if (mAccessibilityDelegate != null) {
4964 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4965 } else {
4966 return null;
4967 }
4968 }
4969
4970 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004971 * Gets the unique identifier of this view on the screen for accessibility purposes.
4972 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4973 *
4974 * @return The view accessibility id.
4975 *
4976 * @hide
4977 */
4978 public int getAccessibilityViewId() {
4979 if (mAccessibilityViewId == NO_ID) {
4980 mAccessibilityViewId = sNextAccessibilityViewId++;
4981 }
4982 return mAccessibilityViewId;
4983 }
4984
4985 /**
4986 * Gets the unique identifier of the window in which this View reseides.
4987 *
4988 * @return The window accessibility id.
4989 *
4990 * @hide
4991 */
4992 public int getAccessibilityWindowId() {
4993 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4994 }
4995
4996 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004997 * Gets the {@link View} description. It briefly describes the view and is
4998 * primarily used for accessibility support. Set this property to enable
4999 * better accessibility support for your application. This is especially
5000 * true for views that do not have textual representation (For example,
5001 * ImageButton).
5002 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005003 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005004 *
5005 * @attr ref android.R.styleable#View_contentDescription
5006 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005007 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005008 public CharSequence getContentDescription() {
5009 return mContentDescription;
5010 }
5011
5012 /**
5013 * Sets the {@link View} description. It briefly describes the view and is
5014 * primarily used for accessibility support. Set this property to enable
5015 * better accessibility support for your application. This is especially
5016 * true for views that do not have textual representation (For example,
5017 * ImageButton).
5018 *
5019 * @param contentDescription The content description.
5020 *
5021 * @attr ref android.R.styleable#View_contentDescription
5022 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005023 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005024 public void setContentDescription(CharSequence contentDescription) {
5025 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005026 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5027 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5028 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5029 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005030 }
5031
5032 /**
Romain Guya2431d02009-04-30 16:30:00 -07005033 * Invoked whenever this view loses focus, either by losing window focus or by losing
5034 * focus within its window. This method can be used to clear any state tied to the
5035 * focus. For instance, if a button is held pressed with the trackball and the window
5036 * loses focus, this method can be used to cancel the press.
5037 *
5038 * Subclasses of View overriding this method should always call super.onFocusLost().
5039 *
5040 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005041 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005042 *
5043 * @hide pending API council approval
5044 */
5045 protected void onFocusLost() {
5046 resetPressedState();
5047 }
5048
5049 private void resetPressedState() {
5050 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5051 return;
5052 }
5053
5054 if (isPressed()) {
5055 setPressed(false);
5056
5057 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005058 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005059 }
5060 }
5061 }
5062
5063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 * Returns true if this view has focus
5065 *
5066 * @return True if this view has focus, false otherwise.
5067 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005068 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005070 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 }
5072
5073 /**
5074 * Find the view in the hierarchy rooted at this view that currently has
5075 * focus.
5076 *
5077 * @return The view that currently has focus, or null if no focused view can
5078 * be found.
5079 */
5080 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005081 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 }
5083
5084 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005085 * Indicates whether this view is one of the set of scrollable containers in
5086 * its window.
5087 *
5088 * @return whether this view is one of the set of scrollable containers in
5089 * its window
5090 *
5091 * @attr ref android.R.styleable#View_isScrollContainer
5092 */
5093 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005094 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005095 }
5096
5097 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005098 * Change whether this view is one of the set of scrollable containers in
5099 * its window. This will be used to determine whether the window can
5100 * resize or must pan when a soft input area is open -- scrollable
5101 * containers allow the window to use resize mode since the container
5102 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005103 *
5104 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 */
5106 public void setScrollContainer(boolean isScrollContainer) {
5107 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005108 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005110 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005112 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005114 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115 mAttachInfo.mScrollContainers.remove(this);
5116 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005117 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 }
5119 }
5120
5121 /**
5122 * Returns the quality of the drawing cache.
5123 *
5124 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5125 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5126 *
5127 * @see #setDrawingCacheQuality(int)
5128 * @see #setDrawingCacheEnabled(boolean)
5129 * @see #isDrawingCacheEnabled()
5130 *
5131 * @attr ref android.R.styleable#View_drawingCacheQuality
5132 */
5133 public int getDrawingCacheQuality() {
5134 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5135 }
5136
5137 /**
5138 * Set the drawing cache quality of this view. This value is used only when the
5139 * drawing cache is enabled
5140 *
5141 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5142 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5143 *
5144 * @see #getDrawingCacheQuality()
5145 * @see #setDrawingCacheEnabled(boolean)
5146 * @see #isDrawingCacheEnabled()
5147 *
5148 * @attr ref android.R.styleable#View_drawingCacheQuality
5149 */
5150 public void setDrawingCacheQuality(int quality) {
5151 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5152 }
5153
5154 /**
5155 * Returns whether the screen should remain on, corresponding to the current
5156 * value of {@link #KEEP_SCREEN_ON}.
5157 *
5158 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5159 *
5160 * @see #setKeepScreenOn(boolean)
5161 *
5162 * @attr ref android.R.styleable#View_keepScreenOn
5163 */
5164 public boolean getKeepScreenOn() {
5165 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5166 }
5167
5168 /**
5169 * Controls whether the screen should remain on, modifying the
5170 * value of {@link #KEEP_SCREEN_ON}.
5171 *
5172 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5173 *
5174 * @see #getKeepScreenOn()
5175 *
5176 * @attr ref android.R.styleable#View_keepScreenOn
5177 */
5178 public void setKeepScreenOn(boolean keepScreenOn) {
5179 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5180 }
5181
5182 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005183 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5184 * @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 -08005185 *
5186 * @attr ref android.R.styleable#View_nextFocusLeft
5187 */
5188 public int getNextFocusLeftId() {
5189 return mNextFocusLeftId;
5190 }
5191
5192 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005193 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5194 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5195 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 *
5197 * @attr ref android.R.styleable#View_nextFocusLeft
5198 */
5199 public void setNextFocusLeftId(int nextFocusLeftId) {
5200 mNextFocusLeftId = nextFocusLeftId;
5201 }
5202
5203 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005204 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5205 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 *
5207 * @attr ref android.R.styleable#View_nextFocusRight
5208 */
5209 public int getNextFocusRightId() {
5210 return mNextFocusRightId;
5211 }
5212
5213 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005214 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5215 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5216 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 *
5218 * @attr ref android.R.styleable#View_nextFocusRight
5219 */
5220 public void setNextFocusRightId(int nextFocusRightId) {
5221 mNextFocusRightId = nextFocusRightId;
5222 }
5223
5224 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005225 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5226 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 *
5228 * @attr ref android.R.styleable#View_nextFocusUp
5229 */
5230 public int getNextFocusUpId() {
5231 return mNextFocusUpId;
5232 }
5233
5234 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005235 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5236 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5237 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 *
5239 * @attr ref android.R.styleable#View_nextFocusUp
5240 */
5241 public void setNextFocusUpId(int nextFocusUpId) {
5242 mNextFocusUpId = nextFocusUpId;
5243 }
5244
5245 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005246 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5247 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 *
5249 * @attr ref android.R.styleable#View_nextFocusDown
5250 */
5251 public int getNextFocusDownId() {
5252 return mNextFocusDownId;
5253 }
5254
5255 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005256 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5257 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5258 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 *
5260 * @attr ref android.R.styleable#View_nextFocusDown
5261 */
5262 public void setNextFocusDownId(int nextFocusDownId) {
5263 mNextFocusDownId = nextFocusDownId;
5264 }
5265
5266 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005267 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5268 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5269 *
5270 * @attr ref android.R.styleable#View_nextFocusForward
5271 */
5272 public int getNextFocusForwardId() {
5273 return mNextFocusForwardId;
5274 }
5275
5276 /**
5277 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5278 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5279 * decide automatically.
5280 *
5281 * @attr ref android.R.styleable#View_nextFocusForward
5282 */
5283 public void setNextFocusForwardId(int nextFocusForwardId) {
5284 mNextFocusForwardId = nextFocusForwardId;
5285 }
5286
5287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 * Returns the visibility of this view and all of its ancestors
5289 *
5290 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5291 */
5292 public boolean isShown() {
5293 View current = this;
5294 //noinspection ConstantConditions
5295 do {
5296 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5297 return false;
5298 }
5299 ViewParent parent = current.mParent;
5300 if (parent == null) {
5301 return false; // We are not attached to the view root
5302 }
5303 if (!(parent instanceof View)) {
5304 return true;
5305 }
5306 current = (View) parent;
5307 } while (current != null);
5308
5309 return false;
5310 }
5311
5312 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005313 * Called by the view hierarchy when the content insets for a window have
5314 * changed, to allow it to adjust its content to fit within those windows.
5315 * The content insets tell you the space that the status bar, input method,
5316 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005318 * <p>You do not normally need to deal with this function, since the default
5319 * window decoration given to applications takes care of applying it to the
5320 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5321 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5322 * and your content can be placed under those system elements. You can then
5323 * use this method within your view hierarchy if you have parts of your UI
5324 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005326 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005327 * inset's to the view's padding, consuming that content (modifying the
5328 * insets to be 0), and returning true. This behavior is off by default, but can
5329 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5330 *
5331 * <p>This function's traversal down the hierarchy is depth-first. The same content
5332 * insets object is propagated down the hierarchy, so any changes made to it will
5333 * be seen by all following views (including potentially ones above in
5334 * the hierarchy since this is a depth-first traversal). The first view
5335 * that returns true will abort the entire traversal.
5336 *
5337 * <p>The default implementation works well for a situation where it is
5338 * used with a container that covers the entire window, allowing it to
5339 * apply the appropriate insets to its content on all edges. If you need
5340 * a more complicated layout (such as two different views fitting system
5341 * windows, one on the top of the window, and one on the bottom),
5342 * you can override the method and handle the insets however you would like.
5343 * Note that the insets provided by the framework are always relative to the
5344 * far edges of the window, not accounting for the location of the called view
5345 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005346 * where the layout will place the view, as it is done before layout happens.)
5347 *
5348 * <p>Note: unlike many View methods, there is no dispatch phase to this
5349 * call. If you are overriding it in a ViewGroup and want to allow the
5350 * call to continue to your children, you must be sure to call the super
5351 * implementation.
5352 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005353 * <p>Here is a sample layout that makes use of fitting system windows
5354 * to have controls for a video view placed inside of the window decorations
5355 * that it hides and shows. This can be used with code like the second
5356 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5357 *
5358 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5359 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005360 * @param insets Current content insets of the window. Prior to
5361 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5362 * the insets or else you and Android will be unhappy.
5363 *
5364 * @return Return true if this view applied the insets and it should not
5365 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005366 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005367 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005368 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 */
5370 protected boolean fitSystemWindows(Rect insets) {
5371 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005372 mUserPaddingStart = UNDEFINED_PADDING;
5373 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005374 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5375 || mAttachInfo == null
5376 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5377 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5378 return true;
5379 } else {
5380 internalSetPadding(0, 0, 0, 0);
5381 return false;
5382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384 return false;
5385 }
5386
5387 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005388 * Sets whether or not this view should account for system screen decorations
5389 * such as the status bar and inset its content; that is, controlling whether
5390 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5391 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005392 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005393 * <p>Note that if you are providing your own implementation of
5394 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5395 * flag to true -- your implementation will be overriding the default
5396 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005397 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005398 * @param fitSystemWindows If true, then the default implementation of
5399 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005400 *
5401 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005402 * @see #getFitsSystemWindows()
5403 * @see #fitSystemWindows(Rect)
5404 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005405 */
5406 public void setFitsSystemWindows(boolean fitSystemWindows) {
5407 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5408 }
5409
5410 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005411 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005412 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5413 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005414 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005415 * @return Returns true if the default implementation of
5416 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005417 *
5418 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005419 * @see #setFitsSystemWindows()
5420 * @see #fitSystemWindows(Rect)
5421 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005422 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005423 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005424 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5425 }
5426
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005427 /** @hide */
5428 public boolean fitsSystemWindows() {
5429 return getFitsSystemWindows();
5430 }
5431
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005432 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005433 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5434 */
5435 public void requestFitSystemWindows() {
5436 if (mParent != null) {
5437 mParent.requestFitSystemWindows();
5438 }
5439 }
5440
5441 /**
5442 * For use by PhoneWindow to make its own system window fitting optional.
5443 * @hide
5444 */
5445 public void makeOptionalFitsSystemWindows() {
5446 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5447 }
5448
5449 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 * Returns the visibility status for this view.
5451 *
5452 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5453 * @attr ref android.R.styleable#View_visibility
5454 */
5455 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005456 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5457 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5458 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 })
5460 public int getVisibility() {
5461 return mViewFlags & VISIBILITY_MASK;
5462 }
5463
5464 /**
5465 * Set the enabled state of this view.
5466 *
5467 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5468 * @attr ref android.R.styleable#View_visibility
5469 */
5470 @RemotableViewMethod
5471 public void setVisibility(int visibility) {
5472 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005473 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 }
5475
5476 /**
5477 * Returns the enabled status for this view. The interpretation of the
5478 * enabled state varies by subclass.
5479 *
5480 * @return True if this view is enabled, false otherwise.
5481 */
5482 @ViewDebug.ExportedProperty
5483 public boolean isEnabled() {
5484 return (mViewFlags & ENABLED_MASK) == ENABLED;
5485 }
5486
5487 /**
5488 * Set the enabled state of this view. The interpretation of the enabled
5489 * state varies by subclass.
5490 *
5491 * @param enabled True if this view is enabled, false otherwise.
5492 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005493 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005495 if (enabled == isEnabled()) return;
5496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5498
5499 /*
5500 * The View most likely has to change its appearance, so refresh
5501 * the drawable state.
5502 */
5503 refreshDrawableState();
5504
5505 // Invalidate too, since the default behavior for views is to be
5506 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005507 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 }
5509
5510 /**
5511 * Set whether this view can receive the focus.
5512 *
5513 * Setting this to false will also ensure that this view is not focusable
5514 * in touch mode.
5515 *
5516 * @param focusable If true, this view can receive the focus.
5517 *
5518 * @see #setFocusableInTouchMode(boolean)
5519 * @attr ref android.R.styleable#View_focusable
5520 */
5521 public void setFocusable(boolean focusable) {
5522 if (!focusable) {
5523 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5524 }
5525 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5526 }
5527
5528 /**
5529 * Set whether this view can receive focus while in touch mode.
5530 *
5531 * Setting this to true will also ensure that this view is focusable.
5532 *
5533 * @param focusableInTouchMode If true, this view can receive the focus while
5534 * in touch mode.
5535 *
5536 * @see #setFocusable(boolean)
5537 * @attr ref android.R.styleable#View_focusableInTouchMode
5538 */
5539 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5540 // Focusable in touch mode should always be set before the focusable flag
5541 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5542 // which, in touch mode, will not successfully request focus on this view
5543 // because the focusable in touch mode flag is not set
5544 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5545 if (focusableInTouchMode) {
5546 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5547 }
5548 }
5549
5550 /**
5551 * Set whether this view should have sound effects enabled for events such as
5552 * clicking and touching.
5553 *
5554 * <p>You may wish to disable sound effects for a view if you already play sounds,
5555 * for instance, a dial key that plays dtmf tones.
5556 *
5557 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5558 * @see #isSoundEffectsEnabled()
5559 * @see #playSoundEffect(int)
5560 * @attr ref android.R.styleable#View_soundEffectsEnabled
5561 */
5562 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5563 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5564 }
5565
5566 /**
5567 * @return whether this view should have sound effects enabled for events such as
5568 * clicking and touching.
5569 *
5570 * @see #setSoundEffectsEnabled(boolean)
5571 * @see #playSoundEffect(int)
5572 * @attr ref android.R.styleable#View_soundEffectsEnabled
5573 */
5574 @ViewDebug.ExportedProperty
5575 public boolean isSoundEffectsEnabled() {
5576 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5577 }
5578
5579 /**
5580 * Set whether this view should have haptic feedback for events such as
5581 * long presses.
5582 *
5583 * <p>You may wish to disable haptic feedback if your view already controls
5584 * its own haptic feedback.
5585 *
5586 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5587 * @see #isHapticFeedbackEnabled()
5588 * @see #performHapticFeedback(int)
5589 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5590 */
5591 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5592 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5593 }
5594
5595 /**
5596 * @return whether this view should have haptic feedback enabled for events
5597 * long presses.
5598 *
5599 * @see #setHapticFeedbackEnabled(boolean)
5600 * @see #performHapticFeedback(int)
5601 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5602 */
5603 @ViewDebug.ExportedProperty
5604 public boolean isHapticFeedbackEnabled() {
5605 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5606 }
5607
5608 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005609 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005610 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005611 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5612 * {@link #LAYOUT_DIRECTION_RTL},
5613 * {@link #LAYOUT_DIRECTION_INHERIT} or
5614 * {@link #LAYOUT_DIRECTION_LOCALE}.
5615 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005616 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005617 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005618 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5619 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5620 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5621 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005622 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005623 public int getLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005624 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005625 }
5626
5627 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005628 * Set the layout direction for this view. This will propagate a reset of layout direction
5629 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005630 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005631 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5632 * {@link #LAYOUT_DIRECTION_RTL},
5633 * {@link #LAYOUT_DIRECTION_INHERIT} or
5634 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005635 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005636 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005637 */
5638 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005639 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005640 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005641 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005642 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005643 resetResolvedLayoutDirection();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005644 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -07005645 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005646 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005647 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005648 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005649 resolveRtlProperties();
5650 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005651 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005652 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005653 }
5654
5655 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005656 * Returns the resolved layout direction for this view.
5657 *
5658 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005659 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005660 */
5661 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005662 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5663 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005664 })
5665 public int getResolvedLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005666 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5667 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005668 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005669 return LAYOUT_DIRECTION_LTR;
5670 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005671 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005672 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005673 resolveLayoutDirection();
5674 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005675 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005676 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5677 }
5678
5679 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005680 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5681 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005682 *
5683 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005684 */
5685 @ViewDebug.ExportedProperty(category = "layout")
5686 public boolean isLayoutRtl() {
5687 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5688 }
5689
5690 /**
Adam Powell539ee872012-02-03 19:00:49 -08005691 * Indicates whether the view is currently tracking transient state that the
5692 * app should not need to concern itself with saving and restoring, but that
5693 * the framework should take special note to preserve when possible.
5694 *
Adam Powell785c4472012-05-02 21:25:39 -07005695 * <p>A view with transient state cannot be trivially rebound from an external
5696 * data source, such as an adapter binding item views in a list. This may be
5697 * because the view is performing an animation, tracking user selection
5698 * of content, or similar.</p>
5699 *
Adam Powell539ee872012-02-03 19:00:49 -08005700 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005701 */
5702 @ViewDebug.ExportedProperty(category = "layout")
5703 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005704 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005705 }
5706
5707 /**
5708 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005709 * framework should attempt to preserve when possible. This flag is reference counted,
5710 * so every call to setHasTransientState(true) should be paired with a later call
5711 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005712 *
Adam Powell785c4472012-05-02 21:25:39 -07005713 * <p>A view with transient state cannot be trivially rebound from an external
5714 * data source, such as an adapter binding item views in a list. This may be
5715 * because the view is performing an animation, tracking user selection
5716 * of content, or similar.</p>
5717 *
Adam Powell539ee872012-02-03 19:00:49 -08005718 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005719 */
5720 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005721 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5722 mTransientStateCount - 1;
5723 if (mTransientStateCount < 0) {
5724 mTransientStateCount = 0;
5725 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5726 "unmatched pair of setHasTransientState calls");
5727 }
5728 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005729 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005730 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005731 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5732 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005733 if (mParent != null) {
5734 try {
5735 mParent.childHasTransientStateChanged(this, hasTransientState);
5736 } catch (AbstractMethodError e) {
5737 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5738 " does not fully implement ViewParent", e);
5739 }
Adam Powell539ee872012-02-03 19:00:49 -08005740 }
5741 }
5742 }
5743
5744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005745 * If this view doesn't do any drawing on its own, set this flag to
5746 * allow further optimizations. By default, this flag is not set on
5747 * View, but could be set on some View subclasses such as ViewGroup.
5748 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005749 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5750 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 *
5752 * @param willNotDraw whether or not this View draw on its own
5753 */
5754 public void setWillNotDraw(boolean willNotDraw) {
5755 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5756 }
5757
5758 /**
5759 * Returns whether or not this View draws on its own.
5760 *
5761 * @return true if this view has nothing to draw, false otherwise
5762 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005763 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 public boolean willNotDraw() {
5765 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5766 }
5767
5768 /**
5769 * When a View's drawing cache is enabled, drawing is redirected to an
5770 * offscreen bitmap. Some views, like an ImageView, must be able to
5771 * bypass this mechanism if they already draw a single bitmap, to avoid
5772 * unnecessary usage of the memory.
5773 *
5774 * @param willNotCacheDrawing true if this view does not cache its
5775 * drawing, false otherwise
5776 */
5777 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5778 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5779 }
5780
5781 /**
5782 * Returns whether or not this View can cache its drawing or not.
5783 *
5784 * @return true if this view does not cache its drawing, false otherwise
5785 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005786 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 public boolean willNotCacheDrawing() {
5788 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5789 }
5790
5791 /**
5792 * Indicates whether this view reacts to click events or not.
5793 *
5794 * @return true if the view is clickable, false otherwise
5795 *
5796 * @see #setClickable(boolean)
5797 * @attr ref android.R.styleable#View_clickable
5798 */
5799 @ViewDebug.ExportedProperty
5800 public boolean isClickable() {
5801 return (mViewFlags & CLICKABLE) == CLICKABLE;
5802 }
5803
5804 /**
5805 * Enables or disables click events for this view. When a view
5806 * is clickable it will change its state to "pressed" on every click.
5807 * Subclasses should set the view clickable to visually react to
5808 * user's clicks.
5809 *
5810 * @param clickable true to make the view clickable, false otherwise
5811 *
5812 * @see #isClickable()
5813 * @attr ref android.R.styleable#View_clickable
5814 */
5815 public void setClickable(boolean clickable) {
5816 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5817 }
5818
5819 /**
5820 * Indicates whether this view reacts to long click events or not.
5821 *
5822 * @return true if the view is long clickable, false otherwise
5823 *
5824 * @see #setLongClickable(boolean)
5825 * @attr ref android.R.styleable#View_longClickable
5826 */
5827 public boolean isLongClickable() {
5828 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5829 }
5830
5831 /**
5832 * Enables or disables long click events for this view. When a view is long
5833 * clickable it reacts to the user holding down the button for a longer
5834 * duration than a tap. This event can either launch the listener or a
5835 * context menu.
5836 *
5837 * @param longClickable true to make the view long clickable, false otherwise
5838 * @see #isLongClickable()
5839 * @attr ref android.R.styleable#View_longClickable
5840 */
5841 public void setLongClickable(boolean longClickable) {
5842 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5843 }
5844
5845 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005846 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 *
5848 * @see #isClickable()
5849 * @see #setClickable(boolean)
5850 *
5851 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5852 * the View's internal state from a previously set "pressed" state.
5853 */
5854 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005855 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005858 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005860 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005862
5863 if (needsRefresh) {
5864 refreshDrawableState();
5865 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 dispatchSetPressed(pressed);
5867 }
5868
5869 /**
5870 * Dispatch setPressed to all of this View's children.
5871 *
5872 * @see #setPressed(boolean)
5873 *
5874 * @param pressed The new pressed state
5875 */
5876 protected void dispatchSetPressed(boolean pressed) {
5877 }
5878
5879 /**
5880 * Indicates whether the view is currently in pressed state. Unless
5881 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5882 * the pressed state.
5883 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005884 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005885 * @see #isClickable()
5886 * @see #setClickable(boolean)
5887 *
5888 * @return true if the view is currently pressed, false otherwise
5889 */
5890 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005891 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 }
5893
5894 /**
5895 * Indicates whether this view will save its state (that is,
5896 * whether its {@link #onSaveInstanceState} method will be called).
5897 *
5898 * @return Returns true if the view state saving is enabled, else false.
5899 *
5900 * @see #setSaveEnabled(boolean)
5901 * @attr ref android.R.styleable#View_saveEnabled
5902 */
5903 public boolean isSaveEnabled() {
5904 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5905 }
5906
5907 /**
5908 * Controls whether the saving of this view's state is
5909 * enabled (that is, whether its {@link #onSaveInstanceState} method
5910 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005911 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 * for its state to be saved. This flag can only disable the
5913 * saving of this view; any child views may still have their state saved.
5914 *
5915 * @param enabled Set to false to <em>disable</em> state saving, or true
5916 * (the default) to allow it.
5917 *
5918 * @see #isSaveEnabled()
5919 * @see #setId(int)
5920 * @see #onSaveInstanceState()
5921 * @attr ref android.R.styleable#View_saveEnabled
5922 */
5923 public void setSaveEnabled(boolean enabled) {
5924 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5925 }
5926
Jeff Brown85a31762010-09-01 17:01:00 -07005927 /**
5928 * Gets whether the framework should discard touches when the view's
5929 * window is obscured by another visible window.
5930 * Refer to the {@link View} security documentation for more details.
5931 *
5932 * @return True if touch filtering is enabled.
5933 *
5934 * @see #setFilterTouchesWhenObscured(boolean)
5935 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5936 */
5937 @ViewDebug.ExportedProperty
5938 public boolean getFilterTouchesWhenObscured() {
5939 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5940 }
5941
5942 /**
5943 * Sets whether the framework should discard touches when the view's
5944 * window is obscured by another visible window.
5945 * Refer to the {@link View} security documentation for more details.
5946 *
5947 * @param enabled True if touch filtering should be enabled.
5948 *
5949 * @see #getFilterTouchesWhenObscured
5950 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5951 */
5952 public void setFilterTouchesWhenObscured(boolean enabled) {
5953 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5954 FILTER_TOUCHES_WHEN_OBSCURED);
5955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956
5957 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005958 * Indicates whether the entire hierarchy under this view will save its
5959 * state when a state saving traversal occurs from its parent. The default
5960 * is true; if false, these views will not be saved unless
5961 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5962 *
5963 * @return Returns true if the view state saving from parent is enabled, else false.
5964 *
5965 * @see #setSaveFromParentEnabled(boolean)
5966 */
5967 public boolean isSaveFromParentEnabled() {
5968 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5969 }
5970
5971 /**
5972 * Controls whether the entire hierarchy under this view will save its
5973 * state when a state saving traversal occurs from its parent. The default
5974 * is true; if false, these views will not be saved unless
5975 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5976 *
5977 * @param enabled Set to false to <em>disable</em> state saving, or true
5978 * (the default) to allow it.
5979 *
5980 * @see #isSaveFromParentEnabled()
5981 * @see #setId(int)
5982 * @see #onSaveInstanceState()
5983 */
5984 public void setSaveFromParentEnabled(boolean enabled) {
5985 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5986 }
5987
5988
5989 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 * Returns whether this View is able to take focus.
5991 *
5992 * @return True if this view can take focus, or false otherwise.
5993 * @attr ref android.R.styleable#View_focusable
5994 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005995 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005996 public final boolean isFocusable() {
5997 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5998 }
5999
6000 /**
6001 * When a view is focusable, it may not want to take focus when in touch mode.
6002 * For example, a button would like focus when the user is navigating via a D-pad
6003 * so that the user can click on it, but once the user starts touching the screen,
6004 * the button shouldn't take focus
6005 * @return Whether the view is focusable in touch mode.
6006 * @attr ref android.R.styleable#View_focusableInTouchMode
6007 */
6008 @ViewDebug.ExportedProperty
6009 public final boolean isFocusableInTouchMode() {
6010 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6011 }
6012
6013 /**
6014 * Find the nearest view in the specified direction that can take focus.
6015 * This does not actually give focus to that view.
6016 *
6017 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6018 *
6019 * @return The nearest focusable in the specified direction, or null if none
6020 * can be found.
6021 */
6022 public View focusSearch(int direction) {
6023 if (mParent != null) {
6024 return mParent.focusSearch(this, direction);
6025 } else {
6026 return null;
6027 }
6028 }
6029
6030 /**
6031 * This method is the last chance for the focused view and its ancestors to
6032 * respond to an arrow key. This is called when the focused view did not
6033 * consume the key internally, nor could the view system find a new view in
6034 * the requested direction to give focus to.
6035 *
6036 * @param focused The currently focused view.
6037 * @param direction The direction focus wants to move. One of FOCUS_UP,
6038 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6039 * @return True if the this view consumed this unhandled move.
6040 */
6041 public boolean dispatchUnhandledMove(View focused, int direction) {
6042 return false;
6043 }
6044
6045 /**
6046 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006047 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006049 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6050 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 * @return The user specified next view, or null if there is none.
6052 */
6053 View findUserSetNextFocus(View root, int direction) {
6054 switch (direction) {
6055 case FOCUS_LEFT:
6056 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006057 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 case FOCUS_RIGHT:
6059 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006060 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006061 case FOCUS_UP:
6062 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006063 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 case FOCUS_DOWN:
6065 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006066 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006067 case FOCUS_FORWARD:
6068 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006069 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006070 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006071 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006072 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006073 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006074 @Override
6075 public boolean apply(View t) {
6076 return t.mNextFocusForwardId == id;
6077 }
6078 });
6079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 }
6081 return null;
6082 }
6083
Jeff Brown4dfbec22011-08-15 14:55:37 -07006084 private View findViewInsideOutShouldExist(View root, final int childViewId) {
6085 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
6086 @Override
6087 public boolean apply(View t) {
6088 return t.mID == childViewId;
6089 }
6090 });
6091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 if (result == null) {
6093 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
6094 + "by user for id " + childViewId);
6095 }
6096 return result;
6097 }
6098
6099 /**
6100 * Find and return all focusable views that are descendants of this view,
6101 * possibly including this view if it is focusable itself.
6102 *
6103 * @param direction The direction of the focus
6104 * @return A list of focusable views
6105 */
6106 public ArrayList<View> getFocusables(int direction) {
6107 ArrayList<View> result = new ArrayList<View>(24);
6108 addFocusables(result, direction);
6109 return result;
6110 }
6111
6112 /**
6113 * Add any focusable views that are descendants of this view (possibly
6114 * including this view if it is focusable itself) to views. If we are in touch mode,
6115 * only add views that are also focusable in touch mode.
6116 *
6117 * @param views Focusable views found so far
6118 * @param direction The direction of the focus
6119 */
6120 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006121 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123
svetoslavganov75986cf2009-05-14 22:28:01 -07006124 /**
6125 * Adds any focusable views that are descendants of this view (possibly
6126 * including this view if it is focusable itself) to views. This method
6127 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006128 * only views focusable in touch mode if we are in touch mode or
6129 * only views that can take accessibility focus if accessibility is enabeld
6130 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006131 *
6132 * @param views Focusable views found so far or null if all we are interested is
6133 * the number of focusables.
6134 * @param direction The direction of the focus.
6135 * @param focusableMode The type of focusables to be added.
6136 *
6137 * @see #FOCUSABLES_ALL
6138 * @see #FOCUSABLES_TOUCH_MODE
6139 */
6140 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006141 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006142 return;
6143 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006144 if (!isFocusable()) {
6145 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006146 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006147 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6148 && isInTouchMode() && !isFocusableInTouchMode()) {
6149 return;
6150 }
6151 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006152 }
6153
6154 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006155 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006156 * The search is performed by either the text that the View renders or the content
6157 * description that describes the view for accessibility purposes and the view does
6158 * not render or both. Clients can specify how the search is to be performed via
6159 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6160 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006161 *
6162 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006163 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006164 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006165 * @see #FIND_VIEWS_WITH_TEXT
6166 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6167 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006168 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006169 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006170 if (getAccessibilityNodeProvider() != null) {
6171 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6172 outViews.add(this);
6173 }
6174 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006175 && (searched != null && searched.length() > 0)
6176 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006177 String searchedLowerCase = searched.toString().toLowerCase();
6178 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6179 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6180 outViews.add(this);
6181 }
6182 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006183 }
6184
6185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 * Find and return all touchable views that are descendants of this view,
6187 * possibly including this view if it is touchable itself.
6188 *
6189 * @return A list of touchable views
6190 */
6191 public ArrayList<View> getTouchables() {
6192 ArrayList<View> result = new ArrayList<View>();
6193 addTouchables(result);
6194 return result;
6195 }
6196
6197 /**
6198 * Add any touchable views that are descendants of this view (possibly
6199 * including this view if it is touchable itself) to views.
6200 *
6201 * @param views Touchable views found so far
6202 */
6203 public void addTouchables(ArrayList<View> views) {
6204 final int viewFlags = mViewFlags;
6205
6206 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6207 && (viewFlags & ENABLED_MASK) == ENABLED) {
6208 views.add(this);
6209 }
6210 }
6211
6212 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006213 * Returns whether this View is accessibility focused.
6214 *
6215 * @return True if this View is accessibility focused.
6216 */
6217 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006218 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006219 }
6220
6221 /**
6222 * Call this to try to give accessibility focus to this view.
6223 *
6224 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6225 * returns false or the view is no visible or the view already has accessibility
6226 * focus.
6227 *
6228 * See also {@link #focusSearch(int)}, which is what you call to say that you
6229 * have focus, and you want your parent to look for the next one.
6230 *
6231 * @return Whether this view actually took accessibility focus.
6232 *
6233 * @hide
6234 */
6235 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006236 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6237 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006238 return false;
6239 }
6240 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6241 return false;
6242 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006243 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6244 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006245 ViewRootImpl viewRootImpl = getViewRootImpl();
6246 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006247 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006248 }
6249 invalidate();
6250 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6251 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006252 return true;
6253 }
6254 return false;
6255 }
6256
6257 /**
6258 * Call this to try to clear accessibility focus of this view.
6259 *
6260 * See also {@link #focusSearch(int)}, which is what you call to say that you
6261 * have focus, and you want your parent to look for the next one.
6262 *
6263 * @hide
6264 */
6265 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006266 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6267 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006268 invalidate();
6269 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6270 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006271 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006272 // Clear the global reference of accessibility focus if this
6273 // view or any of its descendants had accessibility focus.
6274 ViewRootImpl viewRootImpl = getViewRootImpl();
6275 if (viewRootImpl != null) {
6276 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6277 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006278 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006279 }
6280 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006281 }
6282
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006283 private void sendAccessibilityHoverEvent(int eventType) {
6284 // Since we are not delivering to a client accessibility events from not
6285 // important views (unless the clinet request that) we need to fire the
6286 // event from the deepest view exposed to the client. As a consequence if
6287 // the user crosses a not exposed view the client will see enter and exit
6288 // of the exposed predecessor followed by and enter and exit of that same
6289 // predecessor when entering and exiting the not exposed descendant. This
6290 // is fine since the client has a clear idea which view is hovered at the
6291 // price of a couple more events being sent. This is a simple and
6292 // working solution.
6293 View source = this;
6294 while (true) {
6295 if (source.includeForAccessibility()) {
6296 source.sendAccessibilityEvent(eventType);
6297 return;
6298 }
6299 ViewParent parent = source.getParent();
6300 if (parent instanceof View) {
6301 source = (View) parent;
6302 } else {
6303 return;
6304 }
6305 }
6306 }
6307
Svetoslav Ganov42138042012-03-20 11:51:39 -07006308 /**
6309 * Clears accessibility focus without calling any callback methods
6310 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6311 * is used for clearing accessibility focus when giving this focus to
6312 * another view.
6313 */
6314 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006315 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6316 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006317 invalidate();
6318 }
6319 }
6320
6321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 * Call this to try to give focus to a specific view or to one of its
6323 * descendants.
6324 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006325 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6326 * false), or if it is focusable and it is not focusable in touch mode
6327 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006329 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006330 * have focus, and you want your parent to look for the next one.
6331 *
6332 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6333 * {@link #FOCUS_DOWN} and <code>null</code>.
6334 *
6335 * @return Whether this view or one of its descendants actually took focus.
6336 */
6337 public final boolean requestFocus() {
6338 return requestFocus(View.FOCUS_DOWN);
6339 }
6340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 /**
6342 * Call this to try to give focus to a specific view or to one of its
6343 * descendants and give it a hint about what direction focus is heading.
6344 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006345 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6346 * false), or if it is focusable and it is not focusable in touch mode
6347 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006349 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006350 * have focus, and you want your parent to look for the next one.
6351 *
6352 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6353 * <code>null</code> set for the previously focused rectangle.
6354 *
6355 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6356 * @return Whether this view or one of its descendants actually took focus.
6357 */
6358 public final boolean requestFocus(int direction) {
6359 return requestFocus(direction, null);
6360 }
6361
6362 /**
6363 * Call this to try to give focus to a specific view or to one of its descendants
6364 * and give it hints about the direction and a specific rectangle that the focus
6365 * is coming from. The rectangle can help give larger views a finer grained hint
6366 * about where focus is coming from, and therefore, where to show selection, or
6367 * forward focus change internally.
6368 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006369 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6370 * false), or if it is focusable and it is not focusable in touch mode
6371 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 *
6373 * A View will not take focus if it is not visible.
6374 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006375 * A View will not take focus if one of its parents has
6376 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6377 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006379 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 * have focus, and you want your parent to look for the next one.
6381 *
6382 * You may wish to override this method if your custom {@link View} has an internal
6383 * {@link View} that it wishes to forward the request to.
6384 *
6385 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6386 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6387 * to give a finer grained hint about where focus is coming from. May be null
6388 * if there is no hint.
6389 * @return Whether this view or one of its descendants actually took focus.
6390 */
6391 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006392 return requestFocusNoSearch(direction, previouslyFocusedRect);
6393 }
6394
6395 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 // need to be focusable
6397 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6398 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6399 return false;
6400 }
6401
6402 // need to be focusable in touch mode if in touch mode
6403 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006404 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6405 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006406 }
6407
6408 // need to not have any parents blocking us
6409 if (hasAncestorThatBlocksDescendantFocus()) {
6410 return false;
6411 }
6412
6413 handleFocusGainInternal(direction, previouslyFocusedRect);
6414 return true;
6415 }
6416
6417 /**
6418 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6419 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6420 * touch mode to request focus when they are touched.
6421 *
6422 * @return Whether this view or one of its descendants actually took focus.
6423 *
6424 * @see #isInTouchMode()
6425 *
6426 */
6427 public final boolean requestFocusFromTouch() {
6428 // Leave touch mode if we need to
6429 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006430 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006431 if (viewRoot != null) {
6432 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 }
6434 }
6435 return requestFocus(View.FOCUS_DOWN);
6436 }
6437
6438 /**
6439 * @return Whether any ancestor of this view blocks descendant focus.
6440 */
6441 private boolean hasAncestorThatBlocksDescendantFocus() {
6442 ViewParent ancestor = mParent;
6443 while (ancestor instanceof ViewGroup) {
6444 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6445 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6446 return true;
6447 } else {
6448 ancestor = vgAncestor.getParent();
6449 }
6450 }
6451 return false;
6452 }
6453
6454 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006455 * Gets the mode for determining whether this View is important for accessibility
6456 * which is if it fires accessibility events and if it is reported to
6457 * accessibility services that query the screen.
6458 *
6459 * @return The mode for determining whether a View is important for accessibility.
6460 *
6461 * @attr ref android.R.styleable#View_importantForAccessibility
6462 *
6463 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6464 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6465 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6466 */
6467 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006468 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6469 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6470 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006471 })
6472 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006473 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6474 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006475 }
6476
6477 /**
6478 * Sets how to determine whether this view is important for accessibility
6479 * which is if it fires accessibility events and if it is reported to
6480 * accessibility services that query the screen.
6481 *
6482 * @param mode How to determine whether this view is important for accessibility.
6483 *
6484 * @attr ref android.R.styleable#View_importantForAccessibility
6485 *
6486 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6487 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6488 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6489 */
6490 public void setImportantForAccessibility(int mode) {
6491 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006492 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6493 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6494 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006495 notifyAccessibilityStateChanged();
6496 }
6497 }
6498
6499 /**
6500 * Gets whether this view should be exposed for accessibility.
6501 *
6502 * @return Whether the view is exposed for accessibility.
6503 *
6504 * @hide
6505 */
6506 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006507 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6508 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006509 switch (mode) {
6510 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6511 return true;
6512 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6513 return false;
6514 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006515 return isActionableForAccessibility() || hasListenersForAccessibility()
6516 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006517 default:
6518 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6519 + mode);
6520 }
6521 }
6522
6523 /**
6524 * Gets the parent for accessibility purposes. Note that the parent for
6525 * accessibility is not necessary the immediate parent. It is the first
6526 * predecessor that is important for accessibility.
6527 *
6528 * @return The parent for accessibility purposes.
6529 */
6530 public ViewParent getParentForAccessibility() {
6531 if (mParent instanceof View) {
6532 View parentView = (View) mParent;
6533 if (parentView.includeForAccessibility()) {
6534 return mParent;
6535 } else {
6536 return mParent.getParentForAccessibility();
6537 }
6538 }
6539 return null;
6540 }
6541
6542 /**
6543 * Adds the children of a given View for accessibility. Since some Views are
6544 * not important for accessibility the children for accessibility are not
6545 * necessarily direct children of the riew, rather they are the first level of
6546 * descendants important for accessibility.
6547 *
6548 * @param children The list of children for accessibility.
6549 */
6550 public void addChildrenForAccessibility(ArrayList<View> children) {
6551 if (includeForAccessibility()) {
6552 children.add(this);
6553 }
6554 }
6555
6556 /**
6557 * Whether to regard this view for accessibility. A view is regarded for
6558 * accessibility if it is important for accessibility or the querying
6559 * accessibility service has explicitly requested that view not
6560 * important for accessibility are regarded.
6561 *
6562 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006563 *
6564 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006565 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006566 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006567 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006568 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006569 }
6570 return false;
6571 }
6572
6573 /**
6574 * Returns whether the View is considered actionable from
6575 * accessibility perspective. Such view are important for
6576 * accessiiblity.
6577 *
6578 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006579 *
6580 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006581 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006582 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006583 return (isClickable() || isLongClickable() || isFocusable());
6584 }
6585
6586 /**
6587 * Returns whether the View has registered callbacks wich makes it
6588 * important for accessiiblity.
6589 *
6590 * @return True if the view is actionable for accessibility.
6591 */
6592 private boolean hasListenersForAccessibility() {
6593 ListenerInfo info = getListenerInfo();
6594 return mTouchDelegate != null || info.mOnKeyListener != null
6595 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6596 || info.mOnHoverListener != null || info.mOnDragListener != null;
6597 }
6598
6599 /**
6600 * Notifies accessibility services that some view's important for
6601 * accessibility state has changed. Note that such notifications
6602 * are made at most once every
6603 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6604 * to avoid unnecessary load to the system. Also once a view has
6605 * made a notifucation this method is a NOP until the notification has
6606 * been sent to clients.
6607 *
6608 * @hide
6609 *
6610 * TODO: Makse sure this method is called for any view state change
6611 * that is interesting for accessilility purposes.
6612 */
6613 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006614 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6615 return;
6616 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006617 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6618 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006619 if (mParent != null) {
6620 mParent.childAccessibilityStateChanged(this);
6621 }
6622 }
6623 }
6624
6625 /**
6626 * Reset the state indicating the this view has requested clients
6627 * interested in its accessiblity state to be notified.
6628 *
6629 * @hide
6630 */
6631 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006632 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006633 }
6634
6635 /**
6636 * Performs the specified accessibility action on the view. For
6637 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006638 * <p>
6639 * If an {@link AccessibilityDelegate} has been specified via calling
6640 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6641 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6642 * is responsible for handling this call.
6643 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006644 *
6645 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006646 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006647 * @return Whether the action was performed.
6648 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006649 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006650 if (mAccessibilityDelegate != null) {
6651 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6652 } else {
6653 return performAccessibilityActionInternal(action, arguments);
6654 }
6655 }
6656
6657 /**
6658 * @see #performAccessibilityAction(int, Bundle)
6659 *
6660 * Note: Called from the default {@link AccessibilityDelegate}.
6661 */
6662 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006663 switch (action) {
6664 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006665 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006666 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006667 }
6668 } break;
6669 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6670 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006671 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006672 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006673 } break;
6674 case AccessibilityNodeInfo.ACTION_FOCUS: {
6675 if (!hasFocus()) {
6676 // Get out of touch mode since accessibility
6677 // wants to move focus around.
6678 getViewRootImpl().ensureTouchMode(false);
6679 return requestFocus();
6680 }
6681 } break;
6682 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6683 if (hasFocus()) {
6684 clearFocus();
6685 return !isFocused();
6686 }
6687 } break;
6688 case AccessibilityNodeInfo.ACTION_SELECT: {
6689 if (!isSelected()) {
6690 setSelected(true);
6691 return isSelected();
6692 }
6693 } break;
6694 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6695 if (isSelected()) {
6696 setSelected(false);
6697 return !isSelected();
6698 }
6699 } break;
6700 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006701 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006702 return requestAccessibilityFocus();
6703 }
6704 } break;
6705 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6706 if (isAccessibilityFocused()) {
6707 clearAccessibilityFocus();
6708 return true;
6709 }
6710 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006711 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6712 if (arguments != null) {
6713 final int granularity = arguments.getInt(
6714 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6715 return nextAtGranularity(granularity);
6716 }
6717 } break;
6718 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6719 if (arguments != null) {
6720 final int granularity = arguments.getInt(
6721 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6722 return previousAtGranularity(granularity);
6723 }
6724 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006725 }
6726 return false;
6727 }
6728
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006729 private boolean nextAtGranularity(int granularity) {
6730 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006731 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006732 return false;
6733 }
6734 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6735 if (iterator == null) {
6736 return false;
6737 }
6738 final int current = getAccessibilityCursorPosition();
6739 final int[] range = iterator.following(current);
6740 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006741 return false;
6742 }
6743 final int start = range[0];
6744 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006745 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006746 sendViewTextTraversedAtGranularityEvent(
6747 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6748 granularity, start, end);
6749 return true;
6750 }
6751
6752 private boolean previousAtGranularity(int granularity) {
6753 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006754 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006755 return false;
6756 }
6757 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6758 if (iterator == null) {
6759 return false;
6760 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006761 int current = getAccessibilityCursorPosition();
6762 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6763 current = text.length();
6764 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6765 // When traversing by character we always put the cursor after the character
6766 // to ease edit and have to compensate before asking the for previous segment.
6767 current--;
6768 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006769 final int[] range = iterator.preceding(current);
6770 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006771 return false;
6772 }
6773 final int start = range[0];
6774 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006775 // Always put the cursor after the character to ease edit.
6776 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6777 setAccessibilityCursorPosition(end);
6778 } else {
6779 setAccessibilityCursorPosition(start);
6780 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006781 sendViewTextTraversedAtGranularityEvent(
6782 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6783 granularity, start, end);
6784 return true;
6785 }
6786
6787 /**
6788 * Gets the text reported for accessibility purposes.
6789 *
6790 * @return The accessibility text.
6791 *
6792 * @hide
6793 */
6794 public CharSequence getIterableTextForAccessibility() {
6795 return mContentDescription;
6796 }
6797
6798 /**
6799 * @hide
6800 */
6801 public int getAccessibilityCursorPosition() {
6802 return mAccessibilityCursorPosition;
6803 }
6804
6805 /**
6806 * @hide
6807 */
6808 public void setAccessibilityCursorPosition(int position) {
6809 mAccessibilityCursorPosition = position;
6810 }
6811
6812 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6813 int fromIndex, int toIndex) {
6814 if (mParent == null) {
6815 return;
6816 }
6817 AccessibilityEvent event = AccessibilityEvent.obtain(
6818 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6819 onInitializeAccessibilityEvent(event);
6820 onPopulateAccessibilityEvent(event);
6821 event.setFromIndex(fromIndex);
6822 event.setToIndex(toIndex);
6823 event.setAction(action);
6824 event.setMovementGranularity(granularity);
6825 mParent.requestSendAccessibilityEvent(this, event);
6826 }
6827
6828 /**
6829 * @hide
6830 */
6831 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6832 switch (granularity) {
6833 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6834 CharSequence text = getIterableTextForAccessibility();
6835 if (text != null && text.length() > 0) {
6836 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006837 CharacterTextSegmentIterator.getInstance(
6838 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006839 iterator.initialize(text.toString());
6840 return iterator;
6841 }
6842 } break;
6843 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
6844 CharSequence text = getIterableTextForAccessibility();
6845 if (text != null && text.length() > 0) {
6846 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006847 WordTextSegmentIterator.getInstance(
6848 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006849 iterator.initialize(text.toString());
6850 return iterator;
6851 }
6852 } break;
6853 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
6854 CharSequence text = getIterableTextForAccessibility();
6855 if (text != null && text.length() > 0) {
6856 ParagraphTextSegmentIterator iterator =
6857 ParagraphTextSegmentIterator.getInstance();
6858 iterator.initialize(text.toString());
6859 return iterator;
6860 }
6861 } break;
6862 }
6863 return null;
6864 }
6865
Svetoslav Ganov42138042012-03-20 11:51:39 -07006866 /**
Romain Guya440b002010-02-24 15:57:54 -08006867 * @hide
6868 */
6869 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07006870 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07006871 clearDisplayList();
6872
Romain Guya440b002010-02-24 15:57:54 -08006873 onStartTemporaryDetach();
6874 }
6875
6876 /**
6877 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6879 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006880 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006881 */
6882 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006883 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07006884 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006885 }
6886
6887 /**
6888 * @hide
6889 */
6890 public void dispatchFinishTemporaryDetach() {
6891 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 }
Romain Guy8506ab42009-06-11 17:35:47 -07006893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 /**
6895 * Called after {@link #onStartTemporaryDetach} when the container is done
6896 * changing the view.
6897 */
6898 public void onFinishTemporaryDetach() {
6899 }
Romain Guy8506ab42009-06-11 17:35:47 -07006900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006902 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6903 * for this view's window. Returns null if the view is not currently attached
6904 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006905 * just use the standard high-level event callbacks like
6906 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006907 */
6908 public KeyEvent.DispatcherState getKeyDispatcherState() {
6909 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6910 }
Joe Malin32736f02011-01-19 16:14:20 -08006911
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 * Dispatch a key event before it is processed by any input method
6914 * associated with the view hierarchy. This can be used to intercept
6915 * key events in special situations before the IME consumes them; a
6916 * typical example would be handling the BACK key to update the application's
6917 * UI instead of allowing the IME to see it and close itself.
6918 *
6919 * @param event The key event to be dispatched.
6920 * @return True if the event was handled, false otherwise.
6921 */
6922 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6923 return onKeyPreIme(event.getKeyCode(), event);
6924 }
6925
6926 /**
6927 * Dispatch a key event to the next view on the focus path. This path runs
6928 * from the top of the view tree down to the currently focused view. If this
6929 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6930 * the next node down the focus path. This method also fires any key
6931 * listeners.
6932 *
6933 * @param event The key event to be dispatched.
6934 * @return True if the event was handled, false otherwise.
6935 */
6936 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006937 if (mInputEventConsistencyVerifier != null) {
6938 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940
Jeff Brown21bc5c92011-02-28 18:27:14 -08006941 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006942 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006943 ListenerInfo li = mListenerInfo;
6944 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6945 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 return true;
6947 }
6948
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006949 if (event.dispatch(this, mAttachInfo != null
6950 ? mAttachInfo.mKeyDispatchState : null, this)) {
6951 return true;
6952 }
6953
6954 if (mInputEventConsistencyVerifier != null) {
6955 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6956 }
6957 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 }
6959
6960 /**
6961 * Dispatches a key shortcut event.
6962 *
6963 * @param event The key event to be dispatched.
6964 * @return True if the event was handled by the view, false otherwise.
6965 */
6966 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6967 return onKeyShortcut(event.getKeyCode(), event);
6968 }
6969
6970 /**
6971 * Pass the touch screen motion event down to the target view, or this
6972 * view if it is the target.
6973 *
6974 * @param event The motion event to be dispatched.
6975 * @return True if the event was handled by the view, false otherwise.
6976 */
6977 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006978 if (mInputEventConsistencyVerifier != null) {
6979 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6980 }
6981
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006982 if (onFilterTouchEventForSecurity(event)) {
6983 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006984 ListenerInfo li = mListenerInfo;
6985 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6986 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006987 return true;
6988 }
6989
6990 if (onTouchEvent(event)) {
6991 return true;
6992 }
Jeff Brown85a31762010-09-01 17:01:00 -07006993 }
6994
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006995 if (mInputEventConsistencyVerifier != null) {
6996 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006997 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006998 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 }
7000
7001 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007002 * Filter the touch event to apply security policies.
7003 *
7004 * @param event The motion event to be filtered.
7005 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007006 *
Jeff Brown85a31762010-09-01 17:01:00 -07007007 * @see #getFilterTouchesWhenObscured
7008 */
7009 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007010 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007011 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7012 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7013 // Window is obscured, drop this touch.
7014 return false;
7015 }
7016 return true;
7017 }
7018
7019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020 * Pass a trackball motion event down to the focused view.
7021 *
7022 * @param event The motion event to be dispatched.
7023 * @return True if the event was handled by the view, false otherwise.
7024 */
7025 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007026 if (mInputEventConsistencyVerifier != null) {
7027 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7028 }
7029
Romain Guy02ccac62011-06-24 13:20:23 -07007030 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032
7033 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007034 * Dispatch a generic motion event.
7035 * <p>
7036 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7037 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007038 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007039 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007040 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007041 *
7042 * @param event The motion event to be dispatched.
7043 * @return True if the event was handled by the view, false otherwise.
7044 */
7045 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007046 if (mInputEventConsistencyVerifier != null) {
7047 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7048 }
7049
Jeff Browna032cc02011-03-07 16:56:21 -08007050 final int source = event.getSource();
7051 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7052 final int action = event.getAction();
7053 if (action == MotionEvent.ACTION_HOVER_ENTER
7054 || action == MotionEvent.ACTION_HOVER_MOVE
7055 || action == MotionEvent.ACTION_HOVER_EXIT) {
7056 if (dispatchHoverEvent(event)) {
7057 return true;
7058 }
7059 } else if (dispatchGenericPointerEvent(event)) {
7060 return true;
7061 }
7062 } else if (dispatchGenericFocusedEvent(event)) {
7063 return true;
7064 }
7065
Jeff Brown10b62902011-06-20 16:40:37 -07007066 if (dispatchGenericMotionEventInternal(event)) {
7067 return true;
7068 }
7069
7070 if (mInputEventConsistencyVerifier != null) {
7071 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7072 }
7073 return false;
7074 }
7075
7076 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007077 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007078 ListenerInfo li = mListenerInfo;
7079 if (li != null && li.mOnGenericMotionListener != null
7080 && (mViewFlags & ENABLED_MASK) == ENABLED
7081 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007082 return true;
7083 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007084
7085 if (onGenericMotionEvent(event)) {
7086 return true;
7087 }
7088
7089 if (mInputEventConsistencyVerifier != null) {
7090 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7091 }
7092 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007093 }
7094
7095 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007096 * Dispatch a hover event.
7097 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007098 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007099 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007100 * </p>
7101 *
7102 * @param event The motion event to be dispatched.
7103 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007104 */
7105 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007106 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007107 ListenerInfo li = mListenerInfo;
7108 if (li != null && li.mOnHoverListener != null
7109 && (mViewFlags & ENABLED_MASK) == ENABLED
7110 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007111 return true;
7112 }
7113
Jeff Browna032cc02011-03-07 16:56:21 -08007114 return onHoverEvent(event);
7115 }
7116
7117 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007118 * Returns true if the view has a child to which it has recently sent
7119 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7120 * it does not have a hovered child, then it must be the innermost hovered view.
7121 * @hide
7122 */
7123 protected boolean hasHoveredChild() {
7124 return false;
7125 }
7126
7127 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007128 * Dispatch a generic motion event to the view under the first pointer.
7129 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007130 * Do not call this method directly.
7131 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007132 * </p>
7133 *
7134 * @param event The motion event to be dispatched.
7135 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007136 */
7137 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7138 return false;
7139 }
7140
7141 /**
7142 * Dispatch a generic motion event to the currently focused view.
7143 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007144 * Do not call this method directly.
7145 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007146 * </p>
7147 *
7148 * @param event The motion event to be dispatched.
7149 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007150 */
7151 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7152 return false;
7153 }
7154
7155 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007156 * Dispatch a pointer event.
7157 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007158 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7159 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7160 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007161 * and should not be expected to handle other pointing device features.
7162 * </p>
7163 *
7164 * @param event The motion event to be dispatched.
7165 * @return True if the event was handled by the view, false otherwise.
7166 * @hide
7167 */
7168 public final boolean dispatchPointerEvent(MotionEvent event) {
7169 if (event.isTouchEvent()) {
7170 return dispatchTouchEvent(event);
7171 } else {
7172 return dispatchGenericMotionEvent(event);
7173 }
7174 }
7175
7176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 * Called when the window containing this view gains or loses window focus.
7178 * ViewGroups should override to route to their children.
7179 *
7180 * @param hasFocus True if the window containing this view now has focus,
7181 * false otherwise.
7182 */
7183 public void dispatchWindowFocusChanged(boolean hasFocus) {
7184 onWindowFocusChanged(hasFocus);
7185 }
7186
7187 /**
7188 * Called when the window containing this view gains or loses focus. Note
7189 * that this is separate from view focus: to receive key events, both
7190 * your view and its window must have focus. If a window is displayed
7191 * on top of yours that takes input focus, then your own window will lose
7192 * focus but the view focus will remain unchanged.
7193 *
7194 * @param hasWindowFocus True if the window containing this view now has
7195 * focus, false otherwise.
7196 */
7197 public void onWindowFocusChanged(boolean hasWindowFocus) {
7198 InputMethodManager imm = InputMethodManager.peekInstance();
7199 if (!hasWindowFocus) {
7200 if (isPressed()) {
7201 setPressed(false);
7202 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007203 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 imm.focusOut(this);
7205 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007206 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007207 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007208 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007209 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 imm.focusIn(this);
7211 }
7212 refreshDrawableState();
7213 }
7214
7215 /**
7216 * Returns true if this view is in a window that currently has window focus.
7217 * Note that this is not the same as the view itself having focus.
7218 *
7219 * @return True if this view is in a window that currently has window focus.
7220 */
7221 public boolean hasWindowFocus() {
7222 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7223 }
7224
7225 /**
Adam Powell326d8082009-12-09 15:10:07 -08007226 * Dispatch a view visibility change down the view hierarchy.
7227 * ViewGroups should override to route to their children.
7228 * @param changedView The view whose visibility changed. Could be 'this' or
7229 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007230 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7231 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007232 */
7233 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7234 onVisibilityChanged(changedView, visibility);
7235 }
7236
7237 /**
7238 * Called when the visibility of the view or an ancestor of the view is changed.
7239 * @param changedView The view whose visibility changed. Could be 'this' or
7240 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007241 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7242 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007243 */
7244 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007245 if (visibility == VISIBLE) {
7246 if (mAttachInfo != null) {
7247 initialAwakenScrollBars();
7248 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007249 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007250 }
7251 }
Adam Powell326d8082009-12-09 15:10:07 -08007252 }
7253
7254 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007255 * Dispatch a hint about whether this view is displayed. For instance, when
7256 * a View moves out of the screen, it might receives a display hint indicating
7257 * the view is not displayed. Applications should not <em>rely</em> on this hint
7258 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007259 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007260 * @param hint A hint about whether or not this view is displayed:
7261 * {@link #VISIBLE} or {@link #INVISIBLE}.
7262 */
7263 public void dispatchDisplayHint(int hint) {
7264 onDisplayHint(hint);
7265 }
7266
7267 /**
7268 * Gives this view a hint about whether is displayed or not. For instance, when
7269 * a View moves out of the screen, it might receives a display hint indicating
7270 * the view is not displayed. Applications should not <em>rely</em> on this hint
7271 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007272 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007273 * @param hint A hint about whether or not this view is displayed:
7274 * {@link #VISIBLE} or {@link #INVISIBLE}.
7275 */
7276 protected void onDisplayHint(int hint) {
7277 }
7278
7279 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 * Dispatch a window visibility change down the view hierarchy.
7281 * ViewGroups should override to route to their children.
7282 *
7283 * @param visibility The new visibility of the window.
7284 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007285 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 */
7287 public void dispatchWindowVisibilityChanged(int visibility) {
7288 onWindowVisibilityChanged(visibility);
7289 }
7290
7291 /**
7292 * Called when the window containing has change its visibility
7293 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7294 * that this tells you whether or not your window is being made visible
7295 * to the window manager; this does <em>not</em> tell you whether or not
7296 * your window is obscured by other windows on the screen, even if it
7297 * is itself visible.
7298 *
7299 * @param visibility The new visibility of the window.
7300 */
7301 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007302 if (visibility == VISIBLE) {
7303 initialAwakenScrollBars();
7304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 }
7306
7307 /**
7308 * Returns the current visibility of the window this view is attached to
7309 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7310 *
7311 * @return Returns the current visibility of the view's window.
7312 */
7313 public int getWindowVisibility() {
7314 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7315 }
7316
7317 /**
7318 * Retrieve the overall visible display size in which the window this view is
7319 * attached to has been positioned in. This takes into account screen
7320 * decorations above the window, for both cases where the window itself
7321 * is being position inside of them or the window is being placed under
7322 * then and covered insets are used for the window to position its content
7323 * inside. In effect, this tells you the available area where content can
7324 * be placed and remain visible to users.
7325 *
7326 * <p>This function requires an IPC back to the window manager to retrieve
7327 * the requested information, so should not be used in performance critical
7328 * code like drawing.
7329 *
7330 * @param outRect Filled in with the visible display frame. If the view
7331 * is not attached to a window, this is simply the raw display size.
7332 */
7333 public void getWindowVisibleDisplayFrame(Rect outRect) {
7334 if (mAttachInfo != null) {
7335 try {
7336 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7337 } catch (RemoteException e) {
7338 return;
7339 }
7340 // XXX This is really broken, and probably all needs to be done
7341 // in the window manager, and we need to know more about whether
7342 // we want the area behind or in front of the IME.
7343 final Rect insets = mAttachInfo.mVisibleInsets;
7344 outRect.left += insets.left;
7345 outRect.top += insets.top;
7346 outRect.right -= insets.right;
7347 outRect.bottom -= insets.bottom;
7348 return;
7349 }
Jeff Brown98365d72012-08-19 20:30:52 -07007350 Display d = DisplayManager.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007351 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352 }
7353
7354 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007355 * Dispatch a notification about a resource configuration change down
7356 * the view hierarchy.
7357 * ViewGroups should override to route to their children.
7358 *
7359 * @param newConfig The new resource configuration.
7360 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007361 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007362 */
7363 public void dispatchConfigurationChanged(Configuration newConfig) {
7364 onConfigurationChanged(newConfig);
7365 }
7366
7367 /**
7368 * Called when the current configuration of the resources being used
7369 * by the application have changed. You can use this to decide when
7370 * to reload resources that can changed based on orientation and other
7371 * configuration characterstics. You only need to use this if you are
7372 * not relying on the normal {@link android.app.Activity} mechanism of
7373 * recreating the activity instance upon a configuration change.
7374 *
7375 * @param newConfig The new resource configuration.
7376 */
7377 protected void onConfigurationChanged(Configuration newConfig) {
7378 }
7379
7380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007381 * Private function to aggregate all per-view attributes in to the view
7382 * root.
7383 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007384 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7385 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 }
7387
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007388 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7389 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007390 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007391 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007392 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007393 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007394 ListenerInfo li = mListenerInfo;
7395 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007396 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 }
7399 }
7400
7401 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007402 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007404 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7405 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 ai.mRecomputeGlobalAttributes = true;
7407 }
7408 }
7409 }
7410
7411 /**
7412 * Returns whether the device is currently in touch mode. Touch mode is entered
7413 * once the user begins interacting with the device by touch, and affects various
7414 * things like whether focus is always visible to the user.
7415 *
7416 * @return Whether the device is in touch mode.
7417 */
7418 @ViewDebug.ExportedProperty
7419 public boolean isInTouchMode() {
7420 if (mAttachInfo != null) {
7421 return mAttachInfo.mInTouchMode;
7422 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007423 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 }
7425 }
7426
7427 /**
7428 * Returns the context the view is running in, through which it can
7429 * access the current theme, resources, etc.
7430 *
7431 * @return The view's Context.
7432 */
7433 @ViewDebug.CapturedViewProperty
7434 public final Context getContext() {
7435 return mContext;
7436 }
7437
7438 /**
7439 * Handle a key event before it is processed by any input method
7440 * associated with the view hierarchy. This can be used to intercept
7441 * key events in special situations before the IME consumes them; a
7442 * typical example would be handling the BACK key to update the application's
7443 * UI instead of allowing the IME to see it and close itself.
7444 *
7445 * @param keyCode The value in event.getKeyCode().
7446 * @param event Description of the key event.
7447 * @return If you handled the event, return true. If you want to allow the
7448 * event to be handled by the next receiver, return false.
7449 */
7450 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7451 return false;
7452 }
7453
7454 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007455 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7456 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7458 * is released, if the view is enabled and clickable.
7459 *
Jean Chalard405bc512012-05-29 19:12:34 +09007460 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7461 * although some may elect to do so in some situations. Do not rely on this to
7462 * catch software key presses.
7463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 * @param keyCode A key code that represents the button pressed, from
7465 * {@link android.view.KeyEvent}.
7466 * @param event The KeyEvent object that defines the button action.
7467 */
7468 public boolean onKeyDown(int keyCode, KeyEvent event) {
7469 boolean result = false;
7470
7471 switch (keyCode) {
7472 case KeyEvent.KEYCODE_DPAD_CENTER:
7473 case KeyEvent.KEYCODE_ENTER: {
7474 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7475 return true;
7476 }
7477 // Long clickable items don't necessarily have to be clickable
7478 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7479 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7480 (event.getRepeatCount() == 0)) {
7481 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007482 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007483 return true;
7484 }
7485 break;
7486 }
7487 }
7488 return result;
7489 }
7490
7491 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007492 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7493 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7494 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007495 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7496 * although some may elect to do so in some situations. Do not rely on this to
7497 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007498 */
7499 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7500 return false;
7501 }
7502
7503 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007504 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7505 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7507 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007508 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7509 * although some may elect to do so in some situations. Do not rely on this to
7510 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007511 *
7512 * @param keyCode A key code that represents the button pressed, from
7513 * {@link android.view.KeyEvent}.
7514 * @param event The KeyEvent object that defines the button action.
7515 */
7516 public boolean onKeyUp(int keyCode, KeyEvent event) {
7517 boolean result = false;
7518
7519 switch (keyCode) {
7520 case KeyEvent.KEYCODE_DPAD_CENTER:
7521 case KeyEvent.KEYCODE_ENTER: {
7522 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7523 return true;
7524 }
7525 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7526 setPressed(false);
7527
7528 if (!mHasPerformedLongPress) {
7529 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007530 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007531
7532 result = performClick();
7533 }
7534 }
7535 break;
7536 }
7537 }
7538 return result;
7539 }
7540
7541 /**
7542 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7543 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7544 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007545 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7546 * although some may elect to do so in some situations. Do not rely on this to
7547 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 *
7549 * @param keyCode A key code that represents the button pressed, from
7550 * {@link android.view.KeyEvent}.
7551 * @param repeatCount The number of times the action was made.
7552 * @param event The KeyEvent object that defines the button action.
7553 */
7554 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7555 return false;
7556 }
7557
7558 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007559 * Called on the focused view when a key shortcut event is not handled.
7560 * Override this method to implement local key shortcuts for the View.
7561 * Key shortcuts can also be implemented by setting the
7562 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563 *
7564 * @param keyCode The value in event.getKeyCode().
7565 * @param event Description of the key event.
7566 * @return If you handled the event, return true. If you want to allow the
7567 * event to be handled by the next receiver, return false.
7568 */
7569 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7570 return false;
7571 }
7572
7573 /**
7574 * Check whether the called view is a text editor, in which case it
7575 * would make sense to automatically display a soft input window for
7576 * it. Subclasses should override this if they implement
7577 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007578 * a call on that method would return a non-null InputConnection, and
7579 * they are really a first-class editor that the user would normally
7580 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007581 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007582 * <p>The default implementation always returns false. This does
7583 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7584 * will not be called or the user can not otherwise perform edits on your
7585 * view; it is just a hint to the system that this is not the primary
7586 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007587 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 * @return Returns true if this view is a text editor, else false.
7589 */
7590 public boolean onCheckIsTextEditor() {
7591 return false;
7592 }
Romain Guy8506ab42009-06-11 17:35:47 -07007593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 /**
7595 * Create a new InputConnection for an InputMethod to interact
7596 * with the view. The default implementation returns null, since it doesn't
7597 * support input methods. You can override this to implement such support.
7598 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007600 * <p>When implementing this, you probably also want to implement
7601 * {@link #onCheckIsTextEditor()} to indicate you will return a
7602 * non-null InputConnection.
7603 *
7604 * @param outAttrs Fill in with attribute information about the connection.
7605 */
7606 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7607 return null;
7608 }
7609
7610 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007611 * Called by the {@link android.view.inputmethod.InputMethodManager}
7612 * when a view who is not the current
7613 * input connection target is trying to make a call on the manager. The
7614 * default implementation returns false; you can override this to return
7615 * true for certain views if you are performing InputConnection proxying
7616 * to them.
7617 * @param view The View that is making the InputMethodManager call.
7618 * @return Return true to allow the call, false to reject.
7619 */
7620 public boolean checkInputConnectionProxy(View view) {
7621 return false;
7622 }
Romain Guy8506ab42009-06-11 17:35:47 -07007623
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007624 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 * Show the context menu for this view. It is not safe to hold on to the
7626 * menu after returning from this method.
7627 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007628 * You should normally not overload this method. Overload
7629 * {@link #onCreateContextMenu(ContextMenu)} or define an
7630 * {@link OnCreateContextMenuListener} to add items to the context menu.
7631 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 * @param menu The context menu to populate
7633 */
7634 public void createContextMenu(ContextMenu menu) {
7635 ContextMenuInfo menuInfo = getContextMenuInfo();
7636
7637 // Sets the current menu info so all items added to menu will have
7638 // my extra info set.
7639 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7640
7641 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007642 ListenerInfo li = mListenerInfo;
7643 if (li != null && li.mOnCreateContextMenuListener != null) {
7644 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 }
7646
7647 // Clear the extra information so subsequent items that aren't mine don't
7648 // have my extra info.
7649 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7650
7651 if (mParent != null) {
7652 mParent.createContextMenu(menu);
7653 }
7654 }
7655
7656 /**
7657 * Views should implement this if they have extra information to associate
7658 * with the context menu. The return result is supplied as a parameter to
7659 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7660 * callback.
7661 *
7662 * @return Extra information about the item for which the context menu
7663 * should be shown. This information will vary across different
7664 * subclasses of View.
7665 */
7666 protected ContextMenuInfo getContextMenuInfo() {
7667 return null;
7668 }
7669
7670 /**
7671 * Views should implement this if the view itself is going to add items to
7672 * the context menu.
7673 *
7674 * @param menu the context menu to populate
7675 */
7676 protected void onCreateContextMenu(ContextMenu menu) {
7677 }
7678
7679 /**
7680 * Implement this method to handle trackball motion events. The
7681 * <em>relative</em> movement of the trackball since the last event
7682 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7683 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7684 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7685 * they will often be fractional values, representing the more fine-grained
7686 * movement information available from a trackball).
7687 *
7688 * @param event The motion event.
7689 * @return True if the event was handled, false otherwise.
7690 */
7691 public boolean onTrackballEvent(MotionEvent event) {
7692 return false;
7693 }
7694
7695 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007696 * Implement this method to handle generic motion events.
7697 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007698 * Generic motion events describe joystick movements, mouse hovers, track pad
7699 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007700 * {@link MotionEvent#getSource() source} of the motion event specifies
7701 * the class of input that was received. Implementations of this method
7702 * must examine the bits in the source before processing the event.
7703 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007704 * </p><p>
7705 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7706 * are delivered to the view under the pointer. All other generic motion events are
7707 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007708 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007709 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007710 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007711 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7712 * // process the joystick movement...
7713 * return true;
7714 * }
7715 * }
7716 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7717 * switch (event.getAction()) {
7718 * case MotionEvent.ACTION_HOVER_MOVE:
7719 * // process the mouse hover movement...
7720 * return true;
7721 * case MotionEvent.ACTION_SCROLL:
7722 * // process the scroll wheel movement...
7723 * return true;
7724 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007725 * }
7726 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007727 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007728 *
7729 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007730 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007731 */
7732 public boolean onGenericMotionEvent(MotionEvent event) {
7733 return false;
7734 }
7735
7736 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007737 * Implement this method to handle hover events.
7738 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007739 * This method is called whenever a pointer is hovering into, over, or out of the
7740 * bounds of a view and the view is not currently being touched.
7741 * Hover events are represented as pointer events with action
7742 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7743 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7744 * </p>
7745 * <ul>
7746 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7747 * when the pointer enters the bounds of the view.</li>
7748 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7749 * when the pointer has already entered the bounds of the view and has moved.</li>
7750 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7751 * when the pointer has exited the bounds of the view or when the pointer is
7752 * about to go down due to a button click, tap, or similar user action that
7753 * causes the view to be touched.</li>
7754 * </ul>
7755 * <p>
7756 * The view should implement this method to return true to indicate that it is
7757 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007758 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007759 * The default implementation calls {@link #setHovered} to update the hovered state
7760 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007761 * is enabled and is clickable. The default implementation also sends hover
7762 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007763 * </p>
7764 *
7765 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007766 * @return True if the view handled the hover event.
7767 *
7768 * @see #isHovered
7769 * @see #setHovered
7770 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007771 */
7772 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007773 // The root view may receive hover (or touch) events that are outside the bounds of
7774 // the window. This code ensures that we only send accessibility events for
7775 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007776 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007777 if (!mSendingHoverAccessibilityEvents) {
7778 if ((action == MotionEvent.ACTION_HOVER_ENTER
7779 || action == MotionEvent.ACTION_HOVER_MOVE)
7780 && !hasHoveredChild()
7781 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007782 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007783 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007784 }
7785 } else {
7786 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007787 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007788 && !pointInView(event.getX(), event.getY()))) {
7789 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007790 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007791 // If the window does not have input focus we take away accessibility
7792 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007793 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007794 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007795 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007796 }
Jeff Browna1b24182011-07-28 13:38:24 -07007797 }
7798
Jeff Brown87b7f802011-06-21 18:35:45 -07007799 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007800 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007801 case MotionEvent.ACTION_HOVER_ENTER:
7802 setHovered(true);
7803 break;
7804 case MotionEvent.ACTION_HOVER_EXIT:
7805 setHovered(false);
7806 break;
7807 }
Jeff Browna1b24182011-07-28 13:38:24 -07007808
7809 // Dispatch the event to onGenericMotionEvent before returning true.
7810 // This is to provide compatibility with existing applications that
7811 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7812 // break because of the new default handling for hoverable views
7813 // in onHoverEvent.
7814 // Note that onGenericMotionEvent will be called by default when
7815 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7816 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007817 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007818 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007819
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007820 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007821 }
7822
7823 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007824 * Returns true if the view should handle {@link #onHoverEvent}
7825 * by calling {@link #setHovered} to change its hovered state.
7826 *
7827 * @return True if the view is hoverable.
7828 */
7829 private boolean isHoverable() {
7830 final int viewFlags = mViewFlags;
7831 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7832 return false;
7833 }
7834
7835 return (viewFlags & CLICKABLE) == CLICKABLE
7836 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7837 }
7838
7839 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007840 * Returns true if the view is currently hovered.
7841 *
7842 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007843 *
7844 * @see #setHovered
7845 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007846 */
Jeff Brown10b62902011-06-20 16:40:37 -07007847 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007848 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007849 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08007850 }
7851
7852 /**
7853 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007854 * <p>
7855 * Calling this method also changes the drawable state of the view. This
7856 * enables the view to react to hover by using different drawable resources
7857 * to change its appearance.
7858 * </p><p>
7859 * The {@link #onHoverChanged} method is called when the hovered state changes.
7860 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007861 *
7862 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007863 *
7864 * @see #isHovered
7865 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007866 */
7867 public void setHovered(boolean hovered) {
7868 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007869 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
7870 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007871 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007872 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007873 }
7874 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007875 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
7876 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08007877 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007878 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007879 }
7880 }
7881 }
7882
7883 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007884 * Implement this method to handle hover state changes.
7885 * <p>
7886 * This method is called whenever the hover state changes as a result of a
7887 * call to {@link #setHovered}.
7888 * </p>
7889 *
7890 * @param hovered The current hover state, as returned by {@link #isHovered}.
7891 *
7892 * @see #isHovered
7893 * @see #setHovered
7894 */
7895 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007896 }
7897
7898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 * Implement this method to handle touch screen motion events.
7900 *
7901 * @param event The motion event.
7902 * @return True if the event was handled, false otherwise.
7903 */
7904 public boolean onTouchEvent(MotionEvent event) {
7905 final int viewFlags = mViewFlags;
7906
7907 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007908 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007909 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007911 // A disabled view that is clickable still consumes the touch
7912 // events, it just doesn't respond to them.
7913 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7914 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7915 }
7916
7917 if (mTouchDelegate != null) {
7918 if (mTouchDelegate.onTouchEvent(event)) {
7919 return true;
7920 }
7921 }
7922
7923 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7924 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7925 switch (event.getAction()) {
7926 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07007927 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
7928 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 // take focus if we don't have it already and we should in
7930 // touch mode.
7931 boolean focusTaken = false;
7932 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7933 focusTaken = requestFocus();
7934 }
7935
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007936 if (prepressed) {
7937 // The button is being released before we actually
7938 // showed it as pressed. Make it show the pressed
7939 // state now (before scheduling the click) to ensure
7940 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007941 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007942 }
Joe Malin32736f02011-01-19 16:14:20 -08007943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 if (!mHasPerformedLongPress) {
7945 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007946 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947
7948 // Only perform take click actions if we were in the pressed state
7949 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007950 // Use a Runnable and post this rather than calling
7951 // performClick directly. This lets other visual state
7952 // of the view update before click actions start.
7953 if (mPerformClick == null) {
7954 mPerformClick = new PerformClick();
7955 }
7956 if (!post(mPerformClick)) {
7957 performClick();
7958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007959 }
7960 }
7961
7962 if (mUnsetPressedState == null) {
7963 mUnsetPressedState = new UnsetPressedState();
7964 }
7965
Adam Powelle14579b2009-12-16 18:39:52 -08007966 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007967 postDelayed(mUnsetPressedState,
7968 ViewConfiguration.getPressedStateDuration());
7969 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 // If the post failed, unpress right now
7971 mUnsetPressedState.run();
7972 }
Adam Powelle14579b2009-12-16 18:39:52 -08007973 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 }
7975 break;
7976
7977 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007978 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007979
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007980 if (performButtonActionOnTouchDown(event)) {
7981 break;
7982 }
7983
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007984 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007985 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007986
7987 // For views inside a scrolling container, delay the pressed feedback for
7988 // a short period in case this is a scroll.
7989 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007990 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007991 if (mPendingCheckForTap == null) {
7992 mPendingCheckForTap = new CheckForTap();
7993 }
7994 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7995 } else {
7996 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007997 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007998 checkForLongClick(0);
7999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 break;
8001
8002 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008003 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008004 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008005 break;
8006
8007 case MotionEvent.ACTION_MOVE:
8008 final int x = (int) event.getX();
8009 final int y = (int) event.getY();
8010
8011 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008012 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008014 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008015 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008016 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008017 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008018
Adam Powell4d6f0662012-02-21 15:11:11 -08008019 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 }
8022 break;
8023 }
8024 return true;
8025 }
8026
8027 return false;
8028 }
8029
8030 /**
Adam Powell10298662011-08-14 18:26:30 -07008031 * @hide
8032 */
8033 public boolean isInScrollingContainer() {
8034 ViewParent p = getParent();
8035 while (p != null && p instanceof ViewGroup) {
8036 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8037 return true;
8038 }
8039 p = p.getParent();
8040 }
8041 return false;
8042 }
8043
8044 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008045 * Remove the longpress detection timer.
8046 */
8047 private void removeLongPressCallback() {
8048 if (mPendingCheckForLongPress != null) {
8049 removeCallbacks(mPendingCheckForLongPress);
8050 }
8051 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008052
8053 /**
8054 * Remove the pending click action
8055 */
8056 private void removePerformClickCallback() {
8057 if (mPerformClick != null) {
8058 removeCallbacks(mPerformClick);
8059 }
8060 }
8061
Adam Powelle14579b2009-12-16 18:39:52 -08008062 /**
Romain Guya440b002010-02-24 15:57:54 -08008063 * Remove the prepress detection timer.
8064 */
8065 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008066 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008067 setPressed(false);
8068 removeCallbacks(mUnsetPressedState);
8069 }
8070 }
8071
8072 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008073 * Remove the tap detection timer.
8074 */
8075 private void removeTapCallback() {
8076 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008077 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008078 removeCallbacks(mPendingCheckForTap);
8079 }
8080 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008081
8082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 * Cancels a pending long press. Your subclass can use this if you
8084 * want the context menu to come up if the user presses and holds
8085 * at the same place, but you don't want it to come up if they press
8086 * and then move around enough to cause scrolling.
8087 */
8088 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008089 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008090
8091 /*
8092 * The prepressed state handled by the tap callback is a display
8093 * construct, but the tap callback will post a long press callback
8094 * less its own timeout. Remove it here.
8095 */
8096 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008097 }
8098
8099 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008100 * Remove the pending callback for sending a
8101 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8102 */
8103 private void removeSendViewScrolledAccessibilityEventCallback() {
8104 if (mSendViewScrolledAccessibilityEvent != null) {
8105 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008106 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008107 }
8108 }
8109
8110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 * Sets the TouchDelegate for this View.
8112 */
8113 public void setTouchDelegate(TouchDelegate delegate) {
8114 mTouchDelegate = delegate;
8115 }
8116
8117 /**
8118 * Gets the TouchDelegate for this View.
8119 */
8120 public TouchDelegate getTouchDelegate() {
8121 return mTouchDelegate;
8122 }
8123
8124 /**
8125 * Set flags controlling behavior of this view.
8126 *
8127 * @param flags Constant indicating the value which should be set
8128 * @param mask Constant indicating the bit range that should be changed
8129 */
8130 void setFlags(int flags, int mask) {
8131 int old = mViewFlags;
8132 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8133
8134 int changed = mViewFlags ^ old;
8135 if (changed == 0) {
8136 return;
8137 }
8138 int privateFlags = mPrivateFlags;
8139
8140 /* Check if the FOCUSABLE bit has changed */
8141 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008142 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008144 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 /* Give up focus if we are no longer focusable */
8146 clearFocus();
8147 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008148 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 /*
8150 * Tell the view system that we are now available to take focus
8151 * if no one else already has it.
8152 */
8153 if (mParent != null) mParent.focusableViewAvailable(this);
8154 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008155 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8156 notifyAccessibilityStateChanged();
8157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 }
8159
8160 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8161 if ((changed & VISIBILITY_MASK) != 0) {
8162 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008163 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008164 * it was not visible. Marking it drawn ensures that the invalidation will
8165 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008167 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008168 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169
8170 needGlobalAttributesUpdate(true);
8171
8172 // a view becoming visible is worth notifying the parent
8173 // about in case nothing has focus. even if this specific view
8174 // isn't focusable, it may contain something that is, so let
8175 // the root view try to give this focus if nothing else does.
8176 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8177 mParent.focusableViewAvailable(this);
8178 }
8179 }
8180 }
8181
8182 /* Check if the GONE bit has changed */
8183 if ((changed & GONE) != 0) {
8184 needGlobalAttributesUpdate(false);
8185 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186
Romain Guyecd80ee2009-12-03 17:13:02 -08008187 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8188 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008189 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008190 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008191 if (mParent instanceof View) {
8192 // GONE views noop invalidation, so invalidate the parent
8193 ((View) mParent).invalidate(true);
8194 }
8195 // Mark the view drawn to ensure that it gets invalidated properly the next
8196 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008197 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 }
8199 if (mAttachInfo != null) {
8200 mAttachInfo.mViewVisibilityChanged = true;
8201 }
8202 }
8203
8204 /* Check if the VISIBLE bit has changed */
8205 if ((changed & INVISIBLE) != 0) {
8206 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008207 /*
8208 * If this view is becoming invisible, set the DRAWN flag so that
8209 * the next invalidate() will not be skipped.
8210 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008211 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212
8213 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008214 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 if (getRootView() != this) {
8216 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008217 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218 }
8219 }
8220 if (mAttachInfo != null) {
8221 mAttachInfo.mViewVisibilityChanged = true;
8222 }
8223 }
8224
Adam Powell326d8082009-12-09 15:10:07 -08008225 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008226 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008227 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8228 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008229 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008230 } else if (mParent != null) {
8231 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008232 }
Adam Powell326d8082009-12-09 15:10:07 -08008233 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8234 }
8235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8237 destroyDrawingCache();
8238 }
8239
8240 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8241 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008242 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008243 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008244 }
8245
8246 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8247 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008248 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008249 }
8250
8251 if ((changed & DRAW_MASK) != 0) {
8252 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008253 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008254 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8255 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008257 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 }
8259 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008260 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
8262 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008263 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 }
8265
8266 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008267 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 mParent.recomputeViewAttributes(this);
8269 }
8270 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008271
8272 if (AccessibilityManager.getInstance(mContext).isEnabled()
8273 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8274 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8275 notifyAccessibilityStateChanged();
8276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 }
8278
8279 /**
8280 * Change the view's z order in the tree, so it's on top of other sibling
8281 * views
8282 */
8283 public void bringToFront() {
8284 if (mParent != null) {
8285 mParent.bringChildToFront(this);
8286 }
8287 }
8288
8289 /**
8290 * This is called in response to an internal scroll in this view (i.e., the
8291 * view scrolled its own contents). This is typically as a result of
8292 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8293 * called.
8294 *
8295 * @param l Current horizontal scroll origin.
8296 * @param t Current vertical scroll origin.
8297 * @param oldl Previous horizontal scroll origin.
8298 * @param oldt Previous vertical scroll origin.
8299 */
8300 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008301 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8302 postSendViewScrolledAccessibilityEventCallback();
8303 }
8304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 mBackgroundSizeChanged = true;
8306
8307 final AttachInfo ai = mAttachInfo;
8308 if (ai != null) {
8309 ai.mViewScrollChanged = true;
8310 }
8311 }
8312
8313 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008314 * Interface definition for a callback to be invoked when the layout bounds of a view
8315 * changes due to layout processing.
8316 */
8317 public interface OnLayoutChangeListener {
8318 /**
8319 * Called when the focus state of a view has changed.
8320 *
8321 * @param v The view whose state has changed.
8322 * @param left The new value of the view's left property.
8323 * @param top The new value of the view's top property.
8324 * @param right The new value of the view's right property.
8325 * @param bottom The new value of the view's bottom property.
8326 * @param oldLeft The previous value of the view's left property.
8327 * @param oldTop The previous value of the view's top property.
8328 * @param oldRight The previous value of the view's right property.
8329 * @param oldBottom The previous value of the view's bottom property.
8330 */
8331 void onLayoutChange(View v, int left, int top, int right, int bottom,
8332 int oldLeft, int oldTop, int oldRight, int oldBottom);
8333 }
8334
8335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008336 * This is called during layout when the size of this view has changed. If
8337 * you were just added to the view hierarchy, you're called with the old
8338 * values of 0.
8339 *
8340 * @param w Current width of this view.
8341 * @param h Current height of this view.
8342 * @param oldw Old width of this view.
8343 * @param oldh Old height of this view.
8344 */
8345 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8346 }
8347
8348 /**
8349 * Called by draw to draw the child views. This may be overridden
8350 * by derived classes to gain control just before its children are drawn
8351 * (but after its own view has been drawn).
8352 * @param canvas the canvas on which to draw the view
8353 */
8354 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008356 }
8357
8358 /**
8359 * Gets the parent of this view. Note that the parent is a
8360 * ViewParent and not necessarily a View.
8361 *
8362 * @return Parent of this view.
8363 */
8364 public final ViewParent getParent() {
8365 return mParent;
8366 }
8367
8368 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008369 * Set the horizontal scrolled position of your view. This will cause a call to
8370 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8371 * invalidated.
8372 * @param value the x position to scroll to
8373 */
8374 public void setScrollX(int value) {
8375 scrollTo(value, mScrollY);
8376 }
8377
8378 /**
8379 * Set the vertical scrolled position of your view. This will cause a call to
8380 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8381 * invalidated.
8382 * @param value the y position to scroll to
8383 */
8384 public void setScrollY(int value) {
8385 scrollTo(mScrollX, value);
8386 }
8387
8388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008389 * Return the scrolled left position of this view. This is the left edge of
8390 * the displayed part of your view. You do not need to draw any pixels
8391 * farther left, since those are outside of the frame of your view on
8392 * screen.
8393 *
8394 * @return The left edge of the displayed part of your view, in pixels.
8395 */
8396 public final int getScrollX() {
8397 return mScrollX;
8398 }
8399
8400 /**
8401 * Return the scrolled top position of this view. This is the top edge of
8402 * the displayed part of your view. You do not need to draw any pixels above
8403 * it, since those are outside of the frame of your view on screen.
8404 *
8405 * @return The top edge of the displayed part of your view, in pixels.
8406 */
8407 public final int getScrollY() {
8408 return mScrollY;
8409 }
8410
8411 /**
8412 * Return the width of the your view.
8413 *
8414 * @return The width of your view, in pixels.
8415 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008416 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 public final int getWidth() {
8418 return mRight - mLeft;
8419 }
8420
8421 /**
8422 * Return the height of your view.
8423 *
8424 * @return The height of your view, in pixels.
8425 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008426 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 public final int getHeight() {
8428 return mBottom - mTop;
8429 }
8430
8431 /**
8432 * Return the visible drawing bounds of your view. Fills in the output
8433 * rectangle with the values from getScrollX(), getScrollY(),
8434 * getWidth(), and getHeight().
8435 *
8436 * @param outRect The (scrolled) drawing bounds of the view.
8437 */
8438 public void getDrawingRect(Rect outRect) {
8439 outRect.left = mScrollX;
8440 outRect.top = mScrollY;
8441 outRect.right = mScrollX + (mRight - mLeft);
8442 outRect.bottom = mScrollY + (mBottom - mTop);
8443 }
8444
8445 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008446 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8447 * raw width component (that is the result is masked by
8448 * {@link #MEASURED_SIZE_MASK}).
8449 *
8450 * @return The raw measured width of this view.
8451 */
8452 public final int getMeasuredWidth() {
8453 return mMeasuredWidth & MEASURED_SIZE_MASK;
8454 }
8455
8456 /**
8457 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008458 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008459 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 * This should be used during measurement and layout calculations only. Use
8461 * {@link #getWidth()} to see how wide a view is after layout.
8462 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008463 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008465 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 return mMeasuredWidth;
8467 }
8468
8469 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008470 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8471 * raw width component (that is the result is masked by
8472 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008474 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008475 */
8476 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008477 return mMeasuredHeight & MEASURED_SIZE_MASK;
8478 }
8479
8480 /**
8481 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008482 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008483 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8484 * This should be used during measurement and layout calculations only. Use
8485 * {@link #getHeight()} to see how wide a view is after layout.
8486 *
8487 * @return The measured width of this view as a bit mask.
8488 */
8489 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 return mMeasuredHeight;
8491 }
8492
8493 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008494 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8495 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8496 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8497 * and the height component is at the shifted bits
8498 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8499 */
8500 public final int getMeasuredState() {
8501 return (mMeasuredWidth&MEASURED_STATE_MASK)
8502 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8503 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8504 }
8505
8506 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008507 * The transform matrix of this view, which is calculated based on the current
8508 * roation, scale, and pivot properties.
8509 *
8510 * @see #getRotation()
8511 * @see #getScaleX()
8512 * @see #getScaleY()
8513 * @see #getPivotX()
8514 * @see #getPivotY()
8515 * @return The current transform matrix for the view
8516 */
8517 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008518 if (mTransformationInfo != null) {
8519 updateMatrix();
8520 return mTransformationInfo.mMatrix;
8521 }
8522 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008523 }
8524
8525 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008526 * Utility function to determine if the value is far enough away from zero to be
8527 * considered non-zero.
8528 * @param value A floating point value to check for zero-ness
8529 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8530 */
8531 private static boolean nonzero(float value) {
8532 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8533 }
8534
8535 /**
Jeff Brown86671742010-09-30 20:00:15 -07008536 * Returns true if the transform matrix is the identity matrix.
8537 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008538 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008539 * @return True if the transform matrix is the identity matrix, false otherwise.
8540 */
Jeff Brown86671742010-09-30 20:00:15 -07008541 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008542 if (mTransformationInfo != null) {
8543 updateMatrix();
8544 return mTransformationInfo.mMatrixIsIdentity;
8545 }
8546 return true;
8547 }
8548
8549 void ensureTransformationInfo() {
8550 if (mTransformationInfo == null) {
8551 mTransformationInfo = new TransformationInfo();
8552 }
Jeff Brown86671742010-09-30 20:00:15 -07008553 }
8554
8555 /**
8556 * Recomputes the transform matrix if necessary.
8557 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008558 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008559 final TransformationInfo info = mTransformationInfo;
8560 if (info == null) {
8561 return;
8562 }
8563 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008564 // transform-related properties have changed since the last time someone
8565 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008566
8567 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008568 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008569 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8570 info.mPrevWidth = mRight - mLeft;
8571 info.mPrevHeight = mBottom - mTop;
8572 info.mPivotX = info.mPrevWidth / 2f;
8573 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008574 }
8575 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008576 info.mMatrix.reset();
8577 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8578 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8579 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8580 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008581 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008582 if (info.mCamera == null) {
8583 info.mCamera = new Camera();
8584 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008585 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008586 info.mCamera.save();
8587 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8588 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8589 info.mCamera.getMatrix(info.matrix3D);
8590 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8591 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8592 info.mPivotY + info.mTranslationY);
8593 info.mMatrix.postConcat(info.matrix3D);
8594 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008595 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008596 info.mMatrixDirty = false;
8597 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8598 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008600 }
8601
8602 /**
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008603 * When searching for a view to focus this rectangle is used when considering if this view is
8604 * a good candidate for receiving focus.
8605 *
8606 * By default, the rectangle is the {@link #getDrawingRect}) of the view.
8607 *
8608 * @param r The rectangle to fill in, in this view's coordinates.
8609 */
8610 public void getFocusRect(Rect r) {
8611 getDrawingRect(r);
8612 }
8613
8614 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008615 * Utility method to retrieve the inverse of the current mMatrix property.
8616 * We cache the matrix to avoid recalculating it when transform properties
8617 * have not changed.
8618 *
8619 * @return The inverse of the current matrix of this view.
8620 */
Jeff Brown86671742010-09-30 20:00:15 -07008621 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008622 final TransformationInfo info = mTransformationInfo;
8623 if (info != null) {
8624 updateMatrix();
8625 if (info.mInverseMatrixDirty) {
8626 if (info.mInverseMatrix == null) {
8627 info.mInverseMatrix = new Matrix();
8628 }
8629 info.mMatrix.invert(info.mInverseMatrix);
8630 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008631 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008632 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008633 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008634 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008635 }
8636
8637 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008638 * Gets the distance along the Z axis from the camera to this view.
8639 *
8640 * @see #setCameraDistance(float)
8641 *
8642 * @return The distance along the Z axis.
8643 */
8644 public float getCameraDistance() {
8645 ensureTransformationInfo();
8646 final float dpi = mResources.getDisplayMetrics().densityDpi;
8647 final TransformationInfo info = mTransformationInfo;
8648 if (info.mCamera == null) {
8649 info.mCamera = new Camera();
8650 info.matrix3D = new Matrix();
8651 }
8652 return -(info.mCamera.getLocationZ() * dpi);
8653 }
8654
8655 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008656 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8657 * views are drawn) from the camera to this view. The camera's distance
8658 * affects 3D transformations, for instance rotations around the X and Y
8659 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008660 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008661 * use a camera distance that's greater than the height (X axis rotation) or
8662 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008663 *
Romain Guya5364ee2011-02-24 14:46:04 -08008664 * <p>The distance of the camera from the view plane can have an affect on the
8665 * perspective distortion of the view when it is rotated around the x or y axis.
8666 * For example, a large distance will result in a large viewing angle, and there
8667 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008668 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008669 * also result in some drawing artifacts if the rotated view ends up partially
8670 * behind the camera (which is why the recommendation is to use a distance at
8671 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008672 *
Romain Guya5364ee2011-02-24 14:46:04 -08008673 * <p>The distance is expressed in "depth pixels." The default distance depends
8674 * on the screen density. For instance, on a medium density display, the
8675 * default distance is 1280. On a high density display, the default distance
8676 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008677 *
Romain Guya5364ee2011-02-24 14:46:04 -08008678 * <p>If you want to specify a distance that leads to visually consistent
8679 * results across various densities, use the following formula:</p>
8680 * <pre>
8681 * float scale = context.getResources().getDisplayMetrics().density;
8682 * view.setCameraDistance(distance * scale);
8683 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008684 *
Romain Guya5364ee2011-02-24 14:46:04 -08008685 * <p>The density scale factor of a high density display is 1.5,
8686 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008687 *
Romain Guya5364ee2011-02-24 14:46:04 -08008688 * @param distance The distance in "depth pixels", if negative the opposite
8689 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008690 *
8691 * @see #setRotationX(float)
8692 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008693 */
8694 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008695 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008696
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008697 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008698 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008699 final TransformationInfo info = mTransformationInfo;
8700 if (info.mCamera == null) {
8701 info.mCamera = new Camera();
8702 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008703 }
8704
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008705 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8706 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008707
Chet Haase9d1992d2012-03-13 11:03:25 -07008708 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008709 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008710 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008711 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008712 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008713 // View was rejected last time it was drawn by its parent; this may have changed
8714 invalidateParentIfNeeded();
8715 }
Romain Guya5364ee2011-02-24 14:46:04 -08008716 }
8717
8718 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008719 * The degrees that the view is rotated around the pivot point.
8720 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008721 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008722 * @see #getPivotX()
8723 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008724 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008725 * @return The degrees of rotation.
8726 */
Chet Haasea5531132012-02-02 13:41:44 -08008727 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008728 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008729 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008730 }
8731
8732 /**
Chet Haase897247b2010-09-09 14:54:47 -07008733 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8734 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008735 *
8736 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008737 *
8738 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008739 * @see #getPivotX()
8740 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008741 * @see #setRotationX(float)
8742 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008743 *
8744 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008745 */
8746 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008747 ensureTransformationInfo();
8748 final TransformationInfo info = mTransformationInfo;
8749 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008750 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008751 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 info.mRotation = rotation;
8753 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008754 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008755 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008756 mDisplayList.setRotation(rotation);
8757 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008758 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008759 // View was rejected last time it was drawn by its parent; this may have changed
8760 invalidateParentIfNeeded();
8761 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008762 }
8763 }
8764
8765 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008766 * The degrees that the view is rotated around the vertical axis through the pivot point.
8767 *
8768 * @see #getPivotX()
8769 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008770 * @see #setRotationY(float)
8771 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008772 * @return The degrees of Y rotation.
8773 */
Chet Haasea5531132012-02-02 13:41:44 -08008774 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008775 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008776 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008777 }
8778
8779 /**
Chet Haase897247b2010-09-09 14:54:47 -07008780 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8781 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8782 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008783 *
Romain Guya5364ee2011-02-24 14:46:04 -08008784 * When rotating large views, it is recommended to adjust the camera distance
8785 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008786 *
8787 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008788 *
8789 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008790 * @see #getPivotX()
8791 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008792 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008793 * @see #setRotationX(float)
8794 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008795 *
8796 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008797 */
8798 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008799 ensureTransformationInfo();
8800 final TransformationInfo info = mTransformationInfo;
8801 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008802 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008803 info.mRotationY = rotationY;
8804 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008805 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008806 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008807 mDisplayList.setRotationY(rotationY);
8808 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008809 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008810 // View was rejected last time it was drawn by its parent; this may have changed
8811 invalidateParentIfNeeded();
8812 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008813 }
8814 }
8815
8816 /**
8817 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8818 *
8819 * @see #getPivotX()
8820 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008821 * @see #setRotationX(float)
8822 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008823 * @return The degrees of X rotation.
8824 */
Chet Haasea5531132012-02-02 13:41:44 -08008825 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008826 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008827 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008828 }
8829
8830 /**
Chet Haase897247b2010-09-09 14:54:47 -07008831 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8832 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8833 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008834 *
Romain Guya5364ee2011-02-24 14:46:04 -08008835 * When rotating large views, it is recommended to adjust the camera distance
8836 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008837 *
8838 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008839 *
8840 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008841 * @see #getPivotX()
8842 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008843 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008844 * @see #setRotationY(float)
8845 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008846 *
8847 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008848 */
8849 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008850 ensureTransformationInfo();
8851 final TransformationInfo info = mTransformationInfo;
8852 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008853 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008854 info.mRotationX = rotationX;
8855 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008856 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008857 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008858 mDisplayList.setRotationX(rotationX);
8859 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008860 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008861 // View was rejected last time it was drawn by its parent; this may have changed
8862 invalidateParentIfNeeded();
8863 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008864 }
8865 }
8866
8867 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008868 * The amount that the view is scaled in x around the pivot point, as a proportion of
8869 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8870 *
Joe Onorato93162322010-09-16 15:42:01 -04008871 * <p>By default, this is 1.0f.
8872 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008873 * @see #getPivotX()
8874 * @see #getPivotY()
8875 * @return The scaling factor.
8876 */
Chet Haasea5531132012-02-02 13:41:44 -08008877 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008878 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008879 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008880 }
8881
8882 /**
8883 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8884 * the view's unscaled width. A value of 1 means that no scaling is applied.
8885 *
8886 * @param scaleX The scaling factor.
8887 * @see #getPivotX()
8888 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008889 *
8890 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008891 */
8892 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008893 ensureTransformationInfo();
8894 final TransformationInfo info = mTransformationInfo;
8895 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008896 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 info.mScaleX = scaleX;
8898 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008899 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008900 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008901 mDisplayList.setScaleX(scaleX);
8902 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008903 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008904 // View was rejected last time it was drawn by its parent; this may have changed
8905 invalidateParentIfNeeded();
8906 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008907 }
8908 }
8909
8910 /**
8911 * The amount that the view is scaled in y around the pivot point, as a proportion of
8912 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8913 *
Joe Onorato93162322010-09-16 15:42:01 -04008914 * <p>By default, this is 1.0f.
8915 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008916 * @see #getPivotX()
8917 * @see #getPivotY()
8918 * @return The scaling factor.
8919 */
Chet Haasea5531132012-02-02 13:41:44 -08008920 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008921 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008922 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 }
8924
8925 /**
8926 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8927 * the view's unscaled width. A value of 1 means that no scaling is applied.
8928 *
8929 * @param scaleY The scaling factor.
8930 * @see #getPivotX()
8931 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008932 *
8933 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008934 */
8935 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 ensureTransformationInfo();
8937 final TransformationInfo info = mTransformationInfo;
8938 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008940 info.mScaleY = scaleY;
8941 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008942 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008943 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008944 mDisplayList.setScaleY(scaleY);
8945 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008946 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008947 // View was rejected last time it was drawn by its parent; this may have changed
8948 invalidateParentIfNeeded();
8949 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 }
8951 }
8952
8953 /**
8954 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8955 * and {@link #setScaleX(float) scaled}.
8956 *
8957 * @see #getRotation()
8958 * @see #getScaleX()
8959 * @see #getScaleY()
8960 * @see #getPivotY()
8961 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008962 *
8963 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008964 */
Chet Haasea5531132012-02-02 13:41:44 -08008965 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008967 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008968 }
8969
8970 /**
8971 * Sets the x location of the point around which the view is
8972 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008973 * By default, the pivot point is centered on the object.
8974 * Setting this property disables this behavior and causes the view to use only the
8975 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 *
8977 * @param pivotX The x location of the pivot point.
8978 * @see #getRotation()
8979 * @see #getScaleX()
8980 * @see #getScaleY()
8981 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008982 *
8983 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008984 */
8985 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008986 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008987 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008988 final TransformationInfo info = mTransformationInfo;
8989 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008990 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 info.mPivotX = pivotX;
8992 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008993 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008994 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008995 mDisplayList.setPivotX(pivotX);
8996 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008997 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008998 // View was rejected last time it was drawn by its parent; this may have changed
8999 invalidateParentIfNeeded();
9000 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009001 }
9002 }
9003
9004 /**
9005 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9006 * and {@link #setScaleY(float) scaled}.
9007 *
9008 * @see #getRotation()
9009 * @see #getScaleX()
9010 * @see #getScaleY()
9011 * @see #getPivotY()
9012 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009013 *
9014 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009015 */
Chet Haasea5531132012-02-02 13:41:44 -08009016 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009017 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009018 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009019 }
9020
9021 /**
9022 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009023 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9024 * Setting this property disables this behavior and causes the view to use only the
9025 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009026 *
9027 * @param pivotY The y location of the pivot point.
9028 * @see #getRotation()
9029 * @see #getScaleX()
9030 * @see #getScaleY()
9031 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009032 *
9033 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009034 */
9035 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009036 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009037 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009038 final TransformationInfo info = mTransformationInfo;
9039 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009040 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009041 info.mPivotY = pivotY;
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.setPivotY(pivotY);
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 opacity of the view. This is a value from 0 to 1, where 0 means the view is
9056 * completely transparent and 1 means the view is completely opaque.
9057 *
Joe Onorato93162322010-09-16 15:42:01 -04009058 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009059 * @return The opacity of the view.
9060 */
Chet Haasea5531132012-02-02 13:41:44 -08009061 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009062 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009063 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009064 }
9065
9066 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009067 * Returns whether this View has content which overlaps. This function, intended to be
9068 * overridden by specific View types, is an optimization when alpha is set on a view. If
9069 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9070 * and then composited it into place, which can be expensive. If the view has no overlapping
9071 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9072 * An example of overlapping rendering is a TextView with a background image, such as a
9073 * Button. An example of non-overlapping rendering is a TextView with no background, or
9074 * an ImageView with only the foreground image. The default implementation returns true;
9075 * subclasses should override if they have cases which can be optimized.
9076 *
9077 * @return true if the content in this view might overlap, false otherwise.
9078 */
9079 public boolean hasOverlappingRendering() {
9080 return true;
9081 }
9082
9083 /**
Romain Guy171c5922011-01-06 10:04:23 -08009084 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9085 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009086 *
Romain Guy171c5922011-01-06 10:04:23 -08009087 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9088 * responsible for applying the opacity itself. Otherwise, calling this method is
9089 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009090 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009091 *
Chet Haasea5531132012-02-02 13:41:44 -08009092 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9093 * performance implications. It is generally best to use the alpha property sparingly and
9094 * transiently, as in the case of fading animations.</p>
9095 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009096 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009097 *
Joe Malin32736f02011-01-19 16:14:20 -08009098 * @see #setLayerType(int, android.graphics.Paint)
9099 *
Chet Haase73066682010-11-29 15:55:32 -08009100 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009101 */
9102 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009103 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009104 if (mTransformationInfo.mAlpha != alpha) {
9105 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009106 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009107 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009108 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009109 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009110 invalidate(true);
9111 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009112 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009113 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009114 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009115 mDisplayList.setAlpha(alpha);
9116 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009117 }
Chet Haaseed032702010-10-01 14:05:54 -07009118 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009119 }
9120
9121 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009122 * Faster version of setAlpha() which performs the same steps except there are
9123 * no calls to invalidate(). The caller of this function should perform proper invalidation
9124 * on the parent and this object. The return value indicates whether the subclass handles
9125 * alpha (the return value for onSetAlpha()).
9126 *
9127 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009128 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9129 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009130 */
9131 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009132 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009133 if (mTransformationInfo.mAlpha != alpha) {
9134 mTransformationInfo.mAlpha = alpha;
9135 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9136 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009137 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009138 return true;
9139 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009140 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009141 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009142 mDisplayList.setAlpha(alpha);
9143 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009144 }
Chet Haasea00f3862011-02-22 06:34:40 -08009145 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009146 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009147 }
9148
9149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009150 * Top position of this view relative to its parent.
9151 *
9152 * @return The top of this view, in pixels.
9153 */
9154 @ViewDebug.CapturedViewProperty
9155 public final int getTop() {
9156 return mTop;
9157 }
9158
9159 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009160 * Sets the top position of this view relative to its parent. This method is meant to be called
9161 * by the layout system and should not generally be called otherwise, because the property
9162 * may be changed at any time by the layout.
9163 *
9164 * @param top The top of this view, in pixels.
9165 */
9166 public final void setTop(int top) {
9167 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009168 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009169 final boolean matrixIsIdentity = mTransformationInfo == null
9170 || mTransformationInfo.mMatrixIsIdentity;
9171 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009172 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009173 int minTop;
9174 int yLoc;
9175 if (top < mTop) {
9176 minTop = top;
9177 yLoc = top - mTop;
9178 } else {
9179 minTop = mTop;
9180 yLoc = 0;
9181 }
Chet Haasee9140a72011-02-16 16:23:29 -08009182 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009183 }
9184 } else {
9185 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009186 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009187 }
9188
Chet Haaseed032702010-10-01 14:05:54 -07009189 int width = mRight - mLeft;
9190 int oldHeight = mBottom - mTop;
9191
Chet Haase21cd1382010-09-01 17:42:29 -07009192 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009193 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009194 mDisplayList.setTop(mTop);
9195 }
Chet Haase21cd1382010-09-01 17:42:29 -07009196
Chet Haaseed032702010-10-01 14:05:54 -07009197 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9198
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009199 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009200 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009201 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009202 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009203 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009204 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009205 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009206 }
Chet Haase55dbb652010-12-21 20:15:08 -08009207 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009208 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009209 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009210 // View was rejected last time it was drawn by its parent; this may have changed
9211 invalidateParentIfNeeded();
9212 }
Chet Haase21cd1382010-09-01 17:42:29 -07009213 }
9214 }
9215
9216 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 * Bottom position of this view relative to its parent.
9218 *
9219 * @return The bottom of this view, in pixels.
9220 */
9221 @ViewDebug.CapturedViewProperty
9222 public final int getBottom() {
9223 return mBottom;
9224 }
9225
9226 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009227 * True if this view has changed since the last time being drawn.
9228 *
9229 * @return The dirty state of this view.
9230 */
9231 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009232 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009233 }
9234
9235 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009236 * Sets the bottom position of this view relative to its parent. This method is meant to be
9237 * called by the layout system and should not generally be called otherwise, because the
9238 * property may be changed at any time by the layout.
9239 *
9240 * @param bottom The bottom of this view, in pixels.
9241 */
9242 public final void setBottom(int bottom) {
9243 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009244 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009245 final boolean matrixIsIdentity = mTransformationInfo == null
9246 || mTransformationInfo.mMatrixIsIdentity;
9247 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009248 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009249 int maxBottom;
9250 if (bottom < mBottom) {
9251 maxBottom = mBottom;
9252 } else {
9253 maxBottom = bottom;
9254 }
Chet Haasee9140a72011-02-16 16:23:29 -08009255 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009256 }
9257 } else {
9258 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009259 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009260 }
9261
Chet Haaseed032702010-10-01 14:05:54 -07009262 int width = mRight - mLeft;
9263 int oldHeight = mBottom - mTop;
9264
Chet Haase21cd1382010-09-01 17:42:29 -07009265 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009266 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009267 mDisplayList.setBottom(mBottom);
9268 }
Chet Haase21cd1382010-09-01 17:42:29 -07009269
Chet Haaseed032702010-10-01 14:05:54 -07009270 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9271
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009272 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009273 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009274 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009275 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009276 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009277 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009278 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009279 }
Chet Haase55dbb652010-12-21 20:15:08 -08009280 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009281 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009282 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009283 // View was rejected last time it was drawn by its parent; this may have changed
9284 invalidateParentIfNeeded();
9285 }
Chet Haase21cd1382010-09-01 17:42:29 -07009286 }
9287 }
9288
9289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290 * Left position of this view relative to its parent.
9291 *
9292 * @return The left edge of this view, in pixels.
9293 */
9294 @ViewDebug.CapturedViewProperty
9295 public final int getLeft() {
9296 return mLeft;
9297 }
9298
9299 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009300 * Sets the left position of this view relative to its parent. This method is meant to be called
9301 * by the layout system and should not generally be called otherwise, because the property
9302 * may be changed at any time by the layout.
9303 *
9304 * @param left The bottom of this view, in pixels.
9305 */
9306 public final void setLeft(int left) {
9307 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009308 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009309 final boolean matrixIsIdentity = mTransformationInfo == null
9310 || mTransformationInfo.mMatrixIsIdentity;
9311 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009312 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009313 int minLeft;
9314 int xLoc;
9315 if (left < mLeft) {
9316 minLeft = left;
9317 xLoc = left - mLeft;
9318 } else {
9319 minLeft = mLeft;
9320 xLoc = 0;
9321 }
Chet Haasee9140a72011-02-16 16:23:29 -08009322 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009323 }
9324 } else {
9325 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009326 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009327 }
9328
Chet Haaseed032702010-10-01 14:05:54 -07009329 int oldWidth = mRight - mLeft;
9330 int height = mBottom - mTop;
9331
Chet Haase21cd1382010-09-01 17:42:29 -07009332 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009333 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009334 mDisplayList.setLeft(left);
9335 }
Chet Haase21cd1382010-09-01 17:42:29 -07009336
Chet Haaseed032702010-10-01 14:05:54 -07009337 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9338
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009339 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009340 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009341 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009342 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009343 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009344 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009345 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009346 }
Chet Haase55dbb652010-12-21 20:15:08 -08009347 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009348 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009349 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009350 // View was rejected last time it was drawn by its parent; this may have changed
9351 invalidateParentIfNeeded();
9352 }
Chet Haase21cd1382010-09-01 17:42:29 -07009353 }
9354 }
9355
9356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009357 * Right position of this view relative to its parent.
9358 *
9359 * @return The right edge of this view, in pixels.
9360 */
9361 @ViewDebug.CapturedViewProperty
9362 public final int getRight() {
9363 return mRight;
9364 }
9365
9366 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009367 * Sets the right position of this view relative to its parent. This method is meant to be called
9368 * by the layout system and should not generally be called otherwise, because the property
9369 * may be changed at any time by the layout.
9370 *
9371 * @param right The bottom of this view, in pixels.
9372 */
9373 public final void setRight(int right) {
9374 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009375 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009376 final boolean matrixIsIdentity = mTransformationInfo == null
9377 || mTransformationInfo.mMatrixIsIdentity;
9378 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009379 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009380 int maxRight;
9381 if (right < mRight) {
9382 maxRight = mRight;
9383 } else {
9384 maxRight = right;
9385 }
Chet Haasee9140a72011-02-16 16:23:29 -08009386 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009387 }
9388 } else {
9389 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009390 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009391 }
9392
Chet Haaseed032702010-10-01 14:05:54 -07009393 int oldWidth = mRight - mLeft;
9394 int height = mBottom - mTop;
9395
Chet Haase21cd1382010-09-01 17:42:29 -07009396 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009397 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009398 mDisplayList.setRight(mRight);
9399 }
Chet Haase21cd1382010-09-01 17:42:29 -07009400
Chet Haaseed032702010-10-01 14:05:54 -07009401 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9402
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009403 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009404 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009405 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009406 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009407 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009408 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009409 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009410 }
Chet Haase55dbb652010-12-21 20:15:08 -08009411 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009412 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009413 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009414 // View was rejected last time it was drawn by its parent; this may have changed
9415 invalidateParentIfNeeded();
9416 }
Chet Haase21cd1382010-09-01 17:42:29 -07009417 }
9418 }
9419
9420 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009421 * The visual x position of this view, in pixels. This is equivalent to the
9422 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009423 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009424 *
Chet Haasedf030d22010-07-30 17:22:38 -07009425 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009426 */
Chet Haasea5531132012-02-02 13:41:44 -08009427 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009428 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009429 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009430 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009431
Chet Haasedf030d22010-07-30 17:22:38 -07009432 /**
9433 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9434 * {@link #setTranslationX(float) translationX} property to be the difference between
9435 * the x value passed in and the current {@link #getLeft() left} property.
9436 *
9437 * @param x The visual x position of this view, in pixels.
9438 */
9439 public void setX(float x) {
9440 setTranslationX(x - mLeft);
9441 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009442
Chet Haasedf030d22010-07-30 17:22:38 -07009443 /**
9444 * The visual y position of this view, in pixels. This is equivalent to the
9445 * {@link #setTranslationY(float) translationY} property plus the current
9446 * {@link #getTop() top} property.
9447 *
9448 * @return The visual y position of this view, in pixels.
9449 */
Chet Haasea5531132012-02-02 13:41:44 -08009450 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009451 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009452 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009453 }
9454
9455 /**
9456 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9457 * {@link #setTranslationY(float) translationY} property to be the difference between
9458 * the y value passed in and the current {@link #getTop() top} property.
9459 *
9460 * @param y The visual y position of this view, in pixels.
9461 */
9462 public void setY(float y) {
9463 setTranslationY(y - mTop);
9464 }
9465
9466
9467 /**
9468 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9469 * This position is post-layout, in addition to wherever the object's
9470 * layout placed it.
9471 *
9472 * @return The horizontal position of this view relative to its left position, in pixels.
9473 */
Chet Haasea5531132012-02-02 13:41:44 -08009474 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009475 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009476 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009477 }
9478
9479 /**
9480 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9481 * This effectively positions the object post-layout, in addition to wherever the object's
9482 * layout placed it.
9483 *
9484 * @param translationX The horizontal position of this view relative to its left position,
9485 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009486 *
9487 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009488 */
9489 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009490 ensureTransformationInfo();
9491 final TransformationInfo info = mTransformationInfo;
9492 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009493 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009494 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009495 info.mTranslationX = translationX;
9496 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009497 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009498 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009499 mDisplayList.setTranslationX(translationX);
9500 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009501 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009502 // View was rejected last time it was drawn by its parent; this may have changed
9503 invalidateParentIfNeeded();
9504 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009505 }
9506 }
9507
9508 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009509 * The horizontal location of this view relative to its {@link #getTop() top} position.
9510 * This position is post-layout, in addition to wherever the object's
9511 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009512 *
Chet Haasedf030d22010-07-30 17:22:38 -07009513 * @return The vertical position of this view relative to its top position,
9514 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009515 */
Chet Haasea5531132012-02-02 13:41:44 -08009516 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009517 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009518 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009519 }
9520
9521 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009522 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9523 * This effectively positions the object post-layout, in addition to wherever the object's
9524 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009525 *
Chet Haasedf030d22010-07-30 17:22:38 -07009526 * @param translationY The vertical position of this view relative to its top position,
9527 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009528 *
9529 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009530 */
Chet Haasedf030d22010-07-30 17:22:38 -07009531 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009532 ensureTransformationInfo();
9533 final TransformationInfo info = mTransformationInfo;
9534 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009535 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009536 info.mTranslationY = translationY;
9537 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009538 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009539 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009540 mDisplayList.setTranslationY(translationY);
9541 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009542 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009543 // View was rejected last time it was drawn by its parent; this may have changed
9544 invalidateParentIfNeeded();
9545 }
Chet Haasedf030d22010-07-30 17:22:38 -07009546 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009547 }
9548
9549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009550 * Hit rectangle in parent's coordinates
9551 *
9552 * @param outRect The hit rectangle of the view.
9553 */
9554 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009555 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009556 final TransformationInfo info = mTransformationInfo;
9557 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009558 outRect.set(mLeft, mTop, mRight, mBottom);
9559 } else {
9560 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009561 tmpRect.set(-info.mPivotX, -info.mPivotY,
9562 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9563 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009564 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9565 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009566 }
9567 }
9568
9569 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009570 * Determines whether the given point, in local coordinates is inside the view.
9571 */
9572 /*package*/ final boolean pointInView(float localX, float localY) {
9573 return localX >= 0 && localX < (mRight - mLeft)
9574 && localY >= 0 && localY < (mBottom - mTop);
9575 }
9576
9577 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009578 * Utility method to determine whether the given point, in local coordinates,
9579 * is inside the view, where the area of the view is expanded by the slop factor.
9580 * This method is called while processing touch-move events to determine if the event
9581 * is still within the view.
9582 */
9583 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009584 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009585 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 }
9587
9588 /**
9589 * When a view has focus and the user navigates away from it, the next view is searched for
9590 * starting from the rectangle filled in by this method.
9591 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009592 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9593 * of the view. However, if your view maintains some idea of internal selection,
9594 * such as a cursor, or a selected row or column, you should override this method and
9595 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 *
9597 * @param r The rectangle to fill in, in this view's coordinates.
9598 */
9599 public void getFocusedRect(Rect r) {
9600 getDrawingRect(r);
9601 }
9602
9603 /**
9604 * If some part of this view is not clipped by any of its parents, then
9605 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009606 * coordinates (without taking possible View rotations into account), offset
9607 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9608 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009609 *
9610 * @param r If true is returned, r holds the global coordinates of the
9611 * visible portion of this view.
9612 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9613 * between this view and its root. globalOffet may be null.
9614 * @return true if r is non-empty (i.e. part of the view is visible at the
9615 * root level.
9616 */
9617 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9618 int width = mRight - mLeft;
9619 int height = mBottom - mTop;
9620 if (width > 0 && height > 0) {
9621 r.set(0, 0, width, height);
9622 if (globalOffset != null) {
9623 globalOffset.set(-mScrollX, -mScrollY);
9624 }
9625 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9626 }
9627 return false;
9628 }
9629
9630 public final boolean getGlobalVisibleRect(Rect r) {
9631 return getGlobalVisibleRect(r, null);
9632 }
9633
9634 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009635 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009636 if (getGlobalVisibleRect(r, offset)) {
9637 r.offset(-offset.x, -offset.y); // make r local
9638 return true;
9639 }
9640 return false;
9641 }
9642
9643 /**
9644 * Offset this view's vertical location by the specified number of pixels.
9645 *
9646 * @param offset the number of pixels to offset the view by
9647 */
9648 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009649 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009650 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009651 final boolean matrixIsIdentity = mTransformationInfo == null
9652 || mTransformationInfo.mMatrixIsIdentity;
9653 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009654 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009655 invalidateViewProperty(false, false);
9656 } else {
9657 final ViewParent p = mParent;
9658 if (p != null && mAttachInfo != null) {
9659 final Rect r = mAttachInfo.mTmpInvalRect;
9660 int minTop;
9661 int maxBottom;
9662 int yLoc;
9663 if (offset < 0) {
9664 minTop = mTop + offset;
9665 maxBottom = mBottom;
9666 yLoc = offset;
9667 } else {
9668 minTop = mTop;
9669 maxBottom = mBottom + offset;
9670 yLoc = 0;
9671 }
9672 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9673 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009674 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009675 }
9676 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009677 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009678 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009679
Chet Haasec3aa3612010-06-17 08:50:37 -07009680 mTop += offset;
9681 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009682 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009683 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009684 invalidateViewProperty(false, false);
9685 } else {
9686 if (!matrixIsIdentity) {
9687 invalidateViewProperty(false, true);
9688 }
9689 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009690 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 }
9693
9694 /**
9695 * Offset this view's horizontal location by the specified amount of pixels.
9696 *
9697 * @param offset the numer of pixels to offset the view by
9698 */
9699 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009700 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009701 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009702 final boolean matrixIsIdentity = mTransformationInfo == null
9703 || mTransformationInfo.mMatrixIsIdentity;
9704 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009705 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009706 invalidateViewProperty(false, false);
9707 } else {
9708 final ViewParent p = mParent;
9709 if (p != null && mAttachInfo != null) {
9710 final Rect r = mAttachInfo.mTmpInvalRect;
9711 int minLeft;
9712 int maxRight;
9713 if (offset < 0) {
9714 minLeft = mLeft + offset;
9715 maxRight = mRight;
9716 } else {
9717 minLeft = mLeft;
9718 maxRight = mRight + offset;
9719 }
9720 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9721 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009722 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009723 }
9724 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009725 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009726 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009727
Chet Haasec3aa3612010-06-17 08:50:37 -07009728 mLeft += offset;
9729 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009730 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009731 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009732 invalidateViewProperty(false, false);
9733 } else {
9734 if (!matrixIsIdentity) {
9735 invalidateViewProperty(false, true);
9736 }
9737 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009738 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009740 }
9741
9742 /**
9743 * Get the LayoutParams associated with this view. All views should have
9744 * layout parameters. These supply parameters to the <i>parent</i> of this
9745 * view specifying how it should be arranged. There are many subclasses of
9746 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9747 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009748 *
9749 * This method may return null if this View is not attached to a parent
9750 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9751 * was not invoked successfully. When a View is attached to a parent
9752 * ViewGroup, this method must not return null.
9753 *
9754 * @return The LayoutParams associated with this view, or null if no
9755 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009757 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 public ViewGroup.LayoutParams getLayoutParams() {
9759 return mLayoutParams;
9760 }
9761
9762 /**
9763 * Set the layout parameters associated with this view. These supply
9764 * parameters to the <i>parent</i> of this view specifying how it should be
9765 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9766 * correspond to the different subclasses of ViewGroup that are responsible
9767 * for arranging their children.
9768 *
Romain Guy01c174b2011-02-22 11:51:06 -08009769 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 */
9771 public void setLayoutParams(ViewGroup.LayoutParams params) {
9772 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009773 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009774 }
9775 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009776 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009777 if (mParent instanceof ViewGroup) {
9778 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 requestLayout();
9781 }
9782
9783 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009784 * Resolve the layout parameters depending on the resolved layout direction
9785 */
9786 private void resolveLayoutParams() {
9787 if (mLayoutParams != null) {
9788 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
9789 }
9790 }
9791
9792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009793 * Set the scrolled position of your view. This will cause a call to
9794 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9795 * invalidated.
9796 * @param x the x position to scroll to
9797 * @param y the y position to scroll to
9798 */
9799 public void scrollTo(int x, int y) {
9800 if (mScrollX != x || mScrollY != y) {
9801 int oldX = mScrollX;
9802 int oldY = mScrollY;
9803 mScrollX = x;
9804 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009805 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009807 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009808 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 }
9811 }
9812
9813 /**
9814 * Move the scrolled position of your view. This will cause a call to
9815 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9816 * invalidated.
9817 * @param x the amount of pixels to scroll by horizontally
9818 * @param y the amount of pixels to scroll by vertically
9819 */
9820 public void scrollBy(int x, int y) {
9821 scrollTo(mScrollX + x, mScrollY + y);
9822 }
9823
9824 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009825 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9826 * animation to fade the scrollbars out after a default delay. If a subclass
9827 * provides animated scrolling, the start delay should equal the duration
9828 * of the scrolling animation.</p>
9829 *
9830 * <p>The animation starts only if at least one of the scrollbars is
9831 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9832 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9833 * this method returns true, and false otherwise. If the animation is
9834 * started, this method calls {@link #invalidate()}; in that case the
9835 * caller should not call {@link #invalidate()}.</p>
9836 *
9837 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009838 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009839 *
9840 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9841 * and {@link #scrollTo(int, int)}.</p>
9842 *
9843 * @return true if the animation is played, false otherwise
9844 *
9845 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009846 * @see #scrollBy(int, int)
9847 * @see #scrollTo(int, int)
9848 * @see #isHorizontalScrollBarEnabled()
9849 * @see #isVerticalScrollBarEnabled()
9850 * @see #setHorizontalScrollBarEnabled(boolean)
9851 * @see #setVerticalScrollBarEnabled(boolean)
9852 */
9853 protected boolean awakenScrollBars() {
9854 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009855 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009856 }
9857
9858 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009859 * Trigger the scrollbars to draw.
9860 * This method differs from awakenScrollBars() only in its default duration.
9861 * initialAwakenScrollBars() will show the scroll bars for longer than
9862 * usual to give the user more of a chance to notice them.
9863 *
9864 * @return true if the animation is played, false otherwise.
9865 */
9866 private boolean initialAwakenScrollBars() {
9867 return mScrollCache != null &&
9868 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9869 }
9870
9871 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009872 * <p>
9873 * Trigger the scrollbars to draw. When invoked this method starts an
9874 * animation to fade the scrollbars out after a fixed delay. If a subclass
9875 * provides animated scrolling, the start delay should equal the duration of
9876 * the scrolling animation.
9877 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009878 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009879 * <p>
9880 * The animation starts only if at least one of the scrollbars is enabled,
9881 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9882 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9883 * this method returns true, and false otherwise. If the animation is
9884 * started, this method calls {@link #invalidate()}; in that case the caller
9885 * should not call {@link #invalidate()}.
9886 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009887 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009888 * <p>
9889 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009890 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009891 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009892 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009893 * @param startDelay the delay, in milliseconds, after which the animation
9894 * should start; when the delay is 0, the animation starts
9895 * immediately
9896 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009897 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009898 * @see #scrollBy(int, int)
9899 * @see #scrollTo(int, int)
9900 * @see #isHorizontalScrollBarEnabled()
9901 * @see #isVerticalScrollBarEnabled()
9902 * @see #setHorizontalScrollBarEnabled(boolean)
9903 * @see #setVerticalScrollBarEnabled(boolean)
9904 */
9905 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009906 return awakenScrollBars(startDelay, true);
9907 }
Joe Malin32736f02011-01-19 16:14:20 -08009908
Mike Cleron290947b2009-09-29 18:34:32 -07009909 /**
9910 * <p>
9911 * Trigger the scrollbars to draw. When invoked this method starts an
9912 * animation to fade the scrollbars out after a fixed delay. If a subclass
9913 * provides animated scrolling, the start delay should equal the duration of
9914 * the scrolling animation.
9915 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009916 *
Mike Cleron290947b2009-09-29 18:34:32 -07009917 * <p>
9918 * The animation starts only if at least one of the scrollbars is enabled,
9919 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9920 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9921 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009922 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009923 * is set to true; in that case the caller
9924 * should not call {@link #invalidate()}.
9925 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009926 *
Mike Cleron290947b2009-09-29 18:34:32 -07009927 * <p>
9928 * This method should be invoked everytime a subclass directly updates the
9929 * scroll parameters.
9930 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009931 *
Mike Cleron290947b2009-09-29 18:34:32 -07009932 * @param startDelay the delay, in milliseconds, after which the animation
9933 * should start; when the delay is 0, the animation starts
9934 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009935 *
Mike Cleron290947b2009-09-29 18:34:32 -07009936 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009937 *
Mike Cleron290947b2009-09-29 18:34:32 -07009938 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009939 *
Mike Cleron290947b2009-09-29 18:34:32 -07009940 * @see #scrollBy(int, int)
9941 * @see #scrollTo(int, int)
9942 * @see #isHorizontalScrollBarEnabled()
9943 * @see #isVerticalScrollBarEnabled()
9944 * @see #setHorizontalScrollBarEnabled(boolean)
9945 * @see #setVerticalScrollBarEnabled(boolean)
9946 */
9947 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009948 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009949
Mike Cleronf116bf82009-09-27 19:14:12 -07009950 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9951 return false;
9952 }
9953
9954 if (scrollCache.scrollBar == null) {
9955 scrollCache.scrollBar = new ScrollBarDrawable();
9956 }
9957
9958 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9959
Mike Cleron290947b2009-09-29 18:34:32 -07009960 if (invalidate) {
9961 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009962 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009963 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009964
9965 if (scrollCache.state == ScrollabilityCache.OFF) {
9966 // FIXME: this is copied from WindowManagerService.
9967 // We should get this value from the system when it
9968 // is possible to do so.
9969 final int KEY_REPEAT_FIRST_DELAY = 750;
9970 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9971 }
9972
9973 // Tell mScrollCache when we should start fading. This may
9974 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009975 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009976 scrollCache.fadeStartTime = fadeStartTime;
9977 scrollCache.state = ScrollabilityCache.ON;
9978
9979 // Schedule our fader to run, unscheduling any old ones first
9980 if (mAttachInfo != null) {
9981 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9982 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9983 }
9984
9985 return true;
9986 }
9987
9988 return false;
9989 }
9990
9991 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009992 * Do not invalidate views which are not visible and which are not running an animation. They
9993 * will not get drawn and they should not set dirty flags as if they will be drawn
9994 */
9995 private boolean skipInvalidate() {
9996 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9997 (!(mParent instanceof ViewGroup) ||
9998 !((ViewGroup) mParent).isViewTransitioning(this));
9999 }
10000 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010001 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010002 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10003 * in the future. This must be called from a UI thread. To call from a non-UI
10004 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 *
10006 * WARNING: This method is destructive to dirty.
10007 * @param dirty the rectangle representing the bounds of the dirty region
10008 */
10009 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010010 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010011 return;
10012 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010013 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10014 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10015 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10016 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10017 mPrivateFlags |= PFLAG_INVALIDATED;
10018 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 final ViewParent p = mParent;
10020 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010021 //noinspection PointlessBooleanExpression,ConstantConditions
10022 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10023 if (p != null && ai != null && ai.mHardwareAccelerated) {
10024 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010025 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010026 p.invalidateChild(this, null);
10027 return;
10028 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010030 if (p != null && ai != null) {
10031 final int scrollX = mScrollX;
10032 final int scrollY = mScrollY;
10033 final Rect r = ai.mTmpInvalRect;
10034 r.set(dirty.left - scrollX, dirty.top - scrollY,
10035 dirty.right - scrollX, dirty.bottom - scrollY);
10036 mParent.invalidateChild(this, r);
10037 }
10038 }
10039 }
10040
10041 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010042 * 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 -080010043 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010044 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10045 * will be called at some point in the future. This must be called from
10046 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010047 * @param l the left position of the dirty region
10048 * @param t the top position of the dirty region
10049 * @param r the right position of the dirty region
10050 * @param b the bottom position of the dirty region
10051 */
10052 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010053 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010054 return;
10055 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010056 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10057 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10058 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10059 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10060 mPrivateFlags |= PFLAG_INVALIDATED;
10061 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 final ViewParent p = mParent;
10063 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010064 //noinspection PointlessBooleanExpression,ConstantConditions
10065 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10066 if (p != null && ai != null && ai.mHardwareAccelerated) {
10067 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010068 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010069 p.invalidateChild(this, null);
10070 return;
10071 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010073 if (p != null && ai != null && l < r && t < b) {
10074 final int scrollX = mScrollX;
10075 final int scrollY = mScrollY;
10076 final Rect tmpr = ai.mTmpInvalRect;
10077 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10078 p.invalidateChild(this, tmpr);
10079 }
10080 }
10081 }
10082
10083 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010084 * Invalidate the whole view. If the view is visible,
10085 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10086 * the future. This must be called from a UI thread. To call from a non-UI thread,
10087 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 */
10089 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010090 invalidate(true);
10091 }
Joe Malin32736f02011-01-19 16:14:20 -080010092
Chet Haaseed032702010-10-01 14:05:54 -070010093 /**
10094 * This is where the invalidate() work actually happens. A full invalidate()
10095 * causes the drawing cache to be invalidated, but this function can be called with
10096 * invalidateCache set to false to skip that invalidation step for cases that do not
10097 * need it (for example, a component that remains at the same dimensions with the same
10098 * content).
10099 *
10100 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10101 * well. This is usually true for a full invalidate, but may be set to false if the
10102 * View's contents or dimensions have not changed.
10103 */
Romain Guy849d0a32011-02-01 17:20:48 -080010104 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010105 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010106 return;
10107 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010108 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10109 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10110 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010111 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010112 mPrivateFlags &= ~PFLAG_DRAWN;
10113 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010114 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010115 mPrivateFlags |= PFLAG_INVALIDATED;
10116 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010119 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010120 //noinspection PointlessBooleanExpression,ConstantConditions
10121 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10122 if (p != null && ai != null && ai.mHardwareAccelerated) {
10123 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010124 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010125 p.invalidateChild(this, null);
10126 return;
10127 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010128 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 if (p != null && ai != null) {
10131 final Rect r = ai.mTmpInvalRect;
10132 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10133 // Don't call invalidate -- we don't want to internally scroll
10134 // our own bounds
10135 p.invalidateChild(this, r);
10136 }
10137 }
10138 }
10139
10140 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010141 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10142 * set any flags or handle all of the cases handled by the default invalidation methods.
10143 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10144 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10145 * walk up the hierarchy, transforming the dirty rect as necessary.
10146 *
10147 * The method also handles normal invalidation logic if display list properties are not
10148 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10149 * backup approach, to handle these cases used in the various property-setting methods.
10150 *
10151 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10152 * are not being used in this view
10153 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10154 * list properties are not being used in this view
10155 */
10156 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010157 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010158 if (invalidateParent) {
10159 invalidateParentCaches();
10160 }
10161 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010162 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010163 }
10164 invalidate(false);
10165 } else {
10166 final AttachInfo ai = mAttachInfo;
10167 final ViewParent p = mParent;
10168 if (p != null && ai != null) {
10169 final Rect r = ai.mTmpInvalRect;
10170 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10171 if (mParent instanceof ViewGroup) {
10172 ((ViewGroup) mParent).invalidateChildFast(this, r);
10173 } else {
10174 mParent.invalidateChild(this, r);
10175 }
10176 }
10177 }
10178 }
10179
10180 /**
10181 * Utility method to transform a given Rect by the current matrix of this view.
10182 */
10183 void transformRect(final Rect rect) {
10184 if (!getMatrix().isIdentity()) {
10185 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10186 boundingRect.set(rect);
10187 getMatrix().mapRect(boundingRect);
10188 rect.set((int) (boundingRect.left - 0.5f),
10189 (int) (boundingRect.top - 0.5f),
10190 (int) (boundingRect.right + 0.5f),
10191 (int) (boundingRect.bottom + 0.5f));
10192 }
10193 }
10194
10195 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010196 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010197 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10198 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010199 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10200 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010201 *
10202 * @hide
10203 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010204 protected void invalidateParentCaches() {
10205 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010206 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010207 }
10208 }
Joe Malin32736f02011-01-19 16:14:20 -080010209
Romain Guy0fd89bf2011-01-26 15:41:30 -080010210 /**
10211 * Used to indicate that the parent of this view should be invalidated. This functionality
10212 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10213 * which is necessary when various parent-managed properties of the view change, such as
10214 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10215 * an invalidation event to the parent.
10216 *
10217 * @hide
10218 */
10219 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010220 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010221 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010222 }
10223 }
10224
10225 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010226 * Indicates whether this View is opaque. An opaque View guarantees that it will
10227 * draw all the pixels overlapping its bounds using a fully opaque color.
10228 *
10229 * Subclasses of View should override this method whenever possible to indicate
10230 * whether an instance is opaque. Opaque Views are treated in a special way by
10231 * the View hierarchy, possibly allowing it to perform optimizations during
10232 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010233 *
Romain Guy24443ea2009-05-11 11:56:30 -070010234 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010235 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010236 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010237 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010238 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010239 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010240 }
10241
Adam Powell20232d02010-12-08 21:08:53 -080010242 /**
10243 * @hide
10244 */
10245 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010246 // Opaque if:
10247 // - Has a background
10248 // - Background is opaque
10249 // - Doesn't have scrollbars or scrollbars are inside overlay
10250
Philip Milne6c8ea062012-04-03 17:38:43 -070010251 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010252 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010253 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010254 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010255 }
10256
10257 final int flags = mViewFlags;
10258 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10259 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010260 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010261 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010262 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010263 }
10264 }
10265
10266 /**
10267 * @hide
10268 */
10269 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010270 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010271 }
10272
10273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010274 * @return A handler associated with the thread running the View. This
10275 * handler can be used to pump events in the UI events queue.
10276 */
10277 public Handler getHandler() {
10278 if (mAttachInfo != null) {
10279 return mAttachInfo.mHandler;
10280 }
10281 return null;
10282 }
10283
10284 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010285 * Gets the view root associated with the View.
10286 * @return The view root, or null if none.
10287 * @hide
10288 */
10289 public ViewRootImpl getViewRootImpl() {
10290 if (mAttachInfo != null) {
10291 return mAttachInfo.mViewRootImpl;
10292 }
10293 return null;
10294 }
10295
10296 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010297 * <p>Causes the Runnable to be added to the message queue.
10298 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010299 *
Romain Guye63a4f32011-08-11 11:33:31 -070010300 * <p>This method can be invoked from outside of the UI thread
10301 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 *
10303 * @param action The Runnable that will be executed.
10304 *
10305 * @return Returns true if the Runnable was successfully placed in to the
10306 * message queue. Returns false on failure, usually because the
10307 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010308 *
10309 * @see #postDelayed
10310 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 */
10312 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010313 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010314 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010315 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010317 // Assume that post will succeed later
10318 ViewRootImpl.getRunQueue().post(action);
10319 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010320 }
10321
10322 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010323 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010325 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010326 *
Romain Guye63a4f32011-08-11 11:33:31 -070010327 * <p>This method can be invoked from outside of the UI thread
10328 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 *
10330 * @param action The Runnable that will be executed.
10331 * @param delayMillis The delay (in milliseconds) until the Runnable
10332 * will be executed.
10333 *
10334 * @return true if the Runnable was successfully placed in to the
10335 * message queue. Returns false on failure, usually because the
10336 * looper processing the message queue is exiting. Note that a
10337 * result of true does not mean the Runnable will be processed --
10338 * if the looper is quit before the delivery time of the message
10339 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010340 *
10341 * @see #post
10342 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 */
10344 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010345 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010346 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010347 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010349 // Assume that post will succeed later
10350 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10351 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 }
10353
10354 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010355 * <p>Causes the Runnable to execute on the next animation time step.
10356 * The runnable will be run on the user interface thread.</p>
10357 *
10358 * <p>This method can be invoked from outside of the UI thread
10359 * only when this View is attached to a window.</p>
10360 *
10361 * @param action The Runnable that will be executed.
10362 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010363 * @see #postOnAnimationDelayed
10364 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010365 */
10366 public void postOnAnimation(Runnable action) {
10367 final AttachInfo attachInfo = mAttachInfo;
10368 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010369 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10370 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010371 } else {
10372 // Assume that post will succeed later
10373 ViewRootImpl.getRunQueue().post(action);
10374 }
10375 }
10376
10377 /**
10378 * <p>Causes the Runnable to execute on the next animation time step,
10379 * after the specified amount of time elapses.
10380 * The runnable will be run on the user interface thread.</p>
10381 *
10382 * <p>This method can be invoked from outside of the UI thread
10383 * only when this View is attached to a window.</p>
10384 *
10385 * @param action The Runnable that will be executed.
10386 * @param delayMillis The delay (in milliseconds) until the Runnable
10387 * will be executed.
10388 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010389 * @see #postOnAnimation
10390 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010391 */
10392 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10393 final AttachInfo attachInfo = mAttachInfo;
10394 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010395 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10396 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010397 } else {
10398 // Assume that post will succeed later
10399 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10400 }
10401 }
10402
10403 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010404 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010405 *
Romain Guye63a4f32011-08-11 11:33:31 -070010406 * <p>This method can be invoked from outside of the UI thread
10407 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010408 *
10409 * @param action The Runnable to remove from the message handling queue
10410 *
10411 * @return true if this view could ask the Handler to remove the Runnable,
10412 * false otherwise. When the returned value is true, the Runnable
10413 * may or may not have been actually removed from the message queue
10414 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010415 *
10416 * @see #post
10417 * @see #postDelayed
10418 * @see #postOnAnimation
10419 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 */
10421 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010422 if (action != null) {
10423 final AttachInfo attachInfo = mAttachInfo;
10424 if (attachInfo != null) {
10425 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010426 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10427 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010428 } else {
10429 // Assume that post will succeed later
10430 ViewRootImpl.getRunQueue().removeCallbacks(action);
10431 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 return true;
10434 }
10435
10436 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010437 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10438 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010439 *
Romain Guye63a4f32011-08-11 11:33:31 -070010440 * <p>This method can be invoked from outside of the UI thread
10441 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010444 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 */
10446 public void postInvalidate() {
10447 postInvalidateDelayed(0);
10448 }
10449
10450 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010451 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10452 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010453 *
Romain Guye63a4f32011-08-11 11:33:31 -070010454 * <p>This method can be invoked from outside of the UI thread
10455 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010456 *
10457 * @param left The left coordinate of the rectangle to invalidate.
10458 * @param top The top coordinate of the rectangle to invalidate.
10459 * @param right The right coordinate of the rectangle to invalidate.
10460 * @param bottom The bottom coordinate of the rectangle to invalidate.
10461 *
10462 * @see #invalidate(int, int, int, int)
10463 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010464 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010465 */
10466 public void postInvalidate(int left, int top, int right, int bottom) {
10467 postInvalidateDelayed(0, left, top, right, bottom);
10468 }
10469
10470 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010471 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10472 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010473 *
Romain Guye63a4f32011-08-11 11:33:31 -070010474 * <p>This method can be invoked from outside of the UI thread
10475 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 *
10477 * @param delayMilliseconds the duration in milliseconds to delay the
10478 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010479 *
10480 * @see #invalidate()
10481 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 */
10483 public void postInvalidateDelayed(long delayMilliseconds) {
10484 // We try only with the AttachInfo because there's no point in invalidating
10485 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010486 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010487 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010488 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489 }
10490 }
10491
10492 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010493 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10494 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010495 *
Romain Guye63a4f32011-08-11 11:33:31 -070010496 * <p>This method can be invoked from outside of the UI thread
10497 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 *
10499 * @param delayMilliseconds the duration in milliseconds to delay the
10500 * invalidation by
10501 * @param left The left coordinate of the rectangle to invalidate.
10502 * @param top The top coordinate of the rectangle to invalidate.
10503 * @param right The right coordinate of the rectangle to invalidate.
10504 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010505 *
10506 * @see #invalidate(int, int, int, int)
10507 * @see #invalidate(Rect)
10508 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 */
10510 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10511 int right, int bottom) {
10512
10513 // We try only with the AttachInfo because there's no point in invalidating
10514 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010515 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010516 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10518 info.target = this;
10519 info.left = left;
10520 info.top = top;
10521 info.right = right;
10522 info.bottom = bottom;
10523
Jeff Browna175a5b2012-02-15 19:18:31 -080010524 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 }
10526 }
10527
10528 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010529 * <p>Cause an invalidate to happen on the next animation time step, typically the
10530 * next display frame.</p>
10531 *
10532 * <p>This method can be invoked from outside of the UI thread
10533 * only when this View is attached to a window.</p>
10534 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010535 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010536 */
10537 public void postInvalidateOnAnimation() {
10538 // We try only with the AttachInfo because there's no point in invalidating
10539 // if we are not attached to our window
10540 final AttachInfo attachInfo = mAttachInfo;
10541 if (attachInfo != null) {
10542 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10543 }
10544 }
10545
10546 /**
10547 * <p>Cause an invalidate of the specified area to happen on the next animation
10548 * time step, typically the next display frame.</p>
10549 *
10550 * <p>This method can be invoked from outside of the UI thread
10551 * only when this View is attached to a window.</p>
10552 *
10553 * @param left The left coordinate of the rectangle to invalidate.
10554 * @param top The top coordinate of the rectangle to invalidate.
10555 * @param right The right coordinate of the rectangle to invalidate.
10556 * @param bottom The bottom coordinate of the rectangle to invalidate.
10557 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010558 * @see #invalidate(int, int, int, int)
10559 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010560 */
10561 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10562 // We try only with the AttachInfo because there's no point in invalidating
10563 // if we are not attached to our window
10564 final AttachInfo attachInfo = mAttachInfo;
10565 if (attachInfo != null) {
10566 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10567 info.target = this;
10568 info.left = left;
10569 info.top = top;
10570 info.right = right;
10571 info.bottom = bottom;
10572
10573 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10574 }
10575 }
10576
10577 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010578 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10579 * This event is sent at most once every
10580 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10581 */
10582 private void postSendViewScrolledAccessibilityEventCallback() {
10583 if (mSendViewScrolledAccessibilityEvent == null) {
10584 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10585 }
10586 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10587 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10588 postDelayed(mSendViewScrolledAccessibilityEvent,
10589 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10590 }
10591 }
10592
10593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 * Called by a parent to request that a child update its values for mScrollX
10595 * and mScrollY if necessary. This will typically be done if the child is
10596 * animating a scroll using a {@link android.widget.Scroller Scroller}
10597 * object.
10598 */
10599 public void computeScroll() {
10600 }
10601
10602 /**
10603 * <p>Indicate whether the horizontal edges are faded when the view is
10604 * scrolled horizontally.</p>
10605 *
10606 * @return true if the horizontal edges should are faded on scroll, false
10607 * otherwise
10608 *
10609 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010610 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010611 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 */
10613 public boolean isHorizontalFadingEdgeEnabled() {
10614 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10615 }
10616
10617 /**
10618 * <p>Define whether the horizontal edges should be faded when this view
10619 * is scrolled horizontally.</p>
10620 *
10621 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10622 * be faded when the view is scrolled
10623 * horizontally
10624 *
10625 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010626 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010627 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 */
10629 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10630 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10631 if (horizontalFadingEdgeEnabled) {
10632 initScrollCache();
10633 }
10634
10635 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10636 }
10637 }
10638
10639 /**
10640 * <p>Indicate whether the vertical edges are faded when the view is
10641 * scrolled horizontally.</p>
10642 *
10643 * @return true if the vertical edges should are faded on scroll, false
10644 * otherwise
10645 *
10646 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010647 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010648 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 */
10650 public boolean isVerticalFadingEdgeEnabled() {
10651 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10652 }
10653
10654 /**
10655 * <p>Define whether the vertical edges should be faded when this view
10656 * is scrolled vertically.</p>
10657 *
10658 * @param verticalFadingEdgeEnabled true if the vertical edges should
10659 * be faded when the view is scrolled
10660 * vertically
10661 *
10662 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010663 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010664 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 */
10666 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10667 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10668 if (verticalFadingEdgeEnabled) {
10669 initScrollCache();
10670 }
10671
10672 mViewFlags ^= FADING_EDGE_VERTICAL;
10673 }
10674 }
10675
10676 /**
10677 * Returns the strength, or intensity, of the top faded edge. The strength is
10678 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10679 * returns 0.0 or 1.0 but no value in between.
10680 *
10681 * Subclasses should override this method to provide a smoother fade transition
10682 * when scrolling occurs.
10683 *
10684 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10685 */
10686 protected float getTopFadingEdgeStrength() {
10687 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10688 }
10689
10690 /**
10691 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10692 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10693 * returns 0.0 or 1.0 but no value in between.
10694 *
10695 * Subclasses should override this method to provide a smoother fade transition
10696 * when scrolling occurs.
10697 *
10698 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10699 */
10700 protected float getBottomFadingEdgeStrength() {
10701 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10702 computeVerticalScrollRange() ? 1.0f : 0.0f;
10703 }
10704
10705 /**
10706 * Returns the strength, or intensity, of the left faded edge. The strength is
10707 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10708 * returns 0.0 or 1.0 but no value in between.
10709 *
10710 * Subclasses should override this method to provide a smoother fade transition
10711 * when scrolling occurs.
10712 *
10713 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10714 */
10715 protected float getLeftFadingEdgeStrength() {
10716 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10717 }
10718
10719 /**
10720 * Returns the strength, or intensity, of the right faded edge. The strength is
10721 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10722 * returns 0.0 or 1.0 but no value in between.
10723 *
10724 * Subclasses should override this method to provide a smoother fade transition
10725 * when scrolling occurs.
10726 *
10727 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10728 */
10729 protected float getRightFadingEdgeStrength() {
10730 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10731 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10732 }
10733
10734 /**
10735 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10736 * scrollbar is not drawn by default.</p>
10737 *
10738 * @return true if the horizontal scrollbar should be painted, false
10739 * otherwise
10740 *
10741 * @see #setHorizontalScrollBarEnabled(boolean)
10742 */
10743 public boolean isHorizontalScrollBarEnabled() {
10744 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10745 }
10746
10747 /**
10748 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10749 * scrollbar is not drawn by default.</p>
10750 *
10751 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10752 * be painted
10753 *
10754 * @see #isHorizontalScrollBarEnabled()
10755 */
10756 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10757 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10758 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010759 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010760 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010761 }
10762 }
10763
10764 /**
10765 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10766 * scrollbar is not drawn by default.</p>
10767 *
10768 * @return true if the vertical scrollbar should be painted, false
10769 * otherwise
10770 *
10771 * @see #setVerticalScrollBarEnabled(boolean)
10772 */
10773 public boolean isVerticalScrollBarEnabled() {
10774 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10775 }
10776
10777 /**
10778 * <p>Define whether the vertical scrollbar should be drawn or not. The
10779 * scrollbar is not drawn by default.</p>
10780 *
10781 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10782 * be painted
10783 *
10784 * @see #isVerticalScrollBarEnabled()
10785 */
10786 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10787 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10788 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010789 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010790 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010791 }
10792 }
10793
Adam Powell20232d02010-12-08 21:08:53 -080010794 /**
10795 * @hide
10796 */
10797 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010798 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 }
Joe Malin32736f02011-01-19 16:14:20 -080010800
Mike Cleronfe81d382009-09-28 14:22:16 -070010801 /**
10802 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010803 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010804 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010805 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010806 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010807 */
10808 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10809 initScrollCache();
10810 final ScrollabilityCache scrollabilityCache = mScrollCache;
10811 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010812 if (fadeScrollbars) {
10813 scrollabilityCache.state = ScrollabilityCache.OFF;
10814 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010815 scrollabilityCache.state = ScrollabilityCache.ON;
10816 }
10817 }
Joe Malin32736f02011-01-19 16:14:20 -080010818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 /**
Joe Malin32736f02011-01-19 16:14:20 -080010820 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010821 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010822 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010823 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010824 *
10825 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010826 */
10827 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010828 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010829 }
Joe Malin32736f02011-01-19 16:14:20 -080010830
Mike Cleron52f0a642009-09-28 18:21:37 -070010831 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010832 *
10833 * Returns the delay before scrollbars fade.
10834 *
10835 * @return the delay before scrollbars fade
10836 *
10837 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10838 */
10839 public int getScrollBarDefaultDelayBeforeFade() {
10840 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10841 mScrollCache.scrollBarDefaultDelayBeforeFade;
10842 }
10843
10844 /**
10845 * Define the delay before scrollbars fade.
10846 *
10847 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10848 *
10849 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10850 */
10851 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10852 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10853 }
10854
10855 /**
10856 *
10857 * Returns the scrollbar fade duration.
10858 *
10859 * @return the scrollbar fade duration
10860 *
10861 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10862 */
10863 public int getScrollBarFadeDuration() {
10864 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10865 mScrollCache.scrollBarFadeDuration;
10866 }
10867
10868 /**
10869 * Define the scrollbar fade duration.
10870 *
10871 * @param scrollBarFadeDuration - the scrollbar fade duration
10872 *
10873 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10874 */
10875 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10876 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10877 }
10878
10879 /**
10880 *
10881 * Returns the scrollbar size.
10882 *
10883 * @return the scrollbar size
10884 *
10885 * @attr ref android.R.styleable#View_scrollbarSize
10886 */
10887 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010888 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010889 mScrollCache.scrollBarSize;
10890 }
10891
10892 /**
10893 * Define the scrollbar size.
10894 *
10895 * @param scrollBarSize - the scrollbar size
10896 *
10897 * @attr ref android.R.styleable#View_scrollbarSize
10898 */
10899 public void setScrollBarSize(int scrollBarSize) {
10900 getScrollCache().scrollBarSize = scrollBarSize;
10901 }
10902
10903 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010904 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10905 * inset. When inset, they add to the padding of the view. And the scrollbars
10906 * can be drawn inside the padding area or on the edge of the view. For example,
10907 * if a view has a background drawable and you want to draw the scrollbars
10908 * inside the padding specified by the drawable, you can use
10909 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10910 * appear at the edge of the view, ignoring the padding, then you can use
10911 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10912 * @param style the style of the scrollbars. Should be one of
10913 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10914 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10915 * @see #SCROLLBARS_INSIDE_OVERLAY
10916 * @see #SCROLLBARS_INSIDE_INSET
10917 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10918 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010919 *
10920 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010921 */
10922 public void setScrollBarStyle(int style) {
10923 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10924 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010925 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010926 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010927 }
10928 }
10929
10930 /**
10931 * <p>Returns the current scrollbar style.</p>
10932 * @return the current scrollbar style
10933 * @see #SCROLLBARS_INSIDE_OVERLAY
10934 * @see #SCROLLBARS_INSIDE_INSET
10935 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10936 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010937 *
10938 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010940 @ViewDebug.ExportedProperty(mapping = {
10941 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10942 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10943 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10944 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10945 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946 public int getScrollBarStyle() {
10947 return mViewFlags & SCROLLBARS_STYLE_MASK;
10948 }
10949
10950 /**
10951 * <p>Compute the horizontal range that the horizontal scrollbar
10952 * represents.</p>
10953 *
10954 * <p>The range is expressed in arbitrary units that must be the same as the
10955 * units used by {@link #computeHorizontalScrollExtent()} and
10956 * {@link #computeHorizontalScrollOffset()}.</p>
10957 *
10958 * <p>The default range is the drawing width of this view.</p>
10959 *
10960 * @return the total horizontal range represented by the horizontal
10961 * scrollbar
10962 *
10963 * @see #computeHorizontalScrollExtent()
10964 * @see #computeHorizontalScrollOffset()
10965 * @see android.widget.ScrollBarDrawable
10966 */
10967 protected int computeHorizontalScrollRange() {
10968 return getWidth();
10969 }
10970
10971 /**
10972 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10973 * within the horizontal range. This value is used to compute the position
10974 * of the thumb within the scrollbar's track.</p>
10975 *
10976 * <p>The range is expressed in arbitrary units that must be the same as the
10977 * units used by {@link #computeHorizontalScrollRange()} and
10978 * {@link #computeHorizontalScrollExtent()}.</p>
10979 *
10980 * <p>The default offset is the scroll offset of this view.</p>
10981 *
10982 * @return the horizontal offset of the scrollbar's thumb
10983 *
10984 * @see #computeHorizontalScrollRange()
10985 * @see #computeHorizontalScrollExtent()
10986 * @see android.widget.ScrollBarDrawable
10987 */
10988 protected int computeHorizontalScrollOffset() {
10989 return mScrollX;
10990 }
10991
10992 /**
10993 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10994 * within the horizontal range. This value is used to compute the length
10995 * of the thumb within the scrollbar's track.</p>
10996 *
10997 * <p>The range is expressed in arbitrary units that must be the same as the
10998 * units used by {@link #computeHorizontalScrollRange()} and
10999 * {@link #computeHorizontalScrollOffset()}.</p>
11000 *
11001 * <p>The default extent is the drawing width of this view.</p>
11002 *
11003 * @return the horizontal extent of the scrollbar's thumb
11004 *
11005 * @see #computeHorizontalScrollRange()
11006 * @see #computeHorizontalScrollOffset()
11007 * @see android.widget.ScrollBarDrawable
11008 */
11009 protected int computeHorizontalScrollExtent() {
11010 return getWidth();
11011 }
11012
11013 /**
11014 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11015 *
11016 * <p>The range is expressed in arbitrary units that must be the same as the
11017 * units used by {@link #computeVerticalScrollExtent()} and
11018 * {@link #computeVerticalScrollOffset()}.</p>
11019 *
11020 * @return the total vertical range represented by the vertical scrollbar
11021 *
11022 * <p>The default range is the drawing height of this view.</p>
11023 *
11024 * @see #computeVerticalScrollExtent()
11025 * @see #computeVerticalScrollOffset()
11026 * @see android.widget.ScrollBarDrawable
11027 */
11028 protected int computeVerticalScrollRange() {
11029 return getHeight();
11030 }
11031
11032 /**
11033 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11034 * within the horizontal range. This value is used to compute the position
11035 * of the thumb within the scrollbar's track.</p>
11036 *
11037 * <p>The range is expressed in arbitrary units that must be the same as the
11038 * units used by {@link #computeVerticalScrollRange()} and
11039 * {@link #computeVerticalScrollExtent()}.</p>
11040 *
11041 * <p>The default offset is the scroll offset of this view.</p>
11042 *
11043 * @return the vertical offset of the scrollbar's thumb
11044 *
11045 * @see #computeVerticalScrollRange()
11046 * @see #computeVerticalScrollExtent()
11047 * @see android.widget.ScrollBarDrawable
11048 */
11049 protected int computeVerticalScrollOffset() {
11050 return mScrollY;
11051 }
11052
11053 /**
11054 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11055 * within the vertical range. This value is used to compute the length
11056 * of the thumb within the scrollbar's track.</p>
11057 *
11058 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011059 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 * {@link #computeVerticalScrollOffset()}.</p>
11061 *
11062 * <p>The default extent is the drawing height of this view.</p>
11063 *
11064 * @return the vertical extent of the scrollbar's thumb
11065 *
11066 * @see #computeVerticalScrollRange()
11067 * @see #computeVerticalScrollOffset()
11068 * @see android.widget.ScrollBarDrawable
11069 */
11070 protected int computeVerticalScrollExtent() {
11071 return getHeight();
11072 }
11073
11074 /**
Adam Powell69159442011-06-13 17:53:06 -070011075 * Check if this view can be scrolled horizontally in a certain direction.
11076 *
11077 * @param direction Negative to check scrolling left, positive to check scrolling right.
11078 * @return true if this view can be scrolled in the specified direction, false otherwise.
11079 */
11080 public boolean canScrollHorizontally(int direction) {
11081 final int offset = computeHorizontalScrollOffset();
11082 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11083 if (range == 0) return false;
11084 if (direction < 0) {
11085 return offset > 0;
11086 } else {
11087 return offset < range - 1;
11088 }
11089 }
11090
11091 /**
11092 * Check if this view can be scrolled vertically in a certain direction.
11093 *
11094 * @param direction Negative to check scrolling up, positive to check scrolling down.
11095 * @return true if this view can be scrolled in the specified direction, false otherwise.
11096 */
11097 public boolean canScrollVertically(int direction) {
11098 final int offset = computeVerticalScrollOffset();
11099 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11100 if (range == 0) return false;
11101 if (direction < 0) {
11102 return offset > 0;
11103 } else {
11104 return offset < range - 1;
11105 }
11106 }
11107
11108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011109 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11110 * scrollbars are painted only if they have been awakened first.</p>
11111 *
11112 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011113 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011114 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011116 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 // scrollbars are drawn only when the animation is running
11118 final ScrollabilityCache cache = mScrollCache;
11119 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011120
Mike Cleronf116bf82009-09-27 19:14:12 -070011121 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011122
Mike Cleronf116bf82009-09-27 19:14:12 -070011123 if (state == ScrollabilityCache.OFF) {
11124 return;
11125 }
Joe Malin32736f02011-01-19 16:14:20 -080011126
Mike Cleronf116bf82009-09-27 19:14:12 -070011127 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011128
Mike Cleronf116bf82009-09-27 19:14:12 -070011129 if (state == ScrollabilityCache.FADING) {
11130 // We're fading -- get our fade interpolation
11131 if (cache.interpolatorValues == null) {
11132 cache.interpolatorValues = new float[1];
11133 }
Joe Malin32736f02011-01-19 16:14:20 -080011134
Mike Cleronf116bf82009-09-27 19:14:12 -070011135 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011136
Mike Cleronf116bf82009-09-27 19:14:12 -070011137 // Stops the animation if we're done
11138 if (cache.scrollBarInterpolator.timeToValues(values) ==
11139 Interpolator.Result.FREEZE_END) {
11140 cache.state = ScrollabilityCache.OFF;
11141 } else {
11142 cache.scrollBar.setAlpha(Math.round(values[0]));
11143 }
Joe Malin32736f02011-01-19 16:14:20 -080011144
11145 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011146 // drawing. We only want this when we're fading so that
11147 // we prevent excessive redraws
11148 invalidate = true;
11149 } else {
11150 // We're just on -- but we may have been fading before so
11151 // reset alpha
11152 cache.scrollBar.setAlpha(255);
11153 }
11154
Joe Malin32736f02011-01-19 16:14:20 -080011155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011156 final int viewFlags = mViewFlags;
11157
11158 final boolean drawHorizontalScrollBar =
11159 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11160 final boolean drawVerticalScrollBar =
11161 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11162 && !isVerticalScrollBarHidden();
11163
11164 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11165 final int width = mRight - mLeft;
11166 final int height = mBottom - mTop;
11167
11168 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169
Mike Reede8853fc2009-09-04 14:01:48 -040011170 final int scrollX = mScrollX;
11171 final int scrollY = mScrollY;
11172 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11173
Mike Cleronf116bf82009-09-27 19:14:12 -070011174 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011177 int size = scrollBar.getSize(false);
11178 if (size <= 0) {
11179 size = cache.scrollBarSize;
11180 }
11181
Mike Cleronf116bf82009-09-27 19:14:12 -070011182 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011183 computeHorizontalScrollOffset(),
11184 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011185 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011186 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011187 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011188 left = scrollX + (mPaddingLeft & inside);
11189 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11190 bottom = top + size;
11191 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11192 if (invalidate) {
11193 invalidate(left, top, right, bottom);
11194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011195 }
11196
11197 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011198 int size = scrollBar.getSize(true);
11199 if (size <= 0) {
11200 size = cache.scrollBarSize;
11201 }
11202
Mike Reede8853fc2009-09-04 14:01:48 -040011203 scrollBar.setParameters(computeVerticalScrollRange(),
11204 computeVerticalScrollOffset(),
11205 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011206 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11207 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11208 verticalScrollbarPosition = isLayoutRtl() ?
11209 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11210 }
11211 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011212 default:
Adam Powell20232d02010-12-08 21:08:53 -080011213 case SCROLLBAR_POSITION_RIGHT:
11214 left = scrollX + width - size - (mUserPaddingRight & inside);
11215 break;
11216 case SCROLLBAR_POSITION_LEFT:
11217 left = scrollX + (mUserPaddingLeft & inside);
11218 break;
11219 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011220 top = scrollY + (mPaddingTop & inside);
11221 right = left + size;
11222 bottom = scrollY + height - (mUserPaddingBottom & inside);
11223 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11224 if (invalidate) {
11225 invalidate(left, top, right, bottom);
11226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011227 }
11228 }
11229 }
11230 }
Romain Guy8506ab42009-06-11 17:35:47 -070011231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011233 * 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 -080011234 * FastScroller is visible.
11235 * @return whether to temporarily hide the vertical scrollbar
11236 * @hide
11237 */
11238 protected boolean isVerticalScrollBarHidden() {
11239 return false;
11240 }
11241
11242 /**
11243 * <p>Draw the horizontal scrollbar if
11244 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 * @param canvas the canvas on which to draw the scrollbar
11247 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 *
11249 * @see #isHorizontalScrollBarEnabled()
11250 * @see #computeHorizontalScrollRange()
11251 * @see #computeHorizontalScrollExtent()
11252 * @see #computeHorizontalScrollOffset()
11253 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011254 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011255 */
Romain Guy8fb95422010-08-17 18:38:51 -070011256 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11257 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011258 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011259 scrollBar.draw(canvas);
11260 }
Mike Reede8853fc2009-09-04 14:01:48 -040011261
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011263 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11264 * returns true.</p>
11265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011266 * @param canvas the canvas on which to draw the scrollbar
11267 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011268 *
11269 * @see #isVerticalScrollBarEnabled()
11270 * @see #computeVerticalScrollRange()
11271 * @see #computeVerticalScrollExtent()
11272 * @see #computeVerticalScrollOffset()
11273 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011274 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011275 */
Romain Guy8fb95422010-08-17 18:38:51 -070011276 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11277 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011278 scrollBar.setBounds(l, t, r, b);
11279 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011280 }
11281
11282 /**
11283 * Implement this to do your drawing.
11284 *
11285 * @param canvas the canvas on which the background will be drawn
11286 */
11287 protected void onDraw(Canvas canvas) {
11288 }
11289
11290 /*
11291 * Caller is responsible for calling requestLayout if necessary.
11292 * (This allows addViewInLayout to not request a new layout.)
11293 */
11294 void assignParent(ViewParent parent) {
11295 if (mParent == null) {
11296 mParent = parent;
11297 } else if (parent == null) {
11298 mParent = null;
11299 } else {
11300 throw new RuntimeException("view " + this + " being added, but"
11301 + " it already has a parent");
11302 }
11303 }
11304
11305 /**
11306 * This is called when the view is attached to a window. At this point it
11307 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011308 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11309 * however it may be called any time before the first onDraw -- including
11310 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011311 *
11312 * @see #onDetachedFromWindow()
11313 */
11314 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011315 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 mParent.requestTransparentRegion(this);
11317 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011318
Dianne Hackborn4702a852012-08-17 15:18:29 -070011319 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011320 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011321 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011322 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011323
Chet Haasea9b61ac2010-12-20 07:40:25 -080011324 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011325
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011326 resolveRtlProperties();
Romain Guy2a0f2282012-05-08 14:43:12 -070011327
Svetoslav Ganov42138042012-03-20 11:51:39 -070011328 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011329 if (isFocused()) {
11330 InputMethodManager imm = InputMethodManager.peekInstance();
11331 imm.focusIn(this);
11332 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011333
11334 if (mAttachInfo != null && mDisplayList != null) {
11335 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11336 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011337 }
Cibu Johny86666632010-02-22 13:01:02 -080011338
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011339 void resolveRtlProperties() {
11340 // Order is important here: LayoutDirection MUST be resolved first...
11341 resolveLayoutDirection();
11342 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
11343 resolvePadding();
11344 resolveLayoutParams();
11345 resolveTextDirection();
11346 resolveTextAlignment();
11347 }
11348
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011349 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011350 * @see #onScreenStateChanged(int)
11351 */
11352 void dispatchScreenStateChanged(int screenState) {
11353 onScreenStateChanged(screenState);
11354 }
11355
11356 /**
11357 * This method is called whenever the state of the screen this view is
11358 * attached to changes. A state change will usually occurs when the screen
11359 * turns on or off (whether it happens automatically or the user does it
11360 * manually.)
11361 *
11362 * @param screenState The new state of the screen. Can be either
11363 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11364 */
11365 public void onScreenStateChanged(int screenState) {
11366 }
11367
11368 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011369 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11370 */
11371 private boolean hasRtlSupport() {
11372 return mContext.getApplicationInfo().hasRtlSupport();
11373 }
11374
11375 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011376 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11377 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011378 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011379 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011380 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011381 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011382 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011383
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011384 if (hasRtlSupport()) {
11385 // Set resolved depending on layout direction
11386 switch (getLayoutDirection()) {
11387 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011388 // We cannot resolve yet. LTR is by default and let the resolution happen again
11389 // later to get the correct resolved value
11390 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011391
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011392 ViewGroup viewGroup = ((ViewGroup) mParent);
11393
11394 // We cannot resolve yet on the parent too. LTR is by default and let the
11395 // resolution happen again later
11396 if (!viewGroup.canResolveLayoutDirection()) return;
11397
11398 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011399 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011400 }
11401 break;
11402 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011403 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011404 break;
11405 case LAYOUT_DIRECTION_LOCALE:
11406 if(isLayoutDirectionRtl(Locale.getDefault())) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011407 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011408 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011409 break;
11410 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011411 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011412 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011413 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011414
11415 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011416 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011417 onResolvedLayoutDirectionChanged();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011418 }
11419
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011420 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011421 * Called when layout direction has been resolved.
11422 *
11423 * The default implementation does nothing.
11424 */
11425 public void onResolvedLayoutDirectionChanged() {
11426 }
11427
11428 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011429 * Return if padding has been resolved
11430 */
11431 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011432 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011433 }
11434
11435 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011436 * Resolve padding depending on layout direction.
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011437 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011438 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011439 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11440 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11441 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11442 // left / right padding are used if defined. If they are not defined and start / end
11443 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11444 // resolve them as left / right (layout direction is not taken into account).
11445 if (!mUserPaddingLeftDefined && mUserPaddingStart != UNDEFINED_PADDING) {
11446 mUserPaddingLeft = mUserPaddingStart;
11447 }
11448 if (!mUserPaddingRightDefined && mUserPaddingEnd != UNDEFINED_PADDING) {
11449 mUserPaddingRight = mUserPaddingEnd;
11450 }
11451
11452 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11453
11454 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11455 mUserPaddingBottom);
11456 } else {
11457 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11458 // If start / end padding are defined, they will be resolved (hence overriding) to
11459 // left / right or right / left depending on the resolved layout direction.
11460 // If start / end padding are not defined, use the left / right ones.
11461 int resolvedLayoutDirection = getResolvedLayoutDirection();
11462 switch (resolvedLayoutDirection) {
11463 case LAYOUT_DIRECTION_RTL:
11464 if (mUserPaddingStart != UNDEFINED_PADDING) {
11465 mUserPaddingRight = mUserPaddingStart;
11466 }
11467 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11468 mUserPaddingLeft = mUserPaddingEnd;
11469 }
11470 break;
11471 case LAYOUT_DIRECTION_LTR:
11472 default:
11473 if (mUserPaddingStart != UNDEFINED_PADDING) {
11474 mUserPaddingLeft = mUserPaddingStart;
11475 }
11476 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11477 mUserPaddingRight = mUserPaddingEnd;
11478 }
11479 }
11480
11481 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11482
11483 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11484 mUserPaddingBottom);
11485 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011486 }
11487
Dianne Hackborn4702a852012-08-17 15:18:29 -070011488 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011489 }
11490
11491 /**
11492 * Resolve padding depending on the layout direction. Subclasses that care about
11493 * padding resolution should override this method. The default implementation does
11494 * nothing.
11495 *
11496 * @param layoutDirection the direction of the layout
11497 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011498 * @see {@link #LAYOUT_DIRECTION_LTR}
11499 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011500 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011501 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011502 }
11503
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011504 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011505 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011506 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011507 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011508 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011509 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011510 switch (getLayoutDirection()) {
11511 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011512 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011513 default:
11514 return true;
11515 }
11516 }
11517
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011518 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011519 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11520 * when reset is done.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011521 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011522 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011523 // Reset the current resolved bits
Dianne Hackborn4702a852012-08-17 15:18:29 -070011524 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011525 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011526 // Reset also the text direction
11527 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011528 }
11529
11530 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011531 * Called during reset of resolved layout direction.
11532 *
11533 * Subclasses need to override this method to clear cached information that depends on the
11534 * resolved layout direction, or to inform child views that inherit their layout direction.
11535 *
11536 * The default implementation does nothing.
11537 */
11538 public void onResolvedLayoutDirectionReset() {
11539 }
11540
11541 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011542 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011543 *
11544 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011545 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011546 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011547 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011548 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 }
11550
11551 /**
11552 * This is called when the view is detached from a window. At this point it
11553 * no longer has a surface for drawing.
11554 *
11555 * @see #onAttachedToWindow()
11556 */
11557 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011558 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011559
Romain Guya440b002010-02-24 15:57:54 -080011560 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011561 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011562 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011563 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011565 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011566
Romain Guya998dff2012-03-23 18:58:36 -070011567 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011568
11569 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011570 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011571 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011572 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011573 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011574 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011575 // Should never happen
11576 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011577 }
11578
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011579 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011580
11581 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011582 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011583 resetAccessibilityStateChanged();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011584 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011585 }
11586
11587 /**
11588 * @return The number of times this view has been attached to a window
11589 */
11590 protected int getWindowAttachCount() {
11591 return mWindowAttachCount;
11592 }
11593
11594 /**
11595 * Retrieve a unique token identifying the window this view is attached to.
11596 * @return Return the window's token for use in
11597 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11598 */
11599 public IBinder getWindowToken() {
11600 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11601 }
11602
11603 /**
11604 * Retrieve a unique token identifying the top-level "real" window of
11605 * the window that this view is attached to. That is, this is like
11606 * {@link #getWindowToken}, except if the window this view in is a panel
11607 * window (attached to another containing window), then the token of
11608 * the containing window is returned instead.
11609 *
11610 * @return Returns the associated window token, either
11611 * {@link #getWindowToken()} or the containing window's token.
11612 */
11613 public IBinder getApplicationWindowToken() {
11614 AttachInfo ai = mAttachInfo;
11615 if (ai != null) {
11616 IBinder appWindowToken = ai.mPanelParentWindowToken;
11617 if (appWindowToken == null) {
11618 appWindowToken = ai.mWindowToken;
11619 }
11620 return appWindowToken;
11621 }
11622 return null;
11623 }
11624
11625 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011626 * Gets the logical display to which the view's window has been attached.
11627 *
11628 * @return The logical display, or null if the view is not currently attached to a window.
11629 */
11630 public Display getDisplay() {
11631 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11632 }
11633
11634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 * Retrieve private session object this view hierarchy is using to
11636 * communicate with the window manager.
11637 * @return the session object to communicate with the window manager
11638 */
11639 /*package*/ IWindowSession getWindowSession() {
11640 return mAttachInfo != null ? mAttachInfo.mSession : null;
11641 }
11642
11643 /**
11644 * @param info the {@link android.view.View.AttachInfo} to associated with
11645 * this view
11646 */
11647 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11648 //System.out.println("Attached! " + this);
11649 mAttachInfo = info;
11650 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011651 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011652 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011653 if (mFloatingTreeObserver != null) {
11654 info.mTreeObserver.merge(mFloatingTreeObserver);
11655 mFloatingTreeObserver = null;
11656 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011657 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011659 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011660 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011661 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011662 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011663
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011664 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011665 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011666 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011667 if (listeners != null && listeners.size() > 0) {
11668 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11669 // perform the dispatching. The iterator is a safe guard against listeners that
11670 // could mutate the list by calling the various add/remove methods. This prevents
11671 // the array from being modified while we iterate it.
11672 for (OnAttachStateChangeListener listener : listeners) {
11673 listener.onViewAttachedToWindow(this);
11674 }
11675 }
11676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011677 int vis = info.mWindowVisibility;
11678 if (vis != GONE) {
11679 onWindowVisibilityChanged(vis);
11680 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011681 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011682 // If nobody has evaluated the drawable state yet, then do it now.
11683 refreshDrawableState();
11684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011685 }
11686
11687 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011688 AttachInfo info = mAttachInfo;
11689 if (info != null) {
11690 int vis = info.mWindowVisibility;
11691 if (vis != GONE) {
11692 onWindowVisibilityChanged(GONE);
11693 }
11694 }
11695
11696 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011697
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011698 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011699 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011700 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011701 if (listeners != null && listeners.size() > 0) {
11702 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11703 // perform the dispatching. The iterator is a safe guard against listeners that
11704 // could mutate the list by calling the various add/remove methods. This prevents
11705 // the array from being modified while we iterate it.
11706 for (OnAttachStateChangeListener listener : listeners) {
11707 listener.onViewDetachedFromWindow(this);
11708 }
11709 }
11710
Dianne Hackborn4702a852012-08-17 15:18:29 -070011711 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011712 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011713 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011714 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011716 mAttachInfo = null;
11717 }
11718
11719 /**
11720 * Store this view hierarchy's frozen state into the given container.
11721 *
11722 * @param container The SparseArray in which to save the view's state.
11723 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011724 * @see #restoreHierarchyState(android.util.SparseArray)
11725 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11726 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011727 */
11728 public void saveHierarchyState(SparseArray<Parcelable> container) {
11729 dispatchSaveInstanceState(container);
11730 }
11731
11732 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011733 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11734 * this view and its children. May be overridden to modify how freezing happens to a
11735 * 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 -080011736 *
11737 * @param container The SparseArray in which to save the view's state.
11738 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011739 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11740 * @see #saveHierarchyState(android.util.SparseArray)
11741 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 */
11743 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11744 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011745 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011746 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011747 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011748 throw new IllegalStateException(
11749 "Derived class did not call super.onSaveInstanceState()");
11750 }
11751 if (state != null) {
11752 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11753 // + ": " + state);
11754 container.put(mID, state);
11755 }
11756 }
11757 }
11758
11759 /**
11760 * Hook allowing a view to generate a representation of its internal state
11761 * that can later be used to create a new instance with that same state.
11762 * This state should only contain information that is not persistent or can
11763 * not be reconstructed later. For example, you will never store your
11764 * current position on screen because that will be computed again when a
11765 * new instance of the view is placed in its view hierarchy.
11766 * <p>
11767 * Some examples of things you may store here: the current cursor position
11768 * in a text view (but usually not the text itself since that is stored in a
11769 * content provider or other persistent storage), the currently selected
11770 * item in a list view.
11771 *
11772 * @return Returns a Parcelable object containing the view's current dynamic
11773 * state, or null if there is nothing interesting to save. The
11774 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011775 * @see #onRestoreInstanceState(android.os.Parcelable)
11776 * @see #saveHierarchyState(android.util.SparseArray)
11777 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 * @see #setSaveEnabled(boolean)
11779 */
11780 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011781 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 return BaseSavedState.EMPTY_STATE;
11783 }
11784
11785 /**
11786 * Restore this view hierarchy's frozen state from the given container.
11787 *
11788 * @param container The SparseArray which holds previously frozen states.
11789 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011790 * @see #saveHierarchyState(android.util.SparseArray)
11791 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11792 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011793 */
11794 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11795 dispatchRestoreInstanceState(container);
11796 }
11797
11798 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011799 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11800 * state for this view and its children. May be overridden to modify how restoring
11801 * happens to a view's children; for example, some views may want to not store state
11802 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011803 *
11804 * @param container The SparseArray which holds previously saved state.
11805 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011806 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11807 * @see #restoreHierarchyState(android.util.SparseArray)
11808 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011809 */
11810 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11811 if (mID != NO_ID) {
11812 Parcelable state = container.get(mID);
11813 if (state != null) {
11814 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11815 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011816 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011817 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011818 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011819 throw new IllegalStateException(
11820 "Derived class did not call super.onRestoreInstanceState()");
11821 }
11822 }
11823 }
11824 }
11825
11826 /**
11827 * Hook allowing a view to re-apply a representation of its internal state that had previously
11828 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11829 * null state.
11830 *
11831 * @param state The frozen state that had previously been returned by
11832 * {@link #onSaveInstanceState}.
11833 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011834 * @see #onSaveInstanceState()
11835 * @see #restoreHierarchyState(android.util.SparseArray)
11836 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011837 */
11838 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011839 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011840 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011841 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11842 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011843 + "when two views of different type have the same id in the same hierarchy. "
11844 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011845 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011846 }
11847 }
11848
11849 /**
11850 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11851 *
11852 * @return the drawing start time in milliseconds
11853 */
11854 public long getDrawingTime() {
11855 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11856 }
11857
11858 /**
11859 * <p>Enables or disables the duplication of the parent's state into this view. When
11860 * duplication is enabled, this view gets its drawable state from its parent rather
11861 * than from its own internal properties.</p>
11862 *
11863 * <p>Note: in the current implementation, setting this property to true after the
11864 * view was added to a ViewGroup might have no effect at all. This property should
11865 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11866 *
11867 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11868 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011869 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011870 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11871 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011872 *
11873 * @param enabled True to enable duplication of the parent's drawable state, false
11874 * to disable it.
11875 *
11876 * @see #getDrawableState()
11877 * @see #isDuplicateParentStateEnabled()
11878 */
11879 public void setDuplicateParentStateEnabled(boolean enabled) {
11880 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11881 }
11882
11883 /**
11884 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11885 *
11886 * @return True if this view's drawable state is duplicated from the parent,
11887 * false otherwise
11888 *
11889 * @see #getDrawableState()
11890 * @see #setDuplicateParentStateEnabled(boolean)
11891 */
11892 public boolean isDuplicateParentStateEnabled() {
11893 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11894 }
11895
11896 /**
Romain Guy171c5922011-01-06 10:04:23 -080011897 * <p>Specifies the type of layer backing this view. The layer can be
11898 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11899 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011900 *
Romain Guy171c5922011-01-06 10:04:23 -080011901 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11902 * instance that controls how the layer is composed on screen. The following
11903 * properties of the paint are taken into account when composing the layer:</p>
11904 * <ul>
11905 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11906 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11907 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11908 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011909 *
Romain Guy171c5922011-01-06 10:04:23 -080011910 * <p>If this view has an alpha value set to < 1.0 by calling
11911 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11912 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11913 * equivalent to setting a hardware layer on this view and providing a paint with
11914 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011915 *
Romain Guy171c5922011-01-06 10:04:23 -080011916 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11917 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11918 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011919 *
Romain Guy171c5922011-01-06 10:04:23 -080011920 * @param layerType The ype of layer to use with this view, must be one of
11921 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11922 * {@link #LAYER_TYPE_HARDWARE}
11923 * @param paint The paint used to compose the layer. This argument is optional
11924 * and can be null. It is ignored when the layer type is
11925 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011926 *
11927 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011928 * @see #LAYER_TYPE_NONE
11929 * @see #LAYER_TYPE_SOFTWARE
11930 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011931 * @see #setAlpha(float)
11932 *
Romain Guy171c5922011-01-06 10:04:23 -080011933 * @attr ref android.R.styleable#View_layerType
11934 */
11935 public void setLayerType(int layerType, Paint paint) {
11936 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011937 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011938 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11939 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011940
Romain Guyd6cd5722011-01-17 14:42:41 -080011941 if (layerType == mLayerType) {
11942 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11943 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011944 invalidateParentCaches();
11945 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011946 }
11947 return;
11948 }
Romain Guy171c5922011-01-06 10:04:23 -080011949
11950 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011951 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011952 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011953 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011954 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011955 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011956 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011957 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011958 default:
11959 break;
Romain Guy171c5922011-01-06 10:04:23 -080011960 }
11961
11962 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011963 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11964 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11965 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011966
Romain Guy0fd89bf2011-01-26 15:41:30 -080011967 invalidateParentCaches();
11968 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011969 }
11970
11971 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011972 * Indicates whether this view has a static layer. A view with layer type
11973 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11974 * dynamic.
11975 */
11976 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011977 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011978 }
11979
11980 /**
Romain Guy171c5922011-01-06 10:04:23 -080011981 * Indicates what type of layer is currently associated with this view. By default
11982 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11983 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11984 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011985 *
Romain Guy171c5922011-01-06 10:04:23 -080011986 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11987 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011988 *
11989 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011990 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011991 * @see #LAYER_TYPE_NONE
11992 * @see #LAYER_TYPE_SOFTWARE
11993 * @see #LAYER_TYPE_HARDWARE
11994 */
11995 public int getLayerType() {
11996 return mLayerType;
11997 }
Joe Malin32736f02011-01-19 16:14:20 -080011998
Romain Guy6c319ca2011-01-11 14:29:25 -080011999 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012000 * Forces this view's layer to be created and this view to be rendered
12001 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12002 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012003 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012004 * This method can for instance be used to render a view into its layer before
12005 * starting an animation. If this view is complex, rendering into the layer
12006 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012007 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012008 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012009 *
12010 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012011 */
12012 public void buildLayer() {
12013 if (mLayerType == LAYER_TYPE_NONE) return;
12014
12015 if (mAttachInfo == null) {
12016 throw new IllegalStateException("This view must be attached to a window first");
12017 }
12018
12019 switch (mLayerType) {
12020 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012021 if (mAttachInfo.mHardwareRenderer != null &&
12022 mAttachInfo.mHardwareRenderer.isEnabled() &&
12023 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012024 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012025 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012026 break;
12027 case LAYER_TYPE_SOFTWARE:
12028 buildDrawingCache(true);
12029 break;
12030 }
12031 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012032
Romain Guyf1ae1062011-03-02 18:16:04 -080012033 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012034 * <p>Returns a hardware layer that can be used to draw this view again
12035 * without executing its draw method.</p>
12036 *
12037 * @return A HardwareLayer ready to render, or null if an error occurred.
12038 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012039 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012040 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12041 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012042 return null;
12043 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012044
Romain Guy9c4b79a2011-11-10 19:23:58 -080012045 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012046
12047 final int width = mRight - mLeft;
12048 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012049
Romain Guy6c319ca2011-01-11 14:29:25 -080012050 if (width == 0 || height == 0) {
12051 return null;
12052 }
12053
Dianne Hackborn4702a852012-08-17 15:18:29 -070012054 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012055 if (mHardwareLayer == null) {
12056 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12057 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012058 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012059 } else {
12060 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
12061 mHardwareLayer.resize(width, height);
12062 mLocalDirtyRect.set(0, 0, width, height);
12063 }
12064
12065 // This should not be necessary but applications that change
12066 // the parameters of their background drawable without calling
12067 // this.setBackground(Drawable) can leave the view in a bad state
12068 // (for instance isOpaque() returns true, but the background is
12069 // not opaque.)
12070 computeOpaqueFlags();
12071
12072 final boolean opaque = isOpaque();
12073 if (mHardwareLayer.isOpaque() != opaque) {
12074 mHardwareLayer.setOpaque(opaque);
12075 mLocalDirtyRect.set(0, 0, width, height);
12076 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012077 }
12078
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012079 // The layer is not valid if the underlying GPU resources cannot be allocated
12080 if (!mHardwareLayer.isValid()) {
12081 return null;
12082 }
12083
Chet Haasea1cff502012-02-21 13:43:44 -080012084 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012085 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012086 }
12087
12088 return mHardwareLayer;
12089 }
Romain Guy171c5922011-01-06 10:04:23 -080012090
Romain Guy589b0bb2011-10-10 13:57:47 -070012091 /**
12092 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012093 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012094 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012095 *
12096 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012097 * @see #LAYER_TYPE_HARDWARE
12098 */
Romain Guya998dff2012-03-23 18:58:36 -070012099 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012100 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012101 AttachInfo info = mAttachInfo;
12102 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012103 info.mHardwareRenderer.isEnabled() &&
12104 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012105 mHardwareLayer.destroy();
12106 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012107
Romain Guy9c4b79a2011-11-10 19:23:58 -080012108 invalidate(true);
12109 invalidateParentCaches();
12110 }
Romain Guy65b345f2011-07-27 18:51:50 -070012111 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012112 }
Romain Guy65b345f2011-07-27 18:51:50 -070012113 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012114 }
12115
Romain Guy171c5922011-01-06 10:04:23 -080012116 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012117 * Destroys all hardware rendering resources. This method is invoked
12118 * when the system needs to reclaim resources. Upon execution of this
12119 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012120 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012121 * Note: you <strong>must</strong> call
12122 * <code>super.destroyHardwareResources()</code> when overriding
12123 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012124 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012125 * @hide
12126 */
12127 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012128 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012129 }
12130
12131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12133 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12134 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12135 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12136 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12137 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012138 *
Romain Guy171c5922011-01-06 10:04:23 -080012139 * <p>Enabling the drawing cache is similar to
12140 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012141 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12142 * drawing cache has no effect on rendering because the system uses a different mechanism
12143 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12144 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12145 * for information on how to enable software and hardware layers.</p>
12146 *
12147 * <p>This API can be used to manually generate
12148 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12149 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012150 *
12151 * @param enabled true to enable the drawing cache, false otherwise
12152 *
12153 * @see #isDrawingCacheEnabled()
12154 * @see #getDrawingCache()
12155 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012156 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012157 */
12158 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012159 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12161 }
12162
12163 /**
12164 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12165 *
12166 * @return true if the drawing cache is enabled
12167 *
12168 * @see #setDrawingCacheEnabled(boolean)
12169 * @see #getDrawingCache()
12170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012171 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012172 public boolean isDrawingCacheEnabled() {
12173 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12174 }
12175
12176 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012177 * Debugging utility which recursively outputs the dirty state of a view and its
12178 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012179 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012180 * @hide
12181 */
Romain Guy676b1732011-02-14 14:45:33 -080012182 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012183 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012184 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12185 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12186 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12187 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012188 if (clear) {
12189 mPrivateFlags &= clearMask;
12190 }
12191 if (this instanceof ViewGroup) {
12192 ViewGroup parent = (ViewGroup) this;
12193 final int count = parent.getChildCount();
12194 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012195 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012196 child.outputDirtyFlags(indent + " ", clear, clearMask);
12197 }
12198 }
12199 }
12200
12201 /**
12202 * This method is used by ViewGroup to cause its children to restore or recreate their
12203 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12204 * to recreate its own display list, which would happen if it went through the normal
12205 * draw/dispatchDraw mechanisms.
12206 *
12207 * @hide
12208 */
12209 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012210
12211 /**
12212 * A view that is not attached or hardware accelerated cannot create a display list.
12213 * This method checks these conditions and returns the appropriate result.
12214 *
12215 * @return true if view has the ability to create a display list, false otherwise.
12216 *
12217 * @hide
12218 */
12219 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012220 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012221 }
Joe Malin32736f02011-01-19 16:14:20 -080012222
Chet Haasedaf98e92011-01-10 14:10:36 -080012223 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012224 * @return The HardwareRenderer associated with that view or null if hardware rendering
12225 * is not supported or this this has not been attached to a window.
12226 *
12227 * @hide
12228 */
12229 public HardwareRenderer getHardwareRenderer() {
12230 if (mAttachInfo != null) {
12231 return mAttachInfo.mHardwareRenderer;
12232 }
12233 return null;
12234 }
12235
12236 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012237 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12238 * Otherwise, the same display list will be returned (after having been rendered into
12239 * along the way, depending on the invalidation state of the view).
12240 *
12241 * @param displayList The previous version of this displayList, could be null.
12242 * @param isLayer Whether the requester of the display list is a layer. If so,
12243 * the view will avoid creating a layer inside the resulting display list.
12244 * @return A new or reused DisplayList object.
12245 */
12246 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12247 if (!canHaveDisplayList()) {
12248 return null;
12249 }
12250
Dianne Hackborn4702a852012-08-17 15:18:29 -070012251 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012252 displayList == null || !displayList.isValid() ||
12253 (!isLayer && mRecreateDisplayList))) {
12254 // Don't need to recreate the display list, just need to tell our
12255 // children to restore/recreate theirs
12256 if (displayList != null && displayList.isValid() &&
12257 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012258 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12259 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012260 dispatchGetDisplayList();
12261
12262 return displayList;
12263 }
12264
12265 if (!isLayer) {
12266 // If we got here, we're recreating it. Mark it as such to ensure that
12267 // we copy in child display lists into ours in drawChild()
12268 mRecreateDisplayList = true;
12269 }
12270 if (displayList == null) {
12271 final String name = getClass().getSimpleName();
12272 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12273 // If we're creating a new display list, make sure our parent gets invalidated
12274 // since they will need to recreate their display list to account for this
12275 // new child display list.
12276 invalidateParentCaches();
12277 }
12278
12279 boolean caching = false;
12280 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012281 int width = mRight - mLeft;
12282 int height = mBottom - mTop;
12283
12284 try {
12285 canvas.setViewport(width, height);
12286 // The dirty rect should always be null for a display list
12287 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012288 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012289 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012290 if (layerType == LAYER_TYPE_HARDWARE) {
12291 final HardwareLayer layer = getHardwareLayer();
12292 if (layer != null && layer.isValid()) {
12293 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12294 } else {
12295 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12296 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12297 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12298 }
12299 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012300 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012301 buildDrawingCache(true);
12302 Bitmap cache = getDrawingCache(true);
12303 if (cache != null) {
12304 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12305 caching = true;
12306 }
Chet Haasea1cff502012-02-21 13:43:44 -080012307 }
Chet Haasea1cff502012-02-21 13:43:44 -080012308 } else {
12309
12310 computeScroll();
12311
Chet Haasea1cff502012-02-21 13:43:44 -080012312 canvas.translate(-mScrollX, -mScrollY);
12313 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012314 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12315 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012316 }
12317
12318 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012319 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012320 dispatchDraw(canvas);
12321 } else {
12322 draw(canvas);
12323 }
12324 }
12325 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012326 canvas.onPostDraw();
12327
12328 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012329 displayList.setCaching(caching);
12330 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012331 displayList.setLeftTopRightBottom(0, 0, width, height);
12332 } else {
12333 setDisplayListProperties(displayList);
12334 }
12335 }
12336 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012337 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12338 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012339 }
12340
12341 return displayList;
12342 }
12343
12344 /**
12345 * Get the DisplayList for the HardwareLayer
12346 *
12347 * @param layer The HardwareLayer whose DisplayList we want
12348 * @return A DisplayList fopr the specified HardwareLayer
12349 */
12350 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12351 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12352 layer.setDisplayList(displayList);
12353 return displayList;
12354 }
12355
12356
12357 /**
Romain Guyb051e892010-09-28 19:09:36 -070012358 * <p>Returns a display list that can be used to draw this view again
12359 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012360 *
Romain Guyb051e892010-09-28 19:09:36 -070012361 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012362 *
12363 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012364 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012365 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012366 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012367 return mDisplayList;
12368 }
12369
Romain Guy38c2ece2012-05-24 14:20:56 -070012370 private void clearDisplayList() {
12371 if (mDisplayList != null) {
12372 mDisplayList.invalidate();
12373 mDisplayList.clear();
12374 }
12375 }
12376
Romain Guyb051e892010-09-28 19:09:36 -070012377 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012378 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012379 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012380 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012381 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012382 * @see #getDrawingCache(boolean)
12383 */
12384 public Bitmap getDrawingCache() {
12385 return getDrawingCache(false);
12386 }
12387
12388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12390 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12391 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12392 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12393 * request the drawing cache by calling this method and draw it on screen if the
12394 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012395 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012396 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12397 * this method will create a bitmap of the same size as this view. Because this bitmap
12398 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12399 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12400 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12401 * size than the view. This implies that your application must be able to handle this
12402 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012403 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012404 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12405 * the current density of the screen when the application is in compatibility
12406 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012408 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012409 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012410 * @see #setDrawingCacheEnabled(boolean)
12411 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012412 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 * @see #destroyDrawingCache()
12414 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012415 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012416 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12417 return null;
12418 }
12419 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012420 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 }
Romain Guy02890fd2010-08-06 17:58:44 -070012422 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 }
12424
12425 /**
12426 * <p>Frees the resources used by the drawing cache. If you call
12427 * {@link #buildDrawingCache()} manually without calling
12428 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12429 * should cleanup the cache with this method afterwards.</p>
12430 *
12431 * @see #setDrawingCacheEnabled(boolean)
12432 * @see #buildDrawingCache()
12433 * @see #getDrawingCache()
12434 */
12435 public void destroyDrawingCache() {
12436 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012437 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012438 mDrawingCache = null;
12439 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012440 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012441 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012442 mUnscaledDrawingCache = null;
12443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012444 }
12445
12446 /**
12447 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012448 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 * view will always be drawn on top of a solid color.
12450 *
12451 * @param color The background color to use for the drawing cache's bitmap
12452 *
12453 * @see #setDrawingCacheEnabled(boolean)
12454 * @see #buildDrawingCache()
12455 * @see #getDrawingCache()
12456 */
12457 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012458 if (color != mDrawingCacheBackgroundColor) {
12459 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012460 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 }
12463
12464 /**
12465 * @see #setDrawingCacheBackgroundColor(int)
12466 *
12467 * @return The background color to used for the drawing cache's bitmap
12468 */
12469 public int getDrawingCacheBackgroundColor() {
12470 return mDrawingCacheBackgroundColor;
12471 }
12472
12473 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012474 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012475 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012476 * @see #buildDrawingCache(boolean)
12477 */
12478 public void buildDrawingCache() {
12479 buildDrawingCache(false);
12480 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012481
Romain Guyfbd8f692009-06-26 14:51:58 -070012482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12484 *
12485 * <p>If you call {@link #buildDrawingCache()} manually without calling
12486 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12487 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012488 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012489 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12490 * this method will create a bitmap of the same size as this view. Because this bitmap
12491 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12492 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12493 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12494 * size than the view. This implies that your application must be able to handle this
12495 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012496 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012497 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12498 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012499 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012500 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012501 *
12502 * @see #getDrawingCache()
12503 * @see #destroyDrawingCache()
12504 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012505 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012506 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012507 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012508 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509
Romain Guy8506ab42009-06-11 17:35:47 -070012510 int width = mRight - mLeft;
12511 int height = mBottom - mTop;
12512
12513 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012514 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012515
Romain Guye1123222009-06-29 14:24:56 -070012516 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012517 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12518 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012520
12521 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012522 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012523 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012524
12525 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012526 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012527 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12529 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012530 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 return;
12532 }
12533
12534 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012535 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536
12537 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 Bitmap.Config quality;
12539 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012540 // Never pick ARGB_4444 because it looks awful
12541 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012542 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12543 case DRAWING_CACHE_QUALITY_AUTO:
12544 quality = Bitmap.Config.ARGB_8888;
12545 break;
12546 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012547 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012548 break;
12549 case DRAWING_CACHE_QUALITY_HIGH:
12550 quality = Bitmap.Config.ARGB_8888;
12551 break;
12552 default:
12553 quality = Bitmap.Config.ARGB_8888;
12554 break;
12555 }
12556 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012557 // Optimization for translucent windows
12558 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012559 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 }
12561
12562 // Try to cleanup memory
12563 if (bitmap != null) bitmap.recycle();
12564
12565 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012566 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12567 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012568 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012569 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012570 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012571 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012572 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012573 }
Adam Powell26153a32010-11-08 15:22:27 -080012574 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012575 } catch (OutOfMemoryError e) {
12576 // If there is not enough memory to create the bitmap cache, just
12577 // ignore the issue as bitmap caches are not required to draw the
12578 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012579 if (autoScale) {
12580 mDrawingCache = null;
12581 } else {
12582 mUnscaledDrawingCache = null;
12583 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012584 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012585 return;
12586 }
12587
12588 clear = drawingCacheBackgroundColor != 0;
12589 }
12590
12591 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592 if (attachInfo != null) {
12593 canvas = attachInfo.mCanvas;
12594 if (canvas == null) {
12595 canvas = new Canvas();
12596 }
12597 canvas.setBitmap(bitmap);
12598 // Temporarily clobber the cached Canvas in case one of our children
12599 // is also using a drawing cache. Without this, the children would
12600 // steal the canvas by attaching their own bitmap to it and bad, bad
12601 // thing would happen (invisible views, corrupted drawings, etc.)
12602 attachInfo.mCanvas = null;
12603 } else {
12604 // This case should hopefully never or seldom happen
12605 canvas = new Canvas(bitmap);
12606 }
12607
12608 if (clear) {
12609 bitmap.eraseColor(drawingCacheBackgroundColor);
12610 }
12611
12612 computeScroll();
12613 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012614
Romain Guye1123222009-06-29 14:24:56 -070012615 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012616 final float scale = attachInfo.mApplicationScale;
12617 canvas.scale(scale, scale);
12618 }
Joe Malin32736f02011-01-19 16:14:20 -080012619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012620 canvas.translate(-mScrollX, -mScrollY);
12621
Dianne Hackborn4702a852012-08-17 15:18:29 -070012622 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012623 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12624 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012625 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012627
12628 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012629 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12630 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012631 dispatchDraw(canvas);
12632 } else {
12633 draw(canvas);
12634 }
12635
12636 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012637 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012638
12639 if (attachInfo != null) {
12640 // Restore the cached Canvas for our siblings
12641 attachInfo.mCanvas = canvas;
12642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 }
12644 }
12645
12646 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012647 * Create a snapshot of the view into a bitmap. We should probably make
12648 * some form of this public, but should think about the API.
12649 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012650 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012651 int width = mRight - mLeft;
12652 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012653
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012654 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012655 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012656 width = (int) ((width * scale) + 0.5f);
12657 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012658
Dianne Hackborndde331c2012-08-03 14:01:57 -070012659 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12660 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012661 if (bitmap == null) {
12662 throw new OutOfMemoryError();
12663 }
12664
Romain Guyc529d8d2011-09-06 15:01:39 -070012665 Resources resources = getResources();
12666 if (resources != null) {
12667 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12668 }
Joe Malin32736f02011-01-19 16:14:20 -080012669
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012670 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012671 if (attachInfo != null) {
12672 canvas = attachInfo.mCanvas;
12673 if (canvas == null) {
12674 canvas = new Canvas();
12675 }
12676 canvas.setBitmap(bitmap);
12677 // Temporarily clobber the cached Canvas in case one of our children
12678 // is also using a drawing cache. Without this, the children would
12679 // steal the canvas by attaching their own bitmap to it and bad, bad
12680 // things would happen (invisible views, corrupted drawings, etc.)
12681 attachInfo.mCanvas = null;
12682 } else {
12683 // This case should hopefully never or seldom happen
12684 canvas = new Canvas(bitmap);
12685 }
12686
Romain Guy5bcdff42009-05-14 21:27:18 -070012687 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012688 bitmap.eraseColor(backgroundColor);
12689 }
12690
12691 computeScroll();
12692 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012693 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012694 canvas.translate(-mScrollX, -mScrollY);
12695
Romain Guy5bcdff42009-05-14 21:27:18 -070012696 // Temporarily remove the dirty mask
12697 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012698 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012699
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012700 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012701 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012702 dispatchDraw(canvas);
12703 } else {
12704 draw(canvas);
12705 }
12706
Romain Guy5bcdff42009-05-14 21:27:18 -070012707 mPrivateFlags = flags;
12708
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012709 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012710 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012711
12712 if (attachInfo != null) {
12713 // Restore the cached Canvas for our siblings
12714 attachInfo.mCanvas = canvas;
12715 }
Romain Guy8506ab42009-06-11 17:35:47 -070012716
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012717 return bitmap;
12718 }
12719
12720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721 * Indicates whether this View is currently in edit mode. A View is usually
12722 * in edit mode when displayed within a developer tool. For instance, if
12723 * this View is being drawn by a visual user interface builder, this method
12724 * should return true.
12725 *
12726 * Subclasses should check the return value of this method to provide
12727 * different behaviors if their normal behavior might interfere with the
12728 * host environment. For instance: the class spawns a thread in its
12729 * constructor, the drawing code relies on device-specific features, etc.
12730 *
12731 * This method is usually checked in the drawing code of custom widgets.
12732 *
12733 * @return True if this View is in edit mode, false otherwise.
12734 */
12735 public boolean isInEditMode() {
12736 return false;
12737 }
12738
12739 /**
12740 * If the View draws content inside its padding and enables fading edges,
12741 * it needs to support padding offsets. Padding offsets are added to the
12742 * fading edges to extend the length of the fade so that it covers pixels
12743 * drawn inside the padding.
12744 *
12745 * Subclasses of this class should override this method if they need
12746 * to draw content inside the padding.
12747 *
12748 * @return True if padding offset must be applied, false otherwise.
12749 *
12750 * @see #getLeftPaddingOffset()
12751 * @see #getRightPaddingOffset()
12752 * @see #getTopPaddingOffset()
12753 * @see #getBottomPaddingOffset()
12754 *
12755 * @since CURRENT
12756 */
12757 protected boolean isPaddingOffsetRequired() {
12758 return false;
12759 }
12760
12761 /**
12762 * Amount by which to extend the left fading region. Called only when
12763 * {@link #isPaddingOffsetRequired()} returns true.
12764 *
12765 * @return The left padding offset in pixels.
12766 *
12767 * @see #isPaddingOffsetRequired()
12768 *
12769 * @since CURRENT
12770 */
12771 protected int getLeftPaddingOffset() {
12772 return 0;
12773 }
12774
12775 /**
12776 * Amount by which to extend the right fading region. Called only when
12777 * {@link #isPaddingOffsetRequired()} returns true.
12778 *
12779 * @return The right padding offset in pixels.
12780 *
12781 * @see #isPaddingOffsetRequired()
12782 *
12783 * @since CURRENT
12784 */
12785 protected int getRightPaddingOffset() {
12786 return 0;
12787 }
12788
12789 /**
12790 * Amount by which to extend the top fading region. Called only when
12791 * {@link #isPaddingOffsetRequired()} returns true.
12792 *
12793 * @return The top padding offset in pixels.
12794 *
12795 * @see #isPaddingOffsetRequired()
12796 *
12797 * @since CURRENT
12798 */
12799 protected int getTopPaddingOffset() {
12800 return 0;
12801 }
12802
12803 /**
12804 * Amount by which to extend the bottom fading region. Called only when
12805 * {@link #isPaddingOffsetRequired()} returns true.
12806 *
12807 * @return The bottom padding offset in pixels.
12808 *
12809 * @see #isPaddingOffsetRequired()
12810 *
12811 * @since CURRENT
12812 */
12813 protected int getBottomPaddingOffset() {
12814 return 0;
12815 }
12816
12817 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012818 * @hide
12819 * @param offsetRequired
12820 */
12821 protected int getFadeTop(boolean offsetRequired) {
12822 int top = mPaddingTop;
12823 if (offsetRequired) top += getTopPaddingOffset();
12824 return top;
12825 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012826
Romain Guyf2fc4602011-07-19 15:20:03 -070012827 /**
12828 * @hide
12829 * @param offsetRequired
12830 */
12831 protected int getFadeHeight(boolean offsetRequired) {
12832 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012833 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012834 return mBottom - mTop - mPaddingBottom - padding;
12835 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012836
Romain Guyf2fc4602011-07-19 15:20:03 -070012837 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012838 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012839 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012840 *
Romain Guy2bffd262010-09-12 17:40:02 -070012841 * <p>Even if this method returns true, it does not mean that every call
12842 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12843 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012844 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012845 * window is hardware accelerated,
12846 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12847 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012848 *
Romain Guy2bffd262010-09-12 17:40:02 -070012849 * @return True if the view is attached to a window and the window is
12850 * hardware accelerated; false in any other case.
12851 */
12852 public boolean isHardwareAccelerated() {
12853 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12854 }
Joe Malin32736f02011-01-19 16:14:20 -080012855
Romain Guy2bffd262010-09-12 17:40:02 -070012856 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012857 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12858 * case of an active Animation being run on the view.
12859 */
12860 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12861 Animation a, boolean scalingRequired) {
12862 Transformation invalidationTransform;
12863 final int flags = parent.mGroupFlags;
12864 final boolean initialized = a.isInitialized();
12865 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012866 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012867 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070012868 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080012869 onAnimationStart();
12870 }
12871
12872 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12873 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12874 if (parent.mInvalidationTransformation == null) {
12875 parent.mInvalidationTransformation = new Transformation();
12876 }
12877 invalidationTransform = parent.mInvalidationTransformation;
12878 a.getTransformation(drawingTime, invalidationTransform, 1f);
12879 } else {
12880 invalidationTransform = parent.mChildTransformation;
12881 }
Romain Guy393a52c2012-05-22 20:21:08 -070012882
Chet Haasebcca79a2012-02-14 08:45:14 -080012883 if (more) {
12884 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070012885 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
12886 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
12887 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
12888 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012889 // The child need to draw an animation, potentially offscreen, so
12890 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012891 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012892 parent.invalidate(mLeft, mTop, mRight, mBottom);
12893 }
12894 } else {
12895 if (parent.mInvalidateRegion == null) {
12896 parent.mInvalidateRegion = new RectF();
12897 }
12898 final RectF region = parent.mInvalidateRegion;
12899 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12900 invalidationTransform);
12901
12902 // The child need to draw an animation, potentially offscreen, so
12903 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070012904 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080012905
12906 final int left = mLeft + (int) region.left;
12907 final int top = mTop + (int) region.top;
12908 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12909 top + (int) (region.height() + .5f));
12910 }
12911 }
12912 return more;
12913 }
12914
Chet Haasea1cff502012-02-21 13:43:44 -080012915 /**
12916 * This method is called by getDisplayList() when a display list is created or re-rendered.
12917 * It sets or resets the current value of all properties on that display list (resetting is
12918 * necessary when a display list is being re-created, because we need to make sure that
12919 * previously-set transform values
12920 */
12921 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012922 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012923 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012924 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012925 if (mParent instanceof ViewGroup) {
12926 displayList.setClipChildren(
12927 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12928 }
Chet Haase9420abd2012-03-29 16:28:32 -070012929 float alpha = 1;
12930 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12931 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12932 ViewGroup parentVG = (ViewGroup) mParent;
12933 final boolean hasTransform =
12934 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12935 if (hasTransform) {
12936 Transformation transform = parentVG.mChildTransformation;
12937 final int transformType = parentVG.mChildTransformation.getTransformationType();
12938 if (transformType != Transformation.TYPE_IDENTITY) {
12939 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12940 alpha = transform.getAlpha();
12941 }
12942 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12943 displayList.setStaticMatrix(transform.getMatrix());
12944 }
12945 }
12946 }
Chet Haasea1cff502012-02-21 13:43:44 -080012947 }
12948 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012949 alpha *= mTransformationInfo.mAlpha;
12950 if (alpha < 1) {
12951 final int multipliedAlpha = (int) (255 * alpha);
12952 if (onSetAlpha(multipliedAlpha)) {
12953 alpha = 1;
12954 }
12955 }
12956 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012957 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12958 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12959 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12960 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012961 if (mTransformationInfo.mCamera == null) {
12962 mTransformationInfo.mCamera = new Camera();
12963 mTransformationInfo.matrix3D = new Matrix();
12964 }
12965 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070012966 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080012967 displayList.setPivotX(getPivotX());
12968 displayList.setPivotY(getPivotY());
12969 }
Chet Haase9420abd2012-03-29 16:28:32 -070012970 } else if (alpha < 1) {
12971 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012972 }
12973 }
12974 }
12975
Chet Haasebcca79a2012-02-14 08:45:14 -080012976 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012977 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12978 * This draw() method is an implementation detail and is not intended to be overridden or
12979 * to be called from anywhere else other than ViewGroup.drawChild().
12980 */
12981 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012982 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012983 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012984 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012985 final int flags = parent.mGroupFlags;
12986
Chet Haasea1cff502012-02-21 13:43:44 -080012987 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012988 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012989 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012990 }
12991
12992 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012993 boolean concatMatrix = false;
12994
12995 boolean scalingRequired = false;
12996 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070012997 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080012998
12999 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013000 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13001 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013002 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013003 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013004 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13005 } else {
13006 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13007 }
13008
Chet Haasebcca79a2012-02-14 08:45:14 -080013009 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013010 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013011 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013012 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013013 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013014 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013015 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013016 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013017 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013018 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013019 mDisplayList != null) {
13020 // No longer animating: clear out old animation matrix
13021 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013022 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013023 }
13024 if (!useDisplayListProperties &&
13025 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13026 final boolean hasTransform =
13027 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13028 if (hasTransform) {
13029 final int transformType = parent.mChildTransformation.getTransformationType();
13030 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13031 parent.mChildTransformation : null;
13032 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13033 }
Chet Haase64a48c12012-02-13 16:33:29 -080013034 }
13035 }
13036
13037 concatMatrix |= !childHasIdentityMatrix;
13038
13039 // Sets the flag as early as possible to allow draw() implementations
13040 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013041 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013042
Chet Haase599913d2012-07-23 16:22:05 -070013043 if (!concatMatrix && (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) == 0 &&
13044 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013045 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13046 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013047 return more;
13048 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013049 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013050
13051 if (hardwareAccelerated) {
13052 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13053 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013054 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13055 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013056 }
13057
Chet Haase64a48c12012-02-13 16:33:29 -080013058 DisplayList displayList = null;
13059 Bitmap cache = null;
13060 boolean hasDisplayList = false;
13061 if (caching) {
13062 if (!hardwareAccelerated) {
13063 if (layerType != LAYER_TYPE_NONE) {
13064 layerType = LAYER_TYPE_SOFTWARE;
13065 buildDrawingCache(true);
13066 }
13067 cache = getDrawingCache(true);
13068 } else {
13069 switch (layerType) {
13070 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013071 if (useDisplayListProperties) {
13072 hasDisplayList = canHaveDisplayList();
13073 } else {
13074 buildDrawingCache(true);
13075 cache = getDrawingCache(true);
13076 }
Chet Haase64a48c12012-02-13 16:33:29 -080013077 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013078 case LAYER_TYPE_HARDWARE:
13079 if (useDisplayListProperties) {
13080 hasDisplayList = canHaveDisplayList();
13081 }
13082 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013083 case LAYER_TYPE_NONE:
13084 // Delay getting the display list until animation-driven alpha values are
13085 // set up and possibly passed on to the view
13086 hasDisplayList = canHaveDisplayList();
13087 break;
13088 }
13089 }
13090 }
Chet Haasea1cff502012-02-21 13:43:44 -080013091 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013092 if (useDisplayListProperties) {
13093 displayList = getDisplayList();
13094 if (!displayList.isValid()) {
13095 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13096 // to getDisplayList(), the display list will be marked invalid and we should not
13097 // try to use it again.
13098 displayList = null;
13099 hasDisplayList = false;
13100 useDisplayListProperties = false;
13101 }
13102 }
Chet Haase64a48c12012-02-13 16:33:29 -080013103
Chet Haase526057b2012-07-12 17:50:41 -070013104 int sx = 0;
13105 int sy = 0;
13106 if (!hasDisplayList) {
13107 computeScroll();
13108 sx = mScrollX;
13109 sy = mScrollY;
13110 }
13111
Chet Haase64a48c12012-02-13 16:33:29 -080013112 final boolean hasNoCache = cache == null || hasDisplayList;
13113 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13114 layerType != LAYER_TYPE_HARDWARE;
13115
Chet Haasea1cff502012-02-21 13:43:44 -080013116 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013117 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013118 restoreTo = canvas.save();
13119 }
Chet Haase64a48c12012-02-13 16:33:29 -080013120 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013121 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013122 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013123 if (!useDisplayListProperties) {
13124 canvas.translate(mLeft, mTop);
13125 }
Chet Haase64a48c12012-02-13 16:33:29 -080013126 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013127 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013128 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013129 restoreTo = canvas.save();
13130 }
Chet Haase64a48c12012-02-13 16:33:29 -080013131 // mAttachInfo cannot be null, otherwise scalingRequired == false
13132 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13133 canvas.scale(scale, scale);
13134 }
13135 }
13136
Chet Haasea1cff502012-02-21 13:43:44 -080013137 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013138 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013139 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013140 if (transformToApply != null || !childHasIdentityMatrix) {
13141 int transX = 0;
13142 int transY = 0;
13143
13144 if (offsetForScroll) {
13145 transX = -sx;
13146 transY = -sy;
13147 }
13148
13149 if (transformToApply != null) {
13150 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013151 if (useDisplayListProperties) {
13152 displayList.setAnimationMatrix(transformToApply.getMatrix());
13153 } else {
13154 // Undo the scroll translation, apply the transformation matrix,
13155 // then redo the scroll translate to get the correct result.
13156 canvas.translate(-transX, -transY);
13157 canvas.concat(transformToApply.getMatrix());
13158 canvas.translate(transX, transY);
13159 }
Chet Haasea1cff502012-02-21 13:43:44 -080013160 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013161 }
13162
13163 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013164 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013165 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013166 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013167 }
13168 }
13169
Chet Haasea1cff502012-02-21 13:43:44 -080013170 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013171 canvas.translate(-transX, -transY);
13172 canvas.concat(getMatrix());
13173 canvas.translate(transX, transY);
13174 }
13175 }
13176
Chet Haase21433372012-06-05 07:54:09 -070013177 // Deal with alpha if it is or used to be <1
13178 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013179 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013180 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013181 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013182 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013183 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013184 }
Chet Haasea1cff502012-02-21 13:43:44 -080013185 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013186 if (hasNoCache) {
13187 final int multipliedAlpha = (int) (255 * alpha);
13188 if (!onSetAlpha(multipliedAlpha)) {
13189 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013190 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013191 layerType != LAYER_TYPE_NONE) {
13192 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13193 }
Chet Haase9420abd2012-03-29 16:28:32 -070013194 if (useDisplayListProperties) {
13195 displayList.setAlpha(alpha * getAlpha());
13196 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013197 final int scrollX = hasDisplayList ? 0 : sx;
13198 final int scrollY = hasDisplayList ? 0 : sy;
13199 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13200 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013201 }
13202 } else {
13203 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013204 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013205 }
13206 }
13207 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013208 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013209 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013210 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013211 }
13212
Chet Haasea1cff502012-02-21 13:43:44 -080013213 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13214 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013215 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013216 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013217 } else {
13218 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013219 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013220 } else {
13221 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13222 }
13223 }
13224 }
13225
Chet Haase9420abd2012-03-29 16:28:32 -070013226 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013227 displayList = getDisplayList();
13228 if (!displayList.isValid()) {
13229 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13230 // to getDisplayList(), the display list will be marked invalid and we should not
13231 // try to use it again.
13232 displayList = null;
13233 hasDisplayList = false;
13234 }
13235 }
13236
13237 if (hasNoCache) {
13238 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013239 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013240 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013241 if (layer != null && layer.isValid()) {
13242 mLayerPaint.setAlpha((int) (alpha * 255));
13243 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13244 layerRendered = true;
13245 } else {
13246 final int scrollX = hasDisplayList ? 0 : sx;
13247 final int scrollY = hasDisplayList ? 0 : sy;
13248 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013249 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013250 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13251 }
13252 }
13253
13254 if (!layerRendered) {
13255 if (!hasDisplayList) {
13256 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013257 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13258 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013259 dispatchDraw(canvas);
13260 } else {
13261 draw(canvas);
13262 }
13263 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013264 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013265 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013266 }
13267 }
13268 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013269 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013270 Paint cachePaint;
13271
13272 if (layerType == LAYER_TYPE_NONE) {
13273 cachePaint = parent.mCachePaint;
13274 if (cachePaint == null) {
13275 cachePaint = new Paint();
13276 cachePaint.setDither(false);
13277 parent.mCachePaint = cachePaint;
13278 }
Chet Haase9420abd2012-03-29 16:28:32 -070013279 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013280 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013281 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13282 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013283 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013284 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013285 }
13286 } else {
13287 cachePaint = mLayerPaint;
13288 cachePaint.setAlpha((int) (alpha * 255));
13289 }
13290 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13291 }
13292
Chet Haasea1cff502012-02-21 13:43:44 -080013293 if (restoreTo >= 0) {
13294 canvas.restoreToCount(restoreTo);
13295 }
Chet Haase64a48c12012-02-13 16:33:29 -080013296
13297 if (a != null && !more) {
13298 if (!hardwareAccelerated && !a.getFillAfter()) {
13299 onSetAlpha(255);
13300 }
13301 parent.finishAnimatingView(this, a);
13302 }
13303
13304 if (more && hardwareAccelerated) {
13305 // invalidation is the trigger to recreate display lists, so if we're using
13306 // display lists to render, force an invalidate to allow the animation to
13307 // continue drawing another frame
13308 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013309 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013310 // alpha animations should cause the child to recreate its display list
13311 invalidate(true);
13312 }
13313 }
13314
13315 mRecreateDisplayList = false;
13316
13317 return more;
13318 }
13319
13320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013321 * Manually render this view (and all of its children) to the given Canvas.
13322 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013323 * called. When implementing a view, implement
13324 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13325 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013326 *
13327 * @param canvas The Canvas to which the View is rendered.
13328 */
13329 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013330 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013331 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013332 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013333 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013335 /*
13336 * Draw traversal performs several drawing steps which must be executed
13337 * in the appropriate order:
13338 *
13339 * 1. Draw the background
13340 * 2. If necessary, save the canvas' layers to prepare for fading
13341 * 3. Draw view's content
13342 * 4. Draw children
13343 * 5. If necessary, draw the fading edges and restore layers
13344 * 6. Draw decorations (scrollbars for instance)
13345 */
13346
13347 // Step 1, draw the background, if needed
13348 int saveCount;
13349
Romain Guy24443ea2009-05-11 11:56:30 -070013350 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013351 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013352 if (background != null) {
13353 final int scrollX = mScrollX;
13354 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013355
Romain Guy24443ea2009-05-11 11:56:30 -070013356 if (mBackgroundSizeChanged) {
13357 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13358 mBackgroundSizeChanged = false;
13359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013360
Romain Guy24443ea2009-05-11 11:56:30 -070013361 if ((scrollX | scrollY) == 0) {
13362 background.draw(canvas);
13363 } else {
13364 canvas.translate(scrollX, scrollY);
13365 background.draw(canvas);
13366 canvas.translate(-scrollX, -scrollY);
13367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013368 }
13369 }
13370
13371 // skip step 2 & 5 if possible (common case)
13372 final int viewFlags = mViewFlags;
13373 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13374 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13375 if (!verticalEdges && !horizontalEdges) {
13376 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013377 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013378
13379 // Step 4, draw the children
13380 dispatchDraw(canvas);
13381
13382 // Step 6, draw decorations (scrollbars)
13383 onDrawScrollBars(canvas);
13384
13385 // we're done...
13386 return;
13387 }
13388
13389 /*
13390 * Here we do the full fledged routine...
13391 * (this is an uncommon case where speed matters less,
13392 * this is why we repeat some of the tests that have been
13393 * done above)
13394 */
13395
13396 boolean drawTop = false;
13397 boolean drawBottom = false;
13398 boolean drawLeft = false;
13399 boolean drawRight = false;
13400
13401 float topFadeStrength = 0.0f;
13402 float bottomFadeStrength = 0.0f;
13403 float leftFadeStrength = 0.0f;
13404 float rightFadeStrength = 0.0f;
13405
13406 // Step 2, save the canvas' layers
13407 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408
13409 final boolean offsetRequired = isPaddingOffsetRequired();
13410 if (offsetRequired) {
13411 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013412 }
13413
13414 int left = mScrollX + paddingLeft;
13415 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013416 int top = mScrollY + getFadeTop(offsetRequired);
13417 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013418
13419 if (offsetRequired) {
13420 right += getRightPaddingOffset();
13421 bottom += getBottomPaddingOffset();
13422 }
13423
13424 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013425 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013426 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013427
13428 // clip the fade length if top and bottom fades overlap
13429 // overlapping fades produce odd-looking artifacts
13430 if (verticalEdges && (top + length > bottom - length)) {
13431 length = (bottom - top) / 2;
13432 }
13433
13434 // also clip horizontal fades if necessary
13435 if (horizontalEdges && (left + length > right - length)) {
13436 length = (right - left) / 2;
13437 }
13438
13439 if (verticalEdges) {
13440 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013441 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013442 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013443 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 }
13445
13446 if (horizontalEdges) {
13447 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013448 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013449 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013450 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013451 }
13452
13453 saveCount = canvas.getSaveCount();
13454
13455 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013456 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013457 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13458
13459 if (drawTop) {
13460 canvas.saveLayer(left, top, right, top + length, null, flags);
13461 }
13462
13463 if (drawBottom) {
13464 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13465 }
13466
13467 if (drawLeft) {
13468 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13469 }
13470
13471 if (drawRight) {
13472 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13473 }
13474 } else {
13475 scrollabilityCache.setFadeColor(solidColor);
13476 }
13477
13478 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013479 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013480
13481 // Step 4, draw the children
13482 dispatchDraw(canvas);
13483
13484 // Step 5, draw the fade effect and restore layers
13485 final Paint p = scrollabilityCache.paint;
13486 final Matrix matrix = scrollabilityCache.matrix;
13487 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488
13489 if (drawTop) {
13490 matrix.setScale(1, fadeHeight * topFadeStrength);
13491 matrix.postTranslate(left, top);
13492 fade.setLocalMatrix(matrix);
13493 canvas.drawRect(left, top, right, top + length, p);
13494 }
13495
13496 if (drawBottom) {
13497 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13498 matrix.postRotate(180);
13499 matrix.postTranslate(left, bottom);
13500 fade.setLocalMatrix(matrix);
13501 canvas.drawRect(left, bottom - length, right, bottom, p);
13502 }
13503
13504 if (drawLeft) {
13505 matrix.setScale(1, fadeHeight * leftFadeStrength);
13506 matrix.postRotate(-90);
13507 matrix.postTranslate(left, top);
13508 fade.setLocalMatrix(matrix);
13509 canvas.drawRect(left, top, left + length, bottom, p);
13510 }
13511
13512 if (drawRight) {
13513 matrix.setScale(1, fadeHeight * rightFadeStrength);
13514 matrix.postRotate(90);
13515 matrix.postTranslate(right, top);
13516 fade.setLocalMatrix(matrix);
13517 canvas.drawRect(right - length, top, right, bottom, p);
13518 }
13519
13520 canvas.restoreToCount(saveCount);
13521
13522 // Step 6, draw decorations (scrollbars)
13523 onDrawScrollBars(canvas);
13524 }
13525
13526 /**
13527 * Override this if your view is known to always be drawn on top of a solid color background,
13528 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13529 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13530 * should be set to 0xFF.
13531 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013532 * @see #setVerticalFadingEdgeEnabled(boolean)
13533 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013534 *
13535 * @return The known solid color background for this view, or 0 if the color may vary
13536 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013537 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013538 public int getSolidColor() {
13539 return 0;
13540 }
13541
13542 /**
13543 * Build a human readable string representation of the specified view flags.
13544 *
13545 * @param flags the view flags to convert to a string
13546 * @return a String representing the supplied flags
13547 */
13548 private static String printFlags(int flags) {
13549 String output = "";
13550 int numFlags = 0;
13551 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13552 output += "TAKES_FOCUS";
13553 numFlags++;
13554 }
13555
13556 switch (flags & VISIBILITY_MASK) {
13557 case INVISIBLE:
13558 if (numFlags > 0) {
13559 output += " ";
13560 }
13561 output += "INVISIBLE";
13562 // USELESS HERE numFlags++;
13563 break;
13564 case GONE:
13565 if (numFlags > 0) {
13566 output += " ";
13567 }
13568 output += "GONE";
13569 // USELESS HERE numFlags++;
13570 break;
13571 default:
13572 break;
13573 }
13574 return output;
13575 }
13576
13577 /**
13578 * Build a human readable string representation of the specified private
13579 * view flags.
13580 *
13581 * @param privateFlags the private view flags to convert to a string
13582 * @return a String representing the supplied flags
13583 */
13584 private static String printPrivateFlags(int privateFlags) {
13585 String output = "";
13586 int numFlags = 0;
13587
Dianne Hackborn4702a852012-08-17 15:18:29 -070013588 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013589 output += "WANTS_FOCUS";
13590 numFlags++;
13591 }
13592
Dianne Hackborn4702a852012-08-17 15:18:29 -070013593 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013594 if (numFlags > 0) {
13595 output += " ";
13596 }
13597 output += "FOCUSED";
13598 numFlags++;
13599 }
13600
Dianne Hackborn4702a852012-08-17 15:18:29 -070013601 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 if (numFlags > 0) {
13603 output += " ";
13604 }
13605 output += "SELECTED";
13606 numFlags++;
13607 }
13608
Dianne Hackborn4702a852012-08-17 15:18:29 -070013609 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610 if (numFlags > 0) {
13611 output += " ";
13612 }
13613 output += "IS_ROOT_NAMESPACE";
13614 numFlags++;
13615 }
13616
Dianne Hackborn4702a852012-08-17 15:18:29 -070013617 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013618 if (numFlags > 0) {
13619 output += " ";
13620 }
13621 output += "HAS_BOUNDS";
13622 numFlags++;
13623 }
13624
Dianne Hackborn4702a852012-08-17 15:18:29 -070013625 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013626 if (numFlags > 0) {
13627 output += " ";
13628 }
13629 output += "DRAWN";
13630 // USELESS HERE numFlags++;
13631 }
13632 return output;
13633 }
13634
13635 /**
13636 * <p>Indicates whether or not this view's layout will be requested during
13637 * the next hierarchy layout pass.</p>
13638 *
13639 * @return true if the layout will be forced during next layout pass
13640 */
13641 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013642 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 }
13644
13645 /**
13646 * Assign a size and position to a view and all of its
13647 * descendants
13648 *
13649 * <p>This is the second phase of the layout mechanism.
13650 * (The first is measuring). In this phase, each parent calls
13651 * layout on all of its children to position them.
13652 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013653 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013654 *
Chet Haase9c087442011-01-12 16:20:16 -080013655 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 * Derived classes with children should override
13657 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013658 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013659 *
13660 * @param l Left position, relative to parent
13661 * @param t Top position, relative to parent
13662 * @param r Right position, relative to parent
13663 * @param b Bottom position, relative to parent
13664 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013665 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013666 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013667 int oldL = mLeft;
13668 int oldT = mTop;
13669 int oldB = mBottom;
13670 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013672 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013673 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013674 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013675
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013676 ListenerInfo li = mListenerInfo;
13677 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013678 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013679 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013680 int numListeners = listenersCopy.size();
13681 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013682 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013683 }
13684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013685 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013686 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013687 }
13688
13689 /**
13690 * Called from layout when this view should
13691 * assign a size and position to each of its children.
13692 *
13693 * Derived classes with children should override
13694 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013695 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013696 * @param changed This is a new size or position for this view
13697 * @param left Left position, relative to parent
13698 * @param top Top position, relative to parent
13699 * @param right Right position, relative to parent
13700 * @param bottom Bottom position, relative to parent
13701 */
13702 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13703 }
13704
13705 /**
13706 * Assign a size and position to this view.
13707 *
13708 * This is called from layout.
13709 *
13710 * @param left Left position, relative to parent
13711 * @param top Top position, relative to parent
13712 * @param right Right position, relative to parent
13713 * @param bottom Bottom position, relative to parent
13714 * @return true if the new size and position are different than the
13715 * previous ones
13716 * {@hide}
13717 */
13718 protected boolean setFrame(int left, int top, int right, int bottom) {
13719 boolean changed = false;
13720
13721 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013722 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723 + right + "," + bottom + ")");
13724 }
13725
13726 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13727 changed = true;
13728
13729 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013730 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013732 int oldWidth = mRight - mLeft;
13733 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013734 int newWidth = right - left;
13735 int newHeight = bottom - top;
13736 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13737
13738 // Invalidate our old position
13739 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740
13741 mLeft = left;
13742 mTop = top;
13743 mRight = right;
13744 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013745 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013746 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013748
Dianne Hackborn4702a852012-08-17 15:18:29 -070013749 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751
Chet Haase75755e22011-07-18 17:48:25 -070013752 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013753 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013754 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013755 if (mTransformationInfo != null) {
13756 mTransformationInfo.mMatrixDirty = true;
13757 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13760 }
13761
13762 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13763 // If we are visible, force the DRAWN bit to on so that
13764 // this invalidate will go through (at least to our parent).
13765 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013766 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013767 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013768 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013769 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013770 // parent display list may need to be recreated based on a change in the bounds
13771 // of any child
13772 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013773 }
13774
13775 // Reset drawn bit to original value (invalidate turns it off)
13776 mPrivateFlags |= drawn;
13777
13778 mBackgroundSizeChanged = true;
13779 }
13780 return changed;
13781 }
13782
13783 /**
13784 * Finalize inflating a view from XML. This is called as the last phase
13785 * of inflation, after all child views have been added.
13786 *
13787 * <p>Even if the subclass overrides onFinishInflate, they should always be
13788 * sure to call the super method, so that we get called.
13789 */
13790 protected void onFinishInflate() {
13791 }
13792
13793 /**
13794 * Returns the resources associated with this view.
13795 *
13796 * @return Resources object.
13797 */
13798 public Resources getResources() {
13799 return mResources;
13800 }
13801
13802 /**
13803 * Invalidates the specified Drawable.
13804 *
13805 * @param drawable the drawable to invalidate
13806 */
13807 public void invalidateDrawable(Drawable drawable) {
13808 if (verifyDrawable(drawable)) {
13809 final Rect dirty = drawable.getBounds();
13810 final int scrollX = mScrollX;
13811 final int scrollY = mScrollY;
13812
13813 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13814 dirty.right + scrollX, dirty.bottom + scrollY);
13815 }
13816 }
13817
13818 /**
13819 * Schedules an action on a drawable to occur at a specified time.
13820 *
13821 * @param who the recipient of the action
13822 * @param what the action to run on the drawable
13823 * @param when the time at which the action must occur. Uses the
13824 * {@link SystemClock#uptimeMillis} timebase.
13825 */
13826 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013827 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013828 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013829 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013830 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13831 Choreographer.CALLBACK_ANIMATION, what, who,
13832 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013833 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013834 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013836 }
13837 }
13838
13839 /**
13840 * Cancels a scheduled action on a drawable.
13841 *
13842 * @param who the recipient of the action
13843 * @param what the action to cancel
13844 */
13845 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013846 if (verifyDrawable(who) && what != null) {
13847 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013848 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13849 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013850 } else {
13851 ViewRootImpl.getRunQueue().removeCallbacks(what);
13852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013853 }
13854 }
13855
13856 /**
13857 * Unschedule any events associated with the given Drawable. This can be
13858 * used when selecting a new Drawable into a view, so that the previous
13859 * one is completely unscheduled.
13860 *
13861 * @param who The Drawable to unschedule.
13862 *
13863 * @see #drawableStateChanged
13864 */
13865 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013866 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013867 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13868 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869 }
13870 }
13871
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013872 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070013873 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
13874 * that the View directionality can and will be resolved before its Drawables.
13875 *
13876 * Will call {@link View#onResolveDrawables} when resolution is done.
13877 */
13878 public void resolveDrawables() {
13879 if (mBackground != null) {
13880 mBackground.setLayoutDirection(getResolvedLayoutDirection());
13881 }
13882 onResolveDrawables(getResolvedLayoutDirection());
13883 }
13884
13885 /**
13886 * Called when layout direction has been resolved.
13887 *
13888 * The default implementation does nothing.
13889 *
13890 * @param layoutDirection The resolved layout direction.
13891 *
13892 * @see {@link #LAYOUT_DIRECTION_LTR}
13893 * @see {@link #LAYOUT_DIRECTION_RTL}
13894 */
13895 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013896 }
13897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013898 /**
13899 * If your view subclass is displaying its own Drawable objects, it should
13900 * override this function and return true for any Drawable it is
13901 * displaying. This allows animations for those drawables to be
13902 * scheduled.
13903 *
13904 * <p>Be sure to call through to the super class when overriding this
13905 * function.
13906 *
13907 * @param who The Drawable to verify. Return true if it is one you are
13908 * displaying, else return the result of calling through to the
13909 * super class.
13910 *
13911 * @return boolean If true than the Drawable is being displayed in the
13912 * view; else false and it is not allowed to animate.
13913 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013914 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13915 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013916 */
13917 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013918 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 }
13920
13921 /**
13922 * This function is called whenever the state of the view changes in such
13923 * a way that it impacts the state of drawables being shown.
13924 *
13925 * <p>Be sure to call through to the superclass when overriding this
13926 * function.
13927 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013928 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929 */
13930 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013931 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013932 if (d != null && d.isStateful()) {
13933 d.setState(getDrawableState());
13934 }
13935 }
13936
13937 /**
13938 * Call this to force a view to update its drawable state. This will cause
13939 * drawableStateChanged to be called on this view. Views that are interested
13940 * in the new state should call getDrawableState.
13941 *
13942 * @see #drawableStateChanged
13943 * @see #getDrawableState
13944 */
13945 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013946 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013947 drawableStateChanged();
13948
13949 ViewParent parent = mParent;
13950 if (parent != null) {
13951 parent.childDrawableStateChanged(this);
13952 }
13953 }
13954
13955 /**
13956 * Return an array of resource IDs of the drawable states representing the
13957 * current state of the view.
13958 *
13959 * @return The current drawable state
13960 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013961 * @see Drawable#setState(int[])
13962 * @see #drawableStateChanged()
13963 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013964 */
13965 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013966 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 return mDrawableState;
13968 } else {
13969 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013970 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971 return mDrawableState;
13972 }
13973 }
13974
13975 /**
13976 * Generate the new {@link android.graphics.drawable.Drawable} state for
13977 * this view. This is called by the view
13978 * system when the cached Drawable state is determined to be invalid. To
13979 * retrieve the current state, you should use {@link #getDrawableState}.
13980 *
13981 * @param extraSpace if non-zero, this is the number of extra entries you
13982 * would like in the returned array in which you can place your own
13983 * states.
13984 *
13985 * @return Returns an array holding the current {@link Drawable} state of
13986 * the view.
13987 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013988 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 */
13990 protected int[] onCreateDrawableState(int extraSpace) {
13991 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13992 mParent instanceof View) {
13993 return ((View) mParent).onCreateDrawableState(extraSpace);
13994 }
13995
13996 int[] drawableState;
13997
13998 int privateFlags = mPrivateFlags;
13999
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014000 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014001 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014002 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14003 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014004 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014005 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014006 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014007 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14008 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014009 // This is set if HW acceleration is requested, even if the current
14010 // process doesn't allow it. This is just to allow app preview
14011 // windows to better match their app.
14012 viewStateIndex |= VIEW_STATE_ACCELERATED;
14013 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014014 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014015
Christopher Tate3d4bf172011-03-28 16:16:46 -070014016 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014017 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14018 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014020 drawableState = VIEW_STATE_SETS[viewStateIndex];
14021
14022 //noinspection ConstantIfStatement
14023 if (false) {
14024 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14025 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014026 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14028 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014029 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014030 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 + ": " + Arrays.toString(drawableState));
14032 }
14033
14034 if (extraSpace == 0) {
14035 return drawableState;
14036 }
14037
14038 final int[] fullState;
14039 if (drawableState != null) {
14040 fullState = new int[drawableState.length + extraSpace];
14041 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14042 } else {
14043 fullState = new int[extraSpace];
14044 }
14045
14046 return fullState;
14047 }
14048
14049 /**
14050 * Merge your own state values in <var>additionalState</var> into the base
14051 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014052 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014053 *
14054 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014055 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056 * own additional state values.
14057 *
14058 * @param additionalState The additional state values you would like
14059 * added to <var>baseState</var>; this array is not modified.
14060 *
14061 * @return As a convenience, the <var>baseState</var> array you originally
14062 * passed into the function is returned.
14063 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014064 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014065 */
14066 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14067 final int N = baseState.length;
14068 int i = N - 1;
14069 while (i >= 0 && baseState[i] == 0) {
14070 i--;
14071 }
14072 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14073 return baseState;
14074 }
14075
14076 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014077 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14078 * on all Drawable objects associated with this view.
14079 */
14080 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014081 if (mBackground != null) {
14082 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014083 }
14084 }
14085
14086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014087 * Sets the background color for this view.
14088 * @param color the color of the background
14089 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014090 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014092 if (mBackground instanceof ColorDrawable) {
14093 ((ColorDrawable) mBackground).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014094 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014095 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014096 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014098 }
14099
14100 /**
14101 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014102 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014103 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014105 * @attr ref android.R.styleable#View_background
14106 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014107 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108 public void setBackgroundResource(int resid) {
14109 if (resid != 0 && resid == mBackgroundResource) {
14110 return;
14111 }
14112
14113 Drawable d= null;
14114 if (resid != 0) {
14115 d = mResources.getDrawable(resid);
14116 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014117 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014118
14119 mBackgroundResource = resid;
14120 }
14121
14122 /**
14123 * Set the background to a given Drawable, or remove the background. If the
14124 * background has padding, this View's padding is set to the background's
14125 * padding. However, when a background is removed, this View's padding isn't
14126 * touched. If setting the padding is desired, please use
14127 * {@link #setPadding(int, int, int, int)}.
14128 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014129 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014130 * background
14131 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014132 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014133 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014134 setBackgroundDrawable(background);
14135 }
14136
14137 /**
14138 * @deprecated use {@link #setBackground(Drawable)} instead
14139 */
14140 @Deprecated
14141 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014142 computeOpaqueFlags();
14143
Philip Milne6c8ea062012-04-03 17:38:43 -070014144 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014145 return;
14146 }
14147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014148 boolean requestLayout = false;
14149
14150 mBackgroundResource = 0;
14151
14152 /*
14153 * Regardless of whether we're setting a new background or not, we want
14154 * to clear the previous drawable.
14155 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014156 if (mBackground != null) {
14157 mBackground.setCallback(null);
14158 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014159 }
14160
Philip Milne6c8ea062012-04-03 17:38:43 -070014161 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014162 Rect padding = sThreadLocal.get();
14163 if (padding == null) {
14164 padding = new Rect();
14165 sThreadLocal.set(padding);
14166 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014167 background.setLayoutDirection(getResolvedLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014168 if (background.getPadding(padding)) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014169 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014170 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014171 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014172 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014173 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014174 break;
14175 case LAYOUT_DIRECTION_LTR:
14176 default:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014177 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014179 }
14180
14181 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14182 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014183 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14184 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014185 requestLayout = true;
14186 }
14187
Philip Milne6c8ea062012-04-03 17:38:43 -070014188 background.setCallback(this);
14189 if (background.isStateful()) {
14190 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014191 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014192 background.setVisible(getVisibility() == VISIBLE, false);
14193 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014194
Dianne Hackborn4702a852012-08-17 15:18:29 -070014195 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14196 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14197 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014198 requestLayout = true;
14199 }
14200 } else {
14201 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014202 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014203
Dianne Hackborn4702a852012-08-17 15:18:29 -070014204 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014205 /*
14206 * This view ONLY drew the background before and we're removing
14207 * the background, so now it won't draw anything
14208 * (hence we SKIP_DRAW)
14209 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014210 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14211 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212 }
14213
14214 /*
14215 * When the background is set, we try to apply its padding to this
14216 * View. When the background is removed, we don't touch this View's
14217 * padding. This is noted in the Javadocs. Hence, we don't need to
14218 * requestLayout(), the invalidate() below is sufficient.
14219 */
14220
14221 // The old background's minimum size could have affected this
14222 // View's layout, so let's requestLayout
14223 requestLayout = true;
14224 }
14225
Romain Guy8f1344f52009-05-15 16:03:59 -070014226 computeOpaqueFlags();
14227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014228 if (requestLayout) {
14229 requestLayout();
14230 }
14231
14232 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014233 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014234 }
14235
14236 /**
14237 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014239 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014240 *
14241 * @see #setBackground(Drawable)
14242 *
14243 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014244 */
14245 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014246 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 }
14248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014249 /**
14250 * Sets the padding. The view may add on the space required to display
14251 * the scrollbars, depending on the style and visibility of the scrollbars.
14252 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14253 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14254 * from the values set in this call.
14255 *
14256 * @attr ref android.R.styleable#View_padding
14257 * @attr ref android.R.styleable#View_paddingBottom
14258 * @attr ref android.R.styleable#View_paddingLeft
14259 * @attr ref android.R.styleable#View_paddingRight
14260 * @attr ref android.R.styleable#View_paddingTop
14261 * @param left the left padding in pixels
14262 * @param top the top padding in pixels
14263 * @param right the right padding in pixels
14264 * @param bottom the bottom padding in pixels
14265 */
14266 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014267 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014268 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014269
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014270 mUserPaddingStart = UNDEFINED_PADDING;
14271 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014272
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014273 internalSetPadding(left, top, right, bottom);
14274 }
14275
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014276 /**
14277 * @hide
14278 */
14279 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014280 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014281 mUserPaddingRight = right;
14282 mUserPaddingBottom = bottom;
14283
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014284 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014285 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014286
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014287 // Common case is there are no scroll bars.
14288 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014289 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014290 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014291 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014292 switch (mVerticalScrollbarPosition) {
14293 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014294 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014295 left += offset;
14296 } else {
14297 right += offset;
14298 }
14299 break;
Adam Powell20232d02010-12-08 21:08:53 -080014300 case SCROLLBAR_POSITION_RIGHT:
14301 right += offset;
14302 break;
14303 case SCROLLBAR_POSITION_LEFT:
14304 left += offset;
14305 break;
14306 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014307 }
Adam Powell20232d02010-12-08 21:08:53 -080014308 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014309 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14310 ? 0 : getHorizontalScrollbarHeight();
14311 }
14312 }
Romain Guy8506ab42009-06-11 17:35:47 -070014313
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014314 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014315 changed = true;
14316 mPaddingLeft = left;
14317 }
14318 if (mPaddingTop != top) {
14319 changed = true;
14320 mPaddingTop = top;
14321 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014322 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014323 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014324 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014325 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014326 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014327 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014328 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014329 }
14330
14331 if (changed) {
14332 requestLayout();
14333 }
14334 }
14335
14336 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014337 * Sets the relative padding. The view may add on the space required to display
14338 * the scrollbars, depending on the style and visibility of the scrollbars.
14339 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14340 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14341 * from the values set in this call.
14342 *
14343 * @attr ref android.R.styleable#View_padding
14344 * @attr ref android.R.styleable#View_paddingBottom
14345 * @attr ref android.R.styleable#View_paddingStart
14346 * @attr ref android.R.styleable#View_paddingEnd
14347 * @attr ref android.R.styleable#View_paddingTop
14348 * @param start the start padding in pixels
14349 * @param top the top padding in pixels
14350 * @param end the end padding in pixels
14351 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014352 */
14353 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014354 // Reset padding resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070014355 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014356
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014357 mUserPaddingStart = start;
14358 mUserPaddingEnd = end;
14359
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014360 switch(getResolvedLayoutDirection()) {
14361 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014362 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014363 break;
14364 case LAYOUT_DIRECTION_LTR:
14365 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014366 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014367 }
14368 }
14369
14370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014371 * Returns the top padding of this view.
14372 *
14373 * @return the top padding in pixels
14374 */
14375 public int getPaddingTop() {
14376 return mPaddingTop;
14377 }
14378
14379 /**
14380 * Returns the bottom padding of this view. If there are inset and enabled
14381 * scrollbars, this value may include the space required to display the
14382 * scrollbars as well.
14383 *
14384 * @return the bottom padding in pixels
14385 */
14386 public int getPaddingBottom() {
14387 return mPaddingBottom;
14388 }
14389
14390 /**
14391 * Returns the left padding of this view. If there are inset and enabled
14392 * scrollbars, this value may include the space required to display the
14393 * scrollbars as well.
14394 *
14395 * @return the left padding in pixels
14396 */
14397 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014398 if (!isPaddingResolved()) {
14399 resolvePadding();
14400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014401 return mPaddingLeft;
14402 }
14403
14404 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014405 * Returns the start padding of this view depending on its resolved layout direction.
14406 * If there are inset and enabled scrollbars, this value may include the space
14407 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014408 *
14409 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014410 */
14411 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014412 if (!isPaddingResolved()) {
14413 resolvePadding();
14414 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014415 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14416 mPaddingRight : mPaddingLeft;
14417 }
14418
14419 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014420 * Returns the right padding of this view. If there are inset and enabled
14421 * scrollbars, this value may include the space required to display the
14422 * scrollbars as well.
14423 *
14424 * @return the right padding in pixels
14425 */
14426 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014427 if (!isPaddingResolved()) {
14428 resolvePadding();
14429 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014430 return mPaddingRight;
14431 }
14432
14433 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014434 * Returns the end padding of this view depending on its resolved layout direction.
14435 * If there are inset and enabled scrollbars, this value may include the space
14436 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014437 *
14438 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014439 */
14440 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014441 if (!isPaddingResolved()) {
14442 resolvePadding();
14443 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014444 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
14445 mPaddingLeft : mPaddingRight;
14446 }
14447
14448 /**
14449 * Return if the padding as been set thru relative values
14450 * {@link #setPaddingRelative(int, int, int, int)} or thru
14451 * @attr ref android.R.styleable#View_paddingStart or
14452 * @attr ref android.R.styleable#View_paddingEnd
14453 *
14454 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014455 */
14456 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014457 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014458 }
14459
14460 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014461 * @hide
14462 */
Philip Milne7a23b492012-04-24 22:12:36 -070014463 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014464 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014465 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014466 }
14467 return mLayoutInsets;
14468 }
14469
14470 /**
14471 * @hide
14472 */
14473 public void setLayoutInsets(Insets layoutInsets) {
14474 mLayoutInsets = layoutInsets;
14475 }
14476
14477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014478 * Changes the selection state of this view. A view can be selected or not.
14479 * Note that selection is not the same as focus. Views are typically
14480 * selected in the context of an AdapterView like ListView or GridView;
14481 * the selected view is the view that is highlighted.
14482 *
14483 * @param selected true if the view must be selected, false otherwise
14484 */
14485 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014486 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14487 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014488 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014489 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014490 refreshDrawableState();
14491 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014492 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14493 notifyAccessibilityStateChanged();
14494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014495 }
14496 }
14497
14498 /**
14499 * Dispatch setSelected to all of this View's children.
14500 *
14501 * @see #setSelected(boolean)
14502 *
14503 * @param selected The new selected state
14504 */
14505 protected void dispatchSetSelected(boolean selected) {
14506 }
14507
14508 /**
14509 * Indicates the selection state of this view.
14510 *
14511 * @return true if the view is selected, false otherwise
14512 */
14513 @ViewDebug.ExportedProperty
14514 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014515 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 }
14517
14518 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014519 * Changes the activated state of this view. A view can be activated or not.
14520 * Note that activation is not the same as selection. Selection is
14521 * a transient property, representing the view (hierarchy) the user is
14522 * currently interacting with. Activation is a longer-term state that the
14523 * user can move views in and out of. For example, in a list view with
14524 * single or multiple selection enabled, the views in the current selection
14525 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14526 * here.) The activated state is propagated down to children of the view it
14527 * is set on.
14528 *
14529 * @param activated true if the view must be activated, false otherwise
14530 */
14531 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014532 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14533 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014534 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014535 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014536 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014537 }
14538 }
14539
14540 /**
14541 * Dispatch setActivated to all of this View's children.
14542 *
14543 * @see #setActivated(boolean)
14544 *
14545 * @param activated The new activated state
14546 */
14547 protected void dispatchSetActivated(boolean activated) {
14548 }
14549
14550 /**
14551 * Indicates the activation state of this view.
14552 *
14553 * @return true if the view is activated, false otherwise
14554 */
14555 @ViewDebug.ExportedProperty
14556 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014557 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014558 }
14559
14560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014561 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14562 * observer can be used to get notifications when global events, like
14563 * layout, happen.
14564 *
14565 * The returned ViewTreeObserver observer is not guaranteed to remain
14566 * valid for the lifetime of this View. If the caller of this method keeps
14567 * a long-lived reference to ViewTreeObserver, it should always check for
14568 * the return value of {@link ViewTreeObserver#isAlive()}.
14569 *
14570 * @return The ViewTreeObserver for this view's hierarchy.
14571 */
14572 public ViewTreeObserver getViewTreeObserver() {
14573 if (mAttachInfo != null) {
14574 return mAttachInfo.mTreeObserver;
14575 }
14576 if (mFloatingTreeObserver == null) {
14577 mFloatingTreeObserver = new ViewTreeObserver();
14578 }
14579 return mFloatingTreeObserver;
14580 }
14581
14582 /**
14583 * <p>Finds the topmost view in the current view hierarchy.</p>
14584 *
14585 * @return the topmost view containing this view
14586 */
14587 public View getRootView() {
14588 if (mAttachInfo != null) {
14589 final View v = mAttachInfo.mRootView;
14590 if (v != null) {
14591 return v;
14592 }
14593 }
Romain Guy8506ab42009-06-11 17:35:47 -070014594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014595 View parent = this;
14596
14597 while (parent.mParent != null && parent.mParent instanceof View) {
14598 parent = (View) parent.mParent;
14599 }
14600
14601 return parent;
14602 }
14603
14604 /**
14605 * <p>Computes the coordinates of this view on the screen. The argument
14606 * must be an array of two integers. After the method returns, the array
14607 * contains the x and y location in that order.</p>
14608 *
14609 * @param location an array of two integers in which to hold the coordinates
14610 */
14611 public void getLocationOnScreen(int[] location) {
14612 getLocationInWindow(location);
14613
14614 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014615 if (info != null) {
14616 location[0] += info.mWindowLeft;
14617 location[1] += info.mWindowTop;
14618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014619 }
14620
14621 /**
14622 * <p>Computes the coordinates of this view in its window. The argument
14623 * must be an array of two integers. After the method returns, the array
14624 * contains the x and y location in that order.</p>
14625 *
14626 * @param location an array of two integers in which to hold the coordinates
14627 */
14628 public void getLocationInWindow(int[] location) {
14629 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014630 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014631 }
14632
Gilles Debunne6583ce52011-12-06 18:09:02 -080014633 if (mAttachInfo == null) {
14634 // When the view is not attached to a window, this method does not make sense
14635 location[0] = location[1] = 0;
14636 return;
14637 }
14638
Gilles Debunnecea45132011-11-24 02:19:27 +010014639 float[] position = mAttachInfo.mTmpTransformLocation;
14640 position[0] = position[1] = 0.0f;
14641
14642 if (!hasIdentityMatrix()) {
14643 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014645
Gilles Debunnecea45132011-11-24 02:19:27 +010014646 position[0] += mLeft;
14647 position[1] += mTop;
14648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649 ViewParent viewParent = mParent;
14650 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014651 final View view = (View) viewParent;
14652
14653 position[0] -= view.mScrollX;
14654 position[1] -= view.mScrollY;
14655
14656 if (!view.hasIdentityMatrix()) {
14657 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014658 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014659
14660 position[0] += view.mLeft;
14661 position[1] += view.mTop;
14662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014664 }
Romain Guy8506ab42009-06-11 17:35:47 -070014665
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014666 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014667 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014668 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14669 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014670 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014671
14672 location[0] = (int) (position[0] + 0.5f);
14673 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014674 }
14675
14676 /**
14677 * {@hide}
14678 * @param id the id of the view to be found
14679 * @return the view of the specified id, null if cannot be found
14680 */
14681 protected View findViewTraversal(int id) {
14682 if (id == mID) {
14683 return this;
14684 }
14685 return null;
14686 }
14687
14688 /**
14689 * {@hide}
14690 * @param tag the tag of the view to be found
14691 * @return the view of specified tag, null if cannot be found
14692 */
14693 protected View findViewWithTagTraversal(Object tag) {
14694 if (tag != null && tag.equals(mTag)) {
14695 return this;
14696 }
14697 return null;
14698 }
14699
14700 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014701 * {@hide}
14702 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014703 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014704 * @return The first view that matches the predicate or null.
14705 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014706 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014707 if (predicate.apply(this)) {
14708 return this;
14709 }
14710 return null;
14711 }
14712
14713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014714 * Look for a child view with the given id. If this view has the given
14715 * id, return this view.
14716 *
14717 * @param id The id to search for.
14718 * @return The view that has the given id in the hierarchy or null
14719 */
14720 public final View findViewById(int id) {
14721 if (id < 0) {
14722 return null;
14723 }
14724 return findViewTraversal(id);
14725 }
14726
14727 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014728 * Finds a view by its unuque and stable accessibility id.
14729 *
14730 * @param accessibilityId The searched accessibility id.
14731 * @return The found view.
14732 */
14733 final View findViewByAccessibilityId(int accessibilityId) {
14734 if (accessibilityId < 0) {
14735 return null;
14736 }
14737 return findViewByAccessibilityIdTraversal(accessibilityId);
14738 }
14739
14740 /**
14741 * Performs the traversal to find a view by its unuque and stable accessibility id.
14742 *
14743 * <strong>Note:</strong>This method does not stop at the root namespace
14744 * boundary since the user can touch the screen at an arbitrary location
14745 * potentially crossing the root namespace bounday which will send an
14746 * accessibility event to accessibility services and they should be able
14747 * to obtain the event source. Also accessibility ids are guaranteed to be
14748 * unique in the window.
14749 *
14750 * @param accessibilityId The accessibility id.
14751 * @return The found view.
14752 */
14753 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14754 if (getAccessibilityViewId() == accessibilityId) {
14755 return this;
14756 }
14757 return null;
14758 }
14759
14760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014761 * Look for a child view with the given tag. If this view has the given
14762 * tag, return this view.
14763 *
14764 * @param tag The tag to search for, using "tag.equals(getTag())".
14765 * @return The View that has the given tag in the hierarchy or null
14766 */
14767 public final View findViewWithTag(Object tag) {
14768 if (tag == null) {
14769 return null;
14770 }
14771 return findViewWithTagTraversal(tag);
14772 }
14773
14774 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014775 * {@hide}
14776 * Look for a child view that matches the specified predicate.
14777 * If this view matches the predicate, return this view.
14778 *
14779 * @param predicate The predicate to evaluate.
14780 * @return The first view that matches the predicate or null.
14781 */
14782 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014783 return findViewByPredicateTraversal(predicate, null);
14784 }
14785
14786 /**
14787 * {@hide}
14788 * Look for a child view that matches the specified predicate,
14789 * starting with the specified view and its descendents and then
14790 * recusively searching the ancestors and siblings of that view
14791 * until this view is reached.
14792 *
14793 * This method is useful in cases where the predicate does not match
14794 * a single unique view (perhaps multiple views use the same id)
14795 * and we are trying to find the view that is "closest" in scope to the
14796 * starting view.
14797 *
14798 * @param start The view to start from.
14799 * @param predicate The predicate to evaluate.
14800 * @return The first view that matches the predicate or null.
14801 */
14802 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14803 View childToSkip = null;
14804 for (;;) {
14805 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14806 if (view != null || start == this) {
14807 return view;
14808 }
14809
14810 ViewParent parent = start.getParent();
14811 if (parent == null || !(parent instanceof View)) {
14812 return null;
14813 }
14814
14815 childToSkip = start;
14816 start = (View) parent;
14817 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014818 }
14819
14820 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014821 * Sets the identifier for this view. The identifier does not have to be
14822 * unique in this view's hierarchy. The identifier should be a positive
14823 * number.
14824 *
14825 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014826 * @see #getId()
14827 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014828 *
14829 * @param id a number used to identify the view
14830 *
14831 * @attr ref android.R.styleable#View_id
14832 */
14833 public void setId(int id) {
14834 mID = id;
14835 }
14836
14837 /**
14838 * {@hide}
14839 *
14840 * @param isRoot true if the view belongs to the root namespace, false
14841 * otherwise
14842 */
14843 public void setIsRootNamespace(boolean isRoot) {
14844 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014845 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014847 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 }
14849 }
14850
14851 /**
14852 * {@hide}
14853 *
14854 * @return true if the view belongs to the root namespace, false otherwise
14855 */
14856 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014857 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014858 }
14859
14860 /**
14861 * Returns this view's identifier.
14862 *
14863 * @return a positive integer used to identify the view or {@link #NO_ID}
14864 * if the view has no ID
14865 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014866 * @see #setId(int)
14867 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014868 * @attr ref android.R.styleable#View_id
14869 */
14870 @ViewDebug.CapturedViewProperty
14871 public int getId() {
14872 return mID;
14873 }
14874
14875 /**
14876 * Returns this view's tag.
14877 *
14878 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014879 *
14880 * @see #setTag(Object)
14881 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014882 */
14883 @ViewDebug.ExportedProperty
14884 public Object getTag() {
14885 return mTag;
14886 }
14887
14888 /**
14889 * Sets the tag associated with this view. A tag can be used to mark
14890 * a view in its hierarchy and does not have to be unique within the
14891 * hierarchy. Tags can also be used to store data within a view without
14892 * resorting to another data structure.
14893 *
14894 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014895 *
14896 * @see #getTag()
14897 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014898 */
14899 public void setTag(final Object tag) {
14900 mTag = tag;
14901 }
14902
14903 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014904 * Returns the tag associated with this view and the specified key.
14905 *
14906 * @param key The key identifying the tag
14907 *
14908 * @return the Object stored in this view as a tag
14909 *
14910 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014911 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014912 */
14913 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014914 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014915 return null;
14916 }
14917
14918 /**
14919 * Sets a tag associated with this view and a key. A tag can be used
14920 * to mark a view in its hierarchy and does not have to be unique within
14921 * the hierarchy. Tags can also be used to store data within a view
14922 * without resorting to another data structure.
14923 *
14924 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014925 * application to ensure it is unique (see the <a
14926 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14927 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014928 * the Android framework or not associated with any package will cause
14929 * an {@link IllegalArgumentException} to be thrown.
14930 *
14931 * @param key The key identifying the tag
14932 * @param tag An Object to tag the view with
14933 *
14934 * @throws IllegalArgumentException If they specified key is not valid
14935 *
14936 * @see #setTag(Object)
14937 * @see #getTag(int)
14938 */
14939 public void setTag(int key, final Object tag) {
14940 // If the package id is 0x00 or 0x01, it's either an undefined package
14941 // or a framework id
14942 if ((key >>> 24) < 2) {
14943 throw new IllegalArgumentException("The key must be an application-specific "
14944 + "resource id.");
14945 }
14946
Adam Powell2b2f6d62011-09-23 11:15:39 -070014947 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014948 }
14949
14950 /**
14951 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14952 * framework id.
14953 *
14954 * @hide
14955 */
14956 public void setTagInternal(int key, Object tag) {
14957 if ((key >>> 24) != 0x1) {
14958 throw new IllegalArgumentException("The key must be a framework-specific "
14959 + "resource id.");
14960 }
14961
Adam Powell2b2f6d62011-09-23 11:15:39 -070014962 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014963 }
14964
Adam Powell2b2f6d62011-09-23 11:15:39 -070014965 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014966 if (mKeyedTags == null) {
14967 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014968 }
14969
Adam Powell7db82ac2011-09-22 19:44:04 -070014970 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014971 }
14972
14973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014974 * Prints information about this view in the log output, with the tag
14975 * {@link #VIEW_LOG_TAG}.
14976 *
14977 * @hide
14978 */
14979 public void debug() {
14980 debug(0);
14981 }
14982
14983 /**
14984 * Prints information about this view in the log output, with the tag
14985 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14986 * indentation defined by the <code>depth</code>.
14987 *
14988 * @param depth the indentation level
14989 *
14990 * @hide
14991 */
14992 protected void debug(int depth) {
14993 String output = debugIndent(depth - 1);
14994
14995 output += "+ " + this;
14996 int id = getId();
14997 if (id != -1) {
14998 output += " (id=" + id + ")";
14999 }
15000 Object tag = getTag();
15001 if (tag != null) {
15002 output += " (tag=" + tag + ")";
15003 }
15004 Log.d(VIEW_LOG_TAG, output);
15005
Dianne Hackborn4702a852012-08-17 15:18:29 -070015006 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015007 output = debugIndent(depth) + " FOCUSED";
15008 Log.d(VIEW_LOG_TAG, output);
15009 }
15010
15011 output = debugIndent(depth);
15012 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15013 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15014 + "} ";
15015 Log.d(VIEW_LOG_TAG, output);
15016
15017 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15018 || mPaddingBottom != 0) {
15019 output = debugIndent(depth);
15020 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15021 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15022 Log.d(VIEW_LOG_TAG, output);
15023 }
15024
15025 output = debugIndent(depth);
15026 output += "mMeasureWidth=" + mMeasuredWidth +
15027 " mMeasureHeight=" + mMeasuredHeight;
15028 Log.d(VIEW_LOG_TAG, output);
15029
15030 output = debugIndent(depth);
15031 if (mLayoutParams == null) {
15032 output += "BAD! no layout params";
15033 } else {
15034 output = mLayoutParams.debug(output);
15035 }
15036 Log.d(VIEW_LOG_TAG, output);
15037
15038 output = debugIndent(depth);
15039 output += "flags={";
15040 output += View.printFlags(mViewFlags);
15041 output += "}";
15042 Log.d(VIEW_LOG_TAG, output);
15043
15044 output = debugIndent(depth);
15045 output += "privateFlags={";
15046 output += View.printPrivateFlags(mPrivateFlags);
15047 output += "}";
15048 Log.d(VIEW_LOG_TAG, output);
15049 }
15050
15051 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015052 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015053 *
15054 * @param depth the indentation level
15055 * @return a String containing (depth * 2 + 3) * 2 white spaces
15056 *
15057 * @hide
15058 */
15059 protected static String debugIndent(int depth) {
15060 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15061 for (int i = 0; i < (depth * 2) + 3; i++) {
15062 spaces.append(' ').append(' ');
15063 }
15064 return spaces.toString();
15065 }
15066
15067 /**
15068 * <p>Return the offset of the widget's text baseline from the widget's top
15069 * boundary. If this widget does not support baseline alignment, this
15070 * method returns -1. </p>
15071 *
15072 * @return the offset of the baseline within the widget's bounds or -1
15073 * if baseline alignment is not supported
15074 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015075 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015076 public int getBaseline() {
15077 return -1;
15078 }
15079
15080 /**
15081 * Call this when something has changed which has invalidated the
15082 * layout of this view. This will schedule a layout pass of the view
15083 * tree.
15084 */
15085 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015086 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15087 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015088
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015089 if (mParent != null && !mParent.isLayoutRequested()) {
15090 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015091 }
15092 }
15093
15094 /**
15095 * Forces this view to be laid out during the next layout pass.
15096 * This method does not call requestLayout() or forceLayout()
15097 * on the parent.
15098 */
15099 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015100 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15101 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015102 }
15103
15104 /**
15105 * <p>
15106 * This is called to find out how big a view should be. The parent
15107 * supplies constraint information in the width and height parameters.
15108 * </p>
15109 *
15110 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015111 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015113 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015114 * </p>
15115 *
15116 *
15117 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15118 * parent
15119 * @param heightMeasureSpec Vertical space requirements as imposed by the
15120 * parent
15121 *
15122 * @see #onMeasure(int, int)
15123 */
15124 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015125 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015126 widthMeasureSpec != mOldWidthMeasureSpec ||
15127 heightMeasureSpec != mOldHeightMeasureSpec) {
15128
15129 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015130 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015131
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015132 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015133 resolvePadding();
15134 }
15135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015136 // measure ourselves, this should set the measured dimension flag back
15137 onMeasure(widthMeasureSpec, heightMeasureSpec);
15138
15139 // flag not set, setMeasuredDimension() was not invoked, we raise
15140 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015141 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015142 throw new IllegalStateException("onMeasure() did not set the"
15143 + " measured dimension by calling"
15144 + " setMeasuredDimension()");
15145 }
15146
Dianne Hackborn4702a852012-08-17 15:18:29 -070015147 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015148 }
15149
15150 mOldWidthMeasureSpec = widthMeasureSpec;
15151 mOldHeightMeasureSpec = heightMeasureSpec;
15152 }
15153
15154 /**
15155 * <p>
15156 * Measure the view and its content to determine the measured width and the
15157 * measured height. This method is invoked by {@link #measure(int, int)} and
15158 * should be overriden by subclasses to provide accurate and efficient
15159 * measurement of their contents.
15160 * </p>
15161 *
15162 * <p>
15163 * <strong>CONTRACT:</strong> When overriding this method, you
15164 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15165 * measured width and height of this view. Failure to do so will trigger an
15166 * <code>IllegalStateException</code>, thrown by
15167 * {@link #measure(int, int)}. Calling the superclass'
15168 * {@link #onMeasure(int, int)} is a valid use.
15169 * </p>
15170 *
15171 * <p>
15172 * The base class implementation of measure defaults to the background size,
15173 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15174 * override {@link #onMeasure(int, int)} to provide better measurements of
15175 * their content.
15176 * </p>
15177 *
15178 * <p>
15179 * If this method is overridden, it is the subclass's responsibility to make
15180 * sure the measured height and width are at least the view's minimum height
15181 * and width ({@link #getSuggestedMinimumHeight()} and
15182 * {@link #getSuggestedMinimumWidth()}).
15183 * </p>
15184 *
15185 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15186 * The requirements are encoded with
15187 * {@link android.view.View.MeasureSpec}.
15188 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15189 * The requirements are encoded with
15190 * {@link android.view.View.MeasureSpec}.
15191 *
15192 * @see #getMeasuredWidth()
15193 * @see #getMeasuredHeight()
15194 * @see #setMeasuredDimension(int, int)
15195 * @see #getSuggestedMinimumHeight()
15196 * @see #getSuggestedMinimumWidth()
15197 * @see android.view.View.MeasureSpec#getMode(int)
15198 * @see android.view.View.MeasureSpec#getSize(int)
15199 */
15200 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15201 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15202 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15203 }
15204
15205 /**
15206 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15207 * measured width and measured height. Failing to do so will trigger an
15208 * exception at measurement time.</p>
15209 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015210 * @param measuredWidth The measured width of this view. May be a complex
15211 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15212 * {@link #MEASURED_STATE_TOO_SMALL}.
15213 * @param measuredHeight The measured height of this view. May be a complex
15214 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15215 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015216 */
15217 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15218 mMeasuredWidth = measuredWidth;
15219 mMeasuredHeight = measuredHeight;
15220
Dianne Hackborn4702a852012-08-17 15:18:29 -070015221 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015222 }
15223
15224 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015225 * Merge two states as returned by {@link #getMeasuredState()}.
15226 * @param curState The current state as returned from a view or the result
15227 * of combining multiple views.
15228 * @param newState The new view state to combine.
15229 * @return Returns a new integer reflecting the combination of the two
15230 * states.
15231 */
15232 public static int combineMeasuredStates(int curState, int newState) {
15233 return curState | newState;
15234 }
15235
15236 /**
15237 * Version of {@link #resolveSizeAndState(int, int, int)}
15238 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15239 */
15240 public static int resolveSize(int size, int measureSpec) {
15241 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15242 }
15243
15244 /**
15245 * Utility to reconcile a desired size and state, with constraints imposed
15246 * by a MeasureSpec. Will take the desired size, unless a different size
15247 * is imposed by the constraints. The returned value is a compound integer,
15248 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15249 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15250 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015251 *
15252 * @param size How big the view wants to be
15253 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015254 * @return Size information bit mask as defined by
15255 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015256 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015257 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015258 int result = size;
15259 int specMode = MeasureSpec.getMode(measureSpec);
15260 int specSize = MeasureSpec.getSize(measureSpec);
15261 switch (specMode) {
15262 case MeasureSpec.UNSPECIFIED:
15263 result = size;
15264 break;
15265 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015266 if (specSize < size) {
15267 result = specSize | MEASURED_STATE_TOO_SMALL;
15268 } else {
15269 result = size;
15270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015271 break;
15272 case MeasureSpec.EXACTLY:
15273 result = specSize;
15274 break;
15275 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015276 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015277 }
15278
15279 /**
15280 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015281 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015282 * by the MeasureSpec.
15283 *
15284 * @param size Default size for this view
15285 * @param measureSpec Constraints imposed by the parent
15286 * @return The size this view should be.
15287 */
15288 public static int getDefaultSize(int size, int measureSpec) {
15289 int result = size;
15290 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015291 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015292
15293 switch (specMode) {
15294 case MeasureSpec.UNSPECIFIED:
15295 result = size;
15296 break;
15297 case MeasureSpec.AT_MOST:
15298 case MeasureSpec.EXACTLY:
15299 result = specSize;
15300 break;
15301 }
15302 return result;
15303 }
15304
15305 /**
15306 * Returns the suggested minimum height that the view should use. This
15307 * returns the maximum of the view's minimum height
15308 * and the background's minimum height
15309 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15310 * <p>
15311 * When being used in {@link #onMeasure(int, int)}, the caller should still
15312 * ensure the returned height is within the requirements of the parent.
15313 *
15314 * @return The suggested minimum height of the view.
15315 */
15316 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015317 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015319 }
15320
15321 /**
15322 * Returns the suggested minimum width that the view should use. This
15323 * returns the maximum of the view's minimum width)
15324 * and the background's minimum width
15325 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15326 * <p>
15327 * When being used in {@link #onMeasure(int, int)}, the caller should still
15328 * ensure the returned width is within the requirements of the parent.
15329 *
15330 * @return The suggested minimum width of the view.
15331 */
15332 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015333 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015335
Philip Milne6c8ea062012-04-03 17:38:43 -070015336 /**
15337 * Returns the minimum height of the view.
15338 *
15339 * @return the minimum height the view will try to be.
15340 *
15341 * @see #setMinimumHeight(int)
15342 *
15343 * @attr ref android.R.styleable#View_minHeight
15344 */
15345 public int getMinimumHeight() {
15346 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015347 }
15348
15349 /**
15350 * Sets the minimum height of the view. It is not guaranteed the view will
15351 * be able to achieve this minimum height (for example, if its parent layout
15352 * constrains it with less available height).
15353 *
15354 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015355 *
15356 * @see #getMinimumHeight()
15357 *
15358 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015359 */
15360 public void setMinimumHeight(int minHeight) {
15361 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015362 requestLayout();
15363 }
15364
15365 /**
15366 * Returns the minimum width of the view.
15367 *
15368 * @return the minimum width the view will try to be.
15369 *
15370 * @see #setMinimumWidth(int)
15371 *
15372 * @attr ref android.R.styleable#View_minWidth
15373 */
15374 public int getMinimumWidth() {
15375 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015376 }
15377
15378 /**
15379 * Sets the minimum width of the view. It is not guaranteed the view will
15380 * be able to achieve this minimum width (for example, if its parent layout
15381 * constrains it with less available width).
15382 *
15383 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015384 *
15385 * @see #getMinimumWidth()
15386 *
15387 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015388 */
15389 public void setMinimumWidth(int minWidth) {
15390 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015391 requestLayout();
15392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015393 }
15394
15395 /**
15396 * Get the animation currently associated with this view.
15397 *
15398 * @return The animation that is currently playing or
15399 * scheduled to play for this view.
15400 */
15401 public Animation getAnimation() {
15402 return mCurrentAnimation;
15403 }
15404
15405 /**
15406 * Start the specified animation now.
15407 *
15408 * @param animation the animation to start now
15409 */
15410 public void startAnimation(Animation animation) {
15411 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15412 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015413 invalidateParentCaches();
15414 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015415 }
15416
15417 /**
15418 * Cancels any animations for this view.
15419 */
15420 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015421 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015422 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015424 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015425 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015426 }
15427
15428 /**
15429 * Sets the next animation to play for this view.
15430 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015431 * {@link #startAnimation(android.view.animation.Animation)} instead.
15432 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015433 * control over the start time and invalidation, but you
15434 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015435 * 2) the view's parent (which controls animations on its children)
15436 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015437 * start.
15438 *
15439 * @param animation The next animation, or null.
15440 */
15441 public void setAnimation(Animation animation) {
15442 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015444 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015445 // If the screen is off assume the animation start time is now instead of
15446 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15447 // would cause the animation to start when the screen turns back on
15448 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15449 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15450 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 animation.reset();
15453 }
15454 }
15455
15456 /**
15457 * Invoked by a parent ViewGroup to notify the start of the animation
15458 * currently associated with this view. If you override this method,
15459 * always call super.onAnimationStart();
15460 *
15461 * @see #setAnimation(android.view.animation.Animation)
15462 * @see #getAnimation()
15463 */
15464 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015465 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015466 }
15467
15468 /**
15469 * Invoked by a parent ViewGroup to notify the end of the animation
15470 * currently associated with this view. If you override this method,
15471 * always call super.onAnimationEnd();
15472 *
15473 * @see #setAnimation(android.view.animation.Animation)
15474 * @see #getAnimation()
15475 */
15476 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015477 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015478 }
15479
15480 /**
15481 * Invoked if there is a Transform that involves alpha. Subclass that can
15482 * draw themselves with the specified alpha should return true, and then
15483 * respect that alpha when their onDraw() is called. If this returns false
15484 * then the view may be redirected to draw into an offscreen buffer to
15485 * fulfill the request, which will look fine, but may be slower than if the
15486 * subclass handles it internally. The default implementation returns false.
15487 *
15488 * @param alpha The alpha (0..255) to apply to the view's drawing
15489 * @return true if the view can draw with the specified alpha.
15490 */
15491 protected boolean onSetAlpha(int alpha) {
15492 return false;
15493 }
15494
15495 /**
15496 * This is used by the RootView to perform an optimization when
15497 * the view hierarchy contains one or several SurfaceView.
15498 * SurfaceView is always considered transparent, but its children are not,
15499 * therefore all View objects remove themselves from the global transparent
15500 * region (passed as a parameter to this function).
15501 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015502 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015503 *
15504 * @return Returns true if the effective visibility of the view at this
15505 * point is opaque, regardless of the transparent region; returns false
15506 * if it is possible for underlying windows to be seen behind the view.
15507 *
15508 * {@hide}
15509 */
15510 public boolean gatherTransparentRegion(Region region) {
15511 final AttachInfo attachInfo = mAttachInfo;
15512 if (region != null && attachInfo != null) {
15513 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015514 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015515 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15516 // remove it from the transparent region.
15517 final int[] location = attachInfo.mTransparentLocation;
15518 getLocationInWindow(location);
15519 region.op(location[0], location[1], location[0] + mRight - mLeft,
15520 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015521 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015522 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15523 // exists, so we remove the background drawable's non-transparent
15524 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015525 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015526 }
15527 }
15528 return true;
15529 }
15530
15531 /**
15532 * Play a sound effect for this view.
15533 *
15534 * <p>The framework will play sound effects for some built in actions, such as
15535 * clicking, but you may wish to play these effects in your widget,
15536 * for instance, for internal navigation.
15537 *
15538 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15539 * {@link #isSoundEffectsEnabled()} is true.
15540 *
15541 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15542 */
15543 public void playSoundEffect(int soundConstant) {
15544 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15545 return;
15546 }
15547 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15548 }
15549
15550 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015551 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015552 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015553 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015554 *
15555 * <p>The framework will provide haptic feedback for some built in actions,
15556 * such as long presses, but you may wish to provide feedback for your
15557 * own widget.
15558 *
15559 * <p>The feedback will only be performed if
15560 * {@link #isHapticFeedbackEnabled()} is true.
15561 *
15562 * @param feedbackConstant One of the constants defined in
15563 * {@link HapticFeedbackConstants}
15564 */
15565 public boolean performHapticFeedback(int feedbackConstant) {
15566 return performHapticFeedback(feedbackConstant, 0);
15567 }
15568
15569 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015570 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015571 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015572 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015573 *
15574 * @param feedbackConstant One of the constants defined in
15575 * {@link HapticFeedbackConstants}
15576 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15577 */
15578 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15579 if (mAttachInfo == null) {
15580 return false;
15581 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015582 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015583 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015584 && !isHapticFeedbackEnabled()) {
15585 return false;
15586 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015587 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15588 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015589 }
15590
15591 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015592 * Request that the visibility of the status bar or other screen/window
15593 * decorations be changed.
15594 *
15595 * <p>This method is used to put the over device UI into temporary modes
15596 * where the user's attention is focused more on the application content,
15597 * by dimming or hiding surrounding system affordances. This is typically
15598 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15599 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15600 * to be placed behind the action bar (and with these flags other system
15601 * affordances) so that smooth transitions between hiding and showing them
15602 * can be done.
15603 *
15604 * <p>Two representative examples of the use of system UI visibility is
15605 * implementing a content browsing application (like a magazine reader)
15606 * and a video playing application.
15607 *
15608 * <p>The first code shows a typical implementation of a View in a content
15609 * browsing application. In this implementation, the application goes
15610 * into a content-oriented mode by hiding the status bar and action bar,
15611 * and putting the navigation elements into lights out mode. The user can
15612 * then interact with content while in this mode. Such an application should
15613 * provide an easy way for the user to toggle out of the mode (such as to
15614 * check information in the status bar or access notifications). In the
15615 * implementation here, this is done simply by tapping on the content.
15616 *
15617 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15618 * content}
15619 *
15620 * <p>This second code sample shows a typical implementation of a View
15621 * in a video playing application. In this situation, while the video is
15622 * playing the application would like to go into a complete full-screen mode,
15623 * to use as much of the display as possible for the video. When in this state
15624 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015625 * touching on the screen to pop the UI out of full screen mode. See
15626 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015627 *
15628 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15629 * content}
15630 *
15631 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15632 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15633 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15634 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015635 */
15636 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015637 if (visibility != mSystemUiVisibility) {
15638 mSystemUiVisibility = visibility;
15639 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15640 mParent.recomputeViewAttributes(this);
15641 }
Joe Onorato664644d2011-01-23 17:53:23 -080015642 }
15643 }
15644
15645 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015646 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15647 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15648 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15649 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15650 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015651 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015652 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015653 return mSystemUiVisibility;
15654 }
15655
Scott Mainec6331b2011-05-24 16:55:56 -070015656 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015657 * Returns the current system UI visibility that is currently set for
15658 * the entire window. This is the combination of the
15659 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15660 * views in the window.
15661 */
15662 public int getWindowSystemUiVisibility() {
15663 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15664 }
15665
15666 /**
15667 * Override to find out when the window's requested system UI visibility
15668 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15669 * This is different from the callbacks recieved through
15670 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15671 * in that this is only telling you about the local request of the window,
15672 * not the actual values applied by the system.
15673 */
15674 public void onWindowSystemUiVisibilityChanged(int visible) {
15675 }
15676
15677 /**
15678 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15679 * the view hierarchy.
15680 */
15681 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15682 onWindowSystemUiVisibilityChanged(visible);
15683 }
15684
15685 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015686 * Set a listener to receive callbacks when the visibility of the system bar changes.
15687 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15688 */
Joe Onorato664644d2011-01-23 17:53:23 -080015689 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015690 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015691 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15692 mParent.recomputeViewAttributes(this);
15693 }
15694 }
15695
15696 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015697 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15698 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015699 */
15700 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015701 ListenerInfo li = mListenerInfo;
15702 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15703 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015704 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015705 }
15706 }
15707
Dianne Hackborncf675782012-05-10 15:07:24 -070015708 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015709 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15710 if (val != mSystemUiVisibility) {
15711 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015712 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015713 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015714 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015715 }
15716
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015717 /** @hide */
15718 public void setDisabledSystemUiVisibility(int flags) {
15719 if (mAttachInfo != null) {
15720 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15721 mAttachInfo.mDisabledSystemUiVisibility = flags;
15722 if (mParent != null) {
15723 mParent.recomputeViewAttributes(this);
15724 }
15725 }
15726 }
15727 }
15728
Joe Onorato664644d2011-01-23 17:53:23 -080015729 /**
Joe Malin32736f02011-01-19 16:14:20 -080015730 * Creates an image that the system displays during the drag and drop
15731 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15732 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15733 * appearance as the given View. The default also positions the center of the drag shadow
15734 * directly under the touch point. If no View is provided (the constructor with no parameters
15735 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15736 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15737 * default is an invisible drag shadow.
15738 * <p>
15739 * You are not required to use the View you provide to the constructor as the basis of the
15740 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15741 * anything you want as the drag shadow.
15742 * </p>
15743 * <p>
15744 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15745 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15746 * size and position of the drag shadow. It uses this data to construct a
15747 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15748 * so that your application can draw the shadow image in the Canvas.
15749 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015750 *
15751 * <div class="special reference">
15752 * <h3>Developer Guides</h3>
15753 * <p>For a guide to implementing drag and drop features, read the
15754 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15755 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015756 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015757 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015758 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015759
15760 /**
Joe Malin32736f02011-01-19 16:14:20 -080015761 * Constructs a shadow image builder based on a View. By default, the resulting drag
15762 * shadow will have the same appearance and dimensions as the View, with the touch point
15763 * over the center of the View.
15764 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015765 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015766 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015767 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015768 }
15769
Christopher Tate17ed60c2011-01-18 12:50:26 -080015770 /**
15771 * Construct a shadow builder object with no associated View. This
15772 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15773 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15774 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015775 * reference to any View object. If they are not overridden, then the result is an
15776 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015777 */
15778 public DragShadowBuilder() {
15779 mView = new WeakReference<View>(null);
15780 }
15781
15782 /**
15783 * Returns the View object that had been passed to the
15784 * {@link #View.DragShadowBuilder(View)}
15785 * constructor. If that View parameter was {@code null} or if the
15786 * {@link #View.DragShadowBuilder()}
15787 * constructor was used to instantiate the builder object, this method will return
15788 * null.
15789 *
15790 * @return The View object associate with this builder object.
15791 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015792 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015793 final public View getView() {
15794 return mView.get();
15795 }
15796
Christopher Tate2c095f32010-10-04 14:13:40 -070015797 /**
Joe Malin32736f02011-01-19 16:14:20 -080015798 * Provides the metrics for the shadow image. These include the dimensions of
15799 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015800 * be centered under the touch location while dragging.
15801 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015802 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015803 * same as the dimensions of the View itself and centers the shadow under
15804 * the touch point.
15805 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015806 *
Joe Malin32736f02011-01-19 16:14:20 -080015807 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15808 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15809 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15810 * image.
15811 *
15812 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15813 * shadow image that should be underneath the touch point during the drag and drop
15814 * operation. Your application must set {@link android.graphics.Point#x} to the
15815 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015816 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015817 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015818 final View view = mView.get();
15819 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015820 shadowSize.set(view.getWidth(), view.getHeight());
15821 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015822 } else {
15823 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15824 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015825 }
15826
15827 /**
Joe Malin32736f02011-01-19 16:14:20 -080015828 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15829 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015830 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015831 *
Joe Malin32736f02011-01-19 16:14:20 -080015832 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015833 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015834 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015835 final View view = mView.get();
15836 if (view != null) {
15837 view.draw(canvas);
15838 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015839 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015840 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015841 }
15842 }
15843
15844 /**
Joe Malin32736f02011-01-19 16:14:20 -080015845 * Starts a drag and drop operation. When your application calls this method, it passes a
15846 * {@link android.view.View.DragShadowBuilder} object to the system. The
15847 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15848 * to get metrics for the drag shadow, and then calls the object's
15849 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15850 * <p>
15851 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15852 * drag events to all the View objects in your application that are currently visible. It does
15853 * this either by calling the View object's drag listener (an implementation of
15854 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15855 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15856 * Both are passed a {@link android.view.DragEvent} object that has a
15857 * {@link android.view.DragEvent#getAction()} value of
15858 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15859 * </p>
15860 * <p>
15861 * Your application can invoke startDrag() on any attached View object. The View object does not
15862 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15863 * be related to the View the user selected for dragging.
15864 * </p>
15865 * @param data A {@link android.content.ClipData} object pointing to the data to be
15866 * transferred by the drag and drop operation.
15867 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15868 * drag shadow.
15869 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15870 * drop operation. This Object is put into every DragEvent object sent by the system during the
15871 * current drag.
15872 * <p>
15873 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15874 * to the target Views. For example, it can contain flags that differentiate between a
15875 * a copy operation and a move operation.
15876 * </p>
15877 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15878 * so the parameter should be set to 0.
15879 * @return {@code true} if the method completes successfully, or
15880 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15881 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015882 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015883 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015884 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015885 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015886 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015887 }
15888 boolean okay = false;
15889
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015890 Point shadowSize = new Point();
15891 Point shadowTouchPoint = new Point();
15892 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015893
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015894 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15895 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15896 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015897 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015898
Chris Tatea32dcf72010-10-14 12:13:50 -070015899 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015900 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15901 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015902 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015903 Surface surface = new Surface();
15904 try {
15905 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015906 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015907 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015908 + " surface=" + surface);
15909 if (token != null) {
15910 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015911 try {
Chris Tate6b391282010-10-14 15:48:59 -070015912 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015913 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015914 } finally {
15915 surface.unlockCanvasAndPost(canvas);
15916 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015917
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015918 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015919
15920 // Cache the local state object for delivery with DragEvents
15921 root.setLocalDragState(myLocalState);
15922
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015923 // repurpose 'shadowSize' for the last touch point
15924 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015925
Christopher Tatea53146c2010-09-07 11:57:52 -070015926 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015927 shadowSize.x, shadowSize.y,
15928 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015929 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015930
15931 // Off and running! Release our local surface instance; the drag
15932 // shadow surface is now managed by the system process.
15933 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015934 }
15935 } catch (Exception e) {
15936 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15937 surface.destroy();
15938 }
15939
15940 return okay;
15941 }
15942
Christopher Tatea53146c2010-09-07 11:57:52 -070015943 /**
Joe Malin32736f02011-01-19 16:14:20 -080015944 * Handles drag events sent by the system following a call to
15945 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15946 *<p>
15947 * When the system calls this method, it passes a
15948 * {@link android.view.DragEvent} object. A call to
15949 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15950 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15951 * operation.
15952 * @param event The {@link android.view.DragEvent} sent by the system.
15953 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15954 * in DragEvent, indicating the type of drag event represented by this object.
15955 * @return {@code true} if the method was successful, otherwise {@code false}.
15956 * <p>
15957 * The method should return {@code true} in response to an action type of
15958 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15959 * operation.
15960 * </p>
15961 * <p>
15962 * The method should also return {@code true} in response to an action type of
15963 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15964 * {@code false} if it didn't.
15965 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015966 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015967 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015968 return false;
15969 }
15970
15971 /**
Joe Malin32736f02011-01-19 16:14:20 -080015972 * Detects if this View is enabled and has a drag event listener.
15973 * If both are true, then it calls the drag event listener with the
15974 * {@link android.view.DragEvent} it received. If the drag event listener returns
15975 * {@code true}, then dispatchDragEvent() returns {@code true}.
15976 * <p>
15977 * For all other cases, the method calls the
15978 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15979 * method and returns its result.
15980 * </p>
15981 * <p>
15982 * This ensures that a drag event is always consumed, even if the View does not have a drag
15983 * event listener. However, if the View has a listener and the listener returns true, then
15984 * onDragEvent() is not called.
15985 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015986 */
15987 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015988 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015989 ListenerInfo li = mListenerInfo;
15990 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15991 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015992 return true;
15993 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015994 return onDragEvent(event);
15995 }
15996
Christopher Tate3d4bf172011-03-28 16:16:46 -070015997 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015998 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070015999 }
16000
Christopher Tatea53146c2010-09-07 11:57:52 -070016001 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016002 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16003 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016004 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016005 */
16006 public void onCloseSystemDialogs(String reason) {
16007 }
Joe Malin32736f02011-01-19 16:14:20 -080016008
Dianne Hackbornffa42482009-09-23 22:20:11 -070016009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016010 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016011 * update a Region being computed for
16012 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016013 * that any non-transparent parts of the Drawable are removed from the
16014 * given transparent region.
16015 *
16016 * @param dr The Drawable whose transparency is to be applied to the region.
16017 * @param region A Region holding the current transparency information,
16018 * where any parts of the region that are set are considered to be
16019 * transparent. On return, this region will be modified to have the
16020 * transparency information reduced by the corresponding parts of the
16021 * Drawable that are not transparent.
16022 * {@hide}
16023 */
16024 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16025 if (DBG) {
16026 Log.i("View", "Getting transparent region for: " + this);
16027 }
16028 final Region r = dr.getTransparentRegion();
16029 final Rect db = dr.getBounds();
16030 final AttachInfo attachInfo = mAttachInfo;
16031 if (r != null && attachInfo != null) {
16032 final int w = getRight()-getLeft();
16033 final int h = getBottom()-getTop();
16034 if (db.left > 0) {
16035 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16036 r.op(0, 0, db.left, h, Region.Op.UNION);
16037 }
16038 if (db.right < w) {
16039 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16040 r.op(db.right, 0, w, h, Region.Op.UNION);
16041 }
16042 if (db.top > 0) {
16043 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16044 r.op(0, 0, w, db.top, Region.Op.UNION);
16045 }
16046 if (db.bottom < h) {
16047 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16048 r.op(0, db.bottom, w, h, Region.Op.UNION);
16049 }
16050 final int[] location = attachInfo.mTransparentLocation;
16051 getLocationInWindow(location);
16052 r.translate(location[0], location[1]);
16053 region.op(r, Region.Op.INTERSECT);
16054 } else {
16055 region.op(db, Region.Op.DIFFERENCE);
16056 }
16057 }
16058
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016059 private void checkForLongClick(int delayOffset) {
16060 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16061 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016062
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016063 if (mPendingCheckForLongPress == null) {
16064 mPendingCheckForLongPress = new CheckForLongPress();
16065 }
16066 mPendingCheckForLongPress.rememberWindowAttachCount();
16067 postDelayed(mPendingCheckForLongPress,
16068 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016070 }
16071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016072 /**
16073 * Inflate a view from an XML resource. This convenience method wraps the {@link
16074 * LayoutInflater} class, which provides a full range of options for view inflation.
16075 *
16076 * @param context The Context object for your activity or application.
16077 * @param resource The resource ID to inflate
16078 * @param root A view group that will be the parent. Used to properly inflate the
16079 * layout_* parameters.
16080 * @see LayoutInflater
16081 */
16082 public static View inflate(Context context, int resource, ViewGroup root) {
16083 LayoutInflater factory = LayoutInflater.from(context);
16084 return factory.inflate(resource, root);
16085 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016087 /**
Adam Powell637d3372010-08-25 14:37:03 -070016088 * Scroll the view with standard behavior for scrolling beyond the normal
16089 * content boundaries. Views that call this method should override
16090 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16091 * results of an over-scroll operation.
16092 *
16093 * Views can use this method to handle any touch or fling-based scrolling.
16094 *
16095 * @param deltaX Change in X in pixels
16096 * @param deltaY Change in Y in pixels
16097 * @param scrollX Current X scroll value in pixels before applying deltaX
16098 * @param scrollY Current Y scroll value in pixels before applying deltaY
16099 * @param scrollRangeX Maximum content scroll range along the X axis
16100 * @param scrollRangeY Maximum content scroll range along the Y axis
16101 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16102 * along the X axis.
16103 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16104 * along the Y axis.
16105 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16106 * @return true if scrolling was clamped to an over-scroll boundary along either
16107 * axis, false otherwise.
16108 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016109 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016110 protected boolean overScrollBy(int deltaX, int deltaY,
16111 int scrollX, int scrollY,
16112 int scrollRangeX, int scrollRangeY,
16113 int maxOverScrollX, int maxOverScrollY,
16114 boolean isTouchEvent) {
16115 final int overScrollMode = mOverScrollMode;
16116 final boolean canScrollHorizontal =
16117 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16118 final boolean canScrollVertical =
16119 computeVerticalScrollRange() > computeVerticalScrollExtent();
16120 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16121 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16122 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16123 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16124
16125 int newScrollX = scrollX + deltaX;
16126 if (!overScrollHorizontal) {
16127 maxOverScrollX = 0;
16128 }
16129
16130 int newScrollY = scrollY + deltaY;
16131 if (!overScrollVertical) {
16132 maxOverScrollY = 0;
16133 }
16134
16135 // Clamp values if at the limits and record
16136 final int left = -maxOverScrollX;
16137 final int right = maxOverScrollX + scrollRangeX;
16138 final int top = -maxOverScrollY;
16139 final int bottom = maxOverScrollY + scrollRangeY;
16140
16141 boolean clampedX = false;
16142 if (newScrollX > right) {
16143 newScrollX = right;
16144 clampedX = true;
16145 } else if (newScrollX < left) {
16146 newScrollX = left;
16147 clampedX = true;
16148 }
16149
16150 boolean clampedY = false;
16151 if (newScrollY > bottom) {
16152 newScrollY = bottom;
16153 clampedY = true;
16154 } else if (newScrollY < top) {
16155 newScrollY = top;
16156 clampedY = true;
16157 }
16158
16159 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16160
16161 return clampedX || clampedY;
16162 }
16163
16164 /**
16165 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16166 * respond to the results of an over-scroll operation.
16167 *
16168 * @param scrollX New X scroll value in pixels
16169 * @param scrollY New Y scroll value in pixels
16170 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16171 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16172 */
16173 protected void onOverScrolled(int scrollX, int scrollY,
16174 boolean clampedX, boolean clampedY) {
16175 // Intentionally empty.
16176 }
16177
16178 /**
16179 * Returns the over-scroll mode for this view. The result will be
16180 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16181 * (allow over-scrolling only if the view content is larger than the container),
16182 * or {@link #OVER_SCROLL_NEVER}.
16183 *
16184 * @return This view's over-scroll mode.
16185 */
16186 public int getOverScrollMode() {
16187 return mOverScrollMode;
16188 }
16189
16190 /**
16191 * Set the over-scroll mode for this view. Valid over-scroll modes are
16192 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16193 * (allow over-scrolling only if the view content is larger than the container),
16194 * or {@link #OVER_SCROLL_NEVER}.
16195 *
16196 * Setting the over-scroll mode of a view will have an effect only if the
16197 * view is capable of scrolling.
16198 *
16199 * @param overScrollMode The new over-scroll mode for this view.
16200 */
16201 public void setOverScrollMode(int overScrollMode) {
16202 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16203 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16204 overScrollMode != OVER_SCROLL_NEVER) {
16205 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16206 }
16207 mOverScrollMode = overScrollMode;
16208 }
16209
16210 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016211 * Gets a scale factor that determines the distance the view should scroll
16212 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16213 * @return The vertical scroll scale factor.
16214 * @hide
16215 */
16216 protected float getVerticalScrollFactor() {
16217 if (mVerticalScrollFactor == 0) {
16218 TypedValue outValue = new TypedValue();
16219 if (!mContext.getTheme().resolveAttribute(
16220 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16221 throw new IllegalStateException(
16222 "Expected theme to define listPreferredItemHeight.");
16223 }
16224 mVerticalScrollFactor = outValue.getDimension(
16225 mContext.getResources().getDisplayMetrics());
16226 }
16227 return mVerticalScrollFactor;
16228 }
16229
16230 /**
16231 * Gets a scale factor that determines the distance the view should scroll
16232 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16233 * @return The horizontal scroll scale factor.
16234 * @hide
16235 */
16236 protected float getHorizontalScrollFactor() {
16237 // TODO: Should use something else.
16238 return getVerticalScrollFactor();
16239 }
16240
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016241 /**
16242 * Return the value specifying the text direction or policy that was set with
16243 * {@link #setTextDirection(int)}.
16244 *
16245 * @return the defined text direction. It can be one of:
16246 *
16247 * {@link #TEXT_DIRECTION_INHERIT},
16248 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16249 * {@link #TEXT_DIRECTION_ANY_RTL},
16250 * {@link #TEXT_DIRECTION_LTR},
16251 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016252 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016253 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016254 @ViewDebug.ExportedProperty(category = "text", mapping = {
16255 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16256 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16257 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16258 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16259 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16260 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16261 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016262 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016263 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016264 }
16265
16266 /**
16267 * Set the text direction.
16268 *
16269 * @param textDirection the direction to set. Should be one of:
16270 *
16271 * {@link #TEXT_DIRECTION_INHERIT},
16272 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16273 * {@link #TEXT_DIRECTION_ANY_RTL},
16274 * {@link #TEXT_DIRECTION_LTR},
16275 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016276 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016277 */
16278 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016279 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016280 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016281 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016282 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016283 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016284 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016285 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016286 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016287 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016288 }
16289 }
16290
16291 /**
16292 * Return the resolved text direction.
16293 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016294 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
16295 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
16296 * up the parent chain of the view. if there is no parent, then it will return the default
16297 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
16298 *
16299 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016300 *
Doug Feltcb3791202011-07-07 11:57:48 -070016301 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16302 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016303 * {@link #TEXT_DIRECTION_LTR},
16304 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016305 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016306 */
16307 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016308 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016309 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016310 resolveTextDirection();
16311 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016312 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016313 }
16314
16315 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016316 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
16317 * resolution is done.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016318 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016319 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016320 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016321 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016322
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016323 if (hasRtlSupport()) {
16324 // Set resolved text direction flag depending on text direction flag
16325 final int textDirection = getTextDirection();
16326 switch(textDirection) {
16327 case TEXT_DIRECTION_INHERIT:
16328 if (canResolveTextDirection()) {
16329 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016330
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016331 // Set current resolved direction to the same value as the parent's one
16332 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
16333 switch (parentResolvedDirection) {
16334 case TEXT_DIRECTION_FIRST_STRONG:
16335 case TEXT_DIRECTION_ANY_RTL:
16336 case TEXT_DIRECTION_LTR:
16337 case TEXT_DIRECTION_RTL:
16338 case TEXT_DIRECTION_LOCALE:
16339 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016340 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016341 break;
16342 default:
16343 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016344 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016345 }
16346 } else {
16347 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016348 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016349 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016350 break;
16351 case TEXT_DIRECTION_FIRST_STRONG:
16352 case TEXT_DIRECTION_ANY_RTL:
16353 case TEXT_DIRECTION_LTR:
16354 case TEXT_DIRECTION_RTL:
16355 case TEXT_DIRECTION_LOCALE:
16356 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016357 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016358 break;
16359 default:
16360 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016361 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016362 }
16363 } else {
16364 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016365 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016366 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016367
16368 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016369 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016370 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016371 }
16372
16373 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016374 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016375 * resolution should override this method.
16376 *
16377 * The default implementation does nothing.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016378 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016379 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016380 }
16381
16382 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016383 * Check if text direction resolution can be done.
16384 *
16385 * @return true if text direction resolution can be done otherwise return false.
16386 */
16387 public boolean canResolveTextDirection() {
16388 switch (getTextDirection()) {
16389 case TEXT_DIRECTION_INHERIT:
16390 return (mParent != null) && (mParent instanceof ViewGroup);
16391 default:
16392 return true;
16393 }
16394 }
16395
16396 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016397 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016398 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016399 * reset is done.
16400 */
16401 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016402 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016403 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016404 }
16405
16406 /**
16407 * Called when text direction is reset. Subclasses that care about text direction reset should
16408 * override this method and do a reset of the text direction of their children. The default
16409 * implementation does nothing.
16410 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080016411 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016412 }
16413
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016414 /**
16415 * Return the value specifying the text alignment or policy that was set with
16416 * {@link #setTextAlignment(int)}.
16417 *
16418 * @return the defined text alignment. It can be one of:
16419 *
16420 * {@link #TEXT_ALIGNMENT_INHERIT},
16421 * {@link #TEXT_ALIGNMENT_GRAVITY},
16422 * {@link #TEXT_ALIGNMENT_CENTER},
16423 * {@link #TEXT_ALIGNMENT_TEXT_START},
16424 * {@link #TEXT_ALIGNMENT_TEXT_END},
16425 * {@link #TEXT_ALIGNMENT_VIEW_START},
16426 * {@link #TEXT_ALIGNMENT_VIEW_END}
16427 */
16428 @ViewDebug.ExportedProperty(category = "text", mapping = {
16429 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16430 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16431 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16432 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16433 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16434 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16435 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16436 })
16437 public int getTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016438 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016439 }
16440
16441 /**
16442 * Set the text alignment.
16443 *
16444 * @param textAlignment The text alignment to set. Should be one of
16445 *
16446 * {@link #TEXT_ALIGNMENT_INHERIT},
16447 * {@link #TEXT_ALIGNMENT_GRAVITY},
16448 * {@link #TEXT_ALIGNMENT_CENTER},
16449 * {@link #TEXT_ALIGNMENT_TEXT_START},
16450 * {@link #TEXT_ALIGNMENT_TEXT_END},
16451 * {@link #TEXT_ALIGNMENT_VIEW_START},
16452 * {@link #TEXT_ALIGNMENT_VIEW_END}
16453 *
16454 * @attr ref android.R.styleable#View_textAlignment
16455 */
16456 public void setTextAlignment(int textAlignment) {
16457 if (textAlignment != getTextAlignment()) {
16458 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016459 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016460 resetResolvedTextAlignment();
16461 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016462 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016463 // Refresh
16464 requestLayout();
16465 invalidate(true);
16466 }
16467 }
16468
16469 /**
16470 * Return the resolved text alignment.
16471 *
16472 * The resolved text alignment. This needs resolution if the value is
16473 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16474 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16475 *
16476 * @return the resolved text alignment. Returns one of:
16477 *
16478 * {@link #TEXT_ALIGNMENT_GRAVITY},
16479 * {@link #TEXT_ALIGNMENT_CENTER},
16480 * {@link #TEXT_ALIGNMENT_TEXT_START},
16481 * {@link #TEXT_ALIGNMENT_TEXT_END},
16482 * {@link #TEXT_ALIGNMENT_VIEW_START},
16483 * {@link #TEXT_ALIGNMENT_VIEW_END}
16484 */
16485 @ViewDebug.ExportedProperty(category = "text", mapping = {
16486 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16487 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16488 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16489 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16490 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16491 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16492 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16493 })
16494 public int getResolvedTextAlignment() {
16495 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016496 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016497 resolveTextAlignment();
16498 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016499 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016500 }
16501
16502 /**
16503 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16504 * resolution is done.
16505 */
16506 public void resolveTextAlignment() {
16507 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016508 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016509
16510 if (hasRtlSupport()) {
16511 // Set resolved text alignment flag depending on text alignment flag
16512 final int textAlignment = getTextAlignment();
16513 switch (textAlignment) {
16514 case TEXT_ALIGNMENT_INHERIT:
16515 // Check if we can resolve the text alignment
16516 if (canResolveLayoutDirection() && mParent instanceof View) {
16517 View view = (View) mParent;
16518
16519 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16520 switch (parentResolvedTextAlignment) {
16521 case TEXT_ALIGNMENT_GRAVITY:
16522 case TEXT_ALIGNMENT_TEXT_START:
16523 case TEXT_ALIGNMENT_TEXT_END:
16524 case TEXT_ALIGNMENT_CENTER:
16525 case TEXT_ALIGNMENT_VIEW_START:
16526 case TEXT_ALIGNMENT_VIEW_END:
16527 // Resolved text alignment is the same as the parent resolved
16528 // text alignment
16529 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016530 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016531 break;
16532 default:
16533 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016534 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016535 }
16536 }
16537 else {
16538 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016539 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016540 }
16541 break;
16542 case TEXT_ALIGNMENT_GRAVITY:
16543 case TEXT_ALIGNMENT_TEXT_START:
16544 case TEXT_ALIGNMENT_TEXT_END:
16545 case TEXT_ALIGNMENT_CENTER:
16546 case TEXT_ALIGNMENT_VIEW_START:
16547 case TEXT_ALIGNMENT_VIEW_END:
16548 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016549 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016550 break;
16551 default:
16552 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016553 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016554 }
16555 } else {
16556 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016557 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016558 }
16559
16560 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016561 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016562 onResolvedTextAlignmentChanged();
16563 }
16564
16565 /**
16566 * Check if text alignment resolution can be done.
16567 *
16568 * @return true if text alignment resolution can be done otherwise return false.
16569 */
16570 public boolean canResolveTextAlignment() {
16571 switch (getTextAlignment()) {
16572 case TEXT_DIRECTION_INHERIT:
16573 return (mParent != null);
16574 default:
16575 return true;
16576 }
16577 }
16578
16579 /**
16580 * Called when text alignment has been resolved. Subclasses that care about text alignment
16581 * resolution should override this method.
16582 *
16583 * The default implementation does nothing.
16584 */
16585 public void onResolvedTextAlignmentChanged() {
16586 }
16587
16588 /**
16589 * Reset resolved text alignment. Text alignment can be resolved with a call to
16590 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16591 * reset is done.
16592 */
16593 public void resetResolvedTextAlignment() {
16594 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016595 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016596 onResolvedTextAlignmentReset();
16597 }
16598
16599 /**
16600 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16601 * override this method and do a reset of the text alignment of their children. The default
16602 * implementation does nothing.
16603 */
16604 public void onResolvedTextAlignmentReset() {
16605 }
16606
Adam Powella9108a22012-07-18 11:18:09 -070016607 /**
16608 * Generate a value suitable for use in {@link #setId(int)}.
16609 * This value will not collide with ID values generated at build time by aapt for R.id.
16610 *
16611 * @return a generated ID value
16612 */
16613 public static int generateViewId() {
16614 for (;;) {
16615 final int result = sNextGeneratedId.get();
16616 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16617 int newValue = result + 1;
16618 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16619 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16620 return result;
16621 }
16622 }
16623 }
16624
Chet Haaseb39f0512011-05-24 14:36:40 -070016625 //
16626 // Properties
16627 //
16628 /**
16629 * A Property wrapper around the <code>alpha</code> functionality handled by the
16630 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16631 */
Chet Haased47f1532011-12-16 11:18:52 -080016632 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016633 @Override
16634 public void setValue(View object, float value) {
16635 object.setAlpha(value);
16636 }
16637
16638 @Override
16639 public Float get(View object) {
16640 return object.getAlpha();
16641 }
16642 };
16643
16644 /**
16645 * A Property wrapper around the <code>translationX</code> functionality handled by the
16646 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16647 */
Chet Haased47f1532011-12-16 11:18:52 -080016648 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016649 @Override
16650 public void setValue(View object, float value) {
16651 object.setTranslationX(value);
16652 }
16653
16654 @Override
16655 public Float get(View object) {
16656 return object.getTranslationX();
16657 }
16658 };
16659
16660 /**
16661 * A Property wrapper around the <code>translationY</code> functionality handled by the
16662 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16663 */
Chet Haased47f1532011-12-16 11:18:52 -080016664 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016665 @Override
16666 public void setValue(View object, float value) {
16667 object.setTranslationY(value);
16668 }
16669
16670 @Override
16671 public Float get(View object) {
16672 return object.getTranslationY();
16673 }
16674 };
16675
16676 /**
16677 * A Property wrapper around the <code>x</code> functionality handled by the
16678 * {@link View#setX(float)} and {@link View#getX()} methods.
16679 */
Chet Haased47f1532011-12-16 11:18:52 -080016680 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016681 @Override
16682 public void setValue(View object, float value) {
16683 object.setX(value);
16684 }
16685
16686 @Override
16687 public Float get(View object) {
16688 return object.getX();
16689 }
16690 };
16691
16692 /**
16693 * A Property wrapper around the <code>y</code> functionality handled by the
16694 * {@link View#setY(float)} and {@link View#getY()} methods.
16695 */
Chet Haased47f1532011-12-16 11:18:52 -080016696 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016697 @Override
16698 public void setValue(View object, float value) {
16699 object.setY(value);
16700 }
16701
16702 @Override
16703 public Float get(View object) {
16704 return object.getY();
16705 }
16706 };
16707
16708 /**
16709 * A Property wrapper around the <code>rotation</code> functionality handled by the
16710 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16711 */
Chet Haased47f1532011-12-16 11:18:52 -080016712 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016713 @Override
16714 public void setValue(View object, float value) {
16715 object.setRotation(value);
16716 }
16717
16718 @Override
16719 public Float get(View object) {
16720 return object.getRotation();
16721 }
16722 };
16723
16724 /**
16725 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16726 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16727 */
Chet Haased47f1532011-12-16 11:18:52 -080016728 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016729 @Override
16730 public void setValue(View object, float value) {
16731 object.setRotationX(value);
16732 }
16733
16734 @Override
16735 public Float get(View object) {
16736 return object.getRotationX();
16737 }
16738 };
16739
16740 /**
16741 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16742 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16743 */
Chet Haased47f1532011-12-16 11:18:52 -080016744 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016745 @Override
16746 public void setValue(View object, float value) {
16747 object.setRotationY(value);
16748 }
16749
16750 @Override
16751 public Float get(View object) {
16752 return object.getRotationY();
16753 }
16754 };
16755
16756 /**
16757 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16758 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16759 */
Chet Haased47f1532011-12-16 11:18:52 -080016760 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016761 @Override
16762 public void setValue(View object, float value) {
16763 object.setScaleX(value);
16764 }
16765
16766 @Override
16767 public Float get(View object) {
16768 return object.getScaleX();
16769 }
16770 };
16771
16772 /**
16773 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16774 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16775 */
Chet Haased47f1532011-12-16 11:18:52 -080016776 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016777 @Override
16778 public void setValue(View object, float value) {
16779 object.setScaleY(value);
16780 }
16781
16782 @Override
16783 public Float get(View object) {
16784 return object.getScaleY();
16785 }
16786 };
16787
Jeff Brown33bbfd22011-02-24 20:55:35 -080016788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016789 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16790 * Each MeasureSpec represents a requirement for either the width or the height.
16791 * A MeasureSpec is comprised of a size and a mode. There are three possible
16792 * modes:
16793 * <dl>
16794 * <dt>UNSPECIFIED</dt>
16795 * <dd>
16796 * The parent has not imposed any constraint on the child. It can be whatever size
16797 * it wants.
16798 * </dd>
16799 *
16800 * <dt>EXACTLY</dt>
16801 * <dd>
16802 * The parent has determined an exact size for the child. The child is going to be
16803 * given those bounds regardless of how big it wants to be.
16804 * </dd>
16805 *
16806 * <dt>AT_MOST</dt>
16807 * <dd>
16808 * The child can be as large as it wants up to the specified size.
16809 * </dd>
16810 * </dl>
16811 *
16812 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16813 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16814 */
16815 public static class MeasureSpec {
16816 private static final int MODE_SHIFT = 30;
16817 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16818
16819 /**
16820 * Measure specification mode: The parent has not imposed any constraint
16821 * on the child. It can be whatever size it wants.
16822 */
16823 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16824
16825 /**
16826 * Measure specification mode: The parent has determined an exact size
16827 * for the child. The child is going to be given those bounds regardless
16828 * of how big it wants to be.
16829 */
16830 public static final int EXACTLY = 1 << MODE_SHIFT;
16831
16832 /**
16833 * Measure specification mode: The child can be as large as it wants up
16834 * to the specified size.
16835 */
16836 public static final int AT_MOST = 2 << MODE_SHIFT;
16837
16838 /**
16839 * Creates a measure specification based on the supplied size and mode.
16840 *
16841 * The mode must always be one of the following:
16842 * <ul>
16843 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16844 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16845 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16846 * </ul>
16847 *
16848 * @param size the size of the measure specification
16849 * @param mode the mode of the measure specification
16850 * @return the measure specification based on size and mode
16851 */
16852 public static int makeMeasureSpec(int size, int mode) {
16853 return size + mode;
16854 }
16855
16856 /**
16857 * Extracts the mode from the supplied measure specification.
16858 *
16859 * @param measureSpec the measure specification to extract the mode from
16860 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16861 * {@link android.view.View.MeasureSpec#AT_MOST} or
16862 * {@link android.view.View.MeasureSpec#EXACTLY}
16863 */
16864 public static int getMode(int measureSpec) {
16865 return (measureSpec & MODE_MASK);
16866 }
16867
16868 /**
16869 * Extracts the size from the supplied measure specification.
16870 *
16871 * @param measureSpec the measure specification to extract the size from
16872 * @return the size in pixels defined in the supplied measure specification
16873 */
16874 public static int getSize(int measureSpec) {
16875 return (measureSpec & ~MODE_MASK);
16876 }
16877
16878 /**
16879 * Returns a String representation of the specified measure
16880 * specification.
16881 *
16882 * @param measureSpec the measure specification to convert to a String
16883 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16884 */
16885 public static String toString(int measureSpec) {
16886 int mode = getMode(measureSpec);
16887 int size = getSize(measureSpec);
16888
16889 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16890
16891 if (mode == UNSPECIFIED)
16892 sb.append("UNSPECIFIED ");
16893 else if (mode == EXACTLY)
16894 sb.append("EXACTLY ");
16895 else if (mode == AT_MOST)
16896 sb.append("AT_MOST ");
16897 else
16898 sb.append(mode).append(" ");
16899
16900 sb.append(size);
16901 return sb.toString();
16902 }
16903 }
16904
16905 class CheckForLongPress implements Runnable {
16906
16907 private int mOriginalWindowAttachCount;
16908
16909 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016910 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016911 && mOriginalWindowAttachCount == mWindowAttachCount) {
16912 if (performLongClick()) {
16913 mHasPerformedLongPress = true;
16914 }
16915 }
16916 }
16917
16918 public void rememberWindowAttachCount() {
16919 mOriginalWindowAttachCount = mWindowAttachCount;
16920 }
16921 }
Joe Malin32736f02011-01-19 16:14:20 -080016922
Adam Powelle14579b2009-12-16 18:39:52 -080016923 private final class CheckForTap implements Runnable {
16924 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016925 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016926 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016927 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016928 }
16929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016930
Adam Powella35d7682010-03-12 14:48:13 -080016931 private final class PerformClick implements Runnable {
16932 public void run() {
16933 performClick();
16934 }
16935 }
16936
Dianne Hackborn63042d62011-01-26 18:56:29 -080016937 /** @hide */
16938 public void hackTurnOffWindowResizeAnim(boolean off) {
16939 mAttachInfo.mTurnOffWindowResizeAnim = off;
16940 }
Joe Malin32736f02011-01-19 16:14:20 -080016941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016942 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016943 * This method returns a ViewPropertyAnimator object, which can be used to animate
16944 * specific properties on this View.
16945 *
16946 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16947 */
16948 public ViewPropertyAnimator animate() {
16949 if (mAnimator == null) {
16950 mAnimator = new ViewPropertyAnimator(this);
16951 }
16952 return mAnimator;
16953 }
16954
16955 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016956 * Interface definition for a callback to be invoked when a hardware key event is
16957 * dispatched to this view. The callback will be invoked before the key event is
16958 * given to the view. This is only useful for hardware keyboards; a software input
16959 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016960 */
16961 public interface OnKeyListener {
16962 /**
Jean Chalard405bc512012-05-29 19:12:34 +090016963 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016964 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090016965 * <p>Key presses in software keyboards will generally NOT trigger this method,
16966 * although some may elect to do so in some situations. Do not assume a
16967 * software input method has to be key-based; even if it is, it may use key presses
16968 * in a different way than you expect, so there is no way to reliably catch soft
16969 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016970 *
16971 * @param v The view the key has been dispatched to.
16972 * @param keyCode The code for the physical key that was pressed
16973 * @param event The KeyEvent object containing full information about
16974 * the event.
16975 * @return True if the listener has consumed the event, false otherwise.
16976 */
16977 boolean onKey(View v, int keyCode, KeyEvent event);
16978 }
16979
16980 /**
16981 * Interface definition for a callback to be invoked when a touch event is
16982 * dispatched to this view. The callback will be invoked before the touch
16983 * event is given to the view.
16984 */
16985 public interface OnTouchListener {
16986 /**
16987 * Called when a touch event is dispatched to a view. This allows listeners to
16988 * get a chance to respond before the target view.
16989 *
16990 * @param v The view the touch event has been dispatched to.
16991 * @param event The MotionEvent object containing full information about
16992 * the event.
16993 * @return True if the listener has consumed the event, false otherwise.
16994 */
16995 boolean onTouch(View v, MotionEvent event);
16996 }
16997
16998 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016999 * Interface definition for a callback to be invoked when a hover event is
17000 * dispatched to this view. The callback will be invoked before the hover
17001 * event is given to the view.
17002 */
17003 public interface OnHoverListener {
17004 /**
17005 * Called when a hover event is dispatched to a view. This allows listeners to
17006 * get a chance to respond before the target view.
17007 *
17008 * @param v The view the hover event has been dispatched to.
17009 * @param event The MotionEvent object containing full information about
17010 * the event.
17011 * @return True if the listener has consumed the event, false otherwise.
17012 */
17013 boolean onHover(View v, MotionEvent event);
17014 }
17015
17016 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017017 * Interface definition for a callback to be invoked when a generic motion event is
17018 * dispatched to this view. The callback will be invoked before the generic motion
17019 * event is given to the view.
17020 */
17021 public interface OnGenericMotionListener {
17022 /**
17023 * Called when a generic motion event is dispatched to a view. This allows listeners to
17024 * get a chance to respond before the target view.
17025 *
17026 * @param v The view the generic motion event has been dispatched to.
17027 * @param event The MotionEvent object containing full information about
17028 * the event.
17029 * @return True if the listener has consumed the event, false otherwise.
17030 */
17031 boolean onGenericMotion(View v, MotionEvent event);
17032 }
17033
17034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017035 * Interface definition for a callback to be invoked when a view has been clicked and held.
17036 */
17037 public interface OnLongClickListener {
17038 /**
17039 * Called when a view has been clicked and held.
17040 *
17041 * @param v The view that was clicked and held.
17042 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017043 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017044 */
17045 boolean onLongClick(View v);
17046 }
17047
17048 /**
Chris Tate32affef2010-10-18 15:29:21 -070017049 * Interface definition for a callback to be invoked when a drag is being dispatched
17050 * to this view. The callback will be invoked before the hosting view's own
17051 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17052 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017053 *
17054 * <div class="special reference">
17055 * <h3>Developer Guides</h3>
17056 * <p>For a guide to implementing drag and drop features, read the
17057 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17058 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017059 */
17060 public interface OnDragListener {
17061 /**
17062 * Called when a drag event is dispatched to a view. This allows listeners
17063 * to get a chance to override base View behavior.
17064 *
Joe Malin32736f02011-01-19 16:14:20 -080017065 * @param v The View that received the drag event.
17066 * @param event The {@link android.view.DragEvent} object for the drag event.
17067 * @return {@code true} if the drag event was handled successfully, or {@code false}
17068 * if the drag event was not handled. Note that {@code false} will trigger the View
17069 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017070 */
17071 boolean onDrag(View v, DragEvent event);
17072 }
17073
17074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017075 * Interface definition for a callback to be invoked when the focus state of
17076 * a view changed.
17077 */
17078 public interface OnFocusChangeListener {
17079 /**
17080 * Called when the focus state of a view has changed.
17081 *
17082 * @param v The view whose state has changed.
17083 * @param hasFocus The new focus state of v.
17084 */
17085 void onFocusChange(View v, boolean hasFocus);
17086 }
17087
17088 /**
17089 * Interface definition for a callback to be invoked when a view is clicked.
17090 */
17091 public interface OnClickListener {
17092 /**
17093 * Called when a view has been clicked.
17094 *
17095 * @param v The view that was clicked.
17096 */
17097 void onClick(View v);
17098 }
17099
17100 /**
17101 * Interface definition for a callback to be invoked when the context menu
17102 * for this view is being built.
17103 */
17104 public interface OnCreateContextMenuListener {
17105 /**
17106 * Called when the context menu for this view is being built. It is not
17107 * safe to hold onto the menu after this method returns.
17108 *
17109 * @param menu The context menu that is being built
17110 * @param v The view for which the context menu is being built
17111 * @param menuInfo Extra information about the item for which the
17112 * context menu should be shown. This information will vary
17113 * depending on the class of v.
17114 */
17115 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17116 }
17117
Joe Onorato664644d2011-01-23 17:53:23 -080017118 /**
17119 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017120 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017121 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017122 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017123 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017124 */
17125 public interface OnSystemUiVisibilityChangeListener {
17126 /**
17127 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017128 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017129 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017130 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17131 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17132 * This tells you the <strong>global</strong> state of these UI visibility
17133 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017134 */
17135 public void onSystemUiVisibilityChange(int visibility);
17136 }
17137
Adam Powell4afd62b2011-02-18 15:02:18 -080017138 /**
17139 * Interface definition for a callback to be invoked when this view is attached
17140 * or detached from its window.
17141 */
17142 public interface OnAttachStateChangeListener {
17143 /**
17144 * Called when the view is attached to a window.
17145 * @param v The view that was attached
17146 */
17147 public void onViewAttachedToWindow(View v);
17148 /**
17149 * Called when the view is detached from a window.
17150 * @param v The view that was detached
17151 */
17152 public void onViewDetachedFromWindow(View v);
17153 }
17154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017155 private final class UnsetPressedState implements Runnable {
17156 public void run() {
17157 setPressed(false);
17158 }
17159 }
17160
17161 /**
17162 * Base class for derived classes that want to save and restore their own
17163 * state in {@link android.view.View#onSaveInstanceState()}.
17164 */
17165 public static class BaseSavedState extends AbsSavedState {
17166 /**
17167 * Constructor used when reading from a parcel. Reads the state of the superclass.
17168 *
17169 * @param source
17170 */
17171 public BaseSavedState(Parcel source) {
17172 super(source);
17173 }
17174
17175 /**
17176 * Constructor called by derived classes when creating their SavedState objects
17177 *
17178 * @param superState The state of the superclass of this view
17179 */
17180 public BaseSavedState(Parcelable superState) {
17181 super(superState);
17182 }
17183
17184 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17185 new Parcelable.Creator<BaseSavedState>() {
17186 public BaseSavedState createFromParcel(Parcel in) {
17187 return new BaseSavedState(in);
17188 }
17189
17190 public BaseSavedState[] newArray(int size) {
17191 return new BaseSavedState[size];
17192 }
17193 };
17194 }
17195
17196 /**
17197 * A set of information given to a view when it is attached to its parent
17198 * window.
17199 */
17200 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017201 interface Callbacks {
17202 void playSoundEffect(int effectId);
17203 boolean performHapticFeedback(int effectId, boolean always);
17204 }
17205
17206 /**
17207 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17208 * to a Handler. This class contains the target (View) to invalidate and
17209 * the coordinates of the dirty rectangle.
17210 *
17211 * For performance purposes, this class also implements a pool of up to
17212 * POOL_LIMIT objects that get reused. This reduces memory allocations
17213 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017214 */
Romain Guyd928d682009-03-31 17:52:16 -070017215 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017216 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017217 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17218 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017219 public InvalidateInfo newInstance() {
17220 return new InvalidateInfo();
17221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017222
Romain Guyd928d682009-03-31 17:52:16 -070017223 public void onAcquired(InvalidateInfo element) {
17224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017225
Romain Guyd928d682009-03-31 17:52:16 -070017226 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017227 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017228 }
17229 }, POOL_LIMIT)
17230 );
17231
17232 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017233 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017234
17235 View target;
17236
17237 int left;
17238 int top;
17239 int right;
17240 int bottom;
17241
Romain Guyd928d682009-03-31 17:52:16 -070017242 public void setNextPoolable(InvalidateInfo element) {
17243 mNext = element;
17244 }
17245
17246 public InvalidateInfo getNextPoolable() {
17247 return mNext;
17248 }
17249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017250 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017251 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017252 }
17253
17254 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017255 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017256 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017257
17258 public boolean isPooled() {
17259 return mIsPooled;
17260 }
17261
17262 public void setPooled(boolean isPooled) {
17263 mIsPooled = isPooled;
17264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017265 }
17266
17267 final IWindowSession mSession;
17268
17269 final IWindow mWindow;
17270
17271 final IBinder mWindowToken;
17272
Jeff Brown98365d72012-08-19 20:30:52 -070017273 final Display mDisplay;
17274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017275 final Callbacks mRootCallbacks;
17276
Romain Guy59a12ca2011-06-09 17:48:21 -070017277 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017279 /**
17280 * The top view of the hierarchy.
17281 */
17282 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017284 IBinder mPanelParentWindowToken;
17285 Surface mSurface;
17286
Romain Guyb051e892010-09-28 19:09:36 -070017287 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017288 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017289 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017290
Romain Guy7e4e5612012-03-05 14:37:29 -080017291 boolean mScreenOn;
17292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017293 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017294 * Scale factor used by the compatibility mode
17295 */
17296 float mApplicationScale;
17297
17298 /**
17299 * Indicates whether the application is in compatibility mode
17300 */
17301 boolean mScalingRequired;
17302
17303 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017304 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017305 */
17306 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017307
Dianne Hackborn63042d62011-01-26 18:56:29 -080017308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017309 * Left position of this view's window
17310 */
17311 int mWindowLeft;
17312
17313 /**
17314 * Top position of this view's window
17315 */
17316 int mWindowTop;
17317
17318 /**
Adam Powell26153a32010-11-08 15:22:27 -080017319 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017320 */
Adam Powell26153a32010-11-08 15:22:27 -080017321 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017322
17323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017324 * For windows that are full-screen but using insets to layout inside
17325 * of the screen decorations, these are the current insets for the
17326 * content of the window.
17327 */
17328 final Rect mContentInsets = new Rect();
17329
17330 /**
17331 * For windows that are full-screen but using insets to layout inside
17332 * of the screen decorations, these are the current insets for the
17333 * actual visible parts of the window.
17334 */
17335 final Rect mVisibleInsets = new Rect();
17336
17337 /**
17338 * The internal insets given by this window. This value is
17339 * supplied by the client (through
17340 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17341 * be given to the window manager when changed to be used in laying
17342 * out windows behind it.
17343 */
17344 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17345 = new ViewTreeObserver.InternalInsetsInfo();
17346
17347 /**
17348 * All views in the window's hierarchy that serve as scroll containers,
17349 * used to determine if the window can be resized or must be panned
17350 * to adjust for a soft input area.
17351 */
17352 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17353
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017354 final KeyEvent.DispatcherState mKeyDispatchState
17355 = new KeyEvent.DispatcherState();
17356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017357 /**
17358 * Indicates whether the view's window currently has the focus.
17359 */
17360 boolean mHasWindowFocus;
17361
17362 /**
17363 * The current visibility of the window.
17364 */
17365 int mWindowVisibility;
17366
17367 /**
17368 * Indicates the time at which drawing started to occur.
17369 */
17370 long mDrawingTime;
17371
17372 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017373 * Indicates whether or not ignoring the DIRTY_MASK flags.
17374 */
17375 boolean mIgnoreDirtyState;
17376
17377 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017378 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17379 * to avoid clearing that flag prematurely.
17380 */
17381 boolean mSetIgnoreDirtyState = false;
17382
17383 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017384 * Indicates whether the view's window is currently in touch mode.
17385 */
17386 boolean mInTouchMode;
17387
17388 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017389 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017390 * the next time it performs a traversal
17391 */
17392 boolean mRecomputeGlobalAttributes;
17393
17394 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017395 * Always report new attributes at next traversal.
17396 */
17397 boolean mForceReportNewAttributes;
17398
17399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017400 * Set during a traveral if any views want to keep the screen on.
17401 */
17402 boolean mKeepScreenOn;
17403
17404 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017405 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17406 */
17407 int mSystemUiVisibility;
17408
17409 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017410 * Hack to force certain system UI visibility flags to be cleared.
17411 */
17412 int mDisabledSystemUiVisibility;
17413
17414 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017415 * Last global system UI visibility reported by the window manager.
17416 */
17417 int mGlobalSystemUiVisibility;
17418
17419 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017420 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17421 * attached.
17422 */
17423 boolean mHasSystemUiListeners;
17424
17425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017426 * Set if the visibility of any views has changed.
17427 */
17428 boolean mViewVisibilityChanged;
17429
17430 /**
17431 * Set to true if a view has been scrolled.
17432 */
17433 boolean mViewScrollChanged;
17434
17435 /**
17436 * Global to the view hierarchy used as a temporary for dealing with
17437 * x/y points in the transparent region computations.
17438 */
17439 final int[] mTransparentLocation = new int[2];
17440
17441 /**
17442 * Global to the view hierarchy used as a temporary for dealing with
17443 * x/y points in the ViewGroup.invalidateChild implementation.
17444 */
17445 final int[] mInvalidateChildLocation = new int[2];
17446
Chet Haasec3aa3612010-06-17 08:50:37 -070017447
17448 /**
17449 * Global to the view hierarchy used as a temporary for dealing with
17450 * x/y location when view is transformed.
17451 */
17452 final float[] mTmpTransformLocation = new float[2];
17453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017454 /**
17455 * The view tree observer used to dispatch global events like
17456 * layout, pre-draw, touch mode change, etc.
17457 */
17458 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17459
17460 /**
17461 * A Canvas used by the view hierarchy to perform bitmap caching.
17462 */
17463 Canvas mCanvas;
17464
17465 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017466 * The view root impl.
17467 */
17468 final ViewRootImpl mViewRootImpl;
17469
17470 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017471 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017472 * handler can be used to pump events in the UI events queue.
17473 */
17474 final Handler mHandler;
17475
17476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017477 * Temporary for use in computing invalidate rectangles while
17478 * calling up the hierarchy.
17479 */
17480 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017481
17482 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017483 * Temporary for use in computing hit areas with transformed views
17484 */
17485 final RectF mTmpTransformRect = new RectF();
17486
17487 /**
Chet Haase599913d2012-07-23 16:22:05 -070017488 * Temporary for use in transforming invalidation rect
17489 */
17490 final Matrix mTmpMatrix = new Matrix();
17491
17492 /**
17493 * Temporary for use in transforming invalidation rect
17494 */
17495 final Transformation mTmpTransformation = new Transformation();
17496
17497 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017498 * Temporary list for use in collecting focusable descendents of a view.
17499 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017500 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017501
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017502 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017503 * The id of the window for accessibility purposes.
17504 */
17505 int mAccessibilityWindowId = View.NO_ID;
17506
17507 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017508 * Whether to ingore not exposed for accessibility Views when
17509 * reporting the view tree to accessibility services.
17510 */
17511 boolean mIncludeNotImportantViews;
17512
17513 /**
17514 * The drawable for highlighting accessibility focus.
17515 */
17516 Drawable mAccessibilityFocusDrawable;
17517
17518 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017519 * Show where the margins, bounds and layout bounds are for each view.
17520 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017521 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017522
17523 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017524 * Point used to compute visible regions.
17525 */
17526 final Point mPoint = new Point();
17527
17528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017529 * Creates a new set of attachment information with the specified
17530 * events handler and thread.
17531 *
17532 * @param handler the events handler the view must use
17533 */
Jeff Brown98365d72012-08-19 20:30:52 -070017534 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017535 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017536 mSession = session;
17537 mWindow = window;
17538 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017539 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017540 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017541 mHandler = handler;
17542 mRootCallbacks = effectPlayer;
17543 }
17544 }
17545
17546 /**
17547 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17548 * is supported. This avoids keeping too many unused fields in most
17549 * instances of View.</p>
17550 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017551 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017552
Mike Cleronf116bf82009-09-27 19:14:12 -070017553 /**
17554 * Scrollbars are not visible
17555 */
17556 public static final int OFF = 0;
17557
17558 /**
17559 * Scrollbars are visible
17560 */
17561 public static final int ON = 1;
17562
17563 /**
17564 * Scrollbars are fading away
17565 */
17566 public static final int FADING = 2;
17567
17568 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017570 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017571 public int scrollBarDefaultDelayBeforeFade;
17572 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017573
17574 public int scrollBarSize;
17575 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017576 public float[] interpolatorValues;
17577 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017578
17579 public final Paint paint;
17580 public final Matrix matrix;
17581 public Shader shader;
17582
Mike Cleronf116bf82009-09-27 19:14:12 -070017583 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17584
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017585 private static final float[] OPAQUE = { 255 };
17586 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017587
Mike Cleronf116bf82009-09-27 19:14:12 -070017588 /**
17589 * When fading should start. This time moves into the future every time
17590 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17591 */
17592 public long fadeStartTime;
17593
17594
17595 /**
17596 * The current state of the scrollbars: ON, OFF, or FADING
17597 */
17598 public int state = OFF;
17599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017600 private int mLastColor;
17601
Mike Cleronf116bf82009-09-27 19:14:12 -070017602 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017603 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17604 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017605 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17606 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017607
17608 paint = new Paint();
17609 matrix = new Matrix();
17610 // use use a height of 1, and then wack the matrix each time we
17611 // actually use it.
17612 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017614 paint.setShader(shader);
17615 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017616 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017617 }
Romain Guy8506ab42009-06-11 17:35:47 -070017618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017619 public void setFadeColor(int color) {
17620 if (color != 0 && color != mLastColor) {
17621 mLastColor = color;
17622 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017623
Romain Guye55e1a72009-08-27 10:42:26 -070017624 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17625 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017627 paint.setShader(shader);
17628 // Restore the default transfer mode (src_over)
17629 paint.setXfermode(null);
17630 }
17631 }
Joe Malin32736f02011-01-19 16:14:20 -080017632
Mike Cleronf116bf82009-09-27 19:14:12 -070017633 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017634 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017635 if (now >= fadeStartTime) {
17636
17637 // the animation fades the scrollbars out by changing
17638 // the opacity (alpha) from fully opaque to fully
17639 // transparent
17640 int nextFrame = (int) now;
17641 int framesCount = 0;
17642
17643 Interpolator interpolator = scrollBarInterpolator;
17644
17645 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017646 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017647
17648 // End transparent
17649 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017650 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017651
17652 state = FADING;
17653
17654 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017655 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017656 }
17657 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017658 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017659
Svetoslav Ganova0156172011-06-26 17:55:44 -070017660 /**
17661 * Resuable callback for sending
17662 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17663 */
17664 private class SendViewScrolledAccessibilityEvent implements Runnable {
17665 public volatile boolean mIsPending;
17666
17667 public void run() {
17668 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17669 mIsPending = false;
17670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017671 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017672
17673 /**
17674 * <p>
17675 * This class represents a delegate that can be registered in a {@link View}
17676 * to enhance accessibility support via composition rather via inheritance.
17677 * It is specifically targeted to widget developers that extend basic View
17678 * classes i.e. classes in package android.view, that would like their
17679 * applications to be backwards compatible.
17680 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017681 * <div class="special reference">
17682 * <h3>Developer Guides</h3>
17683 * <p>For more information about making applications accessible, read the
17684 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17685 * developer guide.</p>
17686 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017687 * <p>
17688 * A scenario in which a developer would like to use an accessibility delegate
17689 * is overriding a method introduced in a later API version then the minimal API
17690 * version supported by the application. For example, the method
17691 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17692 * in API version 4 when the accessibility APIs were first introduced. If a
17693 * developer would like his application to run on API version 4 devices (assuming
17694 * all other APIs used by the application are version 4 or lower) and take advantage
17695 * of this method, instead of overriding the method which would break the application's
17696 * backwards compatibility, he can override the corresponding method in this
17697 * delegate and register the delegate in the target View if the API version of
17698 * the system is high enough i.e. the API version is same or higher to the API
17699 * version that introduced
17700 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17701 * </p>
17702 * <p>
17703 * Here is an example implementation:
17704 * </p>
17705 * <code><pre><p>
17706 * if (Build.VERSION.SDK_INT >= 14) {
17707 * // If the API version is equal of higher than the version in
17708 * // which onInitializeAccessibilityNodeInfo was introduced we
17709 * // register a delegate with a customized implementation.
17710 * View view = findViewById(R.id.view_id);
17711 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17712 * public void onInitializeAccessibilityNodeInfo(View host,
17713 * AccessibilityNodeInfo info) {
17714 * // Let the default implementation populate the info.
17715 * super.onInitializeAccessibilityNodeInfo(host, info);
17716 * // Set some other information.
17717 * info.setEnabled(host.isEnabled());
17718 * }
17719 * });
17720 * }
17721 * </code></pre></p>
17722 * <p>
17723 * This delegate contains methods that correspond to the accessibility methods
17724 * in View. If a delegate has been specified the implementation in View hands
17725 * off handling to the corresponding method in this delegate. The default
17726 * implementation the delegate methods behaves exactly as the corresponding
17727 * method in View for the case of no accessibility delegate been set. Hence,
17728 * to customize the behavior of a View method, clients can override only the
17729 * corresponding delegate method without altering the behavior of the rest
17730 * accessibility related methods of the host view.
17731 * </p>
17732 */
17733 public static class AccessibilityDelegate {
17734
17735 /**
17736 * Sends an accessibility event of the given type. If accessibility is not
17737 * enabled this method has no effect.
17738 * <p>
17739 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17740 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17741 * been set.
17742 * </p>
17743 *
17744 * @param host The View hosting the delegate.
17745 * @param eventType The type of the event to send.
17746 *
17747 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17748 */
17749 public void sendAccessibilityEvent(View host, int eventType) {
17750 host.sendAccessibilityEventInternal(eventType);
17751 }
17752
17753 /**
alanv8eeefef2012-05-07 16:57:53 -070017754 * Performs the specified accessibility action on the view. For
17755 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17756 * <p>
17757 * The default implementation behaves as
17758 * {@link View#performAccessibilityAction(int, Bundle)
17759 * View#performAccessibilityAction(int, Bundle)} for the case of
17760 * no accessibility delegate been set.
17761 * </p>
17762 *
17763 * @param action The action to perform.
17764 * @return Whether the action was performed.
17765 *
17766 * @see View#performAccessibilityAction(int, Bundle)
17767 * View#performAccessibilityAction(int, Bundle)
17768 */
17769 public boolean performAccessibilityAction(View host, int action, Bundle args) {
17770 return host.performAccessibilityActionInternal(action, args);
17771 }
17772
17773 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017774 * Sends an accessibility event. This method behaves exactly as
17775 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17776 * empty {@link AccessibilityEvent} and does not perform a check whether
17777 * accessibility is enabled.
17778 * <p>
17779 * The default implementation behaves as
17780 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17781 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17782 * the case of no accessibility delegate been set.
17783 * </p>
17784 *
17785 * @param host The View hosting the delegate.
17786 * @param event The event to send.
17787 *
17788 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17789 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17790 */
17791 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17792 host.sendAccessibilityEventUncheckedInternal(event);
17793 }
17794
17795 /**
17796 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17797 * to its children for adding their text content to the event.
17798 * <p>
17799 * The default implementation behaves as
17800 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17801 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17802 * the case of no accessibility delegate been set.
17803 * </p>
17804 *
17805 * @param host The View hosting the delegate.
17806 * @param event The event.
17807 * @return True if the event population was completed.
17808 *
17809 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17810 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17811 */
17812 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17813 return host.dispatchPopulateAccessibilityEventInternal(event);
17814 }
17815
17816 /**
17817 * Gives a chance to the host View to populate the accessibility event with its
17818 * text content.
17819 * <p>
17820 * The default implementation behaves as
17821 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17822 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17823 * the case of no accessibility delegate been set.
17824 * </p>
17825 *
17826 * @param host The View hosting the delegate.
17827 * @param event The accessibility event which to populate.
17828 *
17829 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17830 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17831 */
17832 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17833 host.onPopulateAccessibilityEventInternal(event);
17834 }
17835
17836 /**
17837 * Initializes an {@link AccessibilityEvent} with information about the
17838 * the host View which is the event source.
17839 * <p>
17840 * The default implementation behaves as
17841 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17842 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17843 * the case of no accessibility delegate been set.
17844 * </p>
17845 *
17846 * @param host The View hosting the delegate.
17847 * @param event The event to initialize.
17848 *
17849 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17850 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17851 */
17852 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17853 host.onInitializeAccessibilityEventInternal(event);
17854 }
17855
17856 /**
17857 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17858 * <p>
17859 * The default implementation behaves as
17860 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17861 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17862 * the case of no accessibility delegate been set.
17863 * </p>
17864 *
17865 * @param host The View hosting the delegate.
17866 * @param info The instance to initialize.
17867 *
17868 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17869 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17870 */
17871 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17872 host.onInitializeAccessibilityNodeInfoInternal(info);
17873 }
17874
17875 /**
17876 * Called when a child of the host View has requested sending an
17877 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17878 * to augment the event.
17879 * <p>
17880 * The default implementation behaves as
17881 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17882 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17883 * the case of no accessibility delegate been set.
17884 * </p>
17885 *
17886 * @param host The View hosting the delegate.
17887 * @param child The child which requests sending the event.
17888 * @param event The event to be sent.
17889 * @return True if the event should be sent
17890 *
17891 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17892 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17893 */
17894 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17895 AccessibilityEvent event) {
17896 return host.onRequestSendAccessibilityEventInternal(child, event);
17897 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017898
17899 /**
17900 * Gets the provider for managing a virtual view hierarchy rooted at this View
17901 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17902 * that explore the window content.
17903 * <p>
17904 * The default implementation behaves as
17905 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17906 * the case of no accessibility delegate been set.
17907 * </p>
17908 *
17909 * @return The provider.
17910 *
17911 * @see AccessibilityNodeProvider
17912 */
17913 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17914 return null;
17915 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017917}